Controller Struktur angepasst
[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
12 public class User implements Cloneable {
13
14 private final String userName;
15 private String hashedPassword;
16 private String mail;
17 private String name;
18 private String lastname;
19
20 /**
21 * Constructor for already existing <code>User</code> in the Datacontext.
22 * Only to be used by <code>DataHandler</code> implementation!
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
33 */
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) {
37 this.userName = userName;
38 this.hashedPassword = hashedPassword;
39 this.mail = mail;
40 this.name = name;
41 this.lastname = lastname;
42 }
43
44 /**
45 * Normal constructor for new users created in the UserController
46 * @param userName
47 * user name
48 * @param password
49 * password
50 * @throws IllegalArgumentException
51 * user name or password does not meet requirements
52 * @throws Exception
53 * thrown by <code>Password</code> class
54 */
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");
62
63 this.userName = userName;
64 this.hashedPassword = Password.getSaltedHash(password);
65 }
66
67 public String getUserName() {
68 return userName;
69 }
70
71 public String getMail() {
72 return mail;
73 }
74
75 public String getName() {
76 return name;
77 }
78
79 public String getLastname() {
80 return lastname;
81 }
82
83 public String getEncryptedPassword() {
84 return hashedPassword;
85 }
86
87 public boolean setMail(String mail) {
88 if (mail != null) {
89 if (isValidEmailAddress(mail)) {
90 this.mail = mail;
91 return true;
92 }
93 }
94 throw new IllegalArgumentException("Must be valid e-mail adress");
95 }
96
97 public void setName(String name) {
98 this.name = name;
99 }
100
101 public void setLastname(String lastname) {
102 this.lastname = lastname;
103 }
104
105 /**
106 * Checks if the given password matches the hashedPassword
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
115 */
116 public boolean checkPassword(String password) throws Exception {
117 if (password == null)
118 throw new IllegalArgumentException("Password must not be null");
119 return Password.check(password, this.hashedPassword);
120 }
121
122 /**
123 * Changes the current password
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
130 */
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");
136 this.hashedPassword = Password.getSaltedHash(newPassword);
137 }
138
139 /**
140 * Checks if the given string represents a valid email address
141 * @param mail
142 * address
143 * @return
144 */
145 private boolean isValidEmailAddress(String mail) {
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();
150 }
151
152 /*
153 * (non-Javadoc)
154 * @see java.lang.Object#clone()
155 */
156 @Override
157 public User clone() throws CloneNotSupportedException {
158 return (User) super.clone();
159 }
160 }