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
/
PasswordResetAction.java
142 lines (112 loc) · 4.04 KB
/
PasswordResetAction.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 java.io.Serializable;
import javax.persistence.EntityManager;
import javax.validation.constraints.Size;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.validator.constraints.NotEmpty;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.End;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.security.AuthorizationException;
import org.jboss.seam.security.NotLoggedInException;
import org.jboss.seam.security.RunAsOperation;
import org.jboss.seam.security.management.IdentityManager;
import org.jboss.seam.ui.UnauthorizedCommandException;
import org.zanata.ApplicationConfiguration;
import org.zanata.exception.KeyNotFoundException;
import org.zanata.i18n.Messages;
import org.zanata.model.HAccountResetPasswordKey;
import org.zanata.security.ZanataIdentity;
import org.zanata.ui.faces.FacesMessages;
@Name("passwordReset")
@Scope(ScopeType.CONVERSATION)
public class PasswordResetAction implements Serializable {
private static final long serialVersionUID = -3966625589007754411L;
@In
private EntityManager entityManager;
@In
private IdentityManager identityManager;
@In("jsfMessages")
private FacesMessages facesMessages;
@In
private Messages msgs;
@In
private ApplicationConfiguration applicationConfiguration;
@Getter
private String activationKey;
@Getter
@Setter
@NotEmpty
@Size(min = 6, max = 20)
private String password;
@Getter
private String passwordConfirm;
@Getter
private HAccountResetPasswordKey key;
public void setPasswordConfirm(String passwordConfirm) {
this.passwordConfirm = passwordConfirm;
validatePasswordsMatch();
}
public boolean validatePasswordsMatch() {
if (password == null || !password.equals(passwordConfirm)) {
facesMessages.addToControl("passwordConfirm",
"Passwords do not match");
return false;
}
return true;
}
public void setActivationKey(String activationKey) {
this.activationKey = activationKey;
key =
entityManager.find(HAccountResetPasswordKey.class,
getActivationKey());
}
@Begin(join = true)
public void validateActivationKey() {
if (!applicationConfiguration.isInternalAuth()) {
throw new AuthorizationException(
"Password reset is only available for server using internal authentication");
}
if (getActivationKey() == null)
throw new KeyNotFoundException();
key =
entityManager.find(HAccountResetPasswordKey.class,
getActivationKey());
if (key == null)
throw new KeyNotFoundException();
}
private boolean passwordChanged;
@End
public String changePassword() {
if (!validatePasswordsMatch())
return null;
RunAsOperation operation = new RunAsOperation() {
public void execute() {
try {
passwordChanged =
identityManager.changePassword(getKey()
.getAccount().getUsername(), getPassword());
} catch (AuthorizationException | NotLoggedInException e) {
passwordChanged = false;
facesMessages.addGlobal(
"Error changing password: " + e.getMessage());
}
}
}.addRole("admin");
ZanataIdentity.instance().runAs(operation);
entityManager.remove(getKey());
if (passwordChanged) {
facesMessages
.addGlobal(msgs.get("jsf.password.change.success"));
return "/account/login.xhtml";
} else {
facesMessages
.addGlobal(msgs.get("jsf.password.change.failed"));
return null;
}
}
}