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