AuthView.java
3.84 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
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
package fi.codecrew.moya.web.cdiview.user;
import java.security.Principal;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fi.codecrew.moya.beans.BootstrapBeanLocal;
import fi.codecrew.moya.handler.NavigationHandler;
import fi.codecrew.moya.model.EventUser;
import fi.codecrew.moya.model.User;
import fi.codecrew.moya.web.cdiview.GenericCDIView;
@Named
@RequestScoped
public class AuthView extends GenericCDIView {
private static final long serialVersionUID = -124938035666457802L;
private static final Logger logger = LoggerFactory.getLogger(AuthView.class);
private String login;
private String password;
@Inject
private NavigationHandler navihandler;
@EJB
private transient BootstrapBeanLocal bootStrapBean;
private HttpServletRequest getRequest() {
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
Object request = externalContext.getRequest();
return request instanceof HttpServletRequest ? (HttpServletRequest) request : null;
}
public void executeLogout() {
HttpServletRequest req = getRequest();
if (permbean.isLoggedIn()) {
try {
req.logout();
} catch (ServletException e) {
logger.warn("Error executing logout", e);
}
}
req.getSession().invalidate();
navihandler.forward("/frontpage");
}
public String doLogout() {
HttpServletRequest req = getRequest();
if (permbean.isLoggedIn()) {
try {
req.logout();
} catch (ServletException e) {
logger.warn("Error executing logout", e);
}
}
req.getSession().invalidate();
navihandler.forward("/frontpage");
return "/frontpage";
}
public void executeLogin() {
executeLogin(null);
}
public void executeLogin(String onError) {
if(onError == null)
onError = "/auth/loginError";
doLogin(onError);
}
private void doLogin(String onError) {
bootStrapBean.saneDefaults();
if (login == null || password == null || login.isEmpty() || password.isEmpty()) {
return;
}
HttpServletRequest request = getRequest();
if (request.getUserPrincipal() != null) {
logger.info("Principal not empty. need to logout first");
try {
request.logout();
} catch (ServletException e) {
logger.info("Error logging out before logging in...");
}
}
try {
request.login(login.toLowerCase(), password);
} catch (Throwable e) {
logger.info("Error while trying to login {}", e.getMessage());
} finally {
Principal principal = request.getUserPrincipal();
logger.info("Logged in principal: {}", principal);
if (principal != null) {
navihandler.redirectToSaved();
} else {
navihandler.forward(onError);
try {
request.login(User.ANONYMOUS_LOGINNAME, null);
} catch (ServletException e) {
logger.warn("Error while trying to relogin as anonymous", e);
}
}
}
return;
}
public void executeAdduserViewLogin() {
navihandler.saveNavigation("/admin/adduser/update");
doLogin("/admin/adduser/login");
}
public String executeLoginAction() {
executeLogin();
return null;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void executeAdduserAutoLogin(EventUser user, String password2) {
// TODO Auto-generated method stub
this.login = user.getLogin();
this.password = password2;
navihandler.saveNavigation("/admin/adduser/update");
doLogin("/admin/adduser/loginerror");
}
}