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