Guest User

Untitled

a guest
Aug 22nd, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.06 KB | None | 0 0
  1. package model;
  2.  
  3. import java.text.SimpleDateFormat;
  4. import java.util.Calendar;
  5. import java.util.Date;
  6. import java.util.Locale;
  7.  
  8. import javax.persistence.*;
  9.  
  10. import play.Logger;
  11. import play.db.ebean.Model;
  12. import play.data.validation.*;
  13. import utils.BCrypt;
  14.  
  15. @Entity
  16. public class LoginUser extends Model {
  17.  
  18.     private static final long serialVersionUID = 1L;
  19.  
  20.     @Id
  21.     @Constraints.Required
  22.     public String username;
  23.  
  24.     @Constraints.Required
  25.     public String password;
  26.  
  27.     public String salt = "";
  28.  
  29.     public String middlename;
  30.  
  31.     @Constraints.Required
  32.     public String firstname;
  33.  
  34.     @Constraints.Required
  35.     public String lastname;
  36.  
  37.     @ManyToOne(cascade = {CascadeType.MERGE})
  38.     private Role role;
  39.  
  40.     public boolean enabled = true;
  41.  
  42.     public boolean deleted;
  43.  
  44.     public Date birthdate = new Date();
  45.  
  46.     public String telephone;
  47.  
  48.     @Constraints.Required
  49.     @Constraints.Email
  50.     public String email;
  51.  
  52.     public String city;
  53.  
  54.     public String country;
  55.  
  56.     @Transient
  57.     public boolean changePassword;
  58.  
  59.     public static Finder<String, LoginUser> find = new Finder<String, LoginUser>(
  60.             String.class, LoginUser.class);
  61.  
  62.     /**
  63.      * Authenticate a LoginUser.
  64.      */
  65.     public static LoginUser authenticate(String username, String password) {
  66.         LoginUser loginUser = find.where()
  67.             .eq("username", username)
  68.             .findUnique();
  69.         if(loginUser != null){
  70.             if(loginUser.password.equals(convertPasswordToHash(password, loginUser.salt))){
  71.                 return loginUser;
  72.             }
  73.         }
  74.         return null;
  75.     }
  76.    
  77.     /**
  78.      * Setter for role.
  79.      */
  80.     public void setRole(String role) {
  81.         Role r = Role.findRole(role);
  82.         if(role != null){
  83.             this.role = r;
  84.         } else{
  85.             Role r2 = new Role(role);
  86.             r2.name = role;
  87.             r2.save();
  88.             this.role = r2;
  89.         }
  90.     }
  91.    
  92.     /**
  93.      * Getter for role.
  94.      */
  95.     public Role getRole() {
  96.         return role;
  97.     }  
  98.    
  99.     /**
  100.      * Getter for birth date in string format: MMMM dd, yyyy.
  101.      */
  102.     public String getBirthDateString(){
  103.         String datePatern = "MMMMM dd, yyyy";
  104.         SimpleDateFormat dateFormat = new SimpleDateFormat(datePatern, Locale.getDefault());
  105.         try{
  106.             return dateFormat.format(birthdate);
  107.         }catch(NullPointerException ne){
  108.             return "";
  109.         }
  110.     }
  111.    
  112.     /**
  113.      * Setter for birth date: params: day, month, year.
  114.      */
  115.     public void setBirthDate(String day, String month, String year){
  116.         Calendar cal = Calendar.getInstance(Locale.getDefault());
  117.         try{
  118.             cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day));
  119.             cal.set(Calendar.MONTH, Integer.parseInt(month) - 1);
  120.             cal.set(Calendar.YEAR, Integer.parseInt(year));
  121.         }catch(NumberFormatException ne){
  122.             return;
  123.         }
  124.         this.birthdate = cal.getTime();
  125.     }
  126.    
  127.     /**
  128.      * Setter for birth date.
  129.      */
  130.     public void setBirthdate(Date birthdate) {
  131.         this.birthdate = birthdate;
  132.     }
  133.    
  134.     /**
  135.      * Overrides save, generated new password if salt is missing.
  136.      */
  137.     @Override
  138.     public void save(){
  139.         if(salt.equals("")){
  140.             salt = BCrypt.gensalt();
  141.             password = BCrypt.hashpw(password, salt);
  142.         }
  143.         super.save();
  144.     }
  145.    
  146.     /**
  147.      * Password to BCrypt hash converter.
  148.      */
  149.     public static String convertPasswordToHash(String password, String salt){
  150.         return BCrypt.hashpw(password, salt);
  151.     }
  152.    
  153.     /**
  154.      * Salt generator.
  155.      */
  156.     public static String generateSalt(){
  157.         return BCrypt.gensalt();
  158.     }
  159.    
  160.     /**
  161.      * Role checker.
  162.      */
  163.     public static boolean checkRole(String username, String role){
  164.         LoginUser loginUser = getLoginUser(username);
  165.         if(loginUser != null){
  166.             if(loginUser.getRole() != null){
  167.                 if(loginUser.getRole().name.equals(role)){
  168.                     return true;
  169.                 }
  170.             }
  171.         }
  172.         return false;
  173.     }
  174.    
  175.     /**
  176.      * Getter for LoginUser by user name
  177.      */
  178.     public static LoginUser getLoginUser(String username){
  179.         return LoginUser.find.where().eq("username", username).findUnique();
  180.     }
  181.  
  182. }
Add Comment
Please, Sign In to add comment