Permission handling removed
[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
22 /**
23 * Constructor for already existing <code>User</code> in the Datacontext.
24 * Only to be used by <code>DataHandler</code> implementation!
25 *
26 * @param userName user name
27 * @param hashedPassword encrypted password
28 * @param mail mail
29 * @param name name
30 * @param lastname last name
31 */
32 //TODO: Change constructor to package when DataHandlerForExcel is moved back to this package
33 public User(String userName, String hashedPassword, String mail, String name, String lastname){
34 this.userName = userName;
35 this.hashedPassword = hashedPassword;
36 this.mail = mail;
37 this.name = name;
38 this.lastname = lastname;
39 }
40
41 /**
42 * Normal constructor for new users created in the UserController
43 *
44 * @param userName user name
45 * @param password password
46 * @throws IllegalArgumentException user name or password does not meet requirements
47 */
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");
52
53 this.userName = userName;
54 this.hashedPassword = Password.getSaltedHash(password);
55 }
56
57 public String getUserName(){
58 return userName;
59 }
60
61 public String getMail(){
62 return mail;
63 }
64
65 public String getName() {
66 return name;
67 }
68
69 public String getLastname() {
70 return lastname;
71 }
72
73 public String getEncryptedPassword(){
74 return hashedPassword;
75 }
76
77 public boolean setMail(String mail){
78 if(mail != null){
79 if(isValidEmailAddress(mail)){
80 this.mail = mail;
81 return true;
82 }
83 }
84 throw new IllegalArgumentException("Must be valid e-mail adress");
85 }
86
87 public void setName(String name) {
88 this.name = name;
89 }
90
91 public void setLastname(String lastname) {
92 this.lastname = lastname;
93 }
94
95 /**
96 * Checks if the given password matches the hashedPassword
97 *
98 * @param password
99 * @return
100 * @throws Exception
101 */
102 public boolean checkPassword(String password) throws Exception{
103 if(password == null) throw new IllegalArgumentException("Password must not be null");
104 return Password.check(password, this.hashedPassword);
105 }
106
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 */
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");
116 this.hashedPassword = Password.getSaltedHash(newPassword);
117 }
118
119 /**
120 * Checks if the given string represents a valid email address
121 *
122 * @param mail address
123 * @return
124 */
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();
130 }
131
132 /* (non-Javadoc)
133 * @see java.lang.Object#clone()
134 */
135 @Override
136 public User clone() throws CloneNotSupportedException {
137 return (User)super.clone();
138 }
139 }