Merge remote-tracking branch 'origin/development'
[staff/due1/sed-hs15-srs-purple.git] / src / main / java / ch / bfh / ti / srs / dataschema / 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.dataschema;
9
10 import java.util.HashSet;
11 import java.util.Set;
12
13 import ch.bfh.ti.srs.extern.Password;
14
15 public class User implements Cloneable {
16
17 private final String userName;
18 private String hashedPassword;
19 private String mail;
20 private String name;
21 private String lastname;
22 private Set<Role> roles = new HashSet<Role>();
23
24 /**
25 * Constructor for already existing <code>User</code> in the Datacontext.
26 * Only to be used by <code>DataHandler</code> implementation!
27 * @param userName
28 * user name
29 * @param hashedPassword
30 * encrypted password
31 * @param mail
32 * mail
33 * @param name
34 * name
35 * @param lastname
36 * last name
37 */
38 // TODO: Change constructor to package when DataHandlerForExcel is moved
39 // back to this package
40 public User(String userName, String hashedPassword, String mail, String name, String lastname) {
41 this.userName = userName;
42 this.hashedPassword = hashedPassword;
43 this.mail = mail;
44 this.name = name;
45 this.lastname = lastname;
46 }
47
48 /**
49 * Normal constructor for new users created in the UserController
50 * @param userName
51 * user name
52 * @param password
53 * password
54 * @throws IllegalArgumentException
55 * user name or password does not meet requirements
56 * @throws Exception
57 * thrown by <code>Password</code> class
58 */
59 public User(String userName, String password) throws Exception {
60 if (userName == null)
61 throw new IllegalArgumentException("Username must not be null");
62 if (userName.length() < 4)
63 throw new IllegalArgumentException("Username must have at least 4 characters");
64 if (password == null)
65 throw new IllegalArgumentException("Password must not be null");
66
67 this.userName = userName;
68 this.hashedPassword = Password.getSaltedHash(password);
69 this.roles.add(Role.User);
70 }
71
72 public String getUserName() {
73 return userName;
74 }
75
76 public String getMail() {
77 return mail;
78 }
79
80 public String getName() {
81 return name;
82 }
83
84 public String getLastname() {
85 return lastname;
86 }
87
88 public String getEncryptedPassword() {
89 return hashedPassword;
90 }
91
92 public boolean setMail(String mail) {
93 if (mail != null) {
94 if (isValidEmailAddress(mail)) {
95 this.mail = mail;
96 return true;
97 }
98 }
99 throw new IllegalArgumentException("Must be valid e-mail adress");
100 }
101
102 public void setName(String name) {
103 this.name = name;
104 }
105
106 public void setLastname(String lastname) {
107 this.lastname = lastname;
108 }
109
110 /**
111 * Checks if the given password matches the hashedPassword
112 * @param password
113 * password to check
114 * @return <code>True</code> password accepted, <code>False</code> password
115 * rejected
116 * @throws IllegalArgumentException
117 * password is null
118 * @throws Exception
119 * thrown by <code>Password</code> class
120 */
121 public boolean checkPassword(String password) throws Exception {
122 if (password == null)
123 throw new IllegalArgumentException("Password must not be null");
124 return Password.check(password, this.hashedPassword);
125 }
126
127 /**
128 * Changes the current password
129 * @param newPassword
130 * new password
131 * @throws IllegalArgumentException
132 * password does not meet the requirements
133 * @throws Exception
134 * thrown by <code>Password</code> class
135 */
136 public void changePassword(String newPassword) throws Exception {
137 if (newPassword == null)
138 throw new IllegalArgumentException("Password must not be null");
139 if (checkPassword(newPassword))
140 throw new IllegalArgumentException("Can not be the current password");
141 this.hashedPassword = Password.getSaltedHash(newPassword);
142 }
143
144 /**
145 * Checks if the given string represents a valid email address
146 * @param mail
147 * address
148 * @return
149 */
150 private boolean isValidEmailAddress(String mail) {
151 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,}))$";
152 java.util.regex.Pattern p = java.util.regex.Pattern.compile(ePattern);
153 java.util.regex.Matcher m = p.matcher(mail);
154 return m.matches();
155 }
156
157 @Override
158 public User clone() throws CloneNotSupportedException {
159 return (User) super.clone();
160 }
161
162 public void addRole(Role role) {
163 roles.add(role);
164 }
165
166 public void removeRole(Role role) {
167 if (roles.contains(role))
168 roles.remove(role);
169 }
170
171 public Set<Role> getRoles() {
172 return roles;
173 }
174 }