Search in sources :

Example 1 with AuthenticationException

use of com.willshex.blogwt.server.api.exception.AuthenticationException 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.batchLookup(RoleServiceProvider.provide(), output.session.user.roleKeys);
    }
    if (output.session.user.permissionKeys != null) {
        output.session.user.permissions = PersistenceHelper.batchLookup(PermissionServiceProvider.provide(), output.session.user.permissionKeys);
    }
}
Also used : User(com.willshex.blogwt.shared.api.datatype.User) AuthenticationException(com.willshex.blogwt.server.api.exception.AuthenticationException) ISessionService(com.willshex.blogwt.server.service.session.ISessionService) IUserService(com.willshex.blogwt.server.service.user.IUserService) LoginRequest(com.willshex.blogwt.shared.api.user.call.LoginRequest)

Aggregations

AuthenticationException (com.willshex.blogwt.server.api.exception.AuthenticationException)1 ISessionService (com.willshex.blogwt.server.service.session.ISessionService)1 IUserService (com.willshex.blogwt.server.service.user.IUserService)1 User (com.willshex.blogwt.shared.api.datatype.User)1 LoginRequest (com.willshex.blogwt.shared.api.user.call.LoginRequest)1