packaging updated
[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
M
10import ch.bfh.ti.srs.extern.Password;
11
0a2e269c 12public class User {
9ca2608d 13
0a2e269c 14 private final String userName;
4f169fdc 15 private String hashedPassword;
9ca2608d
M
16 private String mail;
17 private String name;
18 private String lastname;
19
06726c50 20 /**
1f7b5004
M
21 * Constructor for already existing user in the Datacontext
22 * Only to be used by DataHandler implementation!
23 *
eae7526d
M
24 * @param userName Username
25 * @param hashedPassword Encrypted password
26 * @param mail Mail
27 * @param name Name
28 * @param lastname Lastname
06726c50 29 */
1f7b5004 30 //TODO: Change constructor to package when DataHandlerForExcel is moved back to this package
4f169fdc 31 public User(String userName, String hashedPassword, String mail, String name, String lastname){
1ec0e01f 32 this.userName = userName;
4f169fdc 33 this.hashedPassword = hashedPassword;
1ec0e01f
M
34 this.mail = mail;
35 this.name = name;
36 this.lastname = lastname;
37 }
9ca2608d 38
1f7b5004
M
39 /**
40 * Normal constructor for new users created in the UserController
41 *
42 * @param userName
43 * @param password
44 * @throws Exception
45 */
ed6d035d
M
46 public User(String userName, String password) throws Exception{
47 if(userName == null) throw new IllegalArgumentException("Username must not be null");
48 if(userName.length() < 4) throw new IllegalArgumentException("Username must have at least 4 characters");
49 if(password == null) throw new IllegalArgumentException("Password must not be null");
1f7b5004 50
9ca2608d 51 this.userName = userName;
4f169fdc 52 this.hashedPassword = Password.getSaltedHash(password);
0a2e269c 53 }
9ca2608d 54
0a2e269c
M
55 public String getUserName(){
56 return userName;
57 }
9ca2608d 58
ed6d035d
M
59 public String getMail(){
60 return mail;
9ca2608d 61 }
ed6d035d
M
62
63 public String getName() {
64 return name;
65 }
66
67 public String getLastname() {
68 return lastname;
69 }
70
1ec0e01f 71 public String getEncryptedPassword(){
4f169fdc 72 return hashedPassword;
1ec0e01f 73 }
ed6d035d 74
9ca2608d 75 public boolean setMail(String mail){
cb599a4a
M
76 if(mail != null){
77 if(isValidEmailAddress(mail)){
78 this.mail = mail;
79 return true;
80 }
9ca2608d 81 }
ed6d035d 82 throw new IllegalArgumentException("Must be valid e-mail adress");
9ca2608d
M
83 }
84
85 public void setName(String name) {
86 this.name = name;
87 }
ed6d035d 88
4481dffa 89 public void setLastname(String lastname) {
9ca2608d
M
90 this.lastname = lastname;
91 }
ed6d035d 92
09f73ea7
M
93 /**
94 * Checks if the given password matches the hashedPassword
95 *
96 * @param password
97 * @return
98 * @throws Exception
99 */
ed6d035d
M
100 public boolean checkPassword(String password) throws Exception{
101 if(password == null) throw new IllegalArgumentException("Password must not be null");
4f169fdc 102 return Password.check(password, this.hashedPassword);
ed6d035d
M
103 }
104
09f73ea7
M
105 /**
106 * Changes the current password to the given newPassword param, if newPassword is whether null nor equals the current password
107 *
108 * @param newPassword
109 * @throws Exception
110 */
ed6d035d
M
111 public void changePassword(String newPassword) throws Exception{
112 if(newPassword == null) throw new IllegalArgumentException("Password must not be null");
113 if(checkPassword(newPassword)) throw new IllegalArgumentException("Can not be the current password");
4f169fdc 114 this.hashedPassword = Password.getSaltedHash(newPassword);
ed6d035d
M
115 }
116
09f73ea7
M
117 /**
118 * Checks if the given string represents a valid email address
119 *
120 * @param mail address
121 * @return
122 */
9ca2608d
M
123 private boolean isValidEmailAddress(String mail){
124 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,}))$";
125 java.util.regex.Pattern p = java.util.regex.Pattern.compile(ePattern);
126 java.util.regex.Matcher m = p.matcher(mail);
127 return m.matches();
0a2e269c
M
128 }
129}