build fail corrected
[staff/due1/sed-hs15-srs-purple.git] / src / main / java / ch / bfh / ti / srs / core / User.java
1 /*
2 * Copyright (c) 2015 Berner Fachhochschule, Switzerland.
3 *
4 * Project Smart Reservation System.
5 *
6 * Distributable under GPL license. See terms of license at gnu.org.
7 */
8 package ch.bfh.ti.srs.core;
9
10 import ch.bfh.ti.srs.extern.Password;
11
12 public class User {
13
14 private final String userName;
15 private String hashedPassword;
16 private String mail;
17 private String name;
18 private String lastname;
19
20 /**
21 * Constructor for already existing user in the Datacontext
22 * Only to be used by DataHandler implementation!
23 *
24 * @param userName Username
25 * @param hashedPassword Encrypted password
26 * @param mail Mail
27 * @param name Name
28 * @param lastname Lastname
29 */
30 //TODO: Change constructor to package when DataHandlerForExcel is moved back to this package
31 public User(String userName, String hashedPassword, String mail, String name, String lastname){
32 this.userName = userName;
33 this.hashedPassword = hashedPassword;
34 this.mail = mail;
35 this.name = name;
36 this.lastname = lastname;
37 }
38
39 /**
40 * Normal constructor for new users created in the UserController
41 *
42 * @param userName
43 * @param password
44 * @throws Exception
45 */
46 public User(String userName, String password) throws Exception{
47 if(userName == null) throw new IllegalArgumentException("Username must not be null");
48 if(userName.length() < 4) throw new IllegalArgumentException("Username must have at least 4 characters");
49 if(password == null) throw new IllegalArgumentException("Password must not be null");
50
51 this.userName = userName;
52 this.hashedPassword = Password.getSaltedHash(password);
53 }
54
55 //Getter
56 public String getUserName(){
57 return userName;
58 }
59
60 public String getMail(){
61 return mail;
62 }
63
64 public String getName() {
65 return name;
66 }
67
68 public String getLastname() {
69 return lastname;
70 }
71
72 public String getEncryptedPassword(){
73 return hashedPassword;
74 }
75
76 //Setter
77 public boolean setMail(String mail){
78 if(mail != null){
79 if(isValidEmailAddress(mail)){
80 this.mail = mail;
81 return true;
82 }
83 }
84 throw new IllegalArgumentException("Must be valid e-mail adress");
85 }
86
87 public void setName(String name) {
88 this.name = name;
89 }
90
91 public void setLastname(String lastname) {
92 this.lastname = lastname;
93 }
94
95
96 //Password methods
97 public boolean checkPassword(String password) throws Exception{
98 if(password == null) throw new IllegalArgumentException("Password must not be null");
99 return Password.check(password, this.hashedPassword);
100 }
101
102 public void changePassword(String newPassword) throws Exception{
103 if(newPassword == null) throw new IllegalArgumentException("Password must not be null");
104 if(checkPassword(newPassword)) throw new IllegalArgumentException("Can not be the current password");
105 this.hashedPassword = Password.getSaltedHash(newPassword);
106 }
107
108
109 //private Helper methods
110 private boolean isValidEmailAddress(String mail){
111 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,}))$";
112 java.util.regex.Pattern p = java.util.regex.Pattern.compile(ePattern);
113 java.util.regex.Matcher m = p.matcher(mail);
114 return m.matches();
115 }
116 }