use of com.willshex.blogwt.server.service.user.IUserService in project blogwt by billy1380.
the class LoginActionHandler method handle.
/* (non-Javadoc)
*
* @see
* com.willshex.gson.web.service.server.ActionHandler#handle(com.willshex.
* gson.web.service.shared.Request,
* com.willshex.gson.web.service.shared.Response) */
@Override
protected void handle(LoginRequest input, LoginResponse output) throws Exception {
input = ApiValidator.request(input, LoginRequest.class);
input.accessCode = ApiValidator.accessCode(input.accessCode, "input.accessCode");
boolean foundToken = false;
if (input.session != null && input.session.id != null) {
foundToken = true;
}
if (!foundToken) {
IUserService userService = UserServiceProvider.provide();
User user = null;
if (input.username != null) {
user = userService.getLoginUser(input.username, input.password);
if (user == null)
throw new AuthenticationException(input.username);
}
if (user == null && input.email != null) {
user = userService.getEmailLoginUser(input.email, input.password);
if (user == null)
throw new AuthenticationException(input.email);
}
if (user == null)
throw new AuthenticationException("Either username or email addressed cannot be null");
ISessionService sessionService = SessionServiceProvider.provide();
if (LOG.isLoggable(Level.FINER)) {
LOG.finer("Getting user session");
}
output.session = sessionService.getUserSession(user);
if (output.session == null) {
if (LOG.isLoggable(Level.FINER)) {
LOG.finer("Existing session not found, creating new session");
}
output.session = sessionService.createUserSession(user, input.longTerm);
UserServiceProvider.provide().updateUserIdLastLoggedIn(user.id);
} else {
if (input.longTerm != null) {
output.session.longTerm(input.longTerm);
}
output.session = SessionServiceProvider.provide().extendSession(output.session);
output.session.user = user;
}
} else {
output.session = SessionValidator.lookupCheckAndExtend(input.session, "input.session");
}
if (output.session.user.roleKeys != null) {
output.session.user.roles = PersistenceHelper.batchLookupKeys(RoleServiceProvider.provide(), output.session.user.roleKeys);
}
if (output.session.user.permissionKeys != null) {
output.session.user.permissions = PersistenceHelper.batchLookupKeys(PermissionServiceProvider.provide(), output.session.user.permissionKeys);
}
}
Aggregations