private boolean hasAllRoles(Collection<String> roleIdentifiers, AuthorizationInfo info) { if (roleIdentifiers != null && !roleIdentifiers.isEmpty()) { for (String roleName : roleIdentifiers) { if (!hasRole(roleName, info)) { return false; } } } return true; }
assertFalse(realm.hasRole(pCollection, "role1")); assertArrayEquals(new boolean[]{false, false}, realm.hasRoles(pCollection, roleList)); assertFalse(realm.isPermitted(pCollection, "perm1"));
protected boolean[] hasRoles(List<String> roleIdentifiers, AuthorizationInfo info) { boolean[] result; if (roleIdentifiers != null && !roleIdentifiers.isEmpty()) { int size = roleIdentifiers.size(); result = new boolean[size]; int i = 0; for (String roleName : roleIdentifiers) { result[i++] = hasRole(roleName, info); } } else { result = new boolean[0]; } return result; }
protected void checkRole(String role, AuthorizationInfo info) { if (!hasRole(role, info)) { String msg = "User does not have role [" + role + "]"; throw new UnauthorizedException(msg); } }
public boolean hasRole(PrincipalCollection principal, String roleIdentifier) { AuthorizationInfo info = getAuthorizationInfo(principal); return hasRole(roleIdentifier, info); }
private boolean hasAllRoles(Collection<String> roleIdentifiers, AuthorizationInfo info) { if (roleIdentifiers != null && !roleIdentifiers.isEmpty()) { for (String roleName : roleIdentifiers) { if (!hasRole(roleName, info)) { return false; } } } return true; }
protected boolean[] hasRoles(List<String> roleIdentifiers, AuthorizationInfo info) { boolean[] result; if (roleIdentifiers != null && !roleIdentifiers.isEmpty()) { int size = roleIdentifiers.size(); result = new boolean[size]; int i = 0; for (String roleName : roleIdentifiers) { result[i++] = hasRole(roleName, info); } } else { result = new boolean[0]; } return result; }
@Test public void testDefaultConfig() { String localhost = "localhost"; Subject subject = SecurityUtils.getSubject(); subject.login(new UsernamePasswordToken(USERNAME, PASSWORD, localhost)); assertTrue(subject.isAuthenticated()); assertTrue(subject.hasRole(ROLE)); UsernamePrincipal usernamePrincipal = subject.getPrincipals().oneByType(UsernamePrincipal.class); assertTrue(usernamePrincipal.getUsername().equals(USERNAME)); UserIdPrincipal userIdPrincipal = subject.getPrincipals().oneByType(UserIdPrincipal.class); assertTrue(userIdPrincipal.getUserId() == USER_ID); assertTrue(realm.hasRole(subject.getPrincipals(), ROLE)); subject.logout(); }
protected void checkRole(String role, AuthorizationInfo info) { if (!hasRole(role, info)) { String msg = "User does not have role [" + role + "]"; throw new UnauthorizedException(msg); } }
public boolean hasRole(PrincipalCollection principal, String roleIdentifier) { AuthorizationInfo info = getAuthorizationInfo(principal); return hasRole(roleIdentifier, info); }
@Test public void testDefaultConfig() { AuthenticationInfo info = realm.getAuthenticationInfo(new UsernamePasswordToken(USERNAME, PASSWORD, localhost)); assertNotNull(info); assertTrue(realm.hasRole(info.getPrincipals(), ROLE)); Object principal = info.getPrincipals().getPrimaryPrincipal(); assertTrue(principal instanceof UserIdPrincipal); UsernamePrincipal usernamePrincipal = info.getPrincipals().oneByType(UsernamePrincipal.class); assertTrue(usernamePrincipal.getUsername().equals(USERNAME)); UserIdPrincipal userIdPrincipal = info.getPrincipals().oneByType(UserIdPrincipal.class); assertTrue(userIdPrincipal.getUserId() == USER_ID); String stringPrincipal = info.getPrincipals().oneByType(String.class); assertTrue(stringPrincipal.equals(USER_ID + USERNAME)); }
@Test public void testCreateAccountOverride() { AuthorizingRealm realm = new AllowAllRealm() { @Override protected AuthenticationInfo buildAuthenticationInfo(Object principal, Object credentials) { String username = (String) principal; UsernamePrincipal customPrincipal = new UsernamePrincipal(username); return new SimpleAccount(customPrincipal, credentials, getName()); } }; AuthenticationInfo info = realm.getAuthenticationInfo(new UsernamePasswordToken(USERNAME, PASSWORD, localhost)); assertNotNull(info); assertTrue(realm.hasRole(info.getPrincipals(), ROLE)); Object principal = info.getPrincipals().getPrimaryPrincipal(); assertTrue(principal instanceof UsernamePrincipal); assertEquals(USERNAME, ((UsernamePrincipal) principal).getUsername()); }
@Test public void testRealmWithRolePermissionResolver() { Principal principal = new UsernamePrincipal("rolePermResolver"); PrincipalCollection pCollection = new SimplePrincipalCollection(principal, "testRealmWithRolePermissionResolver"); AuthorizingRealm realm = new AllowAllRealm(); realm.setRolePermissionResolver( new RolePermissionResolver() { public Collection<Permission> resolvePermissionsInRole( String roleString ) { Collection<Permission> permissions = new HashSet<Permission>(); if( roleString.equals( ROLE )) { permissions.add( new WildcardPermission( ROLE + ":perm1" ) ); permissions.add( new WildcardPermission( ROLE + ":perm2" ) ); permissions.add( new WildcardPermission( "other:*:foo" ) ); } return permissions; } }); assertTrue( realm.hasRole( pCollection, ROLE ) ); assertTrue( realm.isPermitted( pCollection, ROLE + ":perm1" ) ); assertTrue( realm.isPermitted( pCollection, ROLE + ":perm2" ) ); assertFalse( realm.isPermitted( pCollection, ROLE + ":perm3" ) ); assertTrue( realm.isPermitted( pCollection, "other:bar:foo" ) ); }
private boolean hasAllRoles(Collection<String> roleIdentifiers, AuthorizationInfo info) { if (roleIdentifiers != null && !roleIdentifiers.isEmpty()) { for (String roleName : roleIdentifiers) { if (!hasRole(roleName, info)) { return false; } } } return true; }
protected boolean[] hasRoles(List<String> roleIdentifiers, AuthorizationInfo info) { boolean[] result; if (roleIdentifiers != null && !roleIdentifiers.isEmpty()) { int size = roleIdentifiers.size(); result = new boolean[size]; int i = 0; for (String roleName : roleIdentifiers) { result[i++] = hasRole(roleName, info); } } else { result = new boolean[0]; } return result; }
protected void checkRole(String role, AuthorizationInfo info) { if (!hasRole(role, info)) { String msg = "User does not have role [" + role + "]"; throw new UnauthorizedException(msg); } }
public boolean hasRole(PrincipalCollection principal, String roleIdentifier) { AuthorizationInfo info = getAuthorizationInfo(principal); return hasRole(roleIdentifier, info); }
EnumSet<UserRole> roles = EnumSet.noneOf(UserRole.class); for (UserRole role : UserRole.values()) { if(realm.hasRole(principals, role.toString())) { roles.add(role);
EnumSet<UserRole> roles = EnumSet.noneOf(UserRole.class); for (UserRole role : UserRole.values()) { if(realm.hasRole(principals, role.toString())) { roles.add(role);