ProviderBasedPermission implements ExtendedPermission { @Override public boolean checkImplies(Permission requiredPermission) { if (requiredPermission instanceof ProviderBasedPermission == false) { return false; } ProviderBasedPermission requiredPerm = (ProviderBasedPermission) requiredPermission; UserPrincipals user = (UserPrincipals) AuthUtils.getSubject().getSession().getAttribute(UserPrincipals.ATTRIBUTE_KEY); if (user == null) { throw new UnauthenticatedException("Permission denied: User not logged in: " + requiredPermission); } PermissionCheckProviderRequest request = PermissionCheckProviderRequest.createGet( user.getAlternateIds(), user.getNetworkAddress(), requiredPerm.getPermissionString()); PermissionCheckProviderResult result = _provider.isPermitted(request); result.checkErrors(); return result.isPermitted(requiredPerm.getPermissionString()); } 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 checkImpliesTrue() { 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 test1 = new ProviderBasedPermission(provider, "Data"); ProviderBasedPermission test2 = new ProviderBasedPermission(provider, "Data:12345"); assertTrue(test1.checkImplies(test2)); }
@Test public void checkImpliesFalse() { 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 test1 = new ProviderBasedPermission(provider, "Data"); ProviderBasedPermission test2 = new ProviderBasedPermission(provider, "Data:12345"); assertFalse(test1.checkImplies(test2)); }
@Test(expectedExceptions = UnauthenticatedException.class) public void checkImpliesUnauthenticated() { 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 test1 = new ProviderBasedPermission(provider, "Data"); ProviderBasedPermission test2 = new ProviderBasedPermission(provider, "Data:12345"); test1.checkImplies(test2); }
@Test(expectedExceptions = AuthorizationException.class) public void checkImpliesUnauthorized() { 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 test1 = new ProviderBasedPermission(provider, "Data"); ProviderBasedPermission test2 = new ProviderBasedPermission(provider, "Data:12345"); test1.checkImplies(test2); }
@Test public void checkImpliesFalseAgainstWildcardPermission() { PermissionCheckProvider provider = mock(PermissionCheckProvider.class); ProviderBasedPermission test = new ProviderBasedPermission(provider, "Data:12345"); assertFalse(test.checkImplies(WILDCARD_PERM)); }