java-doc
[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 private Set<Permissions> permissions;
22
23 /**
24 * Constructor for already existing <code>User</code> in the Datacontext.
25 * Only to be used by <code>DataHandler</code> implementation!
26 *
27 * @param userName user name
28 * @param hashedPassword encrypted password
29 * @param mail mail
30 * @param name name
31 * @param lastname last name
32 */
33 //TODO: Change constructor to package when DataHandlerForExcel is moved back to this package
34 public User(String userName, String hashedPassword, String mail, String name, String lastname){
35 this.userName = userName;
36 this.hashedPassword = hashedPassword;
37 this.mail = mail;
38 this.name = name;
39 this.lastname = lastname;
40 permissions = new HashSet<>();
41 }
42
43 /**
44 * Normal constructor for new users created in the UserController
45 *
46 * @param userName user name
47 * @param password password
48 * @throws IllegalArgumentException user name or password does not meet requirements
49 */
50 public User(String userName, String password) throws Exception{
51 if(userName == null) throw new IllegalArgumentException("Username must not be null");
52 if(userName.length() < 4) throw new IllegalArgumentException("Username must have at least 4 characters");
53 if(password == null) throw new IllegalArgumentException("Password must not be null");
54
55 this.userName = userName;
56 this.hashedPassword = Password.getSaltedHash(password);
57 }
58
59 public String getUserName(){
60 return userName;
61 }
62
63 public String getMail(){
64 return mail;
65 }
66
67 public String getName() {
68 return name;
69 }
70
71 public String getLastname() {
72 return lastname;
73 }
74
75 public String getEncryptedPassword(){
76 return hashedPassword;
77 }
78
79 public boolean setMail(String mail){
80 if(mail != null){
81 if(isValidEmailAddress(mail)){
82 this.mail = mail;
83 return true;
84 }
85 }
86 throw new IllegalArgumentException("Must be valid e-mail adress");
87 }
88
89 public void setName(String name) {
90 this.name = name;
91 }
92
93 public void setLastname(String lastname) {
94 this.lastname = lastname;
95 }
96
97 /**
98 * Checks if the given password matches the hashedPassword
99 *
100 * @param password
101 * @return
102 * @throws Exception
103 */
104 public boolean checkPassword(String password) throws Exception{
105 if(password == null) throw new IllegalArgumentException("Password must not be null");
106 return Password.check(password, this.hashedPassword);
107 }
108
109 /**
110 * Changes the current password to the given newPassword param, if newPassword is whether null nor equals the current password
111 *
112 * @param newPassword
113 * @throws Exception
114 */
115 public void changePassword(String newPassword) throws Exception{
116 if(newPassword == null) throw new IllegalArgumentException("Password must not be null");
117 if(checkPassword(newPassword)) throw new IllegalArgumentException("Can not be the current password");
118 this.hashedPassword = Password.getSaltedHash(newPassword);
119 }
120
121 /**
122 * Checks if the given string represents a valid email address
123 *
124 * @param mail address
125 * @return
126 */
127 private boolean isValidEmailAddress(String mail){
128 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,}))$";
129 java.util.regex.Pattern p = java.util.regex.Pattern.compile(ePattern);
130 java.util.regex.Matcher m = p.matcher(mail);
131 return m.matches();
132 }
133
134 /* (non-Javadoc)
135 * @see java.lang.Object#clone()
136 */
137 @Override
138 public User clone() throws CloneNotSupportedException {
139 return (User)super.clone();
140 }
141
142 /**
143 * @param permission
144 */
145 public void addPermission(Permissions permission){
146 permissions.add(permission);
147 }
148
149 /**
150 * @param permission
151 */
152 public void removePermission(Permissions permission){
153 if(permissions.contains(permission)) permissions.remove(permission);
154 }
155
156 /**
157 * @return
158 */
159 public Set<Permissions> getPermissions(){
160 return permissions;
161 }
162
163 /**
164 * @param permission
165 * @return
166 */
167 public boolean hasPermission(Permissions permission){
168 return permissions.contains(permission);
169 }
170 }