@Test public void isLastAdminRole() throws IOException { when(userStore.listUsers()).thenReturn(Lists.newArrayList(USER)); List<RoleGrantedAuthority> authorities = Lists.newArrayList(); when(userStore.getUserAuthorities(USER)).thenReturn(authorities); List<String> roleNames = Lists.newArrayList(); when(userStore.listRoles()).thenReturn(roleNames); roleNames.add("viewer"); RoleGrantedAuthority rga = new RoleGrantedAuthority(GrantedAuthorityTarget.APPLICATION, "viewer"); authorities.add(rga); Role role = mock(Role.class); when(role.getPermissions()).thenReturn(EnumSet.of(Permission.VIEW)); when(userStore.getRole("viewer")).thenReturn(role); assertFalse(permissionEvaluator.isLastAdminRole("viewer")); roleNames.add("admin"); rga = new RoleGrantedAuthority(GrantedAuthorityTarget.APPLICATION, "admin"); authorities.add(rga); role = mock(Role.class); when(role.getPermissions()).thenReturn(EnumSet.of(Permission.ADMIN)); when(userStore.getRole("admin")).thenReturn(role); assertTrue(permissionEvaluator.isLastAdminRole("admin")); roleNames.add("projectAdmin"); GrantedAuthorityTarget target = new GrantedAuthorityTarget("project", GrantedAuthorityTarget.Type.PROJECT); rga = new RoleGrantedAuthority(target, "projectAdmin"); authorities.add(rga); role = mock(Role.class); when(role.getPermissions()).thenReturn(EnumSet.of(Permission.ADMIN)); when(userStore.getRole("projectAdmin")).thenReturn(role); assertFalse(permissionEvaluator.isLastAdminRole("projectAdmin")); roleNames.add("admin2"); rga = new RoleGrantedAuthority(GrantedAuthorityTarget.APPLICATION, "admin2"); authorities.add(rga); role = mock(Role.class); when(role.getPermissions()).thenReturn(EnumSet.of(Permission.ADMIN)); when(userStore.getRole("admin2")).thenReturn(role); assertFalse(permissionEvaluator.isLastAdminRole("admin2")); }
public boolean isLastAdminRole(String roleName) { try { if (userStore.getRole(roleName).getPermissions().contains(Permission.ADMIN)) { Set<String> roles = Sets.newHashSet(userStore.listRoles()); roles.remove(roleName); Set<String> adminRoles = Sets.newHashSet(); for (String role : roles) { Role r = userStore.getRole(role); if (r.getPermissions().contains(Permission.ADMIN)) { adminRoles.add(role); } } if (!adminRoles.isEmpty()) { List<String> users = userStore.listUsers(); for (String user : users) { List<RoleGrantedAuthority> authorities = userStore.getUserAuthorities(user); for (RoleGrantedAuthority rga : authorities) { for (String role : adminRoles) { if (rga.getRoleName().equals(role) && rga.getTarget().equals(GrantedAuthorityTarget.APPLICATION)) { return false; } } } } } return true; } } catch (IOException e) { throw new RuntimeException(e); } return false; }
DocumentrPermissionEvaluator implements PermissionEvaluator { public boolean isLastAdminRole(String roleName) { try { if (userStore.getRole(roleName).getPermissions().contains(Permission.ADMIN)) { Set<String> roles = Sets.newHashSet(userStore.listRoles()); roles.remove(roleName); Set<String> adminRoles = Sets.newHashSet(); for (String role : roles) { Role r = userStore.getRole(role); if (r.getPermissions().contains(Permission.ADMIN)) { adminRoles.add(role); } } if (!adminRoles.isEmpty()) { List<String> users = userStore.listUsers(); for (String user : users) { List<RoleGrantedAuthority> authorities = userStore.getUserAuthorities(user); for (RoleGrantedAuthority rga : authorities) { for (String role : adminRoles) { if (rga.getRoleName().equals(role) && rga.getTarget().equals(GrantedAuthorityTarget.APPLICATION)) { return false; } } } } } return true; } } catch (IOException e) { throw new RuntimeException(e); } return false; } }
DocumentrPermissionEvaluator implements PermissionEvaluator { public boolean isLastAdminRole(String roleName) { try { if (userStore.getRole(roleName).getPermissions().contains(Permission.ADMIN)) { Set<String> roles = Sets.newHashSet(userStore.listRoles()); roles.remove(roleName); Set<String> adminRoles = Sets.newHashSet(); for (String role : roles) { Role r = userStore.getRole(role); if (r.getPermissions().contains(Permission.ADMIN)) { adminRoles.add(role); } } if (!adminRoles.isEmpty()) { List<String> users = userStore.listUsers(); for (String user : users) { List<RoleGrantedAuthority> authorities = userStore.getUserAuthorities(user); for (RoleGrantedAuthority rga : authorities) { for (String role : adminRoles) { if (rga.getRoleName().equals(role) && rga.getTarget().equals(GrantedAuthorityTarget.APPLICATION)) { return false; } } } } } return true; } } catch (IOException e) { throw new RuntimeException(e); } return false; }  }
DocumentrPermissionEvaluator implements PermissionEvaluator { public boolean isLastAdminRole(String roleName) { try { if (userStore.getRole(roleName).getPermissions().contains(Permission.ADMIN)) { Set<String> roles = Sets.newHashSet(userStore.listRoles()); roles.remove(roleName); Set<String> adminRoles = Sets.newHashSet(); for (String role : roles) { Role r = userStore.getRole(role); if (r.getPermissions().contains(Permission.ADMIN)) { adminRoles.add(role); } } if (!adminRoles.isEmpty()) { List<String> users = userStore.listUsers(); for (String user : users) { List<RoleGrantedAuthority> authorities = userStore.getUserAuthorities(user); for (RoleGrantedAuthority rga : authorities) { for (String role : adminRoles) { if (rga.getRoleName().equals(role) && rga.getTarget().equals(GrantedAuthorityTarget.APPLICATION)) { return false; } } } } } return true; } } catch (IOException e) { throw new RuntimeException(e); } return false; }  @Override boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission); @Override boolean hasPermission(Authentication authentication, Serializable targetId, String targetType,
			Object permission); boolean hasApplicationPermission(Authentication authentication, Permission permission); boolean hasProjectPermission(Authentication authentication, String projectName, Permission permission); boolean hasAnyProjectPermission(Authentication authentication, Permission permission); boolean hasBranchPermission(Authentication authentication, String projectName, String branchName,
			Permission permission); boolean hasAnyBranchPermission(Authentication authentication, String projectName, Permission permission); boolean hasPagePermission(Authentication authentication, String projectName, String branchName,
			String path, Permission permission); boolean hasPagePermissionInOtherBranches(Authentication authentication, String projectName,
			String branchName, String path, Permission permission); Set<String> getBranchesForPermission(Authentication authentication, Permission permission); boolean isAdmin(String loginName); boolean isLastAdminRole(String roleName); }
DocumentrPermissionEvaluator implements PermissionEvaluator { public boolean isLastAdminRole(String roleName) { try { if (userStore.getRole(roleName).getPermissions().contains(Permission.ADMIN)) { Set<String> roles = Sets.newHashSet(userStore.listRoles()); roles.remove(roleName); Set<String> adminRoles = Sets.newHashSet(); for (String role : roles) { Role r = userStore.getRole(role); if (r.getPermissions().contains(Permission.ADMIN)) { adminRoles.add(role); } } if (!adminRoles.isEmpty()) { List<String> users = userStore.listUsers(); for (String user : users) { List<RoleGrantedAuthority> authorities = userStore.getUserAuthorities(user); for (RoleGrantedAuthority rga : authorities) { for (String role : adminRoles) { if (rga.getRoleName().equals(role) && rga.getTarget().equals(GrantedAuthorityTarget.APPLICATION)) { return false; } } } } } return true; } } catch (IOException e) { throw new RuntimeException(e); } return false; }  @Override boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission); @Override boolean hasPermission(Authentication authentication, Serializable targetId, String targetType,
			Object permission); boolean hasApplicationPermission(Authentication authentication, Permission permission); boolean hasProjectPermission(Authentication authentication, String projectName, Permission permission); boolean hasAnyProjectPermission(Authentication authentication, Permission permission); boolean hasBranchPermission(Authentication authentication, String projectName, String branchName,
			Permission permission); boolean hasAnyBranchPermission(Authentication authentication, String projectName, Permission permission); boolean hasPagePermission(Authentication authentication, String projectName, String branchName,
			String path, Permission permission); boolean hasPagePermissionInOtherBranches(Authentication authentication, String projectName,
			String branchName, String path, Permission permission); Set<String> getBranchesForPermission(Authentication authentication, Permission permission); boolean isAdmin(String loginName); boolean isLastAdminRole(String roleName);  }