java-doc update
[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
a08b0437 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
54 * @throws Exception thrown by <code>Password</code> class
1f7b5004 55 */
ef666303
M
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");
1f7b5004 63
9ca2608d 64 this.userName = userName;
4f169fdc 65 this.hashedPassword = Password.getSaltedHash(password);
0a2e269c 66 }
9ca2608d 67
ef666303 68 public String getUserName() {
0a2e269c
M
69 return userName;
70 }
9ca2608d 71
ef666303 72 public String getMail() {
ed6d035d 73 return mail;
9ca2608d 74 }
ef666303 75
ed6d035d
M
76 public String getName() {
77 return name;
78 }
ef666303 79
ed6d035d
M
80 public String getLastname() {
81 return lastname;
82 }
ef666303
M
83
84 public String getEncryptedPassword() {
4f169fdc 85 return hashedPassword;
1ec0e01f 86 }
ef666303
M
87
88 public boolean setMail(String mail) {
89 if (mail != null) {
90 if (isValidEmailAddress(mail)) {
cb599a4a
M
91 this.mail = mail;
92 return true;
93 }
9ca2608d 94 }
ed6d035d 95 throw new IllegalArgumentException("Must be valid e-mail adress");
9ca2608d
M
96 }
97
98 public void setName(String name) {
99 this.name = name;
100 }
ef666303 101
4481dffa 102 public void setLastname(String lastname) {
9ca2608d
M
103 this.lastname = lastname;
104 }
ef666303 105
09f73ea7
M
106 /**
107 * Checks if the given password matches the hashedPassword
108 *
ef666303
M
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
09f73ea7 113 */
ef666303
M
114 public boolean checkPassword(String password) throws Exception {
115 if (password == null)
116 throw new IllegalArgumentException("Password must not be null");
4f169fdc 117 return Password.check(password, this.hashedPassword);
ed6d035d
M
118 }
119
09f73ea7 120 /**
ef666303 121 * Changes the current password
09f73ea7 122 *
ef666303
M
123 * @param newPassword new password
124 * @throws IllegalArgumentException password does not meet the requirements
125 * @throws Exception thrown by <code>Password</code> class
09f73ea7 126 */
ef666303
M
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");
4f169fdc 132 this.hashedPassword = Password.getSaltedHash(newPassword);
ed6d035d
M
133 }
134
09f73ea7
M
135 /**
136 * Checks if the given string represents a valid email address
ef666303
M
137 * @param mail
138 * address
09f73ea7
M
139 * @return
140 */
ef666303 141 private boolean isValidEmailAddress(String mail) {
9ca2608d
M
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();
0a2e269c 146 }
ef666303
M
147
148 /*
149 * (non-Javadoc)
87c1587b
M
150 * @see java.lang.Object#clone()
151 */
a08b0437 152 @Override
ef666303
M
153 public User clone() throws CloneNotSupportedException {
154 return (User) super.clone();
155 }
0a2e269c 156}