Example usage for org.apache.shiro.authz SimpleAuthorizationInfo addRoles

List of usage examples for org.apache.shiro.authz SimpleAuthorizationInfo addRoles

Introduction

In this page you can find the example usage for org.apache.shiro.authz SimpleAuthorizationInfo addRoles.

Prototype

public void addRoles(Collection<String> roles) 

Source Link

Document

Adds (assigns) multiple roles to those associated with the account.

Usage

From source file:$.ShiroDbRealm.java

License:Apache License

/**
     * ?, ???.//from  w  ww.  j  av a  2  s  .  co  m
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        ShiroUser shiroUser = (ShiroUser) principals.getPrimaryPrincipal();
        User user = accountService.findUserByLoginName(shiroUser.loginName);
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.addRoles(user.getRoleList());
        return info;
    }

From source file:action.ShiroDbRealm.java

License:Apache License

/**
 * ?, ???./*from  w  ww .j a v  a  2 s.c  om*/
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    ShiroUser shiroUser = (ShiroUser) principals.getPrimaryPrincipal();

    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    // roles
    List<Role> roles = roleService.getByUserId(shiroUser.id);
    List<String> stringRoles = new ArrayList<String>(roles.size());
    for (Role role : roles) {
        stringRoles.add(role.getName());
    }
    info.addRoles(stringRoles);

    // permissions
    List<Permission> permissions = permissionService.getByUserId(shiroUser.id);
    Set<String> stringPermissions = new HashSet<String>(permissions.size());
    for (Permission permission : permissions) {
        stringPermissions.add(permission.getValue());
    }
    info.setStringPermissions(stringPermissions);

    return info;
}

From source file:annis.security.ANNISUserRealm.java

License:Apache License

@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    Validate.isInstanceOf(String.class, principals.getPrimaryPrincipal());
    String userName = (String) principals.getPrimaryPrincipal();

    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();

    User user = confManager.getUser(userName);

    if (user != null) {
        // only add any user role/permission if account is not expired
        if (user.getExpires() == null || user.getExpires().isAfterNow()) {
            info.addRole(userName);// w  w w  .  j av a2 s .co  m

            info.addRoles(user.getGroups());
            info.addRole(defaultUserRole);
            // add the permission to create url short IDs from every IP
            info.addStringPermission("shortener:create:*");
            // add any manual given permissions
            info.addStringPermissions(user.getPermissions());
        }
    } else if (userName.equals(anonymousUser)) {
        info.addRole(anonymousUser);
        if (confManager.getUseShortenerWithoutLogin() != null) {
            // add the permission to create url short IDs from the trusted IPs
            for (String trustedIPs : confManager.getUseShortenerWithoutLogin()) {
                info.addStringPermission("shortener:create:" + trustedIPs.replaceAll("[.:]", "_"));
            }
        }

    }
    return info;
}

From source file:cc.rainier.fss.service.account.ShiroDbRealm.java

License:Apache License

/**
 * ?, ???./*from  w  ww .j a va 2  s  .c om*/
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    ShiroUser shiroUser = (ShiroUser) principals.getPrimaryPrincipal();
    User user = accountService.findUserByLoginName(shiroUser.loginName);
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    info.addRoles(user.getRoleList());
    return info;
}

From source file:cc.sion.base.auth.shiro.ShiroDbRealm.java

License:Apache License

@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    String loginName = principals.getPrimaryPrincipal().toString();//?
    System.out.println("===user name:" + loginName);
    SysUser user = accountService.findUserByLoginName(loginName);
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    info.addRoles(user.getRoleList());
    return info;/*  w  w  w . ja  v  a  2  s.co  m*/
}

From source file:cn.com.xl.core.shiro.ShiroDbRealm.java

License:Apache License

/**
 * ???/*from  w w w .  j  a v a  2  s .  c om*/
 */
@SuppressWarnings({ "rawtypes" })
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    IShiro shiroFactory = ShiroManager.me().getDefaultShiroFactory();
    ShiroUser shiroUser = (ShiroUser) principals.getPrimaryPrincipal();
    Object userId = shiroUser.getId();
    List<Integer> roleList = shiroUser.getRoleList();
    Set<String> urlSet = new HashSet<>();
    Set<String> roleNameSet = new HashSet<>();
    for (Integer roleId : roleList) {
        List<Map> permissions = shiroFactory.findPermissionsByRoleId(userId, roleId);
        if (null != permissions) {
            for (Map map : permissions) {
                if (!Func.isEmpty(map.get("URL"))) {
                    urlSet.add(Func.toStr(map.get("URL")));
                }
            }
        }
        String roleName = shiroFactory.findRoleNameByRoleId(roleId);
        roleNameSet.add(roleName);
    }
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    info.addStringPermissions(urlSet);
    info.addRoles(roleNameSet);
    return info;
}

From source file:cn.newtouch.service.account.ShiroDbRealm.java

License:Apache License

/**
 * ?, ???.//from   ww  w.j a va 2s  .c o m
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    ShiroUser shiroUser = (ShiroUser) principals.getPrimaryPrincipal();
    User user = accountService.findUserByLoginName(shiroUser.loginName);
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    List<String> roles = Lists.newArrayList();
    roles.add(RoleType.getBaseTypeName(user.getRole()));
    info.addRoles(roles);
    return info;
}

From source file:com.agileEAP.security.service.ShiroDbRealm.java

License:Apache License

/**
 * ???, ????//from w  w w .  j  a  v  a 2  s  .  com
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    ShiroUser shiroUser = (ShiroUser) principals.getPrimaryPrincipal();
    Operator user = accountService.getByLoginName(shiroUser.loginName);
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    info.addRoles(user.getRoleList());
    return info;
}

From source file:com.asiainfo.tfsPlatform.web.service.ShiroDbRealm.java

License:Apache License

/**
 * ?, ???./*from w ww.  jav a2 s  .c  o  m*/
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    ShiroUser shiroUser = (ShiroUser) principals.getPrimaryPrincipal();
    UserDto userDto = accountService.findUserDtoByLoginName(shiroUser.loginName);
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    info.addRoles(userDto.getRoleList());
    return info;
}

From source file:com.baguaz.module.user.realm.AdminAuthorizingRealm.java

License:Apache License

@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    //????/*from  w  w w  .  j  a v  a2 s.co  m*/
    UserPrincipal principal = (UserPrincipal) super.getAvailablePrincipal(principals);
    String adminName = principal.getUserName();
    try {
        if (!principal.isAuthorized()) {
            //??????
            List<Record> roleRList = Role.dao.getRolesByUserid(principal.getUser().getInt("id"));
            Set<Role> roleMSet = roleRList.stream().map(r -> new Role().put(r)).collect(Collectors.toSet());
            principal.setRoles(roleMSet);

            List<String> authorities = null;
            if (principal.isAdmin()) {
                authorities = Permission.dao.getAllPermsName();
            } else {
                authorities = Permission.dao.getPermsNameByRoleids(principal.getRoleIds());
            }

            principal.setAuthorities(new LinkedHashSet<>(authorities));

            /*
             * ????
             */
            if (!principal.isAdmin()) {
                List<Integer> catpriv = CategoryPriv.dao.select("catid",
                        "roleid in(" + principal.getRoleIds() + ")", "catid asc", "", "");
                principal.setCatpriv(new LinkedHashSet<>(catpriv));
            }

            principal.setAuthorized(true);
            log.debug("?" + adminName + "???......");
            log.debug("?" + adminName + " " + principal.getRoleNameStrSet());

            Map<String, List<String>> aulistGroups = principal.getAuthorities().stream()
                    .sorted((a, b) -> a.compareTo(b)).collect(Collectors.groupingBy(a -> a.split(":")[0]));
            TreeMap<String, List<String>> map = new TreeMap<>();
            map.putAll(aulistGroups);
            log.debug("?" + adminName + " ??\n"
                    + map.values().stream().map(
                            ss -> ss.stream().collect(Collectors.joining(", ", "  " + ss.size() + " [", "]")))
                            .collect(Collectors.joining("\n", "{\n", "\n}")));
            log.debug("?" + adminName + " ???" + principal.getCatpriv());
        }
    } catch (RuntimeException e) {
        throw new AuthorizationException("?" + adminName + "?");
    }
    //???
    log.debug("???...");
    info.addStringPermissions(principal.getAuthorities());
    info.addRoles(principal.getRoleNameStrSet());
    return info;
}