Roles added
[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);
0a2e269c 69 }
9ca2608d 70
ef666303 71 public String getUserName() {
0a2e269c
M
72 return userName;
73 }
9ca2608d 74
ef666303 75 public String getMail() {
ed6d035d 76 return mail;
9ca2608d 77 }
ef666303 78
ed6d035d
M
79 public String getName() {
80 return name;
81 }
ef666303 82
ed6d035d
M
83 public String getLastname() {
84 return lastname;
85 }
ef666303
M
86
87 public String getEncryptedPassword() {
4f169fdc 88 return hashedPassword;
1ec0e01f 89 }
ef666303
M
90
91 public boolean setMail(String mail) {
92 if (mail != null) {
93 if (isValidEmailAddress(mail)) {
cb599a4a
M
94 this.mail = mail;
95 return true;
96 }
9ca2608d 97 }
ed6d035d 98 throw new IllegalArgumentException("Must be valid e-mail adress");
9ca2608d
M
99 }
100
101 public void setName(String name) {
102 this.name = name;
103 }
ef666303 104
4481dffa 105 public void setLastname(String lastname) {
9ca2608d
M
106 this.lastname = lastname;
107 }
ef666303 108
09f73ea7
M
109 /**
110 * Checks if the given password matches the hashedPassword
66cc653d
D
111 * @param password
112 * password to check
113 * @return <code>True</code> password accepted, <code>False</code> password
114 * rejected
115 * @throws IllegalArgumentException
116 * password is null
117 * @throws Exception
118 * thrown by <code>Password</code> class
09f73ea7 119 */
ef666303
M
120 public boolean checkPassword(String password) throws Exception {
121 if (password == null)
122 throw new IllegalArgumentException("Password must not be null");
4f169fdc 123 return Password.check(password, this.hashedPassword);
ed6d035d
M
124 }
125
09f73ea7 126 /**
ef666303 127 * Changes the current password
66cc653d
D
128 * @param newPassword
129 * new password
130 * @throws IllegalArgumentException
131 * password does not meet the requirements
132 * @throws Exception
133 * thrown by <code>Password</code> class
09f73ea7 134 */
ef666303
M
135 public void changePassword(String newPassword) throws Exception {
136 if (newPassword == null)
137 throw new IllegalArgumentException("Password must not be null");
138 if (checkPassword(newPassword))
139 throw new IllegalArgumentException("Can not be the current password");
4f169fdc 140 this.hashedPassword = Password.getSaltedHash(newPassword);
ed6d035d
M
141 }
142
09f73ea7
M
143 /**
144 * Checks if the given string represents a valid email address
ef666303
M
145 * @param mail
146 * address
09f73ea7
M
147 * @return
148 */
ef666303 149 private boolean isValidEmailAddress(String mail) {
9ca2608d
M
150 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,}))$";
151 java.util.regex.Pattern p = java.util.regex.Pattern.compile(ePattern);
152 java.util.regex.Matcher m = p.matcher(mail);
153 return m.matches();
0a2e269c 154 }
ef666303
M
155
156 /*
157 * (non-Javadoc)
87c1587b
M
158 * @see java.lang.Object#clone()
159 */
a08b0437 160 @Override
ef666303
M
161 public User clone() throws CloneNotSupportedException {
162 return (User) super.clone();
163 }
9608c1e8
M
164
165 public void addRole(Role role){
166 roles.add(role);
167 }
168
169 public void removeRole(Role role){
170 if(roles.contains(role)) roles.remove(role);
171 }
172
173 public Set<Role> getRoles(){
174 return roles;
175 }
66cc653d 176}