PasswordResetView.java
3.63 KB
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
package fi.insomnia.bortal.view;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fi.insomnia.bortal.beans.UserBeanLocal;
import fi.insomnia.bortal.model.User;
import fi.insomnia.bortal.utilities.PasswordFunctions;
@ManagedBean(name = "pwdResetView")
@SessionScoped
public class PasswordResetView extends GenericView {
private Integer id;
private String hash;
private static final Logger logger = LoggerFactory.getLogger(PasswordResetView.class);
private String password;
private String confirm;
private User user;
private String mailuser;
@EJB
private UserBeanLocal userbean;
public boolean hashMatch() {
if (id != null && id > 0 && hash != null && hash.length() > 5) {
user = userbean.findPasswordResetUser(id, hash);
}
return user != null;
}
public String change() {
boolean changed = false;
String ret = null;
if (password.length() < 5) {
addFaceMessage("userview.passwordTooShort");
} else if (!password.equals(confirm)) {
addFaceMessage("userview.passwordsDontMatch");
} else if (user != null) {
changed = userbean.resetPassword(user, password, hash);
}
if (changed)
ret = "passwordChanged";
return ret;
}
public String sendMail() {
User userObj = userbean.getUser(mailuser);
if (userObj != null) {
String hashStr = PasswordFunctions.generateRandomString(25);
ExternalContext extcontext = FacesContext.getCurrentInstance().getExternalContext();
StringBuilder path = new StringBuilder()
.append("http://")
.append(extcontext.getRequestServerName());
if (extcontext.getRequestServerPort() != 80) {
path.append(":").append(extcontext.getRequestServerPort());
}
path.append("/")
.append(FacesContext.getCurrentInstance().getExternalContext().getContextName())
.append("/auth/resetPassword.jsf?id=")
.append(userObj.getId()).append("&hash=")
.append(hashStr);
logger.debug("billpath {}", path);
userbean.initPasswordReset(userObj, hashStr, path.toString());
return "resetmailSent";
}
mailuser = null;
this.addFaceMessage("passwordreset.usernotfound");
return null;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getHash() {
return hash;
}
public void setHash(String hash) {
this.hash = hash;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getConfirm() {
return confirm;
}
public void setConfirm(String confirm) {
this.confirm = confirm;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public UserBeanLocal getUserbean() {
return userbean;
}
public void setUserbean(UserBeanLocal userbean) {
this.userbean = userbean;
}
public void setMailuser(String mailuser) {
this.mailuser = mailuser;
}
public String getMailuser() {
return mailuser;
}
}