use of com.synopsys.integration.alert.common.persistence.model.UserRoleModel in project hub-alert by blackducksoftware.
the class AuthorizationManager method createRoleWithPermissions.
public UserRoleModel createRoleWithPermissions(String roleName, PermissionMatrixModel permissionMatrix) {
UserRoleModel roleWithPermissions = roleAccessor.createRoleWithPermissions(roleName, permissionMatrix);
updateRoleInCache(roleWithPermissions.getName(), roleWithPermissions.getPermissions());
return roleWithPermissions;
}
use of com.synopsys.integration.alert.common.persistence.model.UserRoleModel 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.UserRoleModel 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.UserRoleModel 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.UserRoleModel in project hub-alert by blackducksoftware.
the class DefaultRoleAccessorTest method createRoleWithPermissions.
@Test
public void createRoleWithPermissions() {
final String roleName = "roleName";
final String contextString = "context-test";
final String descriptorName = "descriptorName";
RoleEntity roleEntity = new RoleEntity(roleName, true);
roleEntity.setId(1L);
ConfigContextEntity configContextEntity = new ConfigContextEntity(contextString);
configContextEntity.setId(1L);
RegisteredDescriptorEntity registeredDescriptorEntity = new RegisteredDescriptorEntity(descriptorName, 1L);
registeredDescriptorEntity.setId(1L);
PermissionKey permissionKey = new PermissionKey(contextString, descriptorName);
PermissionMatrixModel permissionMatrixModel = new PermissionMatrixModel(Map.of(permissionKey, AccessOperation.READ.getBit() + AccessOperation.WRITE.getBit()));
PermissionMatrixRelation permissionMatrixRelation = new PermissionMatrixRelation(roleEntity.getId(), configContextEntity.getId(), registeredDescriptorEntity.getId(), AccessOperation.READ.getBit() + AccessOperation.WRITE.getBit());
Mockito.when(roleRepository.save(Mockito.any())).thenReturn(new RoleEntity(roleName, true));
mockUpdateRoleOperations(permissionMatrixRelation, configContextEntity, registeredDescriptorEntity);
mockCreateModelFromPermission(configContextEntity, registeredDescriptorEntity);
DefaultRoleAccessor authorizationUtility = new DefaultRoleAccessor(roleRepository, userRoleRepository, permissionMatrixRepository, registeredDescriptorRepository, configContextRepository);
UserRoleModel userRoleModel = authorizationUtility.createRoleWithPermissions(roleName, permissionMatrixModel);
Mockito.verify(permissionMatrixRepository).deleteAll(Mockito.any());
assertEquals(roleName, userRoleModel.getName());
assertTrue(userRoleModel.isCustom());
assertEquals(permissionMatrixModel, userRoleModel.getPermissions());
}
Aggregations