private void addPermission(Map<String, List<RestrictionDTO>> tmpRolesWithRestrictions, RoleEntity role) { String roleName = role.getName(); if (!tmpRolesWithRestrictions.containsKey(roleName)) { tmpRolesWithRestrictions.put(roleName, new ArrayList<RestrictionDTO>()); } for (RestrictionEntity res : role.getRestrictions()) { // add restriction tmpRolesWithRestrictions.get(roleName).add(new RestrictionDTO(res)); } }
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 shouldReturnTrueIfASameRoleRestrictionAlreadyExists() { // given RoleEntity role = new RoleEntity(); role.setName(CONFIG_ADMIN); PermissionEntity permission = new PermissionEntity(); permission.setValue(Permission.RESOURCE.name()); RestrictionEntity restriction = new RestrictionEntity(); restriction.setRole(role); restriction.setAction(Action.UPDATE); restriction.setContext(envC); restriction.setPermission(permission); myRoles = new HashMap<>(); myRoles.put(role.getName(), Arrays.asList(new RestrictionDTOBuilder().buildRestrictionDTO(Permission.RESOURCE, restriction))); permissionService.rolesWithRestrictions = myRoles; // when boolean exists = permissionService.identicalOrMoreGeneralRestrictionExists(restriction); // then Assert.assertTrue(exists); }
@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()); } }
myRoles.put(role.getName(), Arrays.asList(new RestrictionDTOBuilder().buildRestrictionDTO(Permission.RESOURCE, existingRoleRestriction))); permissionService.rolesWithRestrictions = myRoles;
myRoles.put(role.getName(), Arrays.asList(new RestrictionDTOBuilder().buildRestrictionDTO(Permission.RESOURCE, existingRoleRestriction))); permissionService.rolesWithRestrictions = myRoles;
myRoles.put(role.getName(), Arrays.asList(new RestrictionDTOBuilder().buildRestrictionDTO(Permission.RESOURCE, existingRoleRestriction))); permissionService.rolesWithRestrictions = myRoles;
@Test public void shouldReturnFalseIfASimilarButMoreResourceGroupRestrictedRoleRestrictionExists() { // given RoleEntity role = new RoleEntity(); role.setName(CONFIG_ADMIN); PermissionEntity permission = new PermissionEntity(); permission.setValue(Permission.RESOURCE.name()); ResourceTypeEntity resourceType = new ResourceTypeEntityBuilder().id(7).build(); ResourceGroupEntity resourceGroup = new ResourceGroupEntity(); resourceGroup.setId(23); resourceGroup.setResourceType(resourceType); RestrictionEntity existingRestriction = new RestrictionEntity(); existingRestriction.setRole(role); existingRestriction.setAction(Action.UPDATE); existingRestriction.setContext(envC); existingRestriction.setPermission(permission); existingRestriction.setResourceGroup(resourceGroup); RestrictionEntity newRestriction = new RestrictionEntity(); newRestriction.setRole(role); newRestriction.setAction(Action.UPDATE); newRestriction.setContext(envC); newRestriction.setPermission(permission); myRoles = new HashMap<>(); myRoles.put(role.getName(), Arrays.asList(new RestrictionDTOBuilder().buildRestrictionDTO(Permission.RESOURCE, existingRestriction))); permissionService.rolesWithRestrictions = myRoles; // when boolean exists = permissionService.identicalOrMoreGeneralRestrictionExists(newRestriction); // then Assert.assertFalse(exists); }
@Test public void shouldReturnTrueIfASimilarRoleRestrictionAlreadyExists() { // given RoleEntity role = new RoleEntity(); role.setName(CONFIG_ADMIN); PermissionEntity permission = new PermissionEntity(); permission.setValue(Permission.RESOURCE.name()); ResourceTypeEntity resourceType = new ResourceTypeEntityBuilder().id(7).build(); ResourceGroupEntity resourceGroup = new ResourceGroupEntity(); resourceGroup.setId(23); resourceGroup.setResourceType(resourceType); RestrictionEntity existingRestriction = new RestrictionEntity(); existingRestriction.setRole(role); existingRestriction.setAction(Action.UPDATE); existingRestriction.setContext(envC); existingRestriction.setPermission(permission); RestrictionEntity newRestriction = new RestrictionEntity(); newRestriction.setRole(role); newRestriction.setAction(Action.UPDATE); newRestriction.setContext(envC); newRestriction.setPermission(permission); newRestriction.setResourceGroup(resourceGroup); myRoles = new HashMap<>(); myRoles.put(role.getName(), Arrays.asList(new RestrictionDTOBuilder().buildRestrictionDTO(Permission.RESOURCE, existingRestriction))); permissionService.rolesWithRestrictions = myRoles; // when boolean exists = permissionService.identicalOrMoreGeneralRestrictionExists(newRestriction); // then Assert.assertTrue(exists); }
myRoles.put(role.getName(), Arrays.asList(new RestrictionDTOBuilder().buildRestrictionDTO(Permission.RESOURCE, existingRestriction))); permissionService.rolesWithRestrictions = myRoles;
myRoles.put(role.getName(), Arrays.asList(new RestrictionDTOBuilder().buildRestrictionDTO(Permission.RESOURCE, existingRestriction), new RestrictionDTOBuilder().buildRestrictionDTO(Permission.RESOURCE, anotherExistingRestriction))); permissionService.rolesWithRestrictions = myRoles;