blob: 12cfd4d59015b5849f8058cff196cbd53715e1ea (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
/**
* Copyright (c) 2015 Camil Staps <info@camilstaps.nl>
* See the LICENSE file for copying permission.
*/
package com.camilstaps.shop;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
/**
* A User is a person with a U/S-number, email and password.
* He may be blocked / be an admin, and has a Cart.
* @author Camil Staps, s4498062
*/
public class User extends DatabaseItem {
private final String nr;
private final String email;
private String hash;
private final boolean isAdmin;
private final Cart cart = new Cart();
private boolean isBlocked = false;
public User (String nr, String email) {
this.nr = nr;
this.email = email;
this.isAdmin = false;
}
public User (String nr, String email, boolean isAdmin) {
this.nr = nr;
this.email = email;
this.isAdmin = isAdmin;
}
public String getNr() {
return nr;
}
public String getEmail() {
return email;
}
public boolean isAdmin() {
return isAdmin;
}
public Cart getCart() {
return cart;
}
public void setBlocked(boolean set) {
isBlocked = set;
}
public boolean isBlocked() {
return isBlocked;
}
public Set<Article> getArticles() {
Set<Article> articles = Database.getInstance().getArticles();
Set<Article> result = new HashSet<>();
for (Article a : articles) {
if (a.getOwner().getNr().equals(nr)) {
result.add(a);
}
}
return result;
}
public Set<Order> getOrders() {
Set<Order> orders = Database.getInstance().getOrders();
Set<Order> result = new HashSet<>();
for (Order o : orders) {
if (o.getUser().getNr().equals(nr)) {
result.add(o);
}
}
return result;
}
/**
* Set a random new password for the user
* @return the new password
*/
public String setRandomPassword() {
String pw = generatePassword();
hash = hash(pw);
return pw;
}
/**
* Hash a password. Currently, this is just the identity function.
* @param password
* @return
*/
public static String hash(String password) {
return password;
}
/**
* Generate a random password
* @return
*/
private static String generatePassword() {
// Only characters that cannot easily be confused
final String drawFrom = "123456789abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
final Random r = new Random();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 12; i++) {
sb.append(drawFrom.charAt(r.nextInt(drawFrom.length())));
}
return sb.toString();
}
/**
* Verify if a password matches the user's password
* @param password the password to check
* @return
*/
public boolean verify(String password) {
return hash(password).equals(hash);
}
@Override
public String toString() {
return (isAdmin ? "++ " : "") + nr + " (" + email + ")" + (isBlocked ? " -!-" : "");
}
/**
* This User as a String, with a detailed and a quick view
* @param showSensitive whether to show sensitive data (email, isAdmin, isBlocked) or not
* @return
*/
public String toString(boolean showSensitive) {
return showSensitive ? toString() : nr;
}
}
|