Example usage for org.apache.shiro.authz.annotation Logical AND

List of usage examples for org.apache.shiro.authz.annotation Logical AND

Introduction

In this page you can find the example usage for org.apache.shiro.authz.annotation Logical AND.

Prototype

Logical AND

To view the source code for org.apache.shiro.authz.annotation Logical AND.

Click Source Link

Usage

From source file:club.zhcs.agent.ext.shiro.aop.ThunderPermissionAnnotationHandler.java

License:Apache License

/**
 * Ensures that the calling <code>Subject</code> has the Annotation's
 * specified permissions, and if not, throws an
 * <code>AuthorizingException</code> indicating access is denied.
 *
 * @param a//w w  w. ja  va2s.co m
 *            the NutzRequiresPermissions annotation being inspected to
 *            check for one or more permissions
 * @throws org.apache.shiro.authz.AuthorizationException
 *             if the calling <code>Subject</code> does not have the
 *             permission(s) necessary to continue access or execution.
 */
@Override
public void assertAuthorized(Annotation a) throws AuthorizationException {
    if (!(a instanceof ThunderRequiresPermissions))
        return;

    ThunderRequiresPermissions rpAnnotation = (ThunderRequiresPermissions) a;
    InstallPermission[] perms_ = rpAnnotation.value();
    Subject subject = getSubject();

    final String[] perms = new String[perms_.length];

    Lang.each(perms_, new Each<InstallPermission>() {

        @Override
        public void invoke(int index, InstallPermission ele, int length)
                throws ExitLoop, ContinueLoop, LoopException {
            perms[index] = ele.getName();
        }
    });

    if (perms.length == 1) {
        subject.checkPermission(perms[0]);
        return;
    }
    if (Logical.AND.equals(rpAnnotation.logical())) {
        getSubject().checkPermissions(perms);
        return;
    }
    if (Logical.OR.equals(rpAnnotation.logical())) {
        boolean hasAtLeastOnePermission = false;
        for (String permission : perms)
            if (getSubject().isPermitted(permission))
                hasAtLeastOnePermission = true;
        if (!hasAtLeastOnePermission)
            getSubject().checkPermission(perms[0]);
    }
}

From source file:cn.dreampie.common.plugin.shiro.plugin.PermissionAuthzHandler.java

License:Apache License

@Override
public void assertAuthorized() throws AuthorizationException {
    Subject subject = getSubject();

    if (!(annotation instanceof RequiresPermissions))
        return;//from w  w w  .ja  v a2 s.co  m

    RequiresPermissions rpAnnotation = (RequiresPermissions) annotation;
    String[] perms = rpAnnotation.value();

    if (perms.length == 1) {
        subject.checkPermission(perms[0]);
        return;
    }
    if (Logical.AND.equals(rpAnnotation.logical())) {
        getSubject().checkPermissions(perms);
        return;
    }
    if (Logical.OR.equals(rpAnnotation.logical())) {
        // Avoid processing exceptions unnecessarily - "delay" throwing the
        // exception by calling hasRole first
        boolean hasAtLeastOnePermission = false;
        for (String permission : perms)
            if (subject.isPermitted(permission))
                hasAtLeastOnePermission = true;
        // Cause the exception if none of the role match, note that the
        // exception message will be a bit misleading
        if (!hasAtLeastOnePermission)
            subject.checkPermission(perms[0]);

    }

}

From source file:cn.dreampie.common.plugin.shiro.plugin.RoleAuthzHandler.java

License:Apache License

@Override
public void assertAuthorized() throws AuthorizationException {

    Subject subject = getSubject();

    if (!(annotation instanceof RequiresRoles))
        return;//from   w  w  w. j av  a2  s.  c  om
    RequiresRoles rrAnnotation = (RequiresRoles) annotation;
    String[] roles = rrAnnotation.value();

    if (roles.length == 1) {
        subject.checkRole(roles[0]);
        return;
    }
    if (Logical.AND.equals(rrAnnotation.logical())) {
        subject.checkRoles(Arrays.asList(roles));
        return;
    }
    if (Logical.OR.equals(rrAnnotation.logical())) {
        // Avoid processing exceptions unnecessarily - "delay" throwing the exception by calling hasRole first
        boolean hasAtLeastOneRole = false;
        for (String role : roles)
            if (subject.hasRole(role))
                hasAtLeastOneRole = true;
        // Cause the exception if none of the role match, note that the exception message will be a bit misleading
        if (!hasAtLeastOneRole)
            subject.checkRole(roles[0]);
    }
}

From source file:com.dbumama.market.web.core.plugin.shiro.PermissionAuthzHandler.java

License:Apache License

public void assertAuthorized() throws AuthorizationException {
    if (!(annotation instanceof RequiresPermissions))
        return;// ww  w.  j a va2s  .com

    RequiresPermissions rpAnnotation = (RequiresPermissions) annotation;
    String[] perms = rpAnnotation.value();
    Subject subject = getSubject();

    if (perms.length == 1) {
        subject.checkPermission(perms[0]);
        return;
    }
    if (Logical.AND.equals(rpAnnotation.logical())) {
        getSubject().checkPermissions(perms);
        return;
    }
    if (Logical.OR.equals(rpAnnotation.logical())) {
        // Avoid processing exceptions unnecessarily - "delay" throwing the
        // exception by calling hasRole first
        boolean hasAtLeastOnePermission = false;
        for (String permission : perms)
            if (getSubject().isPermitted(permission))
                hasAtLeastOnePermission = true;
        // Cause the exception if none of the role match, note that the
        // exception message will be a bit misleading
        if (!hasAtLeastOnePermission)
            getSubject().checkPermission(perms[0]);

    }

}

From source file:com.dbumama.market.web.core.plugin.shiro.RoleAuthzHandler.java

License:Apache License

public void assertAuthorized() throws AuthorizationException {
    //if (!(annotation instanceof RequiresRoles)) return;
    RequiresRoles rrAnnotation = (RequiresRoles) annotation;
    String[] roles = rrAnnotation.value();

    if (roles.length == 1) {
        getSubject().checkRole(roles[0]);
        return;/*from  w w w.  j a  v a  2s  .c om*/
    }
    if (Logical.AND.equals(rrAnnotation.logical())) {
        getSubject().checkRoles(Arrays.asList(roles));
        return;
    }
    if (Logical.OR.equals(rrAnnotation.logical())) {
        // Avoid processing exceptions unnecessarily - "delay" throwing the exception by calling hasRole first
        boolean hasAtLeastOneRole = false;
        for (String role : roles)
            if (getSubject().hasRole(role))
                hasAtLeastOneRole = true;
        // Cause the exception if none of the role match, note that the exception message will be a bit misleading
        if (!hasAtLeastOneRole)
            getSubject().checkRole(roles[0]);
    }
}

From source file:com.ftww.basic.plugin.shiro.core.handler.PermissionAuthzHandler.java

License:Apache License

@Override
public void assertAuthorized() throws AuthorizationException {
    Subject subject = getSubject();

    if (!(annotation instanceof RequiresPermissions))
        return;/*from  w ww . jav a 2s. com*/

    RequiresPermissions rpAnnotation = (RequiresPermissions) annotation;
    String[] perms = rpAnnotation.value();

    if (perms.length == 1) {
        subject.checkPermission(perms[0]);
        return;
    }
    if (Logical.AND.equals(rpAnnotation.logical())) {
        getSubject().checkPermissions(perms);
        return;
    }
    if (Logical.OR.equals(rpAnnotation.logical())) {
        // Avoid processing exceptions unnecessarily - "delay" throwing the
        // exception by calling hasRole first
        boolean hasAtLeastOnePermission = false;
        for (String permission : perms)
            if (subject.isPermitted(permission))
                hasAtLeastOnePermission = true;
        // Cause the exception if none of the role match, note that the
        // exception message will be a bit misleading
        if (!hasAtLeastOnePermission)
            subject.checkPermission(perms[0]);
    }
}

From source file:com.ftww.basic.plugin.shiro.core.handler.RoleAuthzHandler.java

License:Apache License

@Override
public void assertAuthorized() throws AuthorizationException {
    Subject subject = getSubject();
    if (!(annotation instanceof RequiresRoles))
        return;/* www.  j  a  v a 2s.c o m*/
    RequiresRoles rrAnnotation = (RequiresRoles) annotation;
    String[] roles = rrAnnotation.value();

    if (roles.length == 1) {
        subject.checkRole(roles[0]);
        return;
    }
    //?&&?
    if (Logical.AND.equals(rrAnnotation.logical())) {
        subject.checkRoles(Arrays.asList(roles));
        return;
    }
    //?||?
    if (Logical.OR.equals(rrAnnotation.logical())) {
        boolean hasAtLeastOneRole = false;
        for (String role : roles)
            if (subject.hasRole(role))//?
                hasAtLeastOneRole = true;
        //???role??
        if (!hasAtLeastOneRole)
            subject.checkRole(roles[0]);//?
    }
}

From source file:com.janeluo.jfinalplus.plugin.shiro.PermissionAuthzHandler.java

License:Apache License

@Override
public void assertAuthorized() throws AuthorizationException {
    if (!(annotation instanceof RequiresPermissions))
        return;// w  w  w. j  a v a  2 s .  c  o  m

    RequiresPermissions rpAnnotation = (RequiresPermissions) annotation;
    String[] perms = rpAnnotation.value();
    Subject subject = getSubject();

    if (perms.length == 1) {
        subject.checkPermission(perms[0]);
        return;
    }
    if (Logical.AND.equals(rpAnnotation.logical())) {
        getSubject().checkPermissions(perms);
        return;
    }
    if (Logical.OR.equals(rpAnnotation.logical())) {
        // Avoid processing exceptions unnecessarily - "delay" throwing the
        // exception by calling hasRole first
        boolean hasAtLeastOnePermission = false;
        for (String permission : perms)
            if (getSubject().isPermitted(permission))
                hasAtLeastOnePermission = true;
        // Cause the exception if none of the role match, note that the
        // exception message will be a bit misleading
        if (!hasAtLeastOnePermission)
            getSubject().checkPermission(perms[0]);

    }

}

From source file:com.jeasyframeworks.extentions.shiro.handler.RoleAuthzHandler.java

License:Apache License

@Override
public void assertAuthorized() throws AuthorizationException {
    //if (!(annotation instanceof RequiresRoles)) return;
    RequiresRoles rrAnnotation = (RequiresRoles) annotation;
    String[] roles = rrAnnotation.value();

    if (roles.length == 1) {
        getSubject().checkRole(roles[0]);
        return;// w ww  . ja  v a 2  s .  co  m
    }
    if (Logical.AND.equals(rrAnnotation.logical())) {
        getSubject().checkRoles(Arrays.asList(roles));
        return;
    }
    if (Logical.OR.equals(rrAnnotation.logical())) {
        // Avoid processing exceptions unnecessarily - "delay" throwing the exception by calling hasRole first
        boolean hasAtLeastOneRole = false;
        for (String role : roles)
            if (getSubject().hasRole(role))
                hasAtLeastOneRole = true;
        // Cause the exception if none of the role match, note that the exception message will be a bit misleading
        if (!hasAtLeastOneRole)
            getSubject().checkRole(roles[0]);
    }
}

From source file:com.klwork.explorer.security.ShiroSecurityNavigator.java

License:Apache License

/**
 * Check access for class/*w ww  .j av  a  2 s.co  m*/
 * ??Shiro?
 * @param clazz
 * @return
 */
public static boolean hasAccess(Class<?> clazz) {
    boolean isAllow = true;

    if (clazz.isAnnotationPresent(RequiresRoles.class)) {
        isAllow = false;

        RequiresRoles requiresRoles = clazz.getAnnotation(RequiresRoles.class);
        String[] roles = requiresRoles.value();
        Logical logical = requiresRoles.logical();
        if (roles.length > 0) {
            Subject subject = SecurityUtils.getSubject();
            if (!subject.isAuthenticated()) {
                return false;
            }

            if (logical == Logical.AND && subject.hasAllRoles(Arrays.asList(roles))) {
                isAllow = true;
            }

            if (logical == Logical.OR) {
                for (boolean hasRole : subject.hasRoles(Arrays.asList(roles))) {
                    if (hasRole) {
                        isAllow = true;
                        break;
                    }
                }
            }
        }
    }

    if (isAllow && clazz.isAnnotationPresent(RequiresPermissions.class)) {
        isAllow = false;

        RequiresPermissions requiresPermissions = clazz.getAnnotation(RequiresPermissions.class);
        String[] permissions = requiresPermissions.value();
        Logical logical = requiresPermissions.logical();
        Subject subject = SecurityUtils.getSubject();

        if (permissions.length > 0) {
            if (!subject.isAuthenticated()) {
                return false;
            }

            if (logical == Logical.AND && subject.isPermittedAll(permissions)) {
                isAllow = true;
            }

            if (logical == Logical.OR && subject.isPermittedAll(permissions)) {
                for (boolean isPermitted : subject.isPermitted(permissions)) {
                    if (isPermitted) {
                        isAllow = true;
                        break;
                    }
                }
            }
        }
    }

    if (isAllow && clazz.isAnnotationPresent(RequiresAuthentication.class)) {
        Subject subject = SecurityUtils.getSubject();
        isAllow = subject.isAuthenticated();
    }

    if (isAllow && clazz.isAnnotationPresent(RequiresGuest.class)) {
        Subject subject = SecurityUtils.getSubject();
        isAllow = subject.getPrincipals() == null;
    }

    if (isAllow && clazz.isAnnotationPresent(RequiresUser.class)) {
        Subject subject = SecurityUtils.getSubject();
        isAllow = subject.getPrincipals() != null && !subject.getPrincipals().isEmpty();
    }

    return isAllow;
}