This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
AbstractProfileAction.java
107 lines (87 loc) · 2.71 KB
/
AbstractProfileAction.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
package org.zanata.action;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;
import javax.inject.Inject;
import org.jboss.seam.security.management.JpaIdentityStore;
import org.zanata.dao.AccountDAO;
import org.zanata.dao.PersonDAO;
import org.zanata.model.HAccount;
import org.zanata.model.HPerson;
import org.zanata.security.ZanataIdentity;
import org.zanata.ui.faces.FacesMessages;
/**
* @author Patrick Huang
* <a href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
*/
public abstract class AbstractProfileAction {
protected String name;
protected String email;
protected String username;
protected boolean valid;
private String activationKey;
@Inject
ZanataIdentity identity;
@Inject
private FacesMessages facesMessages;
@Inject /* TODO [CDI] check this: migrated from @Inject(required = false, value = JpaIdentityStore.AUTHENTICATED_USER) */
HAccount authenticatedAccount;
@Inject
PersonDAO personDAO;
@Inject
AccountDAO accountDAO;
protected void validateEmail(String email) {
HPerson person = personDAO.findByEmail(email);
if (person != null && !person.getAccount().equals(authenticatedAccount)) {
valid = false;
facesMessages.addToControl("email",
"This email address is already taken");
}
}
protected void validateUsername(String username) {
HAccount account = accountDAO.getByUsername(username);
if (account != null && !account.equals(authenticatedAccount)) {
valid = false;
facesMessages.addToControl("username",
"This username is already taken");
}
}
@NotEmpty
@Size(min = 2, max = 80)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Email
@NotEmpty
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.validateEmail(email);
this.email = email;
}
@NotEmpty
@Size(min = 3, max = 20)
@Pattern(regexp = "^[a-z\\d_]{3,20}$",
message = "{validation.username.constraints}")
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
validateUsername(username);
}
public String getActivationKey() {
return activationKey;
}
public void setActivationKey(String keyHash) {
this.activationKey = keyHash;
}
public boolean isValid() {
return valid;
}
}