Example usage for org.apache.shiro.subject PrincipalCollection fromRealm

List of usage examples for org.apache.shiro.subject PrincipalCollection fromRealm

Introduction

In this page you can find the example usage for org.apache.shiro.subject PrincipalCollection fromRealm.

Prototype

Collection fromRealm(String realmName);

Source Link

Document

Returns a single Subject's principals retrieved from the specified Realm only as a Collection, or an empty Collection if there are not any principals from that realm.

Usage

From source file:b4f.seguridad.ShiroAuthorizingRealm.java

public AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {

    System.out.println("ShiroAuthorizingRealm.doGetAuthorizationInfo()");

    String userName = (String) (principals.fromRealm(getName()).iterator().next());
    Usuario user;//from www  . j  a va2s.  c  o m
    try {
        user = UsersManager.getUser(userName);
    } catch (Exception ex) {
        throw new RuntimeException("Error looking up user " + userName, ex);
    }

    if (user != null) {
        List<String> roles;
        try {
            //TODO IMPLEMENTAR ROLES
            roles = new ArrayList<>();
            //                roles = umgr.getRoles(user);
        } catch (Exception ex) {
            throw new RuntimeException("Error looking up roles for user " + userName, ex);
        }
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        for (String role : roles) {
            info.addRole(role);
        }
        System.out.println("Returning " + roles.size() + " roles for user " + userName + " roles= " + roles);
        return info;

    } else {
        throw new RuntimeException("Usuarioname not found: " + userName);
    }
}

From source file:cn.ilongfei.shiro.util.ShiroRealmImpl.java

License:Apache License

@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    System.out.println(//from  www  .j a v  a2s .c om
            " , ?loaddoGetAuthorizationInfo.................");

    //  doGetAuthenticationInfo ?
    ShiroUser shiroUser = (ShiroUser) principals.fromRealm(getName()).iterator().next();

    String userName = shiroUser.getName();
    if ("".equals(userName)) {

        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();

        // ?<shiro:hasRole>name
        info.addRole("admin");
        // ? <shiro:hasPermission> name
        info.addStringPermission("user:edit");

        return info;
    } else if ("test".equals(userName)) {
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();

        // ?<shiro:hasRole>name
        info.addRole("test");
        // ? <shiro:hasPermission> name,  ?,? 
        info.addStringPermission("user:view");

        return info;
    } else {
        return null;
    }
}

From source file:cn.mario256.blog.AuthenticationRealm.java

License:Open Source License

/**
 * ???/*from   w w w .j  a  v  a2  s.c  o m*/
 * 
 * @param principalCollection
 *            PrincipalCollection
 * @return ??
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    Principal principal = (Principal) principalCollection.fromRealm(getName()).iterator().next();
    if (principal != null) {
        List<String> authorities = adminService.findAuthorities(principal.getId());
        if (authorities != null) {
            SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
            authorizationInfo.addStringPermissions(authorities);
            return authorizationInfo;
        }
    }
    return null;
}

From source file:com.app.AuthenticationRealm.java

License:Open Source License

/**
 * ???//from  ww w.  j  a  v a 2s. c o m
 * 
 * @param principals
 *            principals
 * @return ??
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    Principal principal = (Principal) principals.fromRealm(getName()).iterator().next();
    if (principal != null) {
        List<String> authorities = adminService.findAuthorities(principal.getId());
        if (authorities != null) {
            SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
            authorizationInfo.addStringPermissions(authorities);
            return authorizationInfo;
        }
    }
    return null;
}

From source file:com.axelor.auth.AuthRealm.java

License:Open Source License

@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {

    final String code = (String) principals.fromRealm(getName()).iterator().next();
    final User user = AuthUtils.getUser(code);

    if (user == null) {
        return null;
    }/*from w  w w .ja  v  a2  s. c o m*/

    final SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    final Group group = user.getGroup();
    if (group != null) {
        info.addRole(group.getCode());
    }

    return info;
}

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  ww . j  av a  2s. c  o  m*/
        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.blazarquant.bfp.core.security.config.DatabaseUserRealm.java

License:Apache License

@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    UserDetails userDetails = (UserDetails) principals.fromRealm(getName()).iterator().next();
    if (userDetails != null) {
        List<Role> userRoles = userDAO.findUserRoles(userDetails.getUserID());
        SimpleAuthorizationInfo authInfo = new SimpleAuthorizationInfo();
        userRoles.forEach(r -> authInfo.addRole(r.getName()));
        List<String> userPermissions = userDAO.findUserPermissions(userDetails.getUserID());
        userPermissions.forEach(p -> authInfo.addStringPermission(p));
        return authInfo;
    }//from  w w  w .j  a  va2  s.  c o  m
    return null;
}

From source file:com.cc.framework.security.AuthenticationRealm.java

License:Open Source License

/**
 * ???/*from w w w .  j  a va 2  s . c  o m*/
 * 
 * @param principals
 *            principals
 * @return ??
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    Principal principal = (Principal) principals.fromRealm(getName()).iterator().next();
    if (principal != null) {
        List<String> authorities = sysAdminService.findAuthorities(principal.getId());
        if (authorities != null) {
            SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
            authorizationInfo.addStringPermissions(authorities);
            return authorizationInfo;
        }
    }
    return null;
}

From source file:com.cj.config.security.JpaAuthorizingRealm.java

License:Apache License

@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    Long accountId = (Long) principals.fromRealm(getName()).iterator().next();
    Account account = accountRepository.findOne(accountId);
    if (account != null) {
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        for (Role role : account.getRoles()) {
            info.addRole(role.getName());
            info.addStringPermissions(role.getPermissionsAsStringSet());
        }//from   ww w  .  ja  v a2s  . com
        return info;
    } else {
        return null;
    }
}

From source file:com.codestudio.dorm.web.security.shiro.ShiroDataBaseRealm.java

License:Open Source License

@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    if (principals == null) {
        throw new AuthorizationException("Principal?");
    }/*from w  w  w  .  j  a va2 s . c o  m*/

    User user = (User) principals.fromRealm(getName()).iterator().next();

    // ??permission
    List<String> permissions = getPermissionsByUser(user.getId());// CollectionUtils.extractToList(user.getResourcesList(),
                                                                  // "permission", true);

    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();

    info.addStringPermissions(permissions);

    return info;
}