82301ebdc09028860daaafd28ea838647cff0971
[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 thrown by <code>Password</code> class
55 */
56 public User(String userName, String password) throws Exception {
57 if (userName == null)
58 throw new IllegalArgumentException("Username must not be null");
59 if (userName.length() < 4)
60 throw new IllegalArgumentException("Username must have at least 4 characters");
61 if (password == null)
62 throw new IllegalArgumentException("Password must not be null");
63
64 this.userName = userName;
65 this.hashedPassword = Password.getSaltedHash(password);
66 }
67
68 public String getUserName() {
69 return userName;
70 }
71
72 public String getMail() {
73 return mail;
74 }
75
76 public String getName() {
77 return name;
78 }
79
80 public String getLastname() {
81 return lastname;
82 }
83
84 public String getEncryptedPassword() {
85 return hashedPassword;
86 }
87
88 public boolean setMail(String mail) {
89 if (mail != null) {
90 if (isValidEmailAddress(mail)) {
91 this.mail = mail;
92 return true;
93 }
94 }
95 throw new IllegalArgumentException("Must be valid e-mail adress");
96 }
97
98 public void setName(String name) {
99 this.name = name;
100 }
101
102 public void setLastname(String lastname) {
103 this.lastname = lastname;
104 }
105
106 /**
107 * Checks if the given password matches the hashedPassword
108 *
109 * @param password password to check
110 * @return <code>True</code> password accepted, <code>False</code> password rejected
111 * @throws IllegalArgumentException password is null
112 * @throws Exception thrown by <code>Password</code> class
113 */
114 public boolean checkPassword(String password) throws Exception {
115 if (password == null)
116 throw new IllegalArgumentException("Password must not be null");
117 return Password.check(password, this.hashedPassword);
118 }
119
120 /**
121 * Changes the current password
122 *
123 * @param newPassword new password
124 * @throws IllegalArgumentException password does not meet the requirements
125 * @throws Exception thrown by <code>Password</code> class
126 */
127 public void changePassword(String newPassword) throws Exception {
128 if (newPassword == null)
129 throw new IllegalArgumentException("Password must not be null");
130 if (checkPassword(newPassword))
131 throw new IllegalArgumentException("Can not be the current password");
132 this.hashedPassword = Password.getSaltedHash(newPassword);
133 }
134
135 /**
136 * Checks if the given string represents a valid email address
137 * @param mail
138 * address
139 * @return
140 */
141 private boolean isValidEmailAddress(String mail) {
142 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,}))$";
143 java.util.regex.Pattern p = java.util.regex.Pattern.compile(ePattern);
144 java.util.regex.Matcher m = p.matcher(mail);
145 return m.matches();
146 }
147
148 /*
149 * (non-Javadoc)
150 * @see java.lang.Object#clone()
151 */
152 @Override
153 public User clone() throws CloneNotSupportedException {
154 return (User) super.clone();
155 }
156 }