1a77e063cef5a75fd2cb7d1e9b2d634dfbfa1e01
[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 }
70
71 public String getUserName() {
72 return userName;
73 }
74
75 public String getMail() {
76 return mail;
77 }
78
79 public String getName() {
80 return name;
81 }
82
83 public String getLastname() {
84 return lastname;
85 }
86
87 public String getEncryptedPassword() {
88 return hashedPassword;
89 }
90
91 public boolean setMail(String mail) {
92 if (mail != null) {
93 if (isValidEmailAddress(mail)) {
94 this.mail = mail;
95 return true;
96 }
97 }
98 throw new IllegalArgumentException("Must be valid e-mail adress");
99 }
100
101 public void setName(String name) {
102 this.name = name;
103 }
104
105 public void setLastname(String lastname) {
106 this.lastname = lastname;
107 }
108
109 /**
110 * Checks if the given password matches the hashedPassword
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
119 */
120 public boolean checkPassword(String password) throws Exception {
121 if (password == null)
122 throw new IllegalArgumentException("Password must not be null");
123 return Password.check(password, this.hashedPassword);
124 }
125
126 /**
127 * Changes the current password
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
134 */
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");
140 this.hashedPassword = Password.getSaltedHash(newPassword);
141 }
142
143 /**
144 * Checks if the given string represents a valid email address
145 * @param mail
146 * address
147 * @return
148 */
149 private boolean isValidEmailAddress(String mail) {
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();
154 }
155
156 /*
157 * (non-Javadoc)
158 * @see java.lang.Object#clone()
159 */
160 @Override
161 public User clone() throws CloneNotSupportedException {
162 return (User) super.clone();
163 }
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 }
176 }