DefaultPermissionManager implements PermissionManager { @Override public PermissionCollection resolvePermissions(User user, VotingStrategy votingStrategy) { if (user == null) { return new DefaultPermissionCollection(); } switch (votingStrategy) { case AFFIRMATIVE: return resolvePermissionsAffirmative(user); case CONSENSUS: return resolvePermissionsConsensus(user); case UNANIMOUS: return resolvePermissionsUnanimous(user); default: return resolvePermissionsPriority(user); } } @Inject  DefaultPermissionManager(PermissionTypeRegistry permissionTypeRegistry);  DefaultPermissionManager();  DefaultPermissionManager(PermissionTypeRegistry permissionTypeRegistry,
                                    DefaultAuthzResultCache cache); AuthorizationPolicy getAuthorizationPolicy(); void setAuthorizationPolicy(AuthorizationPolicy authorizationPolicy); @Override AuthorizationPolicyBuilder newAuthorizationPolicy(); @Override VotingStrategy getDefaultVotingStrategy(); @Override void setDefaultVotingStrategy(VotingStrategy votingStrategy); VotingAlgorithm getVotingAlgorithm(VotingStrategy votingStrategy); void setVotingAlgorithm(VotingStrategy votingStrategy,
                                   VotingAlgorithm votingAlgorithm); @Override Permission createPermission(String name,
                                       boolean granted); @Override Permission createPermission(Resource resource,
                                       ResourceAction action,
                                       boolean granted); @Override Permission createPermission(ResourceType resourceType,
                                       ResourceAction action,
                                       boolean granted); @Override AuthorizationResult checkPermission(Permission permission,
                                               User user); @Override AuthorizationResult checkPermission(Permission permission,
                                               User user,
                                               VotingStrategy votingStrategy); @Override String resolveResourceId(Permission permission); @Override PermissionCollection resolvePermissions(User user,
                                                   VotingStrategy votingStrategy); @Override void invalidate(final User user);  }
@Test public void testResolvePermissionsCustomDeniedOverDefaultGranted() { final VotingStrategy priority = VotingStrategy.PRIORITY; final Role businessUserRole = new RoleImpl("business-user"); final Group directorGroup = new GroupImpl("director"); final User user = makeUser("director", directorGroup, businessUserRole); mockDefaultPermissions(authorizationPolicy, makeGrantedPermissionCollection()); mockRolePermissions(authorizationPolicy, makeDeniedPermissionCollection(), businessUserRole, 0); mockDefaultGroupPermissions(authorizationPolicy, makeGrantedPermissionCollection(), directorGroup); final PermissionCollection resolvedPermission = defaultPermissionManager.resolvePermissions(user, priority); final Permission permission = resolvedPermission.get(PERMISSION_NAME); assertEquals(AuthorizationResult.ACCESS_DENIED, permission.getResult()); }
@Test public void testResolvePermissionsCustomGrantedOverDefaultDenied() { final VotingStrategy priority = VotingStrategy.PRIORITY; final Role businessUserRole = new RoleImpl("business-user"); final Group directorGroup = new GroupImpl("director"); final User user = makeUser("director", directorGroup, businessUserRole); mockDefaultPermissions(authorizationPolicy, makeDeniedPermissionCollection()); mockRolePermissions(authorizationPolicy, makeGrantedPermissionCollection(), businessUserRole, 0); mockDefaultGroupPermissions(authorizationPolicy, makeDeniedPermissionCollection(), directorGroup); final PermissionCollection resolvedPermission = defaultPermissionManager.resolvePermissions(user, priority); final Permission permission = resolvedPermission.get(PERMISSION_NAME); assertEquals(AuthorizationResult.ACCESS_GRANTED, permission.getResult()); }
@Test public void testResolvePermissionsTwoCustomRolesGranted() { final VotingStrategy priority = VotingStrategy.PRIORITY; final Role businessUserRole = new RoleImpl("business-user"); final Role managerRole = new RoleImpl("manager"); final Group directorGroup = new GroupImpl("director"); final User user = makeUser("director", directorGroup, businessUserRole, managerRole); mockDefaultPermissions(authorizationPolicy, makeDeniedPermissionCollection()); mockRolePermissions(authorizationPolicy, makeDeniedPermissionCollection(), businessUserRole, 0); mockRolePermissions(authorizationPolicy, makeGrantedPermissionCollection(), managerRole, 1); mockDefaultGroupPermissions(authorizationPolicy, makeDeniedPermissionCollection(), directorGroup); final PermissionCollection resolvedPermission = defaultPermissionManager.resolvePermissions(user, priority); final Permission permission = resolvedPermission.get(PERMISSION_NAME); assertEquals(AuthorizationResult.ACCESS_GRANTED, permission.getResult()); }
@Test public void testResolvePermissionsTwoCustomRolesDenied() { final VotingStrategy priority = VotingStrategy.PRIORITY; final Role businessUserRole = new RoleImpl("business-user"); final Role managerRole = new RoleImpl("manager"); final Group directorGroup = new GroupImpl("director"); final User user = makeUser("director", directorGroup, businessUserRole, managerRole); mockDefaultPermissions(authorizationPolicy, makeDeniedPermissionCollection()); mockRolePermissions(authorizationPolicy, makeDeniedPermissionCollection(), businessUserRole, 1); mockRolePermissions(authorizationPolicy, makeGrantedPermissionCollection(), managerRole, 0); mockDefaultGroupPermissions(authorizationPolicy, makeDeniedPermissionCollection(), directorGroup); final PermissionCollection resolvedPermission = defaultPermissionManager.resolvePermissions(user, priority); final Permission permission = resolvedPermission.get(PERMISSION_NAME); assertEquals(AuthorizationResult.ACCESS_DENIED, permission.getResult()); }
@Test public void testResolvePermissionsTwoCustomRolesSamePriority() { final VotingStrategy priority = VotingStrategy.PRIORITY; final Role businessUserRole = new RoleImpl("business-user"); final Role managerRole = new RoleImpl("manager"); final Group directorGroup = new GroupImpl("director"); final User user = makeUser("director", directorGroup, businessUserRole, managerRole); mockDefaultPermissions(authorizationPolicy, makeDeniedPermissionCollection()); mockRolePermissions(authorizationPolicy, makeDeniedPermissionCollection(), businessUserRole, 0); mockRolePermissions(authorizationPolicy, makeGrantedPermissionCollection(), managerRole, 0); mockDefaultGroupPermissions(authorizationPolicy, makeDeniedPermissionCollection(), directorGroup); final PermissionCollection resolvedPermission = defaultPermissionManager.resolvePermissions(user, priority); final Permission permission = resolvedPermission.get(PERMISSION_NAME); assertEquals(AuthorizationResult.ACCESS_GRANTED, permission.getResult()); }