DataHandlerForTestsTest added,
[staff/due1/sed-hs15-srs-purple.git] / src / main / java / ch / bfh / ti / srs / core / User.java
index e1fd775..e964628 100644 (file)
+/*
+ * 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;
-       
-       public User(String name, String password){
-               this.userName = name;
+       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 password              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 password, String mail, String name, String lastname){
+               this.userName = userName;
                this.password = password;
+               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);
+       }
+
+       //Getter
        public String getUserName(){
                return userName;
        }
+
+       public String getMail(){
+               return mail;
+       }
+       
+       public String getName() {
+               return name;
+       }
+       
+       public String getLastname() {
+               return lastname;
+       }
+       
+       public String getEncryptedPassword(){
+               return password;
+       }
+       
+       //Setter
+       public boolean setMail(String mail){
+               if(mail != null){
+                       if(isValidEmailAddress(mail)){
+                               this.mail = mail;
+                               return true;
+                       }
+               }
+               throw new IllegalArgumentException("Must be valid e-mail adress");
+       }
+
+       public void setName(String name) {
+               this.name = name;
+       }
        
-       public boolean checkPassword(String password){
-               return Password.checkPassword(password, this.password);
+       public void setLastname(String lastname) {
+               this.lastname = lastname;
+       }
+       
+       
+       //Password methods
+       public boolean checkPassword(String password) throws Exception{
+               if(password == null) throw new IllegalArgumentException("Password must not be null");
+               return Password.check(password, this.password);
+       }
+
+       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);
+       }
+
+
+       //private Helper methods
+       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);
+               java.util.regex.Matcher m = p.matcher(mail);
+               return m.matches();
        }
 }
\ No newline at end of file