Java Code Examples for org.apache.shiro.subject.SimplePrincipalCollection#getPrimaryPrincipal()

The following examples show how to use org.apache.shiro.subject.SimplePrincipalCollection#getPrimaryPrincipal() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ShiroCache.java    From usergrid with Apache License 2.0 6 votes vote down vote up
/** get cache for application scope */
private ScopedCache<String, V> getCacheScope( K key ) {

    PrincipalIdentifier principal;
    if ( key instanceof SimplePrincipalCollection) {
        SimplePrincipalCollection spc = (SimplePrincipalCollection) key;
        principal = (PrincipalIdentifier) spc.getPrimaryPrincipal();

    } else {
        principal = (PrincipalIdentifier)key;
    }

    CacheScope scope = new CacheScope(new SimpleId(principal.getApplicationId(), "application"));
    ScopedCache<String, V> scopedCache = cacheFactory.getScopedCache(scope);
    return scopedCache;
}
 
Example 2
Source File: UserService.java    From Shiro-Action with MIT License 5 votes vote down vote up
/**
 * 删除所有此用户的在线用户
 */
public void offlineByUserId(Integer userId) {
    Collection<Session> activeSessions = sessionDAO.getActiveSessions();
    for (Session session : activeSessions) {
        SimplePrincipalCollection simplePrincipalCollection = (SimplePrincipalCollection) session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
        if (simplePrincipalCollection != null) {
            User user = (User) simplePrincipalCollection.getPrimaryPrincipal();
            if (user != null && userId.equals(user.getUserId())) {
                offlineBySessionId(String.valueOf(session.getId()));
            }
        }
    }
}
 
Example 3
Source File: GuavaCacheManager.java    From emodb with Apache License 2.0 5 votes vote down vote up
private String extractStringKey(Object key) {
    if(key instanceof SimplePrincipalCollection) {
        SimplePrincipalCollection coll = (SimplePrincipalCollection) key;
        return "SimplePrincipalCollection:" + coll.getPrimaryPrincipal();
    } else {
        return key.toString();
    }
}
 
Example 4
Source File: ShiroCache.java    From usergrid with Apache License 2.0 4 votes vote down vote up
/** key is the user UUID in string form + class name of key */
private String getKeyString( K key ) {

    String ret = null;
    Throwable throwable = null;
    String errorMessage = null;

    try {

        final String typeName = typeRef.getType().getTypeName();
        PrincipalIdentifier principalIdentifier;

        if (key instanceof SimplePrincipalCollection) {

            SimplePrincipalCollection spc = (SimplePrincipalCollection) key;

            if (spc.getPrimaryPrincipal() instanceof PrincipalIdentifier) {

                // principal is not user, try to get something unique as cache key
            	principalIdentifier = (PrincipalIdentifier) spc.getPrimaryPrincipal();

            } else {
                errorMessage = "Unknown principal type: " + key.getClass().getSimpleName();
                throw new CacheException( errorMessage );
            }

        } else if (key instanceof PrincipalIdentifier) {
            principalIdentifier = (PrincipalIdentifier)key;
            
        } else {
            // not a principal identifier, don't cache
            errorMessage = "Unknown key type: " + key.getClass().getSimpleName();
            throw new CacheException(errorMessage);
        }
        
        String token = principalIdentifier != null && principalIdentifier.getAccessTokenCredentials() != null ? principalIdentifier.getAccessTokenCredentials().getToken() : null;
        
        if (principalIdentifier instanceof ApplicationGuestPrincipal) {
        	//Guest principal needs a special identifier to ensure that the key is not the same as application principal
            ApplicationGuestPrincipal agp = (ApplicationGuestPrincipal) principalIdentifier;
            ret = buildKeyString("GUEST",agp.getApplicationId().toString(), typeName, token);

        } else if (principalIdentifier instanceof ApplicationPrincipal) {
            ApplicationPrincipal ap = (ApplicationPrincipal) principalIdentifier;
            ret = buildKeyString(ap.getApplicationId().toString(), typeName, token);

        } else if (principalIdentifier instanceof OrganizationPrincipal) {
            OrganizationPrincipal op = (OrganizationPrincipal) principalIdentifier;
            ret = buildKeyString(op.getOrganizationId().toString(), typeName, token);

        } else if (principalIdentifier instanceof ApplicationUserPrincipal) {
        	ApplicationUserPrincipal apup = (ApplicationUserPrincipal) principalIdentifier;
            ret = buildKeyString(apup.getUser().getUuid().toString(), typeName, token);

        } else if (principalIdentifier instanceof AdminUserPrincipal) {
        	AdminUserPrincipal adup = (AdminUserPrincipal) principalIdentifier;
            ret = buildKeyString(adup.getUser().getUuid().toString(), typeName, token);

        } else {
            errorMessage = "Unknown key type: " + key.getClass().getSimpleName();
        }

    } catch ( Throwable t ) {
        throwable = t;
    }

    if ( throwable != null ) {
        errorMessage = "Error generating cache key for key type " + key.getClass().getSimpleName();
        throw new CacheException( errorMessage, throwable );
    }

    if ( ret == null ) {
        throw new CacheException( errorMessage );
    }

    return ret;
}