use of com.synopsys.integration.alert.database.user.UserEntity in project hub-alert by blackducksoftware.
the class DefaultUserAccessor method deleteUser.
@Override
@Transactional(propagation = Propagation.REQUIRED)
public void deleteUser(String userName) throws AlertForbiddenOperationException {
Optional<UserEntity> optionalUser = userRepository.findByUserName(userName);
if (optionalUser.isPresent()) {
UserEntity user = optionalUser.get();
deleteUserEntity(user);
}
}
use of com.synopsys.integration.alert.database.user.UserEntity 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.database.user.UserEntity in project hub-alert by blackducksoftware.
the class DefaultUserAccessorTest method updateUserTest.
@Test
public void updateUserTest() throws Exception {
final String roleName = "userName";
AuthenticationType authenticationType = AuthenticationType.DATABASE;
UserEntity userEntity = new UserEntity(username, password, emailAddress, 2L);
userEntity.setId(1L);
UserRoleModel roles = createUserRoleModel(1L, roleName, true);
UserModel userModel = UserModel.existingUser(1L, username, password, emailAddress, authenticationType, Set.of(roles), true);
UserRoleRelation userRoleRelation = new UserRoleRelation(1L, 2L);
UserRoleModel userRoleModel = createUserRoleModel(1L, roleName, true);
Mockito.when(userRepository.findById(Mockito.any())).thenReturn(Optional.of(userEntity));
Mockito.when(authenticationTypeAccessor.getAuthenticationType(Mockito.any())).thenReturn(Optional.of(authenticationType));
Mockito.when(userRepository.save(Mockito.any())).thenReturn(userEntity);
createModelMocks(userRoleRelation, userRoleModel, authenticationType);
DefaultUserAccessor defaultUserAccessor = new DefaultUserAccessor(userRepository, userRoleRepository, defaultPasswordEncoder, roleAccessor, authenticationTypeAccessor);
UserModel newUserModel = defaultUserAccessor.updateUser(userModel, false);
Mockito.verify(roleAccessor).updateUserRoles(Mockito.eq(userEntity.getId()), Mockito.any());
testUserModel(userEntity.getId(), username, emailAddress, roleName, newUserModel);
}
use of com.synopsys.integration.alert.database.user.UserEntity in project hub-alert by blackducksoftware.
the class DefaultUserAccessorTest method deleteUserByIdTest.
@Test
public void deleteUserByIdTest() throws Exception {
UserEntity userEntity = new UserEntity(username, password, emailAddress, 2L);
userEntity.setId(5L);
Mockito.when(userRepository.findById(Mockito.eq(userEntity.getId()))).thenReturn(Optional.of(userEntity));
DefaultUserAccessor defaultUserAccessor = new DefaultUserAccessor(userRepository, userRoleRepository, defaultPasswordEncoder, roleAccessor, authenticationTypeAccessor);
defaultUserAccessor.deleteUser(userEntity.getId());
Mockito.verify(roleAccessor).updateUserRoles(Mockito.any(), Mockito.any());
Mockito.verify(userRepository).deleteById(Mockito.any());
}
use of com.synopsys.integration.alert.database.user.UserEntity in project hub-alert by blackducksoftware.
the class DefaultUserAccessorTest method changeUserEmailAddressTest.
@Test
public void changeUserEmailAddressTest() {
final String badUsername = "badUsername";
final String newEmailAddress = "newemail.noreplay@blackducksoftware.com";
UserEntity userEntity = new UserEntity(username, password, emailAddress, 2L);
userEntity.setId(1L);
UserEntity newUserEntity = new UserEntity(username, password, newEmailAddress, 2L);
userEntity.setId(1L);
Mockito.when(userRepository.findByUserName(Mockito.eq(username))).thenReturn(Optional.of(userEntity));
Mockito.when(userRepository.findByUserName(Mockito.eq(badUsername))).thenReturn(Optional.empty());
Mockito.when(userRepository.save(Mockito.any())).thenReturn(newUserEntity);
DefaultUserAccessor defaultUserAccessor = new DefaultUserAccessor(userRepository, userRoleRepository, defaultPasswordEncoder, roleAccessor, authenticationTypeAccessor);
assertTrue(defaultUserAccessor.changeUserEmailAddress(username, newEmailAddress));
assertFalse(defaultUserAccessor.changeUserEmailAddress(badUsername, newEmailAddress));
}
Aggregations