Re-enabled first test.
[staff/due1/sed-hs15-srs-purple.git] / src / main / java / ch / bfh / ti / srs / core / User.java
1 package ch.bfh.ti.srs.core;
2
3 public class User {
4
5 private final String userName;
6 private String password;
7 private String mail;
8 private String name;
9 private String lastname;
10
11
12 public User(String userName, String password){
13 this.userName = userName;
14 try {
15 this.password = Password.getSaltedHash(password);
16 } catch (Exception e) {
17 // TODO: Bad programming style (due1). Discuss.
18 System.err.println("Fehler beim Erstellen des Hashes");
19 }
20 }
21
22 public String getUserName(){
23 return userName;
24 }
25
26 public boolean checkPassword(String password){
27 boolean passwordCorrect = false;
28 try {
29 passwordCorrect = Password.check(password, this.password);
30 } catch (Exception e) {
31 System.err.println("Fehler beim Prüfen des Passwortes");
32 }
33 return passwordCorrect;
34 }
35
36 public boolean setMail(String mail){
37 if(isValidEmailAddress(mail)){
38 this.mail = mail;
39 return true;
40 }
41 return false;
42 }
43
44 public String getMail(){
45 return mail;
46 }
47
48 public void changePassword(String newPassword){
49 try {
50 this.password = Password.getSaltedHash(newPassword);
51 } catch (Exception e) {
52 System.err.println("Fehler beim erstellen des Hashes");
53 }
54 }
55
56 public String getName() {
57 return name;
58 }
59
60 public void setName(String name) {
61 this.name = name;
62 }
63
64 public String getLastname() {
65 return lastname;
66 }
67
68 public void setLastname(String lastname) {
69 this.lastname = lastname;
70 }
71
72 private boolean isValidEmailAddress(String mail){
73 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,}))$";
74 java.util.regex.Pattern p = java.util.regex.Pattern.compile(ePattern);
75 java.util.regex.Matcher m = p.matcher(mail);
76 return m.matches();
77 }
78 }