This repository has been archived by the owner on Feb 12, 2020. It is now read-only.
/
UserAccount.java
406 lines (354 loc) · 9.1 KB
/
UserAccount.java
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
* Copyright (C) 2010-2012 Taylor Leese (tleese22@gmail.com)
*
* This file is part of jappstart.
*
* jappstart is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* jappstart is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with jappstart. If not, see <http://www.gnu.org/licenses/>.
*/
package com.jappstart.model.auth;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import org.springframework.security.crypto.codec.Hex;
import org.springframework.stereotype.Repository;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
/**
* The user account entity class.
*/
@SuppressWarnings("serial")
@Repository
@Entity
public class UserAccount implements Serializable {
/**
* The key.
*/
@Id
private Key key;
/**
* The username.
*/
private String username;
/**
* The e-mail address.
*/
private String email;
/**
* The display name.
*/
private String displayName;
/**
* The password.
*/
private String password;
/**
* The salt.
*/
private String salt;
/**
* The role.
*/
private String role;
/**
* The activation key.
*/
private String activationKey;
/**
* The activation e-mail status.
*/
private boolean activationEmailSent;
/**
* The enabled status.
*/
private boolean enabled;
/**
* The account non-expired status.
*/
private boolean accountNonExpired;
/**
* The account non-locked status.
*/
private boolean accountNonLocked;
/**
* The credentials non-expired status.
*/
private boolean credentialsNonExpired;
/**
* The persistent user.
*/
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private PersistentUser persistentUser;
/**
* Create a user account with a unique username.
*
* @param username the username
*/
public UserAccount(final String username) {
MessageDigest sDigest = null;
MessageDigest aDigest = null;
this.key = KeyFactory.createKey(getClass().getSimpleName(), username);
this.username = username;
this.enabled = false;
this.accountNonExpired = true;
this.accountNonLocked = true;
this.credentialsNonExpired = true;
this.role = "ROLE_USER";
try {
sDigest = MessageDigest.getInstance("SHA-256");
aDigest = MessageDigest.getInstance("SHA-256");
sDigest.update(UUID.randomUUID().toString().getBytes("UTF-8"));
aDigest.update(UUID.randomUUID().toString().getBytes("UTF-8"));
} catch (NoSuchAlgorithmException e) {
throw new IllegalArgumentException(e);
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException(e);
}
this.salt = String.valueOf(Hex.encode(sDigest.digest()));
this.activationKey = String.valueOf(Hex.encode(aDigest.digest()));
this.activationEmailSent = false;
}
/**
* Returns the key.
*
* @return the key
*/
public final Key getKey() {
return key;
}
/**
* Sets the key.
*
* @param key the key
*/
public final void setKey(final Key key) {
this.key = key;
}
/**
* Returns the username.
*
* @return the username
*/
public final String getUsername() {
return username;
}
/**
* Sets the username.
*
* @param username the username
*/
public final void setUsername(final String username) {
this.username = username;
}
/**
* Returns the email.
*
* @return the email
*/
public final String getEmail() {
return email;
}
/**
* Sets the email.
*
* @param email the email
*/
public final void setEmail(final String email) {
this.email = email;
}
/**
* Gets the display name.
*
* @return the display name
*/
public final String getDisplayName() {
return displayName;
}
/**
* Sets the display name.
*
* @param displayName the display name
*/
public final void setDisplayName(final String displayName) {
this.displayName = displayName;
}
/**
* Returns the password.
*
* @return the password
*/
public final String getPassword() {
return password;
}
/**
* Sets the password.
*
* @param password the password
*/
public final void setPassword(final String password) {
this.password = password;
}
/**
* Returns the salt.
*
* @return the salt
*/
public final String getSalt() {
return salt;
}
/**
* Sets the salt.
*
* @param salt the salt
*/
public final void setSalt(final String salt) {
this.salt = salt;
}
/**
* Returns the role.
*
* @return the role
*/
public final String getRole() {
return role;
}
/**
* Sets the role.
*
* @param role the role
*/
public final void setRole(final String role) {
this.role = role;
}
/**
* Gets the activation key.
*
* @return the activation key
*/
public final String getActivationKey() {
return activationKey;
}
/**
* Sets the activation key.
*
* @param activationKey the activation key
*/
public final void setActivationKey(final String activationKey) {
this.activationKey = activationKey;
}
/**
* Indicates if the activation e-mail has been sent.
*
* @return true if sent; false otherwise
*/
public final boolean isActivationEmailSent() {
return activationEmailSent;
}
/**
* Sets the activation e-mail sent status.
*
* @param activationEmailSent true if sent; false otherwise
*/
public final void setActivationEmailSent(
final boolean activationEmailSent) {
this.activationEmailSent = activationEmailSent;
}
/**
* Indicates if the account is enabled.
*
* @return true if enabled; false otherwise
*/
public final boolean isEnabled() {
return enabled;
}
/**
* Sets the enabled status.
*
* @param enabled true if enabled; false otherwise
*/
public final void setEnabled(final boolean enabled) {
this.enabled = enabled;
}
/**
* Indicates if the account is non-expired.
*
* @return true if non-expired; false otherwise
*/
public final boolean isAccountNonExpired() {
return accountNonExpired;
}
/**
* Sets the account non-expired status.
*
* @param accountNonExpired true if non-expired; false otherwise
*/
public final void setAccountNonExpired(final boolean accountNonExpired) {
this.accountNonExpired = accountNonExpired;
}
/**
* Indicates if the account is non-locked.
*
* @return true if non-locked; false otherwise
*/
public final boolean isAccountNonLocked() {
return accountNonLocked;
}
/**
* Sets the account non-locked status.
*
* @param accountNonLocked true if non-locked; false otherwise
*/
public final void setAccountNonLocked(final boolean accountNonLocked) {
this.accountNonLocked = accountNonLocked;
}
/**
* Indicates if the account credentials are non-expired.
*
* @return true if non-expired; false otherwise
*/
public final boolean isCredentialsNonExpired() {
return credentialsNonExpired;
}
/**
* Sets the account credentials non-expired status.
*
* @param credentialsNonExpired true if non-expired; false otherwise
*/
public final void setCredentialsNonExpired(
final boolean credentialsNonExpired) {
this.credentialsNonExpired = credentialsNonExpired;
}
/**
* Gets the persistent user.
*
* @return the persistent user
*/
public final PersistentUser getPersistentUser() {
return persistentUser;
}
/**
* Sets the persistent user.
*
* @param persistentUser the persistent user
*/
public final void setPersistentUser(final PersistentUser persistentUser) {
this.persistentUser = persistentUser;
}
}