Search in sources :

Example 6 with UserAccount

use of com.hack23.cia.model.internal.application.user.impl.UserAccount in project cia by Hack23.

the class LogoutService method processService.

@Override
@Secured({ "ROLE_USER", "ROLE_ADMIN" })
public LogoutResponse processService(final LogoutRequest serviceRequest) {
    final LogoutResponse inputValidation = inputValidation(serviceRequest);
    if (inputValidation != null) {
        return inputValidation;
    }
    final CreateApplicationEventRequest eventRequest = createApplicationEventForService(serviceRequest);
    final UserAccount userAccount = getUserAccountFromSecurityContext();
    LogoutResponse response;
    if (userAccount != null) {
        eventRequest.setElementId(userAccount.getEmail());
        eventRequest.setUserId(userAccount.getUserId());
        final Collection<SimpleGrantedAuthority> authorities = new ArrayList<>();
        authorities.add(new SimpleGrantedAuthority("ROLE_ANONYMOUS"));
        final AnonymousAuthenticationToken anonymousAuthenticationToken = new AnonymousAuthenticationToken(serviceRequest.getSessionId(), "ROLE_ANONYMOUS", authorities);
        SecurityContextHolder.getContext().setAuthentication(anonymousAuthenticationToken);
        response = new LogoutResponse(ServiceResult.SUCCESS);
    } else {
        response = new LogoutResponse(ServiceResult.FAILURE);
    }
    eventRequest.setApplicationMessage(response.getResult().toString());
    createApplicationEventService.processService(eventRequest);
    LOGGER.info("Event: {}", eventRequest);
    return response;
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) LogoutResponse(com.hack23.cia.service.api.action.application.LogoutResponse) ArrayList(java.util.ArrayList) CreateApplicationEventRequest(com.hack23.cia.service.api.action.application.CreateApplicationEventRequest) AnonymousAuthenticationToken(org.springframework.security.authentication.AnonymousAuthenticationToken) UserAccount(com.hack23.cia.model.internal.application.user.impl.UserAccount) Secured(org.springframework.security.access.annotation.Secured)

Example 7 with UserAccount

use of com.hack23.cia.model.internal.application.user.impl.UserAccount in project cia by Hack23.

the class LoginBlockedAccessImpl method blockByUserFailedLoginAttempts.

/**
 * Block by user failed login attempts.
 *
 * @param email
 *            the email
 * @param loginBlockResultImpl
 *            the login block result impl
 */
private void blockByUserFailedLoginAttempts(final String email, final LoginBlockResultImpl loginBlockResultImpl) {
    final UserAccount userExist = userDAO.findFirstByProperty(UserAccount_.email, email);
    if (userExist != null) {
        final ApplicationConfiguration maxLoginAttemptsByUser = applicationConfigurationService.checkValueOrLoadDefault(MAX_FAILED_LOGIN_ATTEMPTS_RECENT_HOUR_PER_USER, BLOCKS_ANY_LOGIN_ATTEMPTS_AFTER_THIS_NUMBER_IS_REACHED, ConfigurationGroup.AUTHENTICATION, LoginBlockedAccessImpl.class.getSimpleName(), LOGIN_BLOCKER, BLOCKS_LOGIN_ATTEMPTS, APPLICATION_AUTHENTICATION_ALLOW_MAX_RECENT_FAILED_LOGINS_BY_USER, DEFAULT_MAX_LOGIN_ATTEMPTS);
        final List<ApplicationActionEvent> failedLoginsByThisUser = applicationActionEventDAO.findListByProperty(new Object[] { email, ApplicationOperationType.AUTHENTICATION, ServiceResult.FAILURE.toString() }, ApplicationActionEvent_.elementId, ApplicationActionEvent_.applicationOperation, ApplicationActionEvent_.applicationMessage);
        final Date oneHourAgo = new Date(System.currentTimeMillis() - ONE_HOUR);
        final Map<Boolean, List<ApplicationActionEvent>> recentOldLoginAttemptsMap = failedLoginsByThisUser.stream().collect(Collectors.groupingBy((final ApplicationActionEvent x) -> x.getCreatedDate().after(oneHourAgo)));
        final List<ApplicationActionEvent> recentFailedLogins = recentOldLoginAttemptsMap.get(Boolean.TRUE);
        if (recentFailedLogins != null && recentFailedLogins.size() > NumberUtils.toInt(maxLoginAttemptsByUser.getPropertyValue(), DEFAULT_MAX_LOGINS)) {
            loginBlockResultImpl.setBlocked(true);
            loginBlockResultImpl.addMessages(BLOCKED_BY_MORE_THAN_5_RECENT_LOGIN_ATTEMPTS_BY_THIS_USER);
        }
    }
}
Also used : ApplicationActionEvent(com.hack23.cia.model.internal.application.system.impl.ApplicationActionEvent) ArrayList(java.util.ArrayList) List(java.util.List) UserAccount(com.hack23.cia.model.internal.application.user.impl.UserAccount) ApplicationConfiguration(com.hack23.cia.model.internal.application.system.impl.ApplicationConfiguration) Date(java.util.Date)

Example 8 with UserAccount

use of com.hack23.cia.model.internal.application.user.impl.UserAccount in project cia by Hack23.

the class ComplianceCheckServiceImpl method processService.

@Override
@Secured({ "ROLE_USER", "ROLE_ADMIN", "ROLE_ANONYMOUS" })
public ComplianceCheckResponse processService(final ComplianceCheckRequest serviceRequest) {
    final ComplianceCheckResponse inputValidation = inputValidation(serviceRequest);
    if (inputValidation != null) {
        return inputValidation;
    }
    LOGGER.info("{}", serviceRequest.getClass().getSimpleName());
    final CreateApplicationEventRequest eventRequest = createApplicationEventForService(serviceRequest);
    final UserAccount userAccount = getUserAccountFromSecurityContext();
    if (userAccount != null) {
        eventRequest.setUserId(userAccount.getUserId());
    }
    final ComplianceCheckResponse response;
    final Set<ConstraintViolation<ComplianceCheckRequest>> requestConstraintViolations = validateRequest(serviceRequest);
    if (!requestConstraintViolations.isEmpty()) {
        response = handleInputViolations(eventRequest, requestConstraintViolations, new ComplianceCheckResponse(ServiceResult.FAILURE));
    } else {
        final List<ComplianceCheck> complianceList = rulesEngine.checkRulesCompliance();
        final List<RuleViolation> ruleViolations = new ArrayList<>();
        for (final ComplianceCheck check : complianceList) {
            ruleViolations.addAll(check.getRuleViolations());
        }
        Collections.sort(complianceList, new Comparator<ComplianceCheck>() {

            @Override
            public int compare(final ComplianceCheck o1, final ComplianceCheck o2) {
                return Integer.compare(o2.getRuleViolations().size(), o1.getRuleViolations().size());
            }
        });
        response = new ComplianceCheckResponse(ServiceResult.SUCCESS);
        response.setList(complianceList);
        response.setStatusMap(ruleViolations.stream().collect(Collectors.groupingBy(RuleViolation::getStatus)));
        response.setResourceTypeMap(ruleViolations.stream().collect(Collectors.groupingBy(RuleViolation::getResourceType)));
        eventRequest.setApplicationMessage(response.getResult().toString());
    }
    getCreateApplicationEventService().processService(eventRequest);
    return response;
}
Also used : ConstraintViolation(javax.validation.ConstraintViolation) ArrayList(java.util.ArrayList) ComplianceCheckResponse(com.hack23.cia.service.api.action.kpi.ComplianceCheckResponse) CreateApplicationEventRequest(com.hack23.cia.service.api.action.application.CreateApplicationEventRequest) ComplianceCheck(com.hack23.cia.service.api.action.kpi.ComplianceCheck) RuleViolation(com.hack23.cia.service.api.action.kpi.RuleViolation) UserAccount(com.hack23.cia.model.internal.application.user.impl.UserAccount) Secured(org.springframework.security.access.annotation.Secured)

Example 9 with UserAccount

use of com.hack23.cia.model.internal.application.user.impl.UserAccount in project cia by Hack23.

the class ManageUserAccountServiceITest method manageUserAccountSuccessUnLockTest.

@Test
public void manageUserAccountSuccessUnLockTest() throws Exception {
    setAuthenticatedAnonymousUser();
    final CreateApplicationSessionRequest createSessionRequest = createTestApplicationSession();
    final RegisterUserRequest createAccountRequest = new RegisterUserRequest();
    createAccountRequest.setCountry("Sweden");
    createAccountRequest.setUsername(UUID.randomUUID().toString());
    createAccountRequest.setEmail(createAccountRequest.getUsername() + "@email.com");
    createAccountRequest.setUserpassword("Userpassword1!");
    createAccountRequest.setUserType(UserType.PRIVATE);
    createAccountRequest.setSessionId(createSessionRequest.getSessionId());
    final RegisterUserResponse response = (RegisterUserResponse) applicationManager.service(createAccountRequest);
    assertNotNull("Expect a result", response);
    assertEquals(EXPECT_SUCCESS, ServiceResult.SUCCESS, response.getResult());
    setAuthenticatedAdminuser();
    final DataContainer<UserAccount, Long> userContainer = applicationManager.getDataContainer(UserAccount.class);
    final List<UserAccount> firstCreatedUsed = userContainer.getAllBy(UserAccount_.username, createAccountRequest.getUsername());
    assertEquals(1, firstCreatedUsed.size());
    final ManageUserAccountRequest deleteAccountRequest = new ManageUserAccountRequest();
    deleteAccountRequest.setSessionId(createSessionRequest.getSessionId());
    deleteAccountRequest.setAccountOperation(AccountOperation.UNLOCK);
    deleteAccountRequest.setUserAcountId(firstCreatedUsed.get(0).getUserId());
    final ManageUserAccountResponse deleteAccountResponse = (ManageUserAccountResponse) applicationManager.service(deleteAccountRequest);
    assertNotNull(EXPECT_A_RESULT, deleteAccountResponse);
    assertEquals(EXPECT_SUCCESS, ServiceResult.SUCCESS, deleteAccountResponse.getResult());
    final List<UserAccount> allByAfterDelete = userContainer.getAllBy(UserAccount_.username, createAccountRequest.getUsername());
    assertEquals(1, allByAfterDelete.size());
    assertEquals(allByAfterDelete.get(0).getUserLockStatus(), UserLockStatus.UNLOCKED);
}
Also used : CreateApplicationSessionRequest(com.hack23.cia.service.api.action.application.CreateApplicationSessionRequest) RegisterUserRequest(com.hack23.cia.service.api.action.application.RegisterUserRequest) ManageUserAccountRequest(com.hack23.cia.service.api.action.admin.ManageUserAccountRequest) RegisterUserResponse(com.hack23.cia.service.api.action.application.RegisterUserResponse) ManageUserAccountResponse(com.hack23.cia.service.api.action.admin.ManageUserAccountResponse) UserAccount(com.hack23.cia.model.internal.application.user.impl.UserAccount) Test(org.junit.Test) AbstractServiceFunctionalIntegrationTest(com.hack23.cia.service.impl.AbstractServiceFunctionalIntegrationTest)

Example 10 with UserAccount

use of com.hack23.cia.model.internal.application.user.impl.UserAccount in project cia by Hack23.

the class RemoveDataService method processService.

@Override
@Secured({ "ROLE_ADMIN" })
public RemoveDataResponse processService(final RemoveDataRequest serviceRequest) {
    final RemoveDataResponse inputValidation = inputValidation(serviceRequest);
    if (inputValidation != null) {
        return inputValidation;
    }
    final CreateApplicationEventRequest eventRequest = createApplicationEventForService(serviceRequest);
    final UserAccount userAccount = getUserAccountFromSecurityContext();
    if (userAccount != null) {
        eventRequest.setUserId(userAccount.getUserId());
    }
    final RemoveDataResponse response = new RemoveDataResponse(ServiceResult.SUCCESS);
    switch(serviceRequest.getDataType()) {
        case POLITICIAN:
            removeDataManager.removePersonData();
            break;
        case DOCUMENTS:
            removeDataManager.removeDocuments();
            removeDataManager.removeCommitteeProposals();
            removeDataManager.removeDocumentStatus();
            break;
        case APPLICATION_HISTORY:
            removeDataManager.removeApplicationHistory();
            break;
    }
    eventRequest.setApplicationMessage(response.getResult().toString());
    createApplicationEventService.processService(eventRequest);
    return response;
}
Also used : RemoveDataResponse(com.hack23.cia.service.api.action.admin.RemoveDataResponse) CreateApplicationEventRequest(com.hack23.cia.service.api.action.application.CreateApplicationEventRequest) UserAccount(com.hack23.cia.model.internal.application.user.impl.UserAccount) Secured(org.springframework.security.access.annotation.Secured)

Aggregations

UserAccount (com.hack23.cia.model.internal.application.user.impl.UserAccount)37 Test (org.junit.Test)19 Secured (org.springframework.security.access.annotation.Secured)18 CreateApplicationEventRequest (com.hack23.cia.service.api.action.application.CreateApplicationEventRequest)14 RegisterUserResponse (com.hack23.cia.service.api.action.application.RegisterUserResponse)14 CreateApplicationSessionRequest (com.hack23.cia.service.api.action.application.CreateApplicationSessionRequest)13 RegisterUserRequest (com.hack23.cia.service.api.action.application.RegisterUserRequest)13 AbstractServiceFunctionalIntegrationTest (com.hack23.cia.service.impl.AbstractServiceFunctionalIntegrationTest)13 PerfTest (org.databene.contiperf.PerfTest)10 LoginResponse (com.hack23.cia.service.api.action.application.LoginResponse)8 LoginRequest (com.hack23.cia.service.api.action.application.LoginRequest)7 PageModeMenuCommand (com.hack23.cia.web.impl.ui.application.views.common.pagelinks.api.PageModeMenuCommand)7 ManageUserAccountResponse (com.hack23.cia.service.api.action.admin.ManageUserAccountResponse)6 ArrayList (java.util.ArrayList)6 WebDriver (org.openqa.selenium.WebDriver)6 ManageUserAccountRequest (com.hack23.cia.service.api.action.admin.ManageUserAccountRequest)5 Required (org.databene.contiperf.Required)5 LogoutRequest (com.hack23.cia.service.api.action.application.LogoutRequest)4 VerticalLayout (com.vaadin.ui.VerticalLayout)4 WebElement (org.openqa.selenium.WebElement)4