Changed variable name password to hashedPassword for encrypted password
[staff/due1/sed-hs15-srs-purple.git] / src / main / java / ch / bfh / ti / srs / core / 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 */
0a2e269c
M
8package ch.bfh.ti.srs.core;
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 *
24 * @param userName Username
25 * @param password 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
ed6d035d 55 //Getter
0a2e269c
M
56 public String getUserName(){
57 return userName;
58 }
9ca2608d 59
ed6d035d
M
60 public String getMail(){
61 return mail;
9ca2608d 62 }
ed6d035d
M
63
64 public String getName() {
65 return name;
66 }
67
68 public String getLastname() {
69 return lastname;
70 }
71
1ec0e01f 72 public String getEncryptedPassword(){
4f169fdc 73 return hashedPassword;
1ec0e01f 74 }
ed6d035d
M
75
76 //Setter
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
M
94
95
96 //Password methods
97 public boolean checkPassword(String password) throws Exception{
98 if(password == null) throw new IllegalArgumentException("Password must not be null");
4f169fdc 99 return Password.check(password, this.hashedPassword);
ed6d035d
M
100 }
101
102 public void changePassword(String newPassword) throws Exception{
103 if(newPassword == null) throw new IllegalArgumentException("Password must not be null");
104 if(checkPassword(newPassword)) throw new IllegalArgumentException("Can not be the current password");
4f169fdc 105 this.hashedPassword = Password.getSaltedHash(newPassword);
ed6d035d
M
106 }
107
9ca2608d 108
ed6d035d 109 //private Helper methods
9ca2608d
M
110 private boolean isValidEmailAddress(String mail){
111 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,}))$";
112 java.util.regex.Pattern p = java.util.regex.Pattern.compile(ePattern);
113 java.util.regex.Matcher m = p.matcher(mail);
114 return m.matches();
0a2e269c
M
115 }
116}