use of com.synopsys.integration.alert.common.persistence.model.PermissionKey in project hub-alert by blackducksoftware.
the class AuthorizationManager method hasPermissions.
public final boolean hasPermissions(ConfigContextEnum context, DescriptorKey descriptorKey) {
PermissionKey permissionKey = new PermissionKey(context.name(), descriptorKey.getUniversalKey());
Collection<String> roleNames = getCurrentUserRoleNames();
return roleNames.stream().filter(this::isAlertRole).anyMatch(name -> permissionCache.containsKey(name) && permissionCache.get(name).hasPermissions(permissionKey));
}
use of com.synopsys.integration.alert.common.persistence.model.PermissionKey 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.common.persistence.model.PermissionKey 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.common.persistence.model.PermissionKey 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.common.persistence.model.PermissionKey in project hub-alert by blackducksoftware.
the class JiraServerGlobalTestActionTest method createAuthorizationManager.
private AuthorizationManager createAuthorizationManager(int assignedPermissions) {
AuthenticationTestUtils authenticationTestUtils = new AuthenticationTestUtils();
DescriptorKey descriptorKey = ChannelKeys.JIRA_SERVER;
PermissionKey permissionKey = new PermissionKey(ConfigContextEnum.GLOBAL.name(), descriptorKey.getUniversalKey());
Map<PermissionKey, Integer> permissions = Map.of(permissionKey, assignedPermissions);
return authenticationTestUtils.createAuthorizationManagerWithCurrentUserSet("admin", "admin", () -> new PermissionMatrixModel(permissions));
}
Aggregations