use of org.apache.shiro.subject.SimplePrincipalCollection in project ANNIS by korpling.
the class ANNISUserRealm method clearCacheForUser.
public void clearCacheForUser(String userName) {
SimplePrincipalCollection principals = new SimplePrincipalCollection(userName, ANNISUserRealm.class.getName());
clearCache(principals);
}
use of org.apache.shiro.subject.SimplePrincipalCollection in project shiro by apache.
the class SimpleAuthenticationInfo method merge.
/**
* Takes the specified <code>info</code> argument and adds its principals and credentials into this instance.
*
* @param info the <code>AuthenticationInfo</code> to add into this instance.
*/
@SuppressWarnings("unchecked")
public void merge(AuthenticationInfo info) {
if (info == null || info.getPrincipals() == null || info.getPrincipals().isEmpty()) {
return;
}
if (this.principals == null) {
this.principals = info.getPrincipals();
} else {
if (!(this.principals instanceof MutablePrincipalCollection)) {
this.principals = new SimplePrincipalCollection(this.principals);
}
((MutablePrincipalCollection) this.principals).addAll(info.getPrincipals());
}
// since 1.1:
if (this.credentialsSalt == null && info instanceof SaltedAuthenticationInfo) {
this.credentialsSalt = ((SaltedAuthenticationInfo) info).getCredentialsSalt();
}
Object thisCredentials = getCredentials();
Object otherCredentials = info.getCredentials();
if (otherCredentials == null) {
return;
}
if (thisCredentials == null) {
this.credentials = otherCredentials;
return;
}
if (!(thisCredentials instanceof Collection)) {
Set newSet = new HashSet();
newSet.add(thisCredentials);
setCredentials(newSet);
}
// At this point, the credentials should be a collection
Collection credentialCollection = (Collection) getCredentials();
if (otherCredentials instanceof Collection) {
credentialCollection.addAll((Collection) otherCredentials);
} else {
credentialCollection.add(otherCredentials);
}
}
use of org.apache.shiro.subject.SimplePrincipalCollection in project shiro by apache.
the class AuthorizingRealmTest method testRealmWithRolePermissionResolver.
@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"));
}
use of org.apache.shiro.subject.SimplePrincipalCollection in project shiro by apache.
the class AuthorizingRealmTest method testNullAuthzInfo.
@Test
public void testNullAuthzInfo() {
AuthorizingRealm realm = new AuthorizingRealm() {
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
return null;
}
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
return null;
}
};
Principal principal = new UsernamePrincipal("blah");
PrincipalCollection pCollection = new SimplePrincipalCollection(principal, "nullAuthzRealm");
List<Permission> permList = new ArrayList<Permission>();
permList.add(new WildcardPermission("stringPerm1"));
permList.add(new WildcardPermission("stringPerm2"));
List<String> roleList = new ArrayList<String>();
roleList.add("role1");
roleList.add("role2");
boolean thrown = false;
try {
realm.checkPermission(pCollection, "stringPermission");
} catch (UnauthorizedException e) {
thrown = true;
}
assertTrue(thrown);
thrown = false;
try {
realm.checkPermission(pCollection, new WildcardPermission("stringPermission"));
} catch (UnauthorizedException e) {
thrown = true;
}
assertTrue(thrown);
thrown = false;
try {
realm.checkPermissions(pCollection, "stringPerm1", "stringPerm2");
} catch (UnauthorizedException e) {
thrown = true;
}
assertTrue(thrown);
thrown = false;
try {
realm.checkPermissions(pCollection, permList);
} catch (UnauthorizedException e) {
thrown = true;
}
assertTrue(thrown);
thrown = false;
try {
realm.checkRole(pCollection, "role1");
} catch (UnauthorizedException e) {
thrown = true;
}
assertTrue(thrown);
thrown = false;
try {
realm.checkRoles(pCollection, roleList);
} catch (UnauthorizedException e) {
thrown = true;
}
assertTrue(thrown);
assertFalse(realm.hasAllRoles(pCollection, roleList));
assertFalse(realm.hasRole(pCollection, "role1"));
assertArrayEquals(new boolean[] { false, false }, realm.hasRoles(pCollection, roleList));
assertFalse(realm.isPermitted(pCollection, "perm1"));
assertFalse(realm.isPermitted(pCollection, new WildcardPermission("perm1")));
assertArrayEquals(new boolean[] { false, false }, realm.isPermitted(pCollection, "perm1", "perm2"));
assertArrayEquals(new boolean[] { false, false }, realm.isPermitted(pCollection, permList));
assertFalse(realm.isPermittedAll(pCollection, "perm1", "perm2"));
assertFalse(realm.isPermittedAll(pCollection, permList));
}
use of org.apache.shiro.subject.SimplePrincipalCollection in project shiro by apache.
the class TextConfigurationRealmTest method testCheckRole.
/*
* Tests that roles can't be checked while the realm is being loaded.
*/
@Test
public void testCheckRole() throws InterruptedException {
setUpForReadConfigurationTest();
executeTest(new Runnable() {
public void run() {
PrincipalCollection principalCollection = new SimplePrincipalCollection("user1", "realm1");
try {
realm.checkRoles(principalCollection, new String[] { "role1", "role2" });
} catch (AuthorizationException ae) {
fail("principal doesn't have all roles when it should");
}
}
});
}
Aggregations