use of com.synopsys.integration.alert.database.authorization.PermissionMatrixRelation in project hub-alert by blackducksoftware.
the class DefaultRoleAccessorTest method updatePermissionsForRole.
@Test
public void updatePermissionsForRole() throws Exception {
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 permissionMatrix = 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.findByRoleName(Mockito.any())).thenReturn(Optional.of(roleEntity));
mockUpdateRoleOperations(permissionMatrixRelation, configContextEntity, registeredDescriptorEntity);
mockCreateModelFromPermission(configContextEntity, registeredDescriptorEntity);
DefaultRoleAccessor authorizationUtility = new DefaultRoleAccessor(roleRepository, userRoleRepository, permissionMatrixRepository, registeredDescriptorRepository, configContextRepository);
PermissionMatrixModel permissionMatrixModel = authorizationUtility.updatePermissionsForRole(roleName, permissionMatrix);
Mockito.verify(permissionMatrixRepository).saveAll(Mockito.any());
assertFalse(permissionMatrixModel.isEmpty());
assertEquals(permissionMatrix, permissionMatrixModel);
}
use of com.synopsys.integration.alert.database.authorization.PermissionMatrixRelation 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());
}
use of com.synopsys.integration.alert.database.authorization.PermissionMatrixRelation in project hub-alert by blackducksoftware.
the class DefaultRoleAccessor method createRoleWithPermissions.
@Override
@Transactional(propagation = Propagation.REQUIRED)
public UserRoleModel createRoleWithPermissions(String roleName, PermissionMatrixModel permissionMatrix) {
RoleEntity roleEntity = createRole(roleName, true);
List<PermissionMatrixRelation> permissions = updateRoleOperations(roleEntity, permissionMatrix);
return new UserRoleModel(roleEntity.getId(), roleEntity.getRoleName(), roleEntity.getCustom(), createModelFromPermission(permissions));
}
use of com.synopsys.integration.alert.database.authorization.PermissionMatrixRelation in project hub-alert by blackducksoftware.
the class DefaultRoleAccessor method createModelFromPermission.
private PermissionMatrixModel createModelFromPermission(List<PermissionMatrixRelation> permissions) {
Map<PermissionKey, Integer> permissionOperations = new HashMap<>();
if (null != permissions) {
for (PermissionMatrixRelation relation : permissions) {
Optional<String> optionalContext = configContextRepository.findById(relation.getContextId()).map(ConfigContextEntity::getContext);
Optional<String> optionalDescriptorName = registeredDescriptorRepository.findById(relation.getDescriptorId()).map(RegisteredDescriptorEntity::getName);
if (optionalDescriptorName.isPresent() && optionalContext.isPresent()) {
PermissionKey permissionKey = new PermissionKey(optionalContext.get(), optionalDescriptorName.get());
int existingPermissions = permissionOperations.getOrDefault(permissionKey, 0);
permissionOperations.put(permissionKey, existingPermissions | relation.getOperations());
}
}
}
return new PermissionMatrixModel(permissionOperations);
}
use of com.synopsys.integration.alert.database.authorization.PermissionMatrixRelation in project hub-alert by blackducksoftware.
the class DefaultRoleAccessor method updateRoleOperations.
private List<PermissionMatrixRelation> updateRoleOperations(RoleEntity roleEntity, PermissionMatrixModel permissionMatrix) {
List<PermissionMatrixRelation> oldPermissionsForRole = permissionMatrixRepository.findAllByRoleId(roleEntity.getId());
if (!oldPermissionsForRole.isEmpty()) {
permissionMatrixRepository.deleteAll(oldPermissionsForRole);
}
List<PermissionMatrixRelation> matrixEntries = new ArrayList<>();
Map<PermissionKey, Integer> permissions = permissionMatrix.getPermissions();
for (Map.Entry<PermissionKey, Integer> permission : permissions.entrySet()) {
PermissionKey permissionKey = permission.getKey();
ConfigContextEntity dbContext = configContextRepository.findFirstByContext(permissionKey.getContext()).orElseThrow(() -> new AlertRuntimeException("Invalid context specified for permission"));
RegisteredDescriptorEntity registeredDescriptor = registeredDescriptorRepository.findFirstByName(permissionKey.getDescriptorName()).orElseThrow(() -> new AlertRuntimeException("Invalid descriptor name specified for permission"));
int accessOperations = permission.getValue();
PermissionMatrixRelation permissionMatrixRelation = new PermissionMatrixRelation(roleEntity.getId(), dbContext.getId(), registeredDescriptor.getId(), accessOperations);
matrixEntries.add(permissionMatrixRelation);
}
if (!matrixEntries.isEmpty()) {
return permissionMatrixRepository.saveAll(matrixEntries);
}
return List.of();
}
Aggregations