-
Notifications
You must be signed in to change notification settings - Fork 51
/
PasswordResetRequestAction.java
142 lines (128 loc) · 4.41 KB
/
PasswordResetRequestAction.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
package org.zanata.action;
import org.apache.commons.lang3.StringUtils;
import org.apache.deltaspike.jpa.api.transaction.Transactional;
import org.hibernate.validator.constraints.NotEmpty;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Model;
import javax.inject.Inject;
import javax.inject.Named;
import org.zanata.dao.AccountActivationKeyDAO;
import org.zanata.dao.AccountDAO;
import org.zanata.i18n.Messages;
import org.zanata.model.HAccount;
import org.zanata.model.HAccountActivationKey;
import org.zanata.model.HAccountResetPasswordKey;
import org.zanata.service.EmailService;
import org.zanata.service.UserAccountService;
import org.zanata.ui.faces.FacesMessages;
import java.io.Serializable;
import java.util.Date;
@Named("passwordResetRequest")
@RequestScoped
@Model
@Transactional
public class PasswordResetRequestAction implements Serializable {
private static final long serialVersionUID = 1L;
@Inject
private FacesMessages facesMessages;
@Inject
private AccountDAO accountDAO;
@Inject
private EmailService emailServiceImpl;
@Inject
private UserAccountService userAccountServiceImpl;
@Inject
private Messages msgs;
@Inject
private AccountActivationKeyDAO accountActivationKeyDAO;
private String activationKey;
@NotEmpty
private String usernameOrEmail;
private HAccount account;
@Transactional
public String requestReset() {
if (getAccount() == null) {
return getAccountNoFoundMessage();
}
HAccountResetPasswordKey key =
userAccountServiceImpl.requestPasswordReset(getAccount());
if (key == null) {
return getAccountNoFoundMessage();
}
if (isAccountWaitingForActivation()) {
facesMessages.addGlobal(msgs.get("jsf.account.notActivated"));
return null;
}
String message = emailServiceImpl.sendPasswordResetEmail(
getAccount().getPerson(), key.getKeyHash());
facesMessages.addGlobal(message);
return "home";
}
private String getAccountNoFoundMessage() {
facesMessages.addGlobal(msgs.get("jsf.account.notFound"));
return null;
}
// @End /* TODO [CDI] commented out end conversation. verify it still work
// */
public String sendActivationEmail(String usernameOrEmail) {
HAccount account = getAccount(usernameOrEmail);
if (account != null) {
HAccountActivationKey key = account.getAccountActivationKey();
if (key != null) {
key.setCreationDate(new Date());
accountActivationKeyDAO.makePersistent(key);
accountActivationKeyDAO.flush();
String message = emailServiceImpl.sendActivationEmail(
account.getPerson().getName(),
account.getPerson().getEmail(),
account.getAccountActivationKey().getKeyHash());
facesMessages.addGlobal(message);
}
}
return "/public/home.xhtml";
}
public boolean isAccountWaitingForActivation() {
HAccount account = getAccount();
if (account == null) {
return false;
}
return account.getAccountActivationKey() != null;
}
public HAccount getAccount() {
if (account == null) {
account = getAccount(usernameOrEmail);
}
return account;
}
private HAccount getAccount(String usernameOrEmail) {
HAccount account = null;
if (isEmailAddress(usernameOrEmail)) {
account = accountDAO.getByEmail(usernameOrEmail);
}
// if account still null after try as email
if (account == null) {
account = accountDAO.getByUsername(usernameOrEmail);
}
return account;
}
/**
* Check if input string has '@' sign
*/
private boolean isEmailAddress(String value) {
return StringUtils.contains(value, "@");
}
public PasswordResetRequestAction() {
}
public void setActivationKey(final String activationKey) {
this.activationKey = activationKey;
}
public String getActivationKey() {
return this.activationKey;
}
public void setUsernameOrEmail(final String usernameOrEmail) {
this.usernameOrEmail = usernameOrEmail;
}
public String getUsernameOrEmail() {
return this.usernameOrEmail;
}
}