packaging updated
[staff/due1/sed-hs15-srs-purple.git] / src / main / java / ch / bfh / ti / srs / core / 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.core;
9
10 import ch.bfh.ti.srs.extern.Password;
11
12 public class User {
13
14 private final String userName;
15 private String hashedPassword;
16 private String mail;
17 private String name;
18 private String lastname;
19
20 /**
21 * Constructor for already existing user in the Datacontext
22 * Only to be used by DataHandler implementation!
23 *
24 * @param userName Username
25 * @param hashedPassword Encrypted password
26 * @param mail Mail
27 * @param name Name
28 * @param lastname Lastname
29 */
30 //TODO: Change constructor to package when DataHandlerForExcel is moved back to this package
31 public User(String userName, String hashedPassword, String mail, String name, String lastname){
32 this.userName = userName;
33 this.hashedPassword = hashedPassword;
34 this.mail = mail;
35 this.name = name;
36 this.lastname = lastname;
37 }
38
39 /**
40 * Normal constructor for new users created in the UserController
41 *
42 * @param userName
43 * @param password
44 * @throws Exception
45 */
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");
50
51 this.userName = userName;
52 this.hashedPassword = Password.getSaltedHash(password);
53 }
54
55 public String getUserName(){
56 return userName;
57 }
58
59 public String getMail(){
60 return mail;
61 }
62
63 public String getName() {
64 return name;
65 }
66
67 public String getLastname() {
68 return lastname;
69 }
70
71 public String getEncryptedPassword(){
72 return hashedPassword;
73 }
74
75 public boolean setMail(String mail){
76 if(mail != null){
77 if(isValidEmailAddress(mail)){
78 this.mail = mail;
79 return true;
80 }
81 }
82 throw new IllegalArgumentException("Must be valid e-mail adress");
83 }
84
85 public void setName(String name) {
86 this.name = name;
87 }
88
89 public void setLastname(String lastname) {
90 this.lastname = lastname;
91 }
92
93 /**
94 * Checks if the given password matches the hashedPassword
95 *
96 * @param password
97 * @return
98 * @throws Exception
99 */
100 public boolean checkPassword(String password) throws Exception{
101 if(password == null) throw new IllegalArgumentException("Password must not be null");
102 return Password.check(password, this.hashedPassword);
103 }
104
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 */
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");
114 this.hashedPassword = Password.getSaltedHash(newPassword);
115 }
116
117 /**
118 * Checks if the given string represents a valid email address
119 *
120 * @param mail address
121 * @return
122 */
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();
128 }
129 }