Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 29th, 2012  |  syntax: None  |  size: 2.57 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package models;
  2.  
  3. import javax.persistence.Entity;
  4. import javax.persistence.Column;
  5. import javax.persistence.Table;
  6. // import javax.persistence.UniqueConstraint;
  7.  
  8. import play.libs.Codec;
  9. import play.db.jpa.Model;
  10. import play.data.validation.Email;
  11. import play.data.validation.Required;
  12.  
  13. @Entity
  14. @Table(name="Users") // , uniqueConstraints=@UniqueConstraint(columnNames={"EMP_ID", "EMP_NAME"}))
  15. public class User extends Model {
  16.     protected static String UNUSABLE_PASSWORD = "!"; // hash would never match
  17.    
  18.     @Email
  19.     @Required
  20.     // @Column(unique=true)
  21.     public String email;
  22.    
  23.     @Required
  24.     public String password;
  25.    
  26.     @Required
  27.     @Column(length=50)
  28.     public String firstName;
  29.    
  30.     @Required
  31.     @Column(length=50)
  32.     public String lastName;
  33.    
  34.     public User(String email, String password, String firstName, String lastName) {
  35.         this.email = email;
  36.         this.firstName = firstName;
  37.         this.lastName = lastName;
  38.        
  39.         // set the password
  40.         setPassword(password);
  41.        
  42.         create();
  43.     }
  44.    
  45.     @Override
  46.     public String toString() {
  47.         return getFullName();
  48.     }
  49.    
  50.     public String getFullName() {
  51.         return this.firstName + " " + this.lastName;
  52.     }
  53.    
  54.    
  55.     public void setUnusablePassword() {
  56.         this.password = UNUSABLE_PASSWORD;
  57.     }
  58.    
  59.     /*
  60.      * Hash and salt the user's password
  61.      * @param password - the plain string
  62.      * @return void - set the password in the instance
  63.      */
  64.     public void setPassword(String password) {
  65.         String salt = Codec.UUID().substring(0,5); // salts are 5 characters long (random)
  66.        
  67.         // no password -- set as unusable
  68.         if(password.length() == 0) {
  69.             setUnusablePassword();
  70.             return;
  71.         }
  72.        
  73.         // generate a password hash
  74.         this.password = String.format("sha1$%s$%s", salt, Codec.hexSHA1(salt + password));
  75.     }
  76.    
  77.     /*
  78.      * Check the user's password
  79.      * @param password - the plain string
  80.      * @return boolean - wheter the password is correct or not
  81.      */
  82.     public boolean checkPassword(String password) {
  83.         String[] passwordHash = this.password.split("\\$");
  84.        
  85.         if(passwordHash.length != 3) {
  86.             // crashes if the password is set as unusable
  87.             return false;
  88.         }
  89.        
  90.         String salt = passwordHash[1];
  91.         String passwordHashed = passwordHash[2];
  92.        
  93.         return passwordHashed.equals(Codec.hexSHA1(salt + password));
  94.     }
  95. }