java-doc updated
[staff/due1/sed-hs15-srs-purple.git] / src / main / java / ch / bfh / ti / srs / core / User.java
index 5efba71..95240e8 100644 (file)
@@ -1,47 +1,77 @@
+/*
+ * Copyright (c) 2015 Berner Fachhochschule, Switzerland.
+ *
+ * Project Smart Reservation System.
+ *
+ * Distributable under GPL license. See terms of license at gnu.org.
+ */
 package ch.bfh.ti.srs.core;
 
+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;
 
+       /**
+        * 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
+        */
+       //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.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");
 
-       public User(String userName, String password){
                this.userName = userName;
-               try {
-                       this.password = Password.getSaltedHash(password);
-               } catch (Exception e) {
-                       // TODO: Bad programming style (due1). Discuss.
-                       // Lösung:
-                       // Logger verwenden da konsole auf server somit würde Meldung User nicht sehen.
-                       // password bleibt null!!?
-                       // wird anschliessend fortgesetzt mit password = null!
-                       // Lösung:
-                       // - User Objekt darf nicht erstellt werden!!
-                       // - exception muss weiter geworfen werden : throws except.
-                       // - Logger verwenden!
-                       
-                       System.err.println("Fehler beim Erstellen des Hashes");
-               }
+               this.hashedPassword = Password.getSaltedHash(password);
        }
 
        public String getUserName(){
                return userName;
        }
 
-       public boolean checkPassword(String password){
-               boolean passwordCorrect = false;
-               try {
-                       passwordCorrect = Password.check(password, this.password);
-               } catch (Exception e) {
-                       System.err.println("Fehler beim Prüfen des Passwortes");
-               }
-               return passwordCorrect;
+       public String getMail(){
+               return mail;
        }
-
+       
+       public String getName() {
+               return name;
+       }
+       
+       public String getLastname() {
+               return lastname;
+       }
+       
+       public String getEncryptedPassword(){
+               return hashedPassword;
+       }
+       
        public boolean setMail(String mail){
                if(mail != null){
                        if(isValidEmailAddress(mail)){
@@ -49,37 +79,47 @@ public class User {
                                return true;
                        }
                }
-               return false;
-       }
-
-       public String getMail(){
-               return mail;
-       }
-
-       public void changePassword(String newPassword){
-               try {
-                       this.password = Password.getSaltedHash(newPassword);
-               } catch (Exception e) {
-                       System.err.println("Fehler beim erstellen des Hashes");
-               }
-       }
-
-       public String getName() {
-               return name;
+               throw new IllegalArgumentException("Must be valid e-mail adress");
        }
 
        public void setName(String name) {
                this.name = name;
        }
-
-       public String getLastname() {
-               return lastname;
-       }
-
+       
        public void setLastname(String lastname) {
                this.lastname = lastname;
        }
+       
+       /**
+        * 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.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.hashedPassword = Password.getSaltedHash(newPassword);
+       }
 
+       /**
+        * 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);