Permission handling removed
[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!
1f7b5004 25 *
87c1587b
M
26 * @param userName user name
27 * @param hashedPassword encrypted password
28 * @param mail mail
29 * @param name name
30 * @param lastname last name
06726c50 31 */
1f7b5004 32 //TODO: Change constructor to package when DataHandlerForExcel is moved back to this package
4f169fdc 33 public User(String userName, String hashedPassword, String mail, String name, String lastname){
1ec0e01f 34 this.userName = userName;
4f169fdc 35 this.hashedPassword = hashedPassword;
1ec0e01f
M
36 this.mail = mail;
37 this.name = name;
38 this.lastname = lastname;
39 }
9ca2608d 40
1f7b5004
M
41 /**
42 * Normal constructor for new users created in the UserController
43 *
87c1587b
M
44 * @param userName user name
45 * @param password password
46 * @throws IllegalArgumentException user name or password does not meet requirements
1f7b5004 47 */
ed6d035d
M
48 public User(String userName, String password) throws Exception{
49 if(userName == null) throw new IllegalArgumentException("Username must not be null");
50 if(userName.length() < 4) throw new IllegalArgumentException("Username must have at least 4 characters");
51 if(password == null) throw new IllegalArgumentException("Password must not be null");
1f7b5004 52
9ca2608d 53 this.userName = userName;
4f169fdc 54 this.hashedPassword = Password.getSaltedHash(password);
0a2e269c 55 }
9ca2608d 56
0a2e269c
M
57 public String getUserName(){
58 return userName;
59 }
9ca2608d 60
ed6d035d
M
61 public String getMail(){
62 return mail;
9ca2608d 63 }
ed6d035d
M
64
65 public String getName() {
66 return name;
67 }
68
69 public String getLastname() {
70 return lastname;
71 }
72
1ec0e01f 73 public String getEncryptedPassword(){
4f169fdc 74 return hashedPassword;
1ec0e01f 75 }
ed6d035d 76
9ca2608d 77 public boolean setMail(String mail){
cb599a4a
M
78 if(mail != null){
79 if(isValidEmailAddress(mail)){
80 this.mail = mail;
81 return true;
82 }
9ca2608d 83 }
ed6d035d 84 throw new IllegalArgumentException("Must be valid e-mail adress");
9ca2608d
M
85 }
86
87 public void setName(String name) {
88 this.name = name;
89 }
ed6d035d 90
4481dffa 91 public void setLastname(String lastname) {
9ca2608d
M
92 this.lastname = lastname;
93 }
ed6d035d 94
09f73ea7
M
95 /**
96 * Checks if the given password matches the hashedPassword
97 *
98 * @param password
99 * @return
100 * @throws Exception
101 */
ed6d035d
M
102 public boolean checkPassword(String password) throws Exception{
103 if(password == null) throw new IllegalArgumentException("Password must not be null");
4f169fdc 104 return Password.check(password, this.hashedPassword);
ed6d035d
M
105 }
106
09f73ea7
M
107 /**
108 * Changes the current password to the given newPassword param, if newPassword is whether null nor equals the current password
109 *
110 * @param newPassword
111 * @throws Exception
112 */
ed6d035d
M
113 public void changePassword(String newPassword) throws Exception{
114 if(newPassword == null) throw new IllegalArgumentException("Password must not be null");
115 if(checkPassword(newPassword)) throw new IllegalArgumentException("Can not be the current password");
4f169fdc 116 this.hashedPassword = Password.getSaltedHash(newPassword);
ed6d035d
M
117 }
118
09f73ea7
M
119 /**
120 * Checks if the given string represents a valid email address
121 *
122 * @param mail address
123 * @return
124 */
9ca2608d
M
125 private boolean isValidEmailAddress(String mail){
126 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,}))$";
127 java.util.regex.Pattern p = java.util.regex.Pattern.compile(ePattern);
128 java.util.regex.Matcher m = p.matcher(mail);
129 return m.matches();
0a2e269c 130 }
a08b0437 131
87c1587b
M
132 /* (non-Javadoc)
133 * @see java.lang.Object#clone()
134 */
a08b0437
M
135 @Override
136 public User clone() throws CloneNotSupportedException {
137 return (User)super.clone();
138 }
0a2e269c 139}