JPA mapping updates
[utils] / security / impl / src / main / java / org / wamblee / security / authentication / User.java
index 03c15eea9297c6296b0a21f8bd2fae93fec9bded..5e6666b7be9027520440e390c8fd6d4c9ab5d90d 100644 (file)
@@ -20,11 +20,14 @@ import java.util.Set;
 import java.util.TreeSet;
 
 import javax.persistence.CascadeType;
+import javax.persistence.Column;
 import javax.persistence.Entity;
 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 +61,7 @@ public class User implements Serializable, Comparable {
 
     @Id
     @GeneratedValue(strategy = GenerationType.AUTO)
-    private Long primaryKey;
+    private Long id;
 
     @Version
     private int version;
@@ -77,6 +80,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<Group> groups;
 
     /**
@@ -98,18 +102,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<Group>();
-        groups.add(aGroup);
         passwordValidator = aPasswordValidator;
         passwordEncoder = aPasswordEncoder;
     }
@@ -119,7 +119,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 +179,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 +195,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 +212,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 +334,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 +397,6 @@ public class User implements Serializable, Comparable {
     }
 
     public Long getPrimaryKey() {
-        return primaryKey;
+        return id;
     }
 }