java-doc updated
[staff/due1/sed-hs15-srs-purple.git] / src / main / java / ch / bfh / ti / srs / core / User.java
index 24b7339..95240e8 100644 (file)
@@ -12,37 +12,46 @@ import ch.bfh.ti.srs.extern.Password;
 public class User {
 
        private final String userName;
-       private String password;
+       private String hashedPassword;
        private String mail;
        private String name;
        private String lastname;
 
        /**
-        * Contructor of already existing user in the system
-        * @param userName
-        * @param password
-        * @param mail
-        * @param name
-        * @param lastname
+        * Constructor for already existing user in the Datacontext
+        * Only to be used by DataHandler implementation!
+        * 
+        * @param userName                      Username
+        * @param hashedPassword        Encrypted password
+        * @param mail                          Mail
+        * @param name                          Name
+        * @param lastname                      Lastname
         */
-       User(String userName, String password, String mail, String name, String lastname){
+       //TODO: Change constructor to package when DataHandlerForExcel is moved back to this package
+       public User(String userName, String hashedPassword, String mail, String name, String lastname){
                this.userName = userName;
-               this.password = password;
+               this.hashedPassword = hashedPassword;
                this.mail = mail;
                this.name = name;
                this.lastname = lastname;
        }
 
+       /**
+        * Normal constructor for new users created in the UserController
+        * 
+        * @param userName
+        * @param password
+        * @throws Exception
+        */
        public User(String userName, String password) throws Exception{
                if(userName == null) throw new IllegalArgumentException("Username must not be null");
                if(userName.length() < 4) throw new IllegalArgumentException("Username must have at least 4 characters");
                if(password == null) throw new IllegalArgumentException("Password must not be null");
-               
+
                this.userName = userName;
-               this.password = Password.getSaltedHash(password);
+               this.hashedPassword = Password.getSaltedHash(password);
        }
 
-       //Getter
        public String getUserName(){
                return userName;
        }
@@ -60,10 +69,9 @@ public class User {
        }
        
        public String getEncryptedPassword(){
-               return password;
+               return hashedPassword;
        }
        
-       //Setter
        public boolean setMail(String mail){
                if(mail != null){
                        if(isValidEmailAddress(mail)){
@@ -82,21 +90,36 @@ public class User {
                this.lastname = lastname;
        }
        
-       
-       //Password methods
+       /**
+        * Checks if the given password matches the hashedPassword
+        * 
+        * @param       password
+        * @return      
+        * @throws      Exception
+        */
        public boolean checkPassword(String password) throws Exception{
                if(password == null) throw new IllegalArgumentException("Password must not be null");
-               return Password.check(password, this.password);
+               return Password.check(password, this.hashedPassword);
        }
 
+       /**
+        * Changes the current password to the given newPassword param, if newPassword is whether null nor equals the current password
+        * 
+        * @param       newPassword
+        * @throws      Exception
+        */
        public void changePassword(String newPassword) throws Exception{
                if(newPassword == null) throw new IllegalArgumentException("Password must not be null");
                if(checkPassword(newPassword)) throw new IllegalArgumentException("Can not be the current password");
-               this.password = Password.getSaltedHash(newPassword);
+               this.hashedPassword = Password.getSaltedHash(newPassword);
        }
 
-
-       //private Helper methods
+       /**
+        * Checks if the given string represents a valid email address
+        * 
+        * @param       mail    address
+        * @return
+        */
        private boolean isValidEmailAddress(String mail){
                String ePattern = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$";
                java.util.regex.Pattern p = java.util.regex.Pattern.compile(ePattern);