/** * 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); } } }
/** * 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()); } } }
/** * Returns a list of ALL Restrictions of the caller (both, Restrictions by User and Role) */ public List<RestrictionEntity> getAllCallerRestrictions() { List<RestrictionEntity> restrictions = new ArrayList<>(); Map<String, List<RestrictionDTO>> roleWithRestrictions = getPermissions(); for (String roleName : roleWithRestrictions.keySet()) { if (sessionContext.isCallerInRole(roleName)) { for (RestrictionDTO restrictionDTO : roleWithRestrictions.get(roleName)) { restrictions.add(restrictionDTO.getRestriction()); } } } List<RestrictionEntity> userRestrictions = getUserRestrictions(getCurrentUserName()); restrictions.addAll(userRestrictions); return restrictions; }
/** * Checks if the caller is allowed to edit Deployments */ public boolean hasPermissionToEditDeployment() { for (Map.Entry<String, List<RestrictionDTO>> entry : getDeployableRoles().entrySet()) { if (sessionContext.isCallerInRole(entry.getKey())) { for (RestrictionDTO restrictionDTO : entry.getValue()) { if (restrictionDTO.getRestriction().getAction().equals(Action.UPDATE) || restrictionDTO.getRestriction().getAction().equals(Action.ALL)) { return true; } } } } return hasUserRestriction(Permission.DEPLOYMENT.name(), null, Action.UPDATE, null, null); }
/** * Checks if the caller is allowed to create (re-)Deployments */ public boolean hasPermissionToCreateDeployment() { for (Map.Entry<String, List<RestrictionDTO>> entry : getDeployableRoles().entrySet()) { if (sessionContext.isCallerInRole(entry.getKey())) { for (RestrictionDTO restrictionDTO : entry.getValue()) { if (restrictionDTO.getRestriction().getAction().equals(Action.CREATE) || restrictionDTO.getRestriction().getAction().equals(Action.ALL)) { return true; } } } } return hasUserRestriction(Permission.DEPLOYMENT.name(), null, Action.CREATE, null, null); }
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; }
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 constructedWithRestrictionItShouldHaveRightContext(){ //given RestrictionEntity res = new RestrictionEntity(); res.setAction(Action.CREATE); res.setContext(new ContextEntityBuilder().buildContextEntity("TEST", null, Collections.EMPTY_SET,false)); res.setPermission(per); res.setRole(rol); //when RestrictionDTO resDTO = new RestrictionDTO(res); //then Assert.assertEquals(rol.getName(), resDTO.getRestriction().getRole().getName()); Assert.assertEquals(res.getContext().getName(), resDTO.getRestriction().getContext().getName()); } }
@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()); }