ProviderBasedPermission implements ExtendedPermission { @Override public boolean implies(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) { return false; } return _provider.isPermitted(user.getAlternateIds(), user.getNetworkAddress(), 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 impliesTrue() { AuthUtils.getSubject().getSession().setAttribute(UserPrincipals.ATTRIBUTE_KEY, PRINCIPALS); PermissionCheckProvider provider = mock(PermissionCheckProvider.class); when(provider.isPermitted(USER_BUNDLE, "1.1.1.1", "Data:12345")).thenReturn(Boolean.TRUE); ProviderBasedPermission test1 = new ProviderBasedPermission(provider, "Data"); ProviderBasedPermission test2 = new ProviderBasedPermission(provider, "Data:12345"); assertTrue(test1.implies(test2)); }
@Test public void impliesFalse() { AuthUtils.getSubject().getSession().setAttribute(UserPrincipals.ATTRIBUTE_KEY, PRINCIPALS); PermissionCheckProvider provider = mock(PermissionCheckProvider.class); when(provider.isPermitted(USER_BUNDLE, "1.1.1.1", "Data:12345")).thenReturn(Boolean.FALSE); ProviderBasedPermission test1 = new ProviderBasedPermission(provider, "Data"); ProviderBasedPermission test2 = new ProviderBasedPermission(provider, "Data:12345"); assertFalse(test1.implies(test2)); }
@Test public void impliesFalseAgainstWildcardPermission() { PermissionCheckProvider provider = mock(PermissionCheckProvider.class); ProviderBasedPermission test = new ProviderBasedPermission(provider, "Data:12345"); assertFalse(test.implies(WILDCARD_PERM)); assertFalse(WILDCARD_PERM.implies(test)); }