ProviderBasedPermission implements ExtendedPermission { @Override public Boolean checkImpliesAll(Collection<Permission> requiredPermissions, boolean exceptionsOnError) { if (requiredPermissions.isEmpty()) { return Boolean.TRUE; } Set<String> required = new HashSet<>(); for (Permission requiredPermission : requiredPermissions) { if (requiredPermission instanceof ProviderBasedPermission == false) { return null; } required.add(((ProviderBasedPermission) requiredPermission).getPermissionString()); } UserPrincipals user = (UserPrincipals) AuthUtils.getSubject().getSession().getAttribute(UserPrincipals.ATTRIBUTE_KEY); if (user == null) { if (exceptionsOnError) { throw new UnauthenticatedException("Permission denied: User not logged in: " + required); } else { return Boolean.FALSE; } } PermissionCheckProviderRequest request = PermissionCheckProviderRequest.createGet( user.getAlternateIds(), user.getNetworkAddress(), required); PermissionCheckProviderResult result = _provider.isPermitted(request); if (exceptionsOnError) { result.checkErrors(); } return Boolean.valueOf(result.isPermittedAll(required)); } ProviderBasedPermission(PermissionCheckProvider provider, String permissionString); @Override boolean implies(Permission requiredPermission); @Override boolean checkImplies(Permission requiredPermission); @Override Boolean checkImpliesAll(Collection<Permission> requiredPermissions, boolean exceptionsOnError); @Override boolean equals(Object obj); @Override int hashCode(); @Override String toString();  }
@Test public void checkImpliesAllOneTrue() { AuthUtils.getSubject().getSession().setAttribute(UserPrincipals.ATTRIBUTE_KEY, PRINCIPALS); PermissionCheckProvider provider = mock(PermissionCheckProvider.class); PermissionCheckProviderRequest request = PermissionCheckProviderRequest.createGet(USER_BUNDLE, "1.1.1.1", "Data:12345"); PermissionCheckProviderResult result = PermissionCheckProviderResult.of( ImmutableMap.of("Data:12345", Boolean.TRUE)); when(provider.isPermitted(request)).thenReturn(result); ProviderBasedPermission test = new ProviderBasedPermission(provider, "Data"); Permission required1 = new ProviderBasedPermission(provider, "Data:12345"); assertTrue(test.checkImpliesAll(ImmutableList.of(required1), true)); assertTrue(test.checkImpliesAll(ImmutableList.of(required1), false)); }
@Test public void checkImpliesAllManyTrue() { AuthUtils.getSubject().getSession().setAttribute(UserPrincipals.ATTRIBUTE_KEY, PRINCIPALS); PermissionCheckProvider provider = mock(PermissionCheckProvider.class); PermissionCheckProviderRequest request = PermissionCheckProviderRequest.createGet( USER_BUNDLE, "1.1.1.1", "Data:12345", "Data:67890"); PermissionCheckProviderResult result = PermissionCheckProviderResult.of( ImmutableMap.of("Data:12345", Boolean.TRUE, "Data:67890", Boolean.TRUE)); when(provider.isPermitted(request)).thenReturn(result); ProviderBasedPermission test = new ProviderBasedPermission(provider, "Data"); Permission required1 = new ProviderBasedPermission(provider, "Data:12345"); Permission required2 = new ProviderBasedPermission(provider, "Data:67890"); assertTrue(test.checkImpliesAll(ImmutableList.of(required1, required2), true)); assertTrue(test.checkImpliesAll(ImmutableList.of(required1, required2), false)); }
@Test public void checkImpliesAllOneFalse() { AuthUtils.getSubject().getSession().setAttribute(UserPrincipals.ATTRIBUTE_KEY, PRINCIPALS); PermissionCheckProvider provider = mock(PermissionCheckProvider.class); PermissionCheckProviderRequest request = PermissionCheckProviderRequest.createGet(USER_BUNDLE, "1.1.1.1", "Data:12345"); PermissionCheckProviderResult result = PermissionCheckProviderResult.of( ImmutableMap.of("Data:12345", Boolean.FALSE)); when(provider.isPermitted(request)).thenReturn(result); ProviderBasedPermission test = new ProviderBasedPermission(provider, "Data"); Permission required1 = new ProviderBasedPermission(provider, "Data:12345"); assertFalse(test.checkImpliesAll(ImmutableList.of(required1), true)); assertFalse(test.checkImpliesAll(ImmutableList.of(required1), false)); }
@Test public void checkImpliesAllManyFalse() { AuthUtils.getSubject().getSession().setAttribute(UserPrincipals.ATTRIBUTE_KEY, PRINCIPALS); PermissionCheckProvider provider = mock(PermissionCheckProvider.class); PermissionCheckProviderRequest request = PermissionCheckProviderRequest.createGet( USER_BUNDLE, "1.1.1.1", "Data:12345", "Data:67890"); PermissionCheckProviderResult result = PermissionCheckProviderResult.of( ImmutableMap.of("Data:12345", Boolean.FALSE, "Data:67890", Boolean.TRUE)); when(provider.isPermitted(request)).thenReturn(result); ProviderBasedPermission test = new ProviderBasedPermission(provider, "Data"); Permission required1 = new ProviderBasedPermission(provider, "Data:12345"); Permission required2 = new ProviderBasedPermission(provider, "Data:67890"); assertFalse(test.checkImpliesAll(ImmutableList.of(required1, required2), true)); assertFalse(test.checkImpliesAll(ImmutableList.of(required1, required2), false)); }
@Test(expectedExceptions = UnauthenticatedException.class) public void checkImpliesAllUnauthenticated() { AuthUtils.getSubject().getSession().setAttribute(UserPrincipals.ATTRIBUTE_KEY, PRINCIPALS); PermissionCheckProvider provider = mock(PermissionCheckProvider.class); PermissionCheckProviderRequest request = PermissionCheckProviderRequest.createGet(USER_BUNDLE, "1.1.1.1", "Data:12345"); PermissionCheckProviderResult result = PermissionCheckProviderResult.ofAuthenticationError("Not authenticated"); when(provider.isPermitted(request)).thenReturn(result); ProviderBasedPermission test = new ProviderBasedPermission(provider, "Data"); Permission required1 = new ProviderBasedPermission(provider, "Data:12345"); assertTrue(test.checkImpliesAll(ImmutableList.of(required1), true)); }
@Test(expectedExceptions = AuthorizationException.class) public void checkImpliesAllUnauthorized() { AuthUtils.getSubject().getSession().setAttribute(UserPrincipals.ATTRIBUTE_KEY, PRINCIPALS); PermissionCheckProvider provider = mock(PermissionCheckProvider.class); PermissionCheckProviderRequest request = PermissionCheckProviderRequest.createGet(USER_BUNDLE, "1.1.1.1", "Data:12345"); PermissionCheckProviderResult result = PermissionCheckProviderResult.ofAuthorizationError("Not authorized"); when(provider.isPermitted(request)).thenReturn(result); ProviderBasedPermission test = new ProviderBasedPermission(provider, "Data"); Permission required1 = new ProviderBasedPermission(provider, "Data:12345"); assertTrue(test.checkImpliesAll(ImmutableList.of(required1), true)); }
@Test public void checkImpliesAllAgainstWildcardPermission() { PermissionCheckProvider provider = mock(PermissionCheckProvider.class); ProviderBasedPermission test = new ProviderBasedPermission(provider, "Data:12345"); Permission required1 = new ProviderBasedPermission(provider, "Data:12345"); assertEquals(null, test.checkImpliesAll(ImmutableList.of(required1, WILDCARD_PERM), true)); assertEquals(null, test.checkImpliesAll(ImmutableList.of(required1, WILDCARD_PERM), false)); }