use of com.hack23.cia.model.internal.application.user.impl.UserAccount 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;
}
use of com.hack23.cia.model.internal.application.user.impl.UserAccount 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;
}
use of com.hack23.cia.model.internal.application.user.impl.UserAccount in project cia by Hack23.
the class ManageUserAccountServiceITest method manageUserAccountSuccessLockTest.
@Test
public void manageUserAccountSuccessLockTest() 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.LOCK);
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.LOCKED);
}
use of com.hack23.cia.model.internal.application.user.impl.UserAccount in project cia by Hack23.
the class ManageUserAccountServiceITest method manageUserAccountSuccessDeleteTest.
@Test
public void manageUserAccountSuccessDeleteTest() 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.DELETE);
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(0, allByAfterDelete.size());
final DataContainer<ApplicationActionEvent, Long> eventContainer = applicationManager.getDataContainer(ApplicationActionEvent.class);
final List<ApplicationActionEvent> events = eventContainer.getAllBy(ApplicationActionEvent_.userId, firstCreatedUsed.get(0).getUserId());
assertEquals(0, events.size());
final DataContainer<ApplicationSession, Serializable> sessionContainer = applicationManager.getDataContainer(ApplicationSession.class);
final List<ApplicationSession> sessions = sessionContainer.getAllBy(ApplicationSession_.userId, firstCreatedUsed.get(0).getUserId());
assertEquals(0, sessions.size());
}
use of com.hack23.cia.model.internal.application.user.impl.UserAccount in project cia by Hack23.
the class LoginServiceITest method serviceLoginRequestSuccessTest.
/**
* Service login request success test.
*
* @throws Exception
* the exception
*/
@Test
@PerfTest(threads = 2, duration = 4000, warmUp = 1500)
@Required(max = 2500, average = 2000, percentile95 = 2200, throughput = 1)
public void serviceLoginRequestSuccessTest() throws Exception {
final CreateApplicationSessionRequest createApplicationSesstion = createApplicationSesstionWithRoleAnonymous();
final RegisterUserRequest serviceRequest = new RegisterUserRequest();
serviceRequest.setCountry("Sweden");
serviceRequest.setUsername(UUID.randomUUID().toString());
serviceRequest.setEmail(serviceRequest.getUsername() + "@email.com");
serviceRequest.setUserpassword("Userpassword1!");
serviceRequest.setUserType(UserType.PRIVATE);
serviceRequest.setSessionId(createApplicationSesstion.getSessionId());
final RegisterUserResponse response = (RegisterUserResponse) applicationManager.service(serviceRequest);
assertNotNull("Expect a result", response);
assertEquals(EXPECT_SUCCESS, ServiceResult.SUCCESS, response.getResult());
final DataContainer<UserAccount, Long> dataContainer = applicationManager.getDataContainer(UserAccount.class);
final List<UserAccount> allBy = dataContainer.getAllBy(UserAccount_.username, serviceRequest.getUsername());
assertEquals(1, allBy.size());
final LoginRequest loginRequest = new LoginRequest();
loginRequest.setEmail(serviceRequest.getEmail());
loginRequest.setSessionId(serviceRequest.getSessionId());
loginRequest.setUserpassword(serviceRequest.getUserpassword());
final LoginResponse loginResponse = (LoginResponse) applicationManager.service(loginRequest);
assertNotNull("Expect a result", loginResponse);
assertEquals(EXPECT_SUCCESS, ServiceResult.SUCCESS, loginResponse.getResult());
}
Aggregations