public RoleEntity createRole(String roleName) { RoleEntity roleEntity = new RoleEntity(); roleEntity.setName(roleName.toLowerCase()); roleEntity.setDeletable(true); entityManager.persist(roleEntity); return roleEntity; }
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)); } }
public void deleteRole(String roleName) { RoleEntity role = getRoleByName(roleName); if (role == null) { throw new IllegalArgumentException("Role " + roleName + " doesn't exist!"); } if (!role.isDeletable()) { throw new IllegalArgumentException("Role " + roleName + " is not deletable!"); } // leads to a cascade delete of the restrictions entityManager.remove(role); }
@Before public void setUp(){ rol = new RoleEntity(); rol.setName("testRole"); per = new PermissionEntity(); per.setValue("testPermission"); }
@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(expected=AMWException.class) public void shouldThrowAMWExceptionOnCreateIfResourceGroupAndResourceTypeAreSet() throws AMWException { // given when(permissionRepository.getRoleByName("existing")).thenReturn(new RoleEntity()); when(permissionRepository.getPermissionByName("good")).thenReturn(resourcePermission); // when // then permissionBoundary.createRestriction("existing", null, "good", 7, "bad", null, null, null, false, true); }
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 shouldReturnTrueIfCallerHasAnIdenticalPermissionGrantedByItsRole() { RoleEntity role = new RoleEntity(); role.setName(CONFIG_ADMIN); myRoles.put(role.getName(), Arrays.asList(new RestrictionDTOBuilder().buildRestrictionDTO(Permission.RESOURCE, existingRoleRestriction))); permissionService.rolesWithRestrictions = myRoles;
@Test public void shouldCreateIfContextIsNull() throws AMWException { // given when(permissionRepository.getRoleByName("existing")).thenReturn(new RoleEntity()); when(permissionRepository.getPermissionByName("good")).thenReturn(resourcePermission); // when permissionBoundary.createRestriction("existing", null, "good", null, null, null, null, CREATE, false, true); // then verify(restrictionRepository).create(any(RestrictionEntity.class)); }
@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 shouldReturnTrueIfCallerHasAMorePowerfulPermissionGrantedByItsRole() { RoleEntity role = new RoleEntity(); role.setName(CONFIG_ADMIN); myRoles.put(role.getName(), Arrays.asList(new RestrictionDTOBuilder().buildRestrictionDTO(Permission.RESOURCE, existingRoleRestriction))); permissionService.rolesWithRestrictions = myRoles;
@Test(expected=AMWException.class) public void shouldThrowAMWExceptionOnCreateIfResourceTypePermissionIsNotEmptyAndResourceGroupIsSet() throws AMWException { // given when(permissionRepository.getRoleByName("existing")).thenReturn(new RoleEntity()); when(permissionRepository.getPermissionByName("good")).thenReturn(resourcePermission); // when // then permissionBoundary.createRestriction("existing", null, "good", 7, null, DEFAULT_ONLY, null, null, false, true); }
@Test public void shouldReturnFalseIfCallerHasALessPowerfulPermissionGrantedByItsRole() { RoleEntity role = new RoleEntity(); role.setName(CONFIG_ADMIN); myRoles.put(role.getName(), Arrays.asList(new RestrictionDTOBuilder().buildRestrictionDTO(Permission.RESOURCE, existingRoleRestriction))); permissionService.rolesWithRestrictions = myRoles;
@Test(expected=AMWException.class) public void shouldThrowAMWExceptionOnCreateIfResourceTypePermissionIsNotEmptyAndResourceTypeIsSet() throws AMWException { // given when(permissionRepository.getRoleByName("existing")).thenReturn(new RoleEntity()); when(permissionRepository.getPermissionByName("good")).thenReturn(resourcePermission); // when // then permissionBoundary.createRestriction("existing", null, "good", null, "bad", NON_DEFAULT_ONLY, null, null, false, true); }
@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(expected=AMWException.class) public void shouldThrowAMWExceptionOnCreateIfResourceIdCanNotBeFound() throws AMWException { // given when(permissionRepository.getRoleByName("existing")).thenReturn(new RoleEntity()); when(permissionRepository.getPermissionByName("good")).thenReturn(resourcePermission); when(resourceGroupRepository.find(7)).thenReturn(null); // when // then permissionBoundary.createRestriction("existing", null, "good", 7, null, null, null, null, false, true); }
@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); }
@Test public void shouldCreateIfContextAndActionAreNull() throws AMWException { // given when(permissionRepository.getRoleByName("existing")).thenReturn(new RoleEntity()); when(permissionRepository.getPermissionByName("good")).thenReturn(resourcePermission); // when permissionBoundary.createRestriction("existing", null, "good", null, null, null, null, null, false, true); // then verify(restrictionRepository).create(any(RestrictionEntity.class)); verify(permissionRepository).forceReloadingOfLists(); }
@Test public void shouldReturnFalseIfUpdatingExistingRoleRestriction() { RoleEntity role = new RoleEntity(); role.setName(CONFIG_ADMIN); PermissionEntity permission = new PermissionEntity(); permission.setValue(Permission.RESOURCE.name()); myRoles.put(role.getName(), Arrays.asList(new RestrictionDTOBuilder().buildRestrictionDTO(Permission.RESOURCE, existingRestriction))); permissionService.rolesWithRestrictions = myRoles;
@Test(expected=AMWException.class) public void shouldThrowAMWExceptionOnUpdateIfResourceTypeCanNotBeFound() throws AMWException { // given when(restrictionRepository.find(1)).thenReturn(new RestrictionEntity()); when(permissionRepository.getRoleByName("existing")).thenReturn(new RoleEntity()); when(permissionRepository.getPermissionByName("good")).thenReturn(resourcePermission); when(resourceTypeRepository.getByName("bad")).thenReturn(null); // when // then permissionBoundary.updateRestriction(1, "existing", null, "good", null, "bad", null, null, null, true); }