renamed DataController -> DataHandler
[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
M
14 private final String userName;
15 private String password;
9ca2608d
M
16 private String mail;
17 private String name;
18 private String lastname;
19
06726c50
M
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){
1ec0e01f
M
29 this.userName = userName;
30 this.password = password;
31 this.mail = mail;
32 this.name = name;
33 this.lastname = lastname;
34 }
9ca2608d 35
ed6d035d
M
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
9ca2608d 41 this.userName = userName;
ed6d035d 42 this.password = Password.getSaltedHash(password);
0a2e269c 43 }
9ca2608d 44
ed6d035d 45 //Getter
0a2e269c
M
46 public String getUserName(){
47 return userName;
48 }
9ca2608d 49
ed6d035d
M
50 public String getMail(){
51 return mail;
9ca2608d 52 }
ed6d035d
M
53
54 public String getName() {
55 return name;
56 }
57
58 public String getLastname() {
59 return lastname;
60 }
61
1ec0e01f
M
62 public String getEncryptedPassword(){
63 return password;
64 }
ed6d035d
M
65
66 //Setter
9ca2608d 67 public boolean setMail(String mail){
cb599a4a
M
68 if(mail != null){
69 if(isValidEmailAddress(mail)){
70 this.mail = mail;
71 return true;
72 }
9ca2608d 73 }
ed6d035d 74 throw new IllegalArgumentException("Must be valid e-mail adress");
9ca2608d
M
75 }
76
77 public void setName(String name) {
78 this.name = name;
79 }
ed6d035d 80
4481dffa 81 public void setLastname(String lastname) {
9ca2608d
M
82 this.lastname = lastname;
83 }
ed6d035d
M
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
9ca2608d 98
ed6d035d 99 //private Helper methods
9ca2608d
M
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();
0a2e269c
M
105 }
106}