/** * Checks whether a Role has the Permission perform a certain Action with a specific ResourceGroup on a specific Context (or on its parent) * If so, it adds the role to the list of the allowed roles * * @param permissionName * @param action * @param context * @param resourceGroup * @param allowedRoles * @param entry */ private void matchPermissionsAndContext(String permissionName, Action action, ContextEntity context, ResourceGroupEntity resourceGroup, ResourceTypeEntity resourceType, List<String> allowedRoles, Map.Entry<String, List<RestrictionDTO>> entry) { for (RestrictionDTO restrictionDTO : entry.getValue()) { if (restrictionDTO.getPermissionName().equals(permissionName)) { checkContextAndActionAndResource(context, action, resourceGroup, resourceType, allowedRoles, entry, restrictionDTO.getRestriction()); } } }
public RestrictionDTO mockRestrictionDTO(Permission permission, RestrictionEntity restrictionEntity) { RestrictionDTO mock = mock(RestrictionDTO.class); when(mock.getPermissionName()).thenReturn(permission.name()); when(mock.getRestriction()).thenReturn(restrictionEntity); return mock; }
/** * Checks whether a Role has the Permission perform a certain Action * If so, it adds the role to the list of the allowed roles * * @param permissionName * @param action * @param resourceGroup * @param resourceType * @param allowedRoles * @param entry */ private void matchPermissions(String permissionName, Action action, ResourceGroupEntity resourceGroup, ResourceTypeEntity resourceType, List<String> allowedRoles, Map.Entry<String, List<RestrictionDTO>> entry) { String roleName = entry.getKey(); for (RestrictionDTO restrictionDTO : entry.getValue()) { if (restrictionDTO.getPermissionName().equals(permissionName) && hasPermissionForAction(restrictionDTO.getRestriction(), action) && hasPermissionForResource(restrictionDTO.getRestriction(), resourceGroup) && hasPermissionForResourceType(restrictionDTO.getRestriction(), resourceType) && hasPermissionForDefaultResourceType(restrictionDTO.getRestriction(), resourceType)) { allowedRoles.add(roleName); } } }
private boolean hasSimilarRoleRestriction(RestrictionEntity newRestriction) { List<RestrictionEntity> similarRestrictions = new ArrayList<>(); Set<Map.Entry<String, List<RestrictionDTO>>> entries = getPermissions().entrySet(); for (Map.Entry<String, List<RestrictionDTO>> entry : entries) { if (entry.getKey().equals(newRestriction.getRole().getName())) { for (RestrictionDTO restrictionDTO : entry.getValue()) { if (restrictionDTO.getPermissionName().equals(newRestriction.getPermission().getValue())) { checkSimilarRestrictions(newRestriction.getPermission().getValue(), newRestriction.getAction(), newRestriction.getContext(), newRestriction.getResourceGroup(), newRestriction.getResourceType(), similarRestrictions, restrictionDTO.getRestriction()); } } return aMoreGeneralRestrictionExists(newRestriction, similarRestrictions); } } return false; }
@Test public void constructedWithRestrictionItShouldHaveRightAction(){ //given RestrictionEntity res = new RestrictionEntity(); res.setAction(Action.READ); res.setPermission(per); //when RestrictionDTO resDTO = new RestrictionDTO(res); //then Assert.assertEquals(per.getValue(), resDTO.getPermissionName()); Assert.assertEquals(res.getAction(), resDTO.getRestriction().getAction()); }