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

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

Introduction

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

Prototype

boolean isEmpty();

Source Link

Document

Returns true if this collection is empty, false otherwise.

Usage

From source file:br.com.criativasoft.opendevice.restapi.auth.AbstractAuthorizingRealm.java

License:Open Source License

@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {

    if (principals.isEmpty())
        throw new AuthorizationException("Empty principals list!");

    AccountPrincipal principal = (AccountPrincipal) principals.getPrimaryPrincipal();

    Set<String> roles = new HashSet(Arrays.asList(principal.getType().name()));

    if (principal.getType() == AccountType.CLOUD_MANAGER)
        roles.add(AccountType.ROLES.ACCOUNT_MANAGER);

    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roles);
    return info;//from  w w w.  j a  va 2  s . c  o m
}

From source file:com.caricah.iotracah.bootstrap.security.IOTSecurityManager.java

License:Apache License

/**
 * Logs out the specified Subject from the system.
 * <p>/* w w w . j  av  a  2s .c  om*/
 * Note that most application developers should not call this method unless they have a good reason for doing
 * so.  The preferred way to logout a Subject is to call
 * <code>{@link Subject#logout Subject.logout()}</code>, not the
 * {@code SecurityManager} directly.
 * <p>
 * Framework developers on the other hand might find calling this method directly useful in certain cases.
 *
 * @param subject the subject to log out.
 * @since 1.0
 */
@Override
public void logout(Subject subject) {

    if (subject == null) {
        throw new IllegalArgumentException("Subject argument argument cannot be null.");
    }

    PrincipalCollection principals = subject.getPrincipals();
    if (principals != null && !principals.isEmpty()) {
        if (log.isDebugEnabled()) {
            log.debug("Logging out subject with primary principal {}", principals.getPrimaryPrincipal());
        }
        Authenticator authc = getAuthenticator();
        if (authc instanceof LogoutAware) {
            ((LogoutAware) authc).onLogout(principals);
        }
    }

    try {
        stopSession(subject);
    } catch (Exception e) {
        if (log.isDebugEnabled()) {
            String msg = "Unable to cleanly stop Session for Subject [" + subject.getPrincipal() + "] "
                    + "Ignoring (logging out).";
            log.debug(msg, e);
        }
    }

}

From source file:com.caricah.iotracah.bootstrap.security.realm.state.IOTSubject.java

License:Apache License

public void login(AuthenticationToken token) throws AuthenticationException {
    Subject subject = securityManager.login(this, token);

    PrincipalCollection principals;

    IOTSubject iotSubject = (IOTSubject) subject;
    //we have to do this in case there are assumed identities - we don't want to lose the 'real' principals:
    principals = iotSubject.principals;/*ww w .  ja v a2 s . c o  m*/
    String host = iotSubject.host;

    if (principals == null || principals.isEmpty()) {
        String msg = "Principals returned from securityManager.login( token ) returned a null or "
                + "empty value.  This value must be non null and populated with one or more elements.";
        throw new IllegalStateException(msg);
    }
    this.principals = principals;
    this.authenticated = true;
    if (token instanceof HostAuthenticationToken) {
        host = ((HostAuthenticationToken) token).getHost();
    }
    if (host != null) {
        this.host = host;
    }
    this.session = subject.getSession(false);

}

From source file:com.caricah.iotracah.bootstrap.security.realm.state.IOTSubject.java

License:Apache License

public boolean isRemembered() {
    PrincipalCollection principals = getPrincipals();
    return principals != null && !principals.isEmpty() && !isAuthenticated();
}

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   www .ja v a2s .  com

    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.metropolitan.methotels727.services.UserRealm.java

@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    if (principals == null) {
        throw new AuthorizationException("PrincipalCollection was null, which should not happen");
    }/*from ww w. j a v  a  2s  .  c o m*/
    if (principals.isEmpty()) {
        return null;
    }
    if (principals.fromRealm(getName()).size() <= 0) {
        return null;
    }
    String email = (String) principals.fromRealm(getName()).iterator().next();
    System.out.println("Username is" + email);
    if (email == null) {
        return null;
    }
    Korisnik korisnik = findByEmail(email);
    if (korisnik == null) {
        return null;
    }
    Set<String> roles = new HashSet<String>(1);
    roles.add(korisnik.getUloga().name());
    return new SimpleAuthorizationInfo(roles);
}

From source file:com.milospaunovic.sedam.services.UserRealm.java

@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    if (principals == null) {
        throw new AuthorizationException("PrincipalCollection was null, which should not happen");
    }/*from   ww  w.  j  av a  2s.  co m*/
    if (principals.isEmpty()) {
        return null;
    }

    if (principals.fromRealm(getName()).size() <= 0) {
        return null;
    }
    String username = (String) principals.fromRealm(getName()).iterator().next();
    System.out.println("Username is" + username);
    if (username == null) {
        return null;
    }
    User user = findByUsername(username);
    if (user == null) {
        return null;
    }
    Set<String> roles = new HashSet<String>(1);
    roles.add(user.getRola().name());
    return new SimpleAuthorizationInfo(roles);
}

From source file:com.mycompany.methotels.services.UserRealm.java

@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    if (principals == null) {
        throw new AuthorizationException("PrincipalCollection was null, which should not happen");
    }//from  w w w.j ava  2s . c om
    if (principals.isEmpty()) {
        return null;
    }
    if (principals.fromRealm(getName()).size() <= 0) {
        return null;
    }
    String username = (String) principals.fromRealm(getName()).iterator().next();
    System.out.println("Username is" + username);
    if (username == null) {
        return null;
    }
    Admin user = findByUsername(username);
    if (user == null) {
        return null;
    }
    Set<String> roles = new HashSet<String>(1);
    roles.add(user.getRola().name());
    return new SimpleAuthorizationInfo(roles);
}

From source file:com.sonicle.webtop.core.app.RunContext.java

License:Open Source License

private static boolean isPermitted(boolean strict, PrincipalCollection principals, String serviceId, String key,
        String action, String instance) {
    if (principals.isEmpty())
        return false;
    SecurityManager manager = SecurityUtils.getSecurityManager();
    if (!strict && isWebTopAdmin(principals))
        return true;
    //if (manager.isPermitted(principals, WebTopManager.WTADMIN_PSTRING)) return true;
    return manager.isPermitted(principals, ServicePermission
            .permissionString(ServicePermission.namespacedName(serviceId, key), action, instance));
}

From source file:com.webarch.common.shiro.authentication.ShiroRealm.java

License:Apache License

/**
 * ???/* ww  w . j  av  a  2 s. c o m*/
 *
 * @param principals
 * @return
 */
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    if (!principals.isEmpty() && principals.fromRealm(getName()).size() > 0) {
        Object id = principals.fromRealm(getName()).iterator().next();
        if (id != null) {
            SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
            if (enableRoles && enablePerms) {
                Map<String, Collection<String>> rolesAndPerms = realmService.getUserRolesAndPerms(id);
                Collection<String> roles = rolesAndPerms.get(roles_in_map_key);
                Collection<String> perms = rolesAndPerms.get(perms_in_map_key);
                if (roles != null && !roles.isEmpty()) {
                    info.addRoles(roles);
                }
                if (perms != null && !perms.isEmpty()) {
                    info.addStringPermissions(perms);
                }
            } else if (enableRoles && !enablePerms) {
                Collection<String> perms = realmService.getPermissions(id);
                if (perms != null && !perms.isEmpty()) {
                    info.addStringPermissions(perms);
                }
            } else if (enablePerms && !enableRoles) {
                Collection<String> roles = realmService.getRoles(id);
                if (roles != null && !roles.isEmpty()) {
                    info.addRoles(roles);
                }
            }
            return info;
        } else {
            return null;
        }
    } else
        return null;
}