Search in sources :

Example 21 with CreateApplicationEventRequest

use of com.hack23.cia.service.api.action.application.CreateApplicationEventRequest in project cia by Hack23.

the class CreateApplicationEventService method processService.

@Override
public CreateApplicationEventResponse processService(final CreateApplicationEventRequest serviceRequest) {
    final CreateApplicationEventResponse inputValidation = inputValidation(serviceRequest);
    if (inputValidation != null) {
        return inputValidation;
    }
    final ApplicationSession applicationSession = applicationSessionDAO.findFirstByProperty(ApplicationSession_.sessionId, serviceRequest.getSessionId());
    if (applicationSession != null) {
        final ApplicationActionEvent applicationActionEvent = new ApplicationActionEvent();
        applicationActionEvent.setEventGroup(serviceRequest.getEventGroup());
        applicationActionEvent.setCreatedDate(new Date());
        applicationActionEvent.setSessionId(serviceRequest.getSessionId());
        applicationActionEvent.setPage(serviceRequest.getPage());
        applicationActionEvent.setPageMode(serviceRequest.getPageMode());
        applicationActionEvent.setElementId(serviceRequest.getElementId());
        applicationActionEvent.setApplicationOperation(serviceRequest.getApplicationOperation());
        applicationActionEvent.setActionName(serviceRequest.getActionName());
        applicationActionEvent.setUserId(serviceRequest.getUserId());
        if (serviceRequest.getUserId() != null && ApplicationSessionType.ANONYMOUS == applicationSession.getSessionType()) {
            applicationSession.setSessionType(ApplicationSessionType.REGISTERED_USER);
            applicationSession.setUserId(serviceRequest.getUserId());
        }
        applicationActionEvent.setApplicationMessage(serviceRequest.getApplicationMessage());
        applicationActionEvent.setErrorMessage(serviceRequest.getErrorMessage());
        applicationSession.getEvents().add(applicationActionEvent);
        applicationSessionDAO.persist(applicationSession);
        return new CreateApplicationEventResponse(ServiceResult.SUCCESS);
    } else {
        return new CreateApplicationEventResponse(ServiceResult.FAILURE);
    }
}
Also used : ApplicationSession(com.hack23.cia.model.internal.application.system.impl.ApplicationSession) ApplicationActionEvent(com.hack23.cia.model.internal.application.system.impl.ApplicationActionEvent) CreateApplicationEventResponse(com.hack23.cia.service.api.action.application.CreateApplicationEventResponse) Date(java.util.Date)

Example 22 with CreateApplicationEventRequest

use of com.hack23.cia.service.api.action.application.CreateApplicationEventRequest in project cia by Hack23.

the class LoginService method createApplicationEventForService.

@Override
protected CreateApplicationEventRequest createApplicationEventForService(final LoginRequest serviceRequest) {
    final CreateApplicationEventRequest eventRequest = new CreateApplicationEventRequest();
    eventRequest.setEventGroup(ApplicationEventGroup.USER);
    eventRequest.setApplicationOperation(ApplicationOperationType.AUTHENTICATION);
    eventRequest.setActionName(LoginRequest.class.getSimpleName());
    eventRequest.setSessionId(serviceRequest.getSessionId());
    eventRequest.setElementId(serviceRequest.getEmail());
    return eventRequest;
}
Also used : CreateApplicationEventRequest(com.hack23.cia.service.api.action.application.CreateApplicationEventRequest) LoginRequest(com.hack23.cia.service.api.action.application.LoginRequest)

Example 23 with CreateApplicationEventRequest

use of com.hack23.cia.service.api.action.application.CreateApplicationEventRequest in project cia by Hack23.

the class LoginService method processService.

@Override
@Secured({ "ROLE_ANONYMOUS", "ROLE_USER", "ROLE_ADMIN" })
public LoginResponse processService(final LoginRequest serviceRequest) {
    final LoginResponse inputValidation = inputValidation(serviceRequest);
    if (inputValidation != null) {
        return inputValidation;
    }
    final CreateApplicationEventRequest eventRequest = createApplicationEventForService(serviceRequest);
    final UserAccount userExist = userDAO.findFirstByProperty(UserAccount_.email, serviceRequest.getEmail());
    final LoginBlockResult loginBlockResult = loginBlockedAccess.isBlocked(serviceRequest.getSessionId(), serviceRequest.getEmail());
    LoginResponse response;
    if (!loginBlockResult.isBlocked() && userExist != null && userExist.getUserLockStatus() == UserLockStatus.UNLOCKED && verifyOtp(serviceRequest, userExist) && passwordEncoder.matches(userExist.getUserId() + ".uuid" + serviceRequest.getUserpassword(), userExist.getUserpassword())) {
        final Collection<SimpleGrantedAuthority> authorities = new ArrayList<>();
        if (UserRole.ADMIN == userExist.getUserRole()) {
            authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        } else if (UserRole.USER == userExist.getUserRole()) {
            authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
        }
        eventRequest.setUserId(userExist.getUserId());
        SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(userExist, userExist.getUserpassword(), authorities));
        userExist.setNumberOfVisits(userExist.getNumberOfVisits() + 1);
        userDAO.persist(userExist);
        response = new LoginResponse(ServiceResult.SUCCESS);
    } else {
        response = new LoginResponse(ServiceResult.FAILURE);
        response.setErrorMessage(LoginResponse.ErrorMessage.USERNAME_OR_PASSWORD_DO_NOT_MATCH.toString());
        if (loginBlockResult.isBlocked()) {
            eventRequest.setErrorMessage(loginBlockResult.getMessages().toString());
        } else {
            eventRequest.setErrorMessage(LoginResponse.ErrorMessage.USERNAME_OR_PASSWORD_DO_NOT_MATCH.toString());
        }
    }
    eventRequest.setApplicationMessage(response.getResult().toString());
    createApplicationEventService.processService(eventRequest);
    LOGGER.info("Event: {}", eventRequest);
    return response;
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) LoginResponse(com.hack23.cia.service.api.action.application.LoginResponse) ArrayList(java.util.ArrayList) CreateApplicationEventRequest(com.hack23.cia.service.api.action.application.CreateApplicationEventRequest) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) UserAccount(com.hack23.cia.model.internal.application.user.impl.UserAccount) LoginBlockResult(com.hack23.cia.service.impl.action.application.access.LoginBlockedAccess.LoginBlockResult) Secured(org.springframework.security.access.annotation.Secured)

Example 24 with CreateApplicationEventRequest

use of com.hack23.cia.service.api.action.application.CreateApplicationEventRequest in project cia by Hack23.

the class LogoutService method createApplicationEventForService.

@Override
protected CreateApplicationEventRequest createApplicationEventForService(final LogoutRequest serviceRequest) {
    final CreateApplicationEventRequest eventRequest = new CreateApplicationEventRequest();
    eventRequest.setEventGroup(ApplicationEventGroup.USER);
    eventRequest.setApplicationOperation(ApplicationOperationType.AUTHENTICATION);
    eventRequest.setActionName(LogoutRequest.class.getSimpleName());
    eventRequest.setSessionId(serviceRequest.getSessionId());
    return eventRequest;
}
Also used : CreateApplicationEventRequest(com.hack23.cia.service.api.action.application.CreateApplicationEventRequest) LogoutRequest(com.hack23.cia.service.api.action.application.LogoutRequest)

Example 25 with CreateApplicationEventRequest

use of com.hack23.cia.service.api.action.application.CreateApplicationEventRequest in project cia by Hack23.

the class RegisterUserService method processService.

@Override
@Secured({ "ROLE_ANONYMOUS" })
public RegisterUserResponse processService(final RegisterUserRequest serviceRequest) {
    final RegisterUserResponse inputValidation = inputValidation(serviceRequest);
    if (inputValidation != null) {
        return inputValidation;
    }
    final CreateApplicationEventRequest eventRequest = createApplicationEventForService(serviceRequest);
    RegisterUserResponse response;
    final ApplicationConfiguration registeredUsersGetAdminConfig = applicationConfigurationService.checkValueOrLoadDefault("Registered User All get Role Admin", "Registered User All get Role Admin", ConfigurationGroup.AUTHORIZATION, RegisterUserService.class.getSimpleName(), "Register User Service", "Responsible for create of useraccounts", "registered.users.get.admin", "true");
    final UserAccount userNameExist = userDAO.findFirstByProperty(UserAccount_.username, serviceRequest.getUsername());
    final UserAccount userEmailExist = userDAO.findFirstByProperty(UserAccount_.email, serviceRequest.getEmail());
    final RuleResult passwordRuleResults = passwordValidator.validate(new PasswordData(serviceRequest.getUserpassword()));
    if (userEmailExist == null && userNameExist == null && passwordRuleResults.isValid()) {
        final UserAccount userAccount = new UserAccount();
        userAccount.setCountry(serviceRequest.getCountry());
        userAccount.setEmail(serviceRequest.getEmail());
        userAccount.setUsername(serviceRequest.getUsername());
        userAccount.setUserId(UUID.randomUUID().toString());
        userAccount.setUserpassword(passwordEncoder.encode(userAccount.getUserId() + ".uuid" + serviceRequest.getUserpassword()));
        userAccount.setNumberOfVisits(1);
        if (serviceRequest.getUserType() == null) {
            userAccount.setUserType(UserType.PRIVATE);
        } else {
            userAccount.setUserType(serviceRequest.getUserType());
        }
        userAccount.setUserEmailStatus(UserEmailStatus.UNKNOWN);
        userAccount.setUserLockStatus(UserLockStatus.UNLOCKED);
        userAccount.setCreatedDate(new Date());
        userDAO.persist(userAccount);
        if ("true".equals(registeredUsersGetAdminConfig.getPropertyValue())) {
            userAccount.setUserRole(UserRole.ADMIN);
        } else {
            userAccount.setUserRole(UserRole.USER);
        }
        final Collection<SimpleGrantedAuthority> authorities = new ArrayList<>();
        if (UserRole.ADMIN == userAccount.getUserRole()) {
            authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        } else if (UserRole.USER == userAccount.getUserRole()) {
            authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
        }
        SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(userAccount, userAccount.getUserpassword(), authorities));
        eventRequest.setUserId(userAccount.getUserId());
        response = new RegisterUserResponse(ServiceResult.SUCCESS);
    } else {
        response = new RegisterUserResponse(ServiceResult.FAILURE);
        if (passwordRuleResults.isValid()) {
            response.setErrorMessage(RegisterUserResponse.ErrorMessage.USER_ALREADY_EXIST.toString());
            eventRequest.setErrorMessage(RegisterUserResponse.ErrorMessage.USER_ALREADY_EXIST.toString());
        } else {
            final String errorMessage = passwordValidator.getMessages(passwordRuleResults).toString();
            response.setErrorMessage(errorMessage);
            eventRequest.setErrorMessage(errorMessage);
        }
    }
    eventRequest.setApplicationMessage(response.getResult().toString());
    createApplicationEventService.processService(eventRequest);
    LOGGER.info("Event: {}", eventRequest);
    return response;
}
Also used : ArrayList(java.util.ArrayList) RuleResult(org.passay.RuleResult) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Date(java.util.Date) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) PasswordData(org.passay.PasswordData) RegisterUserResponse(com.hack23.cia.service.api.action.application.RegisterUserResponse) CreateApplicationEventRequest(com.hack23.cia.service.api.action.application.CreateApplicationEventRequest) ApplicationConfiguration(com.hack23.cia.model.internal.application.system.impl.ApplicationConfiguration) UserAccount(com.hack23.cia.model.internal.application.user.impl.UserAccount) Secured(org.springframework.security.access.annotation.Secured)

Aggregations

CreateApplicationEventRequest (com.hack23.cia.service.api.action.application.CreateApplicationEventRequest)32 UserAccount (com.hack23.cia.model.internal.application.user.impl.UserAccount)15 Secured (org.springframework.security.access.annotation.Secured)14 ArrayList (java.util.ArrayList)5 ConstraintViolation (javax.validation.ConstraintViolation)3 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)3 DocumentContentData (com.hack23.cia.model.external.riksdagen.documentcontent.impl.DocumentContentData)2 ApplicationConfiguration (com.hack23.cia.model.internal.application.system.impl.ApplicationConfiguration)2 ApplicationSession (com.hack23.cia.model.internal.application.system.impl.ApplicationSession)2 ManageUserAccountRequest (com.hack23.cia.service.api.action.admin.ManageUserAccountRequest)2 ManageUserAccountResponse (com.hack23.cia.service.api.action.admin.ManageUserAccountResponse)2 CreateApplicationEventResponse (com.hack23.cia.service.api.action.application.CreateApplicationEventResponse)2 Date (java.util.Date)2 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)2 DocumentElement (com.hack23.cia.model.external.riksdagen.dokumentlista.impl.DocumentElement)1 ApplicationActionEvent (com.hack23.cia.model.internal.application.system.impl.ApplicationActionEvent)1 ApplicationEventGroup (com.hack23.cia.model.internal.application.system.impl.ApplicationEventGroup)1 ApplicationOperationType (com.hack23.cia.model.internal.application.system.impl.ApplicationOperationType)1 UserAccount_ (com.hack23.cia.model.internal.application.user.impl.UserAccount_)1 UserLockStatus (com.hack23.cia.model.internal.application.user.impl.UserLockStatus)1