use of com.synopsys.integration.alert.common.persistence.model.UserModel in project hub-alert by blackducksoftware.
the class DefaultUserAccessor method addUser.
@Override
@Transactional(propagation = Propagation.REQUIRED)
public UserModel addUser(UserModel user, boolean passwordEncoded) throws AlertConfigurationException {
String username = user.getName();
Optional<UserEntity> userWithSameUsername = userRepository.findByUserName(username);
if (userWithSameUsername.isPresent()) {
throw new AlertConfigurationException(String.format("A user with username '%s' is already present", username));
}
String password = passwordEncoded ? user.getPassword() : defaultPasswordEncoder.encode(user.getPassword());
AuthenticationTypeDetails authenticationType = authenticationTypeAccessor.getAuthenticationTypeDetails(user.getAuthenticationType()).orElseThrow(() -> new AlertRuntimeException("Cannot find Authentication Type."));
UserEntity newEntity = new UserEntity(username, password, user.getEmailAddress(), authenticationType.getId());
UserEntity savedEntity = userRepository.save(newEntity);
UserModel model = createModel(savedEntity);
roleAccessor.updateUserRoles(model.getId(), user.getRoles());
return model;
}
use of com.synopsys.integration.alert.common.persistence.model.UserModel in project hub-alert by blackducksoftware.
the class AuthenticationActionsTestIT method testAuthenticateDBUserRoleFailIT.
@Test
public void testAuthenticateDBUserRoleFailIT() throws AlertForbiddenOperationException, AlertConfigurationException {
HttpServletRequest servletRequest = new MockHttpServletRequest();
HttpServletResponse servletResponse = new MockHttpServletResponse();
// add a user test then delete a user.
String userName = String.format("testuser_%s", UUID.randomUUID());
mockLoginRestModel.setAlertUsername(userName);
AuthenticationActions authenticationActions = new AuthenticationActions(authenticationProvider, csrfTokenRepository);
userAccessor.addUser(userName, mockLoginRestModel.getAlertPassword(), "");
ActionResponse<Void> response = authenticationActions.authenticateUser(servletRequest, servletResponse, mockLoginRestModel.createRestModel());
assertTrue(response.isError());
Optional<UserModel> userModel = userAccessor.getUser(userName);
assertTrue(userModel.isPresent());
UserModel model = userModel.get();
assertFalse(model.hasRole(AlertIntegrationTestConstants.ROLE_ALERT_ADMIN));
assertTrue(model.getRoles().isEmpty());
userAccessor.deleteUser(userName);
}
use of com.synopsys.integration.alert.common.persistence.model.UserModel in project hub-alert by blackducksoftware.
the class UserActionsTest method testReadWithoutChecks.
@Test
public void testReadWithoutChecks() {
UserModel userModel = UserModel.existingUser(id, name, password, emailAddress, authenticationType, roles, true);
Mockito.when(authorizationManager.hasReadPermission(Mockito.any(ConfigContextEnum.class), Mockito.any(DescriptorKey.class))).thenReturn(true);
Mockito.when(userAccessor.getUser(id)).thenReturn(Optional.of(userModel));
Mockito.when(userAccessor.getUser(2L)).thenReturn(Optional.empty());
UserActions userActions = new UserActions(userManagementDescriptorKey, userAccessor, roleAccessor, authorizationManager, authenticationTypeAccessor, userSystemValidator);
ActionResponse<UserConfig> actionResponse = userActions.getOne(id);
ActionResponse<UserConfig> actionResponseEmpty = userActions.getOne(2L);
assertTrue(actionResponse.hasContent());
assertEquals(HttpStatus.OK, actionResponse.getHttpStatus());
UserConfig userConfig = actionResponse.getContent().get();
assertUserConfig(userConfig);
assertFalse(actionResponseEmpty.hasContent());
assertEquals(HttpStatus.NOT_FOUND, actionResponseEmpty.getHttpStatus());
}
use of com.synopsys.integration.alert.common.persistence.model.UserModel in project hub-alert by blackducksoftware.
the class UserActionsTest method testReadAllWithoutChecks.
@Test
public void testReadAllWithoutChecks() {
UserModel userModel = UserModel.existingUser(id, name, password, emailAddress, authenticationType, roles, true);
AuthenticationTypeDetails authenticationTypeDetails = new AuthenticationTypeDetails(1L, authenticationType.name());
Mockito.when(authorizationManager.hasReadPermission(Mockito.any(ConfigContextEnum.class), Mockito.any(DescriptorKey.class))).thenReturn(true);
Mockito.when(userAccessor.getUsers()).thenReturn(List.of(userModel));
Mockito.when(authenticationTypeAccessor.getAuthenticationTypeDetails(Mockito.any())).thenReturn(Optional.of(authenticationTypeDetails));
UserActions userActions = new UserActions(userManagementDescriptorKey, userAccessor, roleAccessor, authorizationManager, authenticationTypeAccessor, userSystemValidator);
ActionResponse<MultiUserConfigResponseModel> actionResponse = userActions.getAll();
assertTrue(actionResponse.hasContent());
List<UserConfig> userModels = actionResponse.getContent().get().getUsers();
assertEquals(1, userModels.size());
UserConfig userConfig = userModels.get(0);
assertUserConfig(userConfig);
assertEquals(authenticationType.name(), userConfig.getAuthenticationType());
assertNull(userConfig.getPassword());
}
use of com.synopsys.integration.alert.common.persistence.model.UserModel in project hub-alert by blackducksoftware.
the class UserActionsTest method testDeleteWithoutChecks.
@Test
public void testDeleteWithoutChecks() throws Exception {
UserModel userModel = UserModel.existingUser(id, name, password, emailAddress, authenticationType, roles, true);
Mockito.when(userAccessor.getUser(id)).thenReturn(Optional.of(userModel));
UserActions userActions = new UserActions(userManagementDescriptorKey, userAccessor, roleAccessor, authorizationManager, authenticationTypeAccessor, userSystemValidator);
ActionResponse<UserConfig> userConfigActionResponse = userActions.deleteWithoutChecks(id);
Mockito.verify(userAccessor).deleteUser(id);
assertFalse(userConfigActionResponse.hasContent());
assertEquals(HttpStatus.NO_CONTENT, userConfigActionResponse.getHttpStatus());
}
Aggregations