Search in sources :

Example 1 with CachedUser

use of org.keycloak.models.cache.infinispan.entities.CachedUser in project keycloak by keycloak.

the class UserCacheSession method cacheUser.

protected UserModel cacheUser(RealmModel realm, UserModel delegate, Long revision) {
    int notBefore = getDelegate().getNotBeforeOfUser(realm, delegate);
    StorageId storageId = delegate.getFederationLink() != null ? new StorageId(delegate.getFederationLink(), delegate.getId()) : new StorageId(delegate.getId());
    CachedUser cached = null;
    UserAdapter adapter = null;
    if (!storageId.isLocal()) {
        ComponentModel component = realm.getComponent(storageId.getProviderId());
        UserStorageProviderModel model = new UserStorageProviderModel(component);
        if (!model.isEnabled()) {
            return new ReadOnlyUserModelDelegate(delegate) {

                @Override
                public boolean isEnabled() {
                    return false;
                }
            };
        }
        UserStorageProviderModel.CachePolicy policy = model.getCachePolicy();
        if (policy != null && policy == UserStorageProviderModel.CachePolicy.NO_CACHE) {
            return delegate;
        }
        cached = new CachedUser(revision, realm, delegate, notBefore);
        adapter = new UserAdapter(cached, this, session, realm);
        onCache(realm, adapter, delegate);
        long lifespan = model.getLifespan();
        if (lifespan > 0) {
            cache.addRevisioned(cached, startupRevision, lifespan);
        } else {
            cache.addRevisioned(cached, startupRevision);
        }
    } else {
        cached = new CachedUser(revision, realm, delegate, notBefore);
        adapter = new UserAdapter(cached, this, session, realm);
        onCache(realm, adapter, delegate);
        cache.addRevisioned(cached, startupRevision);
    }
    return adapter;
}
Also used : ReadOnlyUserModelDelegate(org.keycloak.models.utils.ReadOnlyUserModelDelegate) ComponentModel(org.keycloak.component.ComponentModel) CachedUser(org.keycloak.models.cache.infinispan.entities.CachedUser) UserStorageProviderModel(org.keycloak.storage.UserStorageProviderModel) StorageId(org.keycloak.storage.StorageId)

Example 2 with CachedUser

use of org.keycloak.models.cache.infinispan.entities.CachedUser in project keycloak by keycloak.

the class UserCacheSession method getUserById.

@Override
public UserModel getUserById(RealmModel realm, String id) {
    logger.tracev("getuserById {0}", id);
    if (isRegisteredForInvalidation(realm, id)) {
        logger.trace("registered for invalidation return delegate");
        return getDelegate().getUserById(realm, id);
    }
    if (managedUsers.containsKey(id)) {
        logger.trace("return managedusers");
        return managedUsers.get(id);
    }
    CachedUser cached = cache.get(id, CachedUser.class);
    if (cached != null && !cached.getRealm().equals(realm.getId())) {
        cached = null;
    }
    UserModel adapter = null;
    if (cached == null) {
        logger.trace("not cached");
        Long loaded = cache.getCurrentRevision(id);
        UserModel delegate = getDelegate().getUserById(realm, id);
        if (delegate == null) {
            logger.trace("delegate returning null");
            return null;
        }
        adapter = cacheUser(realm, delegate, loaded);
    } else {
        adapter = validateCache(realm, cached);
    }
    managedUsers.put(id, adapter);
    return adapter;
}
Also used : UserModel(org.keycloak.models.UserModel) CachedUserModel(org.keycloak.models.cache.CachedUserModel) CachedUser(org.keycloak.models.cache.infinispan.entities.CachedUser)

Aggregations

CachedUser (org.keycloak.models.cache.infinispan.entities.CachedUser)2 ComponentModel (org.keycloak.component.ComponentModel)1 UserModel (org.keycloak.models.UserModel)1 CachedUserModel (org.keycloak.models.cache.CachedUserModel)1 ReadOnlyUserModelDelegate (org.keycloak.models.utils.ReadOnlyUserModelDelegate)1 StorageId (org.keycloak.storage.StorageId)1 UserStorageProviderModel (org.keycloak.storage.UserStorageProviderModel)1