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