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