Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package model;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.Locale;
- import javax.persistence.*;
- import play.Logger;
- import play.db.ebean.Model;
- import play.data.validation.*;
- import utils.BCrypt;
- @Entity
- public class LoginUser extends Model {
- private static final long serialVersionUID = 1L;
- @Id
- @Constraints.Required
- public String username;
- @Constraints.Required
- public String password;
- public String salt = "";
- public String middlename;
- @Constraints.Required
- public String firstname;
- @Constraints.Required
- public String lastname;
- @ManyToOne(cascade = {CascadeType.MERGE})
- private Role role;
- public boolean enabled = true;
- public boolean deleted;
- public Date birthdate = new Date();
- public String telephone;
- @Constraints.Required
- @Constraints.Email
- public String email;
- public String city;
- public String country;
- @Transient
- public boolean changePassword;
- public static Finder<String, LoginUser> find = new Finder<String, LoginUser>(
- String.class, LoginUser.class);
- /**
- * Authenticate a LoginUser.
- */
- public static LoginUser authenticate(String username, String password) {
- LoginUser loginUser = find.where()
- .eq("username", username)
- .findUnique();
- if(loginUser != null){
- if(loginUser.password.equals(convertPasswordToHash(password, loginUser.salt))){
- return loginUser;
- }
- }
- return null;
- }
- /**
- * Setter for role.
- */
- public void setRole(String role) {
- Role r = Role.findRole(role);
- if(role != null){
- this.role = r;
- } else{
- Role r2 = new Role(role);
- r2.name = role;
- r2.save();
- this.role = r2;
- }
- }
- /**
- * Getter for role.
- */
- public Role getRole() {
- return role;
- }
- /**
- * Getter for birth date in string format: MMMM dd, yyyy.
- */
- public String getBirthDateString(){
- String datePatern = "MMMMM dd, yyyy";
- SimpleDateFormat dateFormat = new SimpleDateFormat(datePatern, Locale.getDefault());
- try{
- return dateFormat.format(birthdate);
- }catch(NullPointerException ne){
- return "";
- }
- }
- /**
- * Setter for birth date: params: day, month, year.
- */
- public void setBirthDate(String day, String month, String year){
- Calendar cal = Calendar.getInstance(Locale.getDefault());
- try{
- cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day));
- cal.set(Calendar.MONTH, Integer.parseInt(month) - 1);
- cal.set(Calendar.YEAR, Integer.parseInt(year));
- }catch(NumberFormatException ne){
- return;
- }
- this.birthdate = cal.getTime();
- }
- /**
- * Setter for birth date.
- */
- public void setBirthdate(Date birthdate) {
- this.birthdate = birthdate;
- }
- /**
- * Overrides save, generated new password if salt is missing.
- */
- @Override
- public void save(){
- if(salt.equals("")){
- salt = BCrypt.gensalt();
- password = BCrypt.hashpw(password, salt);
- }
- super.save();
- }
- /**
- * Password to BCrypt hash converter.
- */
- public static String convertPasswordToHash(String password, String salt){
- return BCrypt.hashpw(password, salt);
- }
- /**
- * Salt generator.
- */
- public static String generateSalt(){
- return BCrypt.gensalt();
- }
- /**
- * Role checker.
- */
- public static boolean checkRole(String username, String role){
- LoginUser loginUser = getLoginUser(username);
- if(loginUser != null){
- if(loginUser.getRole() != null){
- if(loginUser.getRole().name.equals(role)){
- return true;
- }
- }
- }
- return false;
- }
- /**
- * Getter for LoginUser by user name
- */
- public static LoginUser getLoginUser(String username){
- return LoginUser.find.where().eq("username", username).findUnique();
- }
- }
Add Comment
Please, Sign In to add comment