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