use of com.synopsys.integration.alert.common.persistence.model.UserModel in project hub-alert by blackducksoftware.
the class UserActions method createWithoutChecks.
@Override
protected ActionResponse<UserConfig> createWithoutChecks(UserConfig resource) {
try {
String userName = resource.getUsername();
String password = resource.getPassword();
String emailAddress = resource.getEmailAddress();
logger.debug(actionMessageCreator.createStartMessage("user", userName));
UserModel userModel = userAccessor.addUser(userName, password, emailAddress);
Long userId = userModel.getId();
Set<String> configuredRoleNames = resource.getRoleNames();
if (null != configuredRoleNames && !configuredRoleNames.isEmpty()) {
Collection<UserRoleModel> roleNames = roleAccessor.getRoles().stream().filter(role -> configuredRoleNames.contains(role.getName())).collect(Collectors.toList());
authorizationManager.updateUserRoles(userId, roleNames);
}
userModel = userAccessor.getUser(userId).orElse(userModel);
logger.debug(actionMessageCreator.createSuccessMessage("User", userName));
return new ActionResponse<>(HttpStatus.CREATED, convertDatabaseModelToRestModel(userModel));
} catch (AlertException ex) {
logger.error(actionMessageCreator.createErrorMessage("user", resource.getUsername()));
return new ActionResponse<>(HttpStatus.INTERNAL_SERVER_ERROR, String.format("There was an issue creating the user. %s", ex.getMessage()));
}
}
use of com.synopsys.integration.alert.common.persistence.model.UserModel in project hub-alert by blackducksoftware.
the class ConfigurationOverridesStartupComponentTest method cleanUp.
@AfterEach
public void cleanUp() throws AlertException {
Optional<UserModel> sysadminOptional = userAccessor.getUser(UserAccessor.DEFAULT_ADMIN_USER_ID);
assertTrue(sysadminOptional.isPresent());
UserModel sysadmin = sysadminOptional.get();
UserModel updatedSysadmin = changeUserPassword(sysadmin, DEFAULT_PASSWORD_ENCODED);
userAccessor.updateUser(updatedSysadmin, true);
}
use of com.synopsys.integration.alert.common.persistence.model.UserModel in project hub-alert by blackducksoftware.
the class ConfigurationOverridesStartupComponentTest method testInitializeNoChange.
@Test
public void testInitializeNoChange() throws AlertException {
Environment environment = Mockito.mock(Environment.class);
EnvironmentVariableUtility environmentVariableUtility = new EnvironmentVariableUtility(environment);
ConfigurationOverridesStartupComponent configurationOverridesStartupComponent = new ConfigurationOverridesStartupComponent(environmentVariableUtility, userAccessor, descriptorKey, configurationModelConfigurationAccessor, apiAction, configurationFieldModelConverter);
// Update the sysadmin password
Optional<UserModel> sysadminOptional = userAccessor.getUser(UserAccessor.DEFAULT_ADMIN_USER_ID);
assertTrue(sysadminOptional.isPresent());
UserModel sysadmin = sysadminOptional.get();
assertEquals(DEFAULT_PASSWORD_ENCODED, sysadmin.getPassword());
UserModel updatedSysadmin = changeUserPassword(sysadmin, UPDATED_PASSWORD);
userAccessor.updateUser(updatedSysadmin, false);
// Run the initialize method
configurationOverridesStartupComponent.initialize();
// Verify the sysadmin password is the updated password
sysadminOptional = userAccessor.getUser(UserAccessor.DEFAULT_ADMIN_USER_ID);
assertTrue(sysadminOptional.isPresent());
sysadmin = sysadminOptional.get();
assertNotEquals(DEFAULT_PASSWORD_ENCODED, sysadmin.getPassword());
HttpServletRequest servletRequest = Mockito.mock(HttpServletRequest.class);
HttpSession session = Mockito.mock(HttpSession.class);
Mockito.when(servletRequest.getSession()).thenReturn(session);
HttpServletResponse servletResponse = Mockito.mock(HttpServletResponse.class);
// Try to login with the updated password
LoginConfig updatedLoginConfig = new LoginConfig(DEFAULT_ADMIN_USER, UPDATED_PASSWORD);
ActionResponse<Void> actionResponse = authenticationActions.authenticateUser(servletRequest, servletResponse, updatedLoginConfig);
assertEquals(HttpStatus.NO_CONTENT, actionResponse.getHttpStatus());
// Try to login with the default password
LoginConfig defaultLoginConfig = new LoginConfig(DEFAULT_ADMIN_USER, DEFAULT_PASSWORD);
actionResponse = authenticationActions.authenticateUser(servletRequest, servletResponse, defaultLoginConfig);
assertEquals(HttpStatus.UNAUTHORIZED, actionResponse.getHttpStatus());
}
use of com.synopsys.integration.alert.common.persistence.model.UserModel in project hub-alert by blackducksoftware.
the class ConfigurationOverridesStartupComponentTest method testInitializeResetPassword.
@Test
public void testInitializeResetPassword() throws AlertException {
Environment environment = Mockito.mock(Environment.class);
Mockito.when(environment.getProperty(ConfigurationOverridesStartupComponent.ENV_VAR_ADMIN_USER_PASSWORD_RESET)).thenReturn("true");
EnvironmentVariableUtility environmentVariableUtility = new EnvironmentVariableUtility(environment);
ConfigurationOverridesStartupComponent configurationOverridesStartupComponent = new ConfigurationOverridesStartupComponent(environmentVariableUtility, userAccessor, descriptorKey, configurationModelConfigurationAccessor, apiAction, configurationFieldModelConverter);
// Update the sysadmin password
Optional<UserModel> sysadminOptional = userAccessor.getUser(UserAccessor.DEFAULT_ADMIN_USER_ID);
assertTrue(sysadminOptional.isPresent());
UserModel sysadmin = sysadminOptional.get();
assertEquals(DEFAULT_PASSWORD_ENCODED, sysadmin.getPassword());
UserModel updatedSysadmin = changeUserPassword(sysadmin, UPDATED_PASSWORD);
userAccessor.updateUser(updatedSysadmin, false);
// Run the initialize method
configurationOverridesStartupComponent.initialize();
// Verify the sysadmin password is the default password
sysadminOptional = userAccessor.getUser(UserAccessor.DEFAULT_ADMIN_USER_ID);
assertTrue(sysadminOptional.isPresent());
sysadmin = sysadminOptional.get();
assertEquals(DEFAULT_PASSWORD_ENCODED, sysadmin.getPassword());
HttpServletRequest servletRequest = Mockito.mock(HttpServletRequest.class);
HttpSession session = Mockito.mock(HttpSession.class);
Mockito.when(servletRequest.getSession()).thenReturn(session);
HttpServletResponse servletResponse = Mockito.mock(HttpServletResponse.class);
// Try to login with the updated password
LoginConfig updatedLoginConfig = new LoginConfig(DEFAULT_ADMIN_USER, UPDATED_PASSWORD);
ActionResponse<Void> actionResponse = authenticationActions.authenticateUser(servletRequest, servletResponse, updatedLoginConfig);
assertEquals(HttpStatus.UNAUTHORIZED, actionResponse.getHttpStatus());
// Try to login with the default password
LoginConfig defaultLoginConfig = new LoginConfig(DEFAULT_ADMIN_USER, DEFAULT_PASSWORD);
actionResponse = authenticationActions.authenticateUser(servletRequest, servletResponse, defaultLoginConfig);
assertEquals(HttpStatus.NO_CONTENT, actionResponse.getHttpStatus());
}
use of com.synopsys.integration.alert.common.persistence.model.UserModel in project hub-alert by blackducksoftware.
the class DefaultUserAccessorTest method getUserByUserIdTest.
@Test
public void getUserByUserIdTest() {
final Long userId = 1L;
final Long emptyUserId = 5L;
final Long authenticationTypeId = 1L;
final String roleName = "userName";
UserEntity userEntity = new UserEntity(username, password, emailAddress, authenticationTypeId);
userEntity.setId(1L);
UserRoleRelation userRoleRelation = new UserRoleRelation(1L, 2L);
UserRoleModel userRoleModel = createUserRoleModel(1L, roleName, true);
Mockito.when(userRepository.findById(userId)).thenReturn(Optional.of(userEntity));
Mockito.when(userRepository.findById(emptyUserId)).thenReturn(Optional.empty());
createModelMocks(userRoleRelation, userRoleModel, AuthenticationType.DATABASE);
DefaultUserAccessor defaultUserAccessor = new DefaultUserAccessor(userRepository, userRoleRepository, defaultPasswordEncoder, roleAccessor, authenticationTypeAccessor);
Optional<UserModel> userModelOptional = defaultUserAccessor.getUser(userId);
Optional<UserModel> userModelOptionalEmpty = defaultUserAccessor.getUser(emptyUserId);
assertTrue(userModelOptional.isPresent());
assertFalse(userModelOptionalEmpty.isPresent());
UserModel userModel = userModelOptional.get();
testUserModel(userEntity.getId(), username, emailAddress, roleName, userModel);
}
Aggregations