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

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

Introduction

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

Prototype

public void setRoles(Set<String> roles) 

Source Link

Document

Sets the roles assigned to the account.

Usage

From source file:cn.itganhuo.app.web.shiro.ShiroDbRealm.java

License:Apache License

/**
 * ???????//from   ww w .j  a va2  s.c o  m
 *
 * @version 0.0.1-SNAPSHOT
 * @author -?
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    log.debug("Start reading user permissions.");

    String account = (String) getAvailablePrincipal(principals);
    User user = userService.loadByAccount(account);
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    Set<String> role = new HashSet<String>();
    Set<String> stringPermissions = new HashSet<String>();
    // TODO ?????
    if (1 == user.getType()) {
        role.add("user");
        stringPermissions.add("user:*");
    } else if (999 == user.getType()) {
        role.add("admin");
        stringPermissions.add("admin:*");
    }
    info.setRoles(role);
    info.setStringPermissions(stringPermissions);
    return info;
}

From source file:cn.mypandora.shiro.realm.UserRealm.java

License:Apache License

/**
 * ?Subject??//from  ww w  . ja  v  a2  s .co  m
 *
 * @param principals
 * @return
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    //????,(String)principals.fromRealm(this.getName()).iterator().next()
    String username = (String) principals.getPrimaryPrincipal();
    //???
    SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
    authorizationInfo.setRoles(baseUserService.findRole(username));
    authorizationInfo.setStringPermissions(baseUserService.findPermission(username));

    return authorizationInfo;
}

From source file:com.appleframework.pay.permission.shiro.realm.OperatorRealm.java

License:Apache License

@SuppressWarnings("unchecked")
@Override//  w  ww  .  j  av  a2s . co m
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    String loginName = (String) principals.getPrimaryPrincipal();

    SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();

    Subject subject = SecurityUtils.getSubject();
    Session session = subject.getSession();
    PmsOperator operator = (PmsOperator) session.getAttribute("PmsOperator");
    if (operator == null) {
        operator = pmsOperatorService.findOperatorByLoginName(loginName);
        session.setAttribute("PmsOperator", operator);
    }
    // ????
    Long operatorId = operator.getId();

    Set<String> roles = (Set<String>) session.getAttribute("ROLES");
    if (roles == null || roles.isEmpty()) {
        roles = pmsOperatorRoleService.getRoleCodeByOperatorId(operatorId);
        session.setAttribute("ROLES", roles);
    }
    // ?
    authorizationInfo.setRoles(roles);

    Set<String> permisstions = (Set<String>) session.getAttribute("PERMISSIONS");
    if (permisstions == null || permisstions.isEmpty()) {
        permisstions = pmsRolePermissionService.getPermissionsByOperatorId(operatorId);
        session.setAttribute("PERMISSIONS", permisstions);
    }
    // ?????
    authorizationInfo.setStringPermissions(permisstions);
    return authorizationInfo;
}

From source file:com.bennavetta.appsite.security.ObjectifyRealm.java

License:Apache License

@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    log.trace("Loading authorization info for {}", principals);
    Set<String> roles = new HashSet<>();
    Set<Permission> permissions = new HashSet<>();

    for (Object principal : principals.fromRealm(REALM_NAME)) // they're each strings
    {//from  w  w  w. ja  va  2 s  . com
        User user = ofy().load().type(User.class).id(principal.toString()).get();
        log.trace("Found user {}", user);
        roles.addAll(user.getRoles());
        for (String permStr : user.getPermissions()) {
            if (permStr.equals("all")) {
                permissions.add(new AllPermission());
            } else {
                permissions.add(new WildcardPermission(permStr));
            }
        }
    }

    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    info.setRoles(roles);
    info.setObjectPermissions(permissions);
    log.trace("Authorization info loaded: {}", info);
    return info;
}

From source file:com.cuisongliu.springboot.shiro.support.realm.ShiroAbstractRealm.java

License:Open Source License

@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

    String username = (String) principalCollection.getPrimaryPrincipal();
    SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
    String appKey = "";
    if (springShiroProperties.getEnableServer()) {
        appKey = springShiroProperties.getAppSuperKey();
    } else {//from  w w w  .  ja v a 2 s.  c o  m
        appKey = springShiroProperties.getAppKey();
    }
    UserInfo userInfo = userCache.selectUserInfoByUsername(appKey, username);
    authorizationInfo.setRoles(userInfo.getRoles());
    authorizationInfo.setStringPermissions(userInfo.getPermissions());
    return authorizationInfo;
}

From source file:com.digitalplay.network.ireader.shiro.ShiroDbRealm.java

License:Apache License

/**
 * ?, ???.//w w  w .j  ava 2s.c o m
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    String username = (String) principals.getPrimaryPrincipal();
    User user = userService.findByUsername(username);
    if (user != null) {
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        authorizationInfo.setRoles(userAuthService.findStringRoles(user));
        authorizationInfo.setStringPermissions(userAuthService.findStringPermissions(user));

        return authorizationInfo;
    }
    return null;
}

From source file:com.github.ibole.infrastructure.web.security.spring.shiro.realm.FormRealm.java

License:Apache License

@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    String userId = String.valueOf(principals.getPrimaryPrincipal());
    SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
    authorizationInfo.setRoles(wsService.findUserRoles(userId));
    authorizationInfo.setStringPermissions(wsService.findUserPermissions(userId));
    logger.debug("doGetAuthorizationInfo from DB.");
    return authorizationInfo;
}

From source file:com.github.richardwilly98.esdms.shiro.EsRealm.java

License:Open Source License

@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    log.trace("*** doGetAuthorizationInfo ***");
    Collection<User> principalList = principals.byType(User.class);
    if (principals.isEmpty()) {
        throw new AuthorizationException("Empty principal list!");
    }/*from w w w .j  a  v  a2  s . c  o  m*/

    User principal = Iterables.get(principalList, 0);//.iterator().next();
    log.debug(String.format("getAuthorization for %s", principal.getId()));
    Set<String> roles = new HashSet<String>();
    Set<String> permissions = new HashSet<String>();
    for (Role role : principal.getRoles()) {
        log.trace(String.format("add role %s to %s", role.getId(), principal.getId()));
        roles.add(role.getId());
        try {
            role = roleService.get(role.getId());
            for (Permission permission : role.getPermissions()) {
                log.trace(String.format("add permission %s to %s", permission.getId(), principal.getId()));
                permissions.add(permission.getId());
            }
        } catch (ServiceException ex) {
            log.error(String.format("Cannot get role from id [%s]", role.getId()), ex);
        }
    }
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    info.setRoles(roles);
    info.setStringPermissions(permissions);
    return info;
}

From source file:com.josue.shiro.authorization.custom.CustomRealm.java

@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    String principalUsername = (String) getAvailablePrincipal(principals);

    //FETCH FROM DATABASE.. OR SO.
    RoleLevel fetchedPermission = RoleLevel.LEVEL_1;
    String fetchedDomainName = "uuid-doc-123-TODO-check-if-OK";
    // ... multiple permissions map

    AccessLevelPermission perm = new AccessLevelPermission();
    perm.addAccessLevel(fetchedDomainName, fetchedPermission);

    Set<Permission> permissions = new HashSet<>();
    permissions.add(perm);/* w w w  .j  a v  a  2  s .c o m*/

    info.setObjectPermissions(permissions);
    //TODO each map key is mapped as a role, jus a example of use
    info.setRoles(new HashSet<>(Arrays.asList(fetchedDomainName)));
    return info;
}

From source file:com.kingen.shiro.realm.ShiroDbRealm.java

License:Apache License

/**
 * ?, ???.//from ww w  .  java2 s .  c  o  m
 * controller@RequeirePermissions? AuthorizationInfo ??????info????
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    logger.info("doGetAuthorizationInfo----");

    User user = (User) principals.getPrimaryPrincipal();
    //Authorization ????????????
    SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
    try {
        Set<String> roles = new HashSet<String>();
        //?employee?finance?hr?boss..??????
        // ?adminuser???
        roles.add("admin".equals(user.getUserId()) ? "admin" : "user");

        List<Menu> menus = accountService.findMenuByuserId(user.getUserId());
        Set<String> resources = new HashSet<String>();
        for (Menu m : menus) {
            if (!StringUtils.isEmpty(m.getFunId())) { //???? NULL?????
                resources.add(m.getFunId());
            }
        }

        authorizationInfo.setRoles(roles);
        authorizationInfo.setStringPermissions(resources);

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        logger.error("realm ?");
    }
    return authorizationInfo;

}