use of com.synopsys.integration.alert.common.persistence.model.UserModel in project hub-alert by blackducksoftware.
the class AuthenticationEventHandler method handle.
@Override
public void handle(AlertAuthenticationEvent event) {
UserModel user = event.getUser();
if (null != user) {
try {
Optional<UserModel> userModel = userAccessor.getUser(user.getName());
if (userModel.isPresent() && user.isExternal()) {
UserModel model = userModel.get();
UserModel updatedUser = UserModel.existingUser(model.getId(), user.getName(), user.getPassword(), user.getEmailAddress(), user.getAuthenticationType(), user.getRoles(), user.isEnabled());
userAccessor.updateUser(updatedUser, true);
} else {
userAccessor.addUser(user, true);
}
} catch (AlertForbiddenOperationException ignored) {
// Cannot update an external user's credentials
} catch (AlertConfigurationException ignored) {
// User already exists. Nothing to do.
}
}
}
use of com.synopsys.integration.alert.common.persistence.model.UserModel in project hub-alert by blackducksoftware.
the class AuthenticationEventManager method sendAuthenticationEvent.
private void sendAuthenticationEvent(String username, String emailAddress, AuthenticationType authenticationType, Collection<? extends GrantedAuthority> authorities) throws AlertException {
if (username == null) {
throw new AlertException("Unable to send authentication event with null username");
}
Set<UserRoleModel> alertRoles = authorities.stream().map(this::getRoleFromAuthority).flatMap(Optional::stream).map(UserRoleModel::of).collect(Collectors.toSet());
// The database users will not be enabled because they already exist in the database when this is called. So a new entry will not be added to the database.
UserModel userModel = UserModel.newUser(username, null, emailAddress, authenticationType, alertRoles, true);
sendAuthenticationEvent(userModel);
}
use of com.synopsys.integration.alert.common.persistence.model.UserModel in project hub-alert by blackducksoftware.
the class UserModelTest method testUserModel.
@Test
public void testUserModel() {
String expectedUserName = "expectedUser";
String expectedPassword = "expectedPassword";
String expectedEmail = "expectedEmail";
Set<String> roleNames = new LinkedHashSet<>(Arrays.asList(DefaultUserRole.values()).stream().map(DefaultUserRole::name).collect(Collectors.toList()));
Set<UserRoleModel> expectedRoles = roleNames.stream().map(UserRoleModel::of).collect(Collectors.toSet());
UserModel userModel = UserModel.newUser(expectedUserName, expectedPassword, expectedEmail, AuthenticationType.DATABASE, expectedRoles, true);
assertEquals(expectedUserName, userModel.getName());
assertEquals(expectedPassword, userModel.getPassword());
assertEquals(expectedEmail, userModel.getEmailAddress());
assertEquals(expectedRoles.size(), userModel.getRoles().size());
assertTrue(userModel.hasRole(DefaultUserRole.ALERT_ADMIN.name()));
assertFalse(userModel.hasRole("UNKNOWN_ROLE"));
assertFalse(userModel.isExpired());
assertFalse(userModel.isLocked());
assertFalse(userModel.isPasswordExpired());
assertTrue(userModel.isEnabled());
assertFalse(userModel.isExternal());
}
use of com.synopsys.integration.alert.common.persistence.model.UserModel in project hub-alert by blackducksoftware.
the class UserAccessorTestIT method testUpdateUser.
@Test
public void testUpdateUser() throws AlertForbiddenOperationException, AlertConfigurationException {
String userName = "testUser";
String password = "testPassword";
String email = "testEmail";
UserModel userModel = userAccessor.addUser(userName, password, email);
assertNotNull(userModel);
assertEquals(userName, userModel.getName());
assertEquals(email, userModel.getEmailAddress());
assertTrue(userModel.getRoles().isEmpty());
String another_role = "ANOTHER_ROLE";
String admin_role = AlertIntegrationTestConstants.ROLE_ALERT_ADMIN;
Set<String> roleNames = new LinkedHashSet<>(Arrays.asList(admin_role, another_role));
Set<UserRoleModel> roles = roleNames.stream().map(UserRoleModel::of).collect(Collectors.toSet());
UserModel updatedModel = userAccessor.updateUser(UserModel.existingUser(userModel.getId(), userModel.getName(), userModel.getPassword(), userModel.getEmailAddress(), AuthenticationType.DATABASE, roles, true), true);
assertEquals(userModel.getName(), updatedModel.getName());
assertEquals(userModel.getEmailAddress(), updatedModel.getEmailAddress());
assertEquals(userModel.getPassword(), updatedModel.getPassword());
assertEquals(1, updatedModel.getRoles().size());
assertFalse(updatedModel.hasRole(another_role));
assertTrue(updatedModel.hasRole(admin_role));
assertFalse(updatedModel.isExternal());
userAccessor.deleteUser(userName);
}
use of com.synopsys.integration.alert.common.persistence.model.UserModel in project hub-alert by blackducksoftware.
the class UserAccessorTestIT method testExternalUserUpdateEmailException.
@Test
public void testExternalUserUpdateEmailException() throws AlertForbiddenOperationException, AlertConfigurationException {
String userName = "testUser";
String password = "testPassword";
String email = "testEmail";
UserModel userModel = UserModel.newUser(userName, password, email, AuthenticationType.LDAP, Collections.emptySet(), true);
userModel = userAccessor.addUser(userModel, false);
UserModel updatedUser = UserModel.existingUser(userModel.getId(), userName, null, email + "_updated", AuthenticationType.LDAP, Collections.emptySet(), true);
testUserUpdateException(updatedUser);
userAccessor.deleteUser(userName);
}
Aggregations