Added isModified method to Resource, Reservation.
[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
ed6d035d 10import ch.bfh.ti.srs.extern.Password;
ff1658f6
M
11import java.util.HashSet;
12import java.util.Set;
ed6d035d 13
66cc653d 14public class User implements Cloneable {
9ca2608d 15
0a2e269c 16 private final String userName;
4f169fdc 17 private String hashedPassword;
9ca2608d
M
18 private String mail;
19 private String name;
20 private String lastname;
21
06726c50 22 /**
87c1587b
M
23 * Constructor for already existing <code>User</code> in the Datacontext.
24 * Only to be used by <code>DataHandler</code> implementation!
ef666303
M
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
06726c50 35 */
ef666303
M
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) {
1ec0e01f 39 this.userName = userName;
4f169fdc 40 this.hashedPassword = hashedPassword;
1ec0e01f
M
41 this.mail = mail;
42 this.name = name;
43 this.lastname = lastname;
44 }
9ca2608d 45
1f7b5004
M
46 /**
47 * Normal constructor for new users created in the UserController
ef666303
M
48 * @param userName
49 * user name
50 * @param password
51 * password
52 * @throws IllegalArgumentException
53 * user name or password does not meet requirements
66cc653d
D
54 * @throws Exception
55 * thrown by <code>Password</code> class
1f7b5004 56 */
ef666303
M
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");
1f7b5004 64
9ca2608d 65 this.userName = userName;
4f169fdc 66 this.hashedPassword = Password.getSaltedHash(password);
0a2e269c 67 }
9ca2608d 68
ef666303 69 public String getUserName() {
0a2e269c
M
70 return userName;
71 }
9ca2608d 72
ef666303 73 public String getMail() {
ed6d035d 74 return mail;
9ca2608d 75 }
ef666303 76
ed6d035d
M
77 public String getName() {
78 return name;
79 }
ef666303 80
ed6d035d
M
81 public String getLastname() {
82 return lastname;
83 }
ef666303
M
84
85 public String getEncryptedPassword() {
4f169fdc 86 return hashedPassword;
1ec0e01f 87 }
ef666303
M
88
89 public boolean setMail(String mail) {
90 if (mail != null) {
91 if (isValidEmailAddress(mail)) {
cb599a4a
M
92 this.mail = mail;
93 return true;
94 }
9ca2608d 95 }
ed6d035d 96 throw new IllegalArgumentException("Must be valid e-mail adress");
9ca2608d
M
97 }
98
99 public void setName(String name) {
100 this.name = name;
101 }
ef666303 102
4481dffa 103 public void setLastname(String lastname) {
9ca2608d
M
104 this.lastname = lastname;
105 }
ef666303 106
09f73ea7
M
107 /**
108 * Checks if the given password matches the hashedPassword
66cc653d
D
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
09f73ea7 117 */
ef666303
M
118 public boolean checkPassword(String password) throws Exception {
119 if (password == null)
120 throw new IllegalArgumentException("Password must not be null");
4f169fdc 121 return Password.check(password, this.hashedPassword);
ed6d035d
M
122 }
123
09f73ea7 124 /**
ef666303 125 * Changes the current password
66cc653d
D
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
09f73ea7 132 */
ef666303
M
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");
4f169fdc 138 this.hashedPassword = Password.getSaltedHash(newPassword);
ed6d035d
M
139 }
140
09f73ea7
M
141 /**
142 * Checks if the given string represents a valid email address
ef666303
M
143 * @param mail
144 * address
09f73ea7
M
145 * @return
146 */
ef666303 147 private boolean isValidEmailAddress(String mail) {
9ca2608d
M
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();
0a2e269c 152 }
ef666303
M
153
154 /*
155 * (non-Javadoc)
87c1587b
M
156 * @see java.lang.Object#clone()
157 */
a08b0437 158 @Override
ef666303
M
159 public User clone() throws CloneNotSupportedException {
160 return (User) super.clone();
161 }
66cc653d 162}