UserSourceRealm extends AuthorizingRealm { @Override public void checkPermission(PrincipalCollection subjectPrincipal, String requiredPermission) throws AuthorizationException { Permission required = getPermissionResolver().resolvePermission(requiredPermission); checkPermission(subjectPrincipal, required); } UserSourceRealm(UserSource userSource); @Override boolean isAuthenticationCachingEnabled(); @Override boolean supports(AuthenticationToken token); @Override ShiroPermissionResolver getPermissionResolver(); @Override boolean isPermitted(PrincipalCollection subjectPrincipal, String requiredPermission); @Override boolean isPermitted(PrincipalCollection subjectPrincipal, Permission requiredPermission); @Override boolean isPermittedAll(PrincipalCollection subjectPrincipal, String... requiredPermissions); @Override boolean isPermittedAll(PrincipalCollection subjectPrincipal, Collection<Permission> requiredPermissions); @Override void checkPermission(PrincipalCollection subjectPrincipal, String requiredPermission); @Override void checkPermission(PrincipalCollection subjectPrincipal, Permission requiredPermission); @Override void checkPermissions(PrincipalCollection subjectPrincipal, String... requiredPermissions); @Override void checkPermissions(PrincipalCollection subjectPrincipal, Collection<Permission> requiredPermissions);  }
@Test public void testCheckPermission_true() { final ShiroPermissionResolver resolver = new ShiroPermissionResolver(); UserSourceRealm realm = new UserSourceRealm(_userSource) { @Override protected AuthorizationInfo getAuthorizationInfo(PrincipalCollection principals) { SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); info.addObjectPermission(resolver.resolvePermission("Master:view")); return info; } }; realm.setPermissionResolver(resolver); realm.checkPermission(PRINCIPALS, "Master:view"); }
@Test(expectedExceptions = UnauthorizedException.class) public void testCheckPermission_false() { final ShiroPermissionResolver resolver = new ShiroPermissionResolver(); UserSourceRealm realm = new UserSourceRealm(_userSource) { @Override protected AuthorizationInfo getAuthorizationInfo(PrincipalCollection principals) { SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); info.addObjectPermission(resolver.resolvePermission("Master:view")); return info; } }; realm.setPermissionResolver(resolver); realm.checkPermission(PRINCIPALS, "Master:edit"); }
@Test(expectedExceptions = UnauthorizedException.class) public void testCheckPermission_otherType() { final ShiroPermissionResolver resolver = new ShiroPermissionResolver(); UserSourceRealm realm = new UserSourceRealm(_userSource) { @Override protected AuthorizationInfo getAuthorizationInfo(PrincipalCollection principals) { SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); info.addObjectPermission(resolver.resolvePermission("Master:view")); return info; } }; realm.setPermissionResolver(resolver); realm.checkPermission(PRINCIPALS, PERMISSION_OTHER_TYPE); }