UserTest updated
[staff/due1/sed-hs15-srs-purple.git] / src / main / java / ch / bfh / ti / srs / dataschema / User.java
CommitLineData
5555b609
M
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 */
5c0ee08b 8package ch.bfh.ti.srs.dataschema;
0a2e269c 9
9608c1e8
M
10import java.util.HashSet;
11import java.util.Set;
12
ed6d035d
M
13import ch.bfh.ti.srs.extern.Password;
14
66cc653d 15public class User implements Cloneable {
9ca2608d 16
0a2e269c 17 private final String userName;
4f169fdc 18 private String hashedPassword;
9ca2608d
M
19 private String mail;
20 private String name;
21 private String lastname;
9608c1e8 22 private Set<Role> roles = new HashSet<Role>();
9ca2608d 23
06726c50 24 /**
87c1587b
M
25 * Constructor for already existing <code>User</code> in the Datacontext.
26 * Only to be used by <code>DataHandler</code> implementation!
ef666303
M
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
06726c50 37 */
ef666303
M
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) {
1ec0e01f 41 this.userName = userName;
4f169fdc 42 this.hashedPassword = hashedPassword;
1ec0e01f
M
43 this.mail = mail;
44 this.name = name;
45 this.lastname = lastname;
46 }
9ca2608d 47
1f7b5004
M
48 /**
49 * Normal constructor for new users created in the UserController
ef666303
M
50 * @param userName
51 * user name
52 * @param password
53 * password
54 * @throws IllegalArgumentException
55 * user name or password does not meet requirements
66cc653d
D
56 * @throws Exception
57 * thrown by <code>Password</code> class
1f7b5004 58 */
ef666303
M
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");
1f7b5004 66
9ca2608d 67 this.userName = userName;
4f169fdc 68 this.hashedPassword = Password.getSaltedHash(password);
d040b026 69 this.roles.add(Role.User);
0a2e269c 70 }
9ca2608d 71
ef666303 72 public String getUserName() {
0a2e269c
M
73 return userName;
74 }
9ca2608d 75
ef666303 76 public String getMail() {
ed6d035d 77 return mail;
9ca2608d 78 }
ef666303 79
ed6d035d
M
80 public String getName() {
81 return name;
82 }
ef666303 83
ed6d035d
M
84 public String getLastname() {
85 return lastname;
86 }
ef666303
M
87
88 public String getEncryptedPassword() {
4f169fdc 89 return hashedPassword;
1ec0e01f 90 }
ef666303
M
91
92 public boolean setMail(String mail) {
93 if (mail != null) {
94 if (isValidEmailAddress(mail)) {
cb599a4a
M
95 this.mail = mail;
96 return true;
97 }
9ca2608d 98 }
ed6d035d 99 throw new IllegalArgumentException("Must be valid e-mail adress");
9ca2608d
M
100 }
101
102 public void setName(String name) {
103 this.name = name;
104 }
ef666303 105
4481dffa 106 public void setLastname(String lastname) {
9ca2608d
M
107 this.lastname = lastname;
108 }
ef666303 109
09f73ea7
M
110 /**
111 * Checks if the given password matches the hashedPassword
66cc653d
D
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
09f73ea7 120 */
ef666303
M
121 public boolean checkPassword(String password) throws Exception {
122 if (password == null)
123 throw new IllegalArgumentException("Password must not be null");
4f169fdc 124 return Password.check(password, this.hashedPassword);
ed6d035d
M
125 }
126
09f73ea7 127 /**
ef666303 128 * Changes the current password
66cc653d
D
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
09f73ea7 135 */
ef666303
M
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");
4f169fdc 141 this.hashedPassword = Password.getSaltedHash(newPassword);
ed6d035d
M
142 }
143
09f73ea7
M
144 /**
145 * Checks if the given string represents a valid email address
ef666303
M
146 * @param mail
147 * address
09f73ea7
M
148 * @return
149 */
ef666303 150 private boolean isValidEmailAddress(String mail) {
9ca2608d
M
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();
0a2e269c 155 }
ef666303
M
156
157 /*
158 * (non-Javadoc)
87c1587b
M
159 * @see java.lang.Object#clone()
160 */
a08b0437 161 @Override
ef666303
M
162 public User clone() throws CloneNotSupportedException {
163 return (User) super.clone();
164 }
9608c1e8
M
165
166 public void addRole(Role role){
167 roles.add(role);
168 }
169
170 public void removeRole(Role role){
171 if(roles.contains(role)) roles.remove(role);
172 }
173
174 public Set<Role> getRoles(){
175 return roles;
176 }
66cc653d 177}