All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.ocoop.shiro.authz.SingleModularRealmAuthorizer Maven / Gradle / Ivy

package cn.ocoop.shiro.authz;

import cn.ocoop.shiro.authc.realm.AbstractAuthorizingRealm;
import com.google.common.collect.Lists;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.Authorizer;
import org.apache.shiro.authz.ModularRealmAuthorizer;
import org.apache.shiro.authz.Permission;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.subject.PrincipalCollection;

import java.util.List;

public class SingleModularRealmAuthorizer extends ModularRealmAuthorizer {

    public boolean hasRole(PrincipalCollection principals, String roleIdentifier) {
        assertRealmsConfigured();
        for (Realm realm : getRealms()) {
            if (!(realm instanceof Authorizer) || principals.fromRealm(realm.getName()).isEmpty()) continue;
            if (((Authorizer) realm).hasRole(principals, roleIdentifier)) return true;
        }
        return false;
    }

    public boolean isPermitted(PrincipalCollection principals, Permission permission) {
        assertRealmsConfigured();
        for (Realm realm : getRealms()) {
            if (!(realm instanceof Authorizer) || principals.fromRealm(realm.getName()).isEmpty()) continue;
            if (((Authorizer) realm).isPermitted(principals, permission)) return true;
        }
        return false;
    }

    @Override
    public boolean isPermitted(PrincipalCollection principals, String permission) {
        assertRealmsConfigured();
        for (Realm realm : getRealms()) {
            if (!(realm instanceof Authorizer) || principals.fromRealm(realm.getName()).isEmpty()) continue;
            if (((Authorizer) realm).isPermitted(principals, permission)) {
                return true;
            }
        }
        return false;
    }

    public List getRoles() {
        assertRealmsConfigured();
        PrincipalCollection principals = SecurityUtils.getSubject().getPrincipals();
        List roles = Lists.newArrayList();
        for (Realm realm : getRealms()) {
            if (!(realm instanceof Authorizer) || principals.fromRealm(realm.getName()).isEmpty()) continue;
            roles.addAll(((AbstractAuthorizingRealm) realm).getAuthorizationInfo(principals).getRoles());
        }
        return roles;
    }

    public List getPermissions() {
        assertRealmsConfigured();
        PrincipalCollection principals = SecurityUtils.getSubject().getPrincipals();
        List permissions = Lists.newArrayList();
        for (Realm realm : getRealms()) {
            if (!(realm instanceof Authorizer) || principals.fromRealm(realm.getName()).isEmpty()) continue;
            permissions.addAll(((AbstractAuthorizingRealm) realm).getAuthorizationInfo(principals).getStringPermissions());
        }
        return permissions;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy