renamed DataController -> DataHandler
[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 password;
16 private String mail;
17 private String name;
18 private String lastname;
19
20 /**
21 * Contructor of already existing user in the system
22 * @param userName
23 * @param password
24 * @param mail
25 * @param name
26 * @param lastname
27 */
28 User(String userName, String password, String mail, String name, String lastname){
29 this.userName = userName;
30 this.password = password;
31 this.mail = mail;
32 this.name = name;
33 this.lastname = lastname;
34 }
35
36 public User(String userName, String password) throws Exception{
37 if(userName == null) throw new IllegalArgumentException("Username must not be null");
38 if(userName.length() < 4) throw new IllegalArgumentException("Username must have at least 4 characters");
39 if(password == null) throw new IllegalArgumentException("Password must not be null");
40
41 this.userName = userName;
42 this.password = Password.getSaltedHash(password);
43 }
44
45 //Getter
46 public String getUserName(){
47 return userName;
48 }
49
50 public String getMail(){
51 return mail;
52 }
53
54 public String getName() {
55 return name;
56 }
57
58 public String getLastname() {
59 return lastname;
60 }
61
62 public String getEncryptedPassword(){
63 return password;
64 }
65
66 //Setter
67 public boolean setMail(String mail){
68 if(mail != null){
69 if(isValidEmailAddress(mail)){
70 this.mail = mail;
71 return true;
72 }
73 }
74 throw new IllegalArgumentException("Must be valid e-mail adress");
75 }
76
77 public void setName(String name) {
78 this.name = name;
79 }
80
81 public void setLastname(String lastname) {
82 this.lastname = lastname;
83 }
84
85
86 //Password methods
87 public boolean checkPassword(String password) throws Exception{
88 if(password == null) throw new IllegalArgumentException("Password must not be null");
89 return Password.check(password, this.password);
90 }
91
92 public void changePassword(String newPassword) throws Exception{
93 if(newPassword == null) throw new IllegalArgumentException("Password must not be null");
94 if(checkPassword(newPassword)) throw new IllegalArgumentException("Can not be the current password");
95 this.password = Password.getSaltedHash(newPassword);
96 }
97
98
99 //private Helper methods
100 private boolean isValidEmailAddress(String mail){
101 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,}))$";
102 java.util.regex.Pattern p = java.util.regex.Pattern.compile(ePattern);
103 java.util.regex.Matcher m = p.matcher(mail);
104 return m.matches();
105 }
106 }