X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=security%2Fimpl%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fsecurity%2Fauthentication%2FUser.java;h=12db71a3de98ebf65d1e29ac3718032aa0194a59;hb=e8b988e92306a4aea2f047af1b48588147288831;hp=03c15eea9297c6296b0a21f8bd2fae93fec9bded;hpb=0adf8fb6e00f08a022379cff5edb43fcde30184c;p=utils diff --git a/security/impl/src/main/java/org/wamblee/security/authentication/User.java b/security/impl/src/main/java/org/wamblee/security/authentication/User.java index 03c15eea..12db71a3 100644 --- a/security/impl/src/main/java/org/wamblee/security/authentication/User.java +++ b/security/impl/src/main/java/org/wamblee/security/authentication/User.java @@ -25,6 +25,8 @@ import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; @@ -58,7 +60,7 @@ public class User implements Serializable, Comparable { @Id @GeneratedValue(strategy = GenerationType.AUTO) - private Long primaryKey; + private Long id; @Version private int version; @@ -77,6 +79,7 @@ public class User implements Serializable, Comparable { * Groups the user belongs to. */ @ManyToMany(cascade = CascadeType.MERGE, fetch = FetchType.EAGER) + @JoinTable(name = "SEC_USER_GROUP", joinColumns = { @JoinColumn(name = "USER_ID") }, inverseJoinColumns = { @JoinColumn(name = "GROUP_ID") }) private Set groups; /** @@ -98,18 +101,14 @@ public class User implements Serializable, Comparable { * User name. * @param aPassword * Password. - * @param aGroup - * Group the user belongs to. */ - User(String aName, String aPassword, Group aGroup, - NameValidator aPasswordValidator, MessageDigester aPasswordEncoder) - throws UserMgtException { + User(String aName, String aPassword, NameValidator aPasswordValidator, + MessageDigester aPasswordEncoder) { super(); name = aName; aPasswordValidator.validate(aPassword); password = aPasswordEncoder.hash(aPassword); groups = new TreeSet(); - groups.add(aGroup); passwordValidator = aPasswordValidator; passwordEncoder = aPasswordEncoder; } @@ -119,7 +118,7 @@ public class User implements Serializable, Comparable { * */ public User(User aUser) { - primaryKey = aUser.primaryKey; + id = aUser.id; version = aUser.version; name = aUser.name; password = aUser.password; @@ -179,15 +178,12 @@ public class User implements Serializable, Comparable { * @param aPassword * Password to check. * - * @throws UserMgtException - * In case the password is incorrect. + * @return True iff the password is correct. */ - public void checkPassword(String aPassword) throws UserMgtException { + public boolean checkPassword(String aPassword) { String encoded = passwordEncoder.hash(aPassword); - if (!password.equals(encoded)) { - throw new UserMgtException(Reason.INVALID_PASSWORD, this); - } + return password.equals(encoded); } /** @@ -198,14 +194,15 @@ public class User implements Serializable, Comparable { * @param aNewPassword * New password. * - * @throws UserMgtException - * In case the old password is incorrect. + * @return True iff the password was changed successfully. */ - public void changePassword(String aOldPassword, String aNewPassword) - throws UserMgtException { - checkPassword(aOldPassword); + public boolean changePassword(String aOldPassword, String aNewPassword) { + if (!checkPassword(aOldPassword)) { + return false; + } passwordValidator.validate(aNewPassword); setPassword(aNewPassword); + return true; } /** @@ -214,7 +211,7 @@ public class User implements Serializable, Comparable { * The password to set. * */ - public void setPassword(String aPassword) throws UserMgtException { + public void setPassword(String aPassword) { passwordValidator.validate(aPassword); password = passwordEncoder.hash(aPassword); } @@ -336,16 +333,11 @@ public class User implements Serializable, Comparable { * @throws UserMgtException * In case the user does not belong to the group. */ - void removeGroup(Group aGroup) throws UserMgtException { + void removeGroup(Group aGroup) { if (!groups.contains(aGroup)) { throw new UserMgtException(Reason.USER_NOT_IN_GROUP, this, aGroup); } - if (groups.size() == 1) { - throw new UserMgtException(Reason.USER_MUST_BE_IN_A_GROUP, this, - aGroup); - } - groups.remove(aGroup); } @@ -404,6 +396,6 @@ public class User implements Serializable, Comparable { } public Long getPrimaryKey() { - return primaryKey; + return id; } }