Search in sources :

Example 1 with UserListQuery

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

the class UserCacheSession method getUserByUsername.

@Override
public UserModel getUserByUsername(RealmModel realm, String username) {
    logger.tracev("getUserByUsername: {0}", username);
    username = username.toLowerCase();
    if (realmInvalidations.contains(realm.getId())) {
        logger.tracev("realmInvalidations");
        return getDelegate().getUserByUsername(realm, username);
    }
    String cacheKey = getUserByUsernameCacheKey(realm.getId(), username);
    if (invalidations.contains(cacheKey)) {
        logger.tracev("invalidations");
        return getDelegate().getUserByUsername(realm, username);
    }
    UserListQuery query = cache.get(cacheKey, UserListQuery.class);
    String userId = null;
    if (query == null) {
        logger.tracev("query null");
        Long loaded = cache.getCurrentRevision(cacheKey);
        UserModel model = getDelegate().getUserByUsername(realm, username);
        if (model == null) {
            logger.tracev("model from delegate null");
            return null;
        }
        userId = model.getId();
        if (invalidations.contains(userId))
            return model;
        if (managedUsers.containsKey(userId)) {
            logger.tracev("return managed user");
            return managedUsers.get(userId);
        }
        UserModel adapter = getUserAdapter(realm, userId, loaded, model);
        if (adapter instanceof UserAdapter) {
            // this was cached, so we can cache query too
            query = new UserListQuery(loaded, cacheKey, realm, model.getId());
            cache.addRevisioned(query, startupRevision);
        }
        managedUsers.put(userId, adapter);
        return adapter;
    } else {
        userId = query.getUsers().iterator().next();
        if (invalidations.contains(userId)) {
            logger.tracev("invalidated cache return delegate");
            return getDelegate().getUserByUsername(realm, username);
        }
        logger.trace("return getUserById");
        return getUserById(realm, userId);
    }
}
Also used : UserModel(org.keycloak.models.UserModel) CachedUserModel(org.keycloak.models.cache.CachedUserModel) UserListQuery(org.keycloak.models.cache.infinispan.entities.UserListQuery)

Example 2 with UserListQuery

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

the class UserCacheSession method getUserByEmail.

@Override
public UserModel getUserByEmail(RealmModel realm, String email) {
    if (email == null)
        return null;
    email = email.toLowerCase();
    if (realmInvalidations.contains(realm.getId())) {
        return getDelegate().getUserByEmail(realm, email);
    }
    String cacheKey = getUserByEmailCacheKey(realm.getId(), email);
    if (invalidations.contains(cacheKey)) {
        return getDelegate().getUserByEmail(realm, email);
    }
    UserListQuery query = cache.get(cacheKey, UserListQuery.class);
    String userId = null;
    if (query == null) {
        Long loaded = cache.getCurrentRevision(cacheKey);
        UserModel model = getDelegate().getUserByEmail(realm, email);
        if (model == null)
            return null;
        userId = model.getId();
        if (invalidations.contains(userId))
            return model;
        if (managedUsers.containsKey(userId))
            return managedUsers.get(userId);
        UserModel adapter = getUserAdapter(realm, userId, loaded, model);
        if (adapter instanceof UserAdapter) {
            query = new UserListQuery(loaded, cacheKey, realm, model.getId());
            cache.addRevisioned(query, startupRevision);
        }
        managedUsers.put(userId, adapter);
        return adapter;
    } else {
        userId = query.getUsers().iterator().next();
        if (invalidations.contains(userId)) {
            return getDelegate().getUserByEmail(realm, email);
        }
        return getUserById(realm, userId);
    }
}
Also used : UserModel(org.keycloak.models.UserModel) CachedUserModel(org.keycloak.models.cache.CachedUserModel) UserListQuery(org.keycloak.models.cache.infinispan.entities.UserListQuery)

Example 3 with UserListQuery

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

the class UserCacheSession method getUserByFederatedIdentity.

@Override
public UserModel getUserByFederatedIdentity(RealmModel realm, FederatedIdentityModel socialLink) {
    if (socialLink == null)
        return null;
    if (!realm.isIdentityFederationEnabled())
        return null;
    if (realmInvalidations.contains(realm.getId())) {
        return getDelegate().getUserByFederatedIdentity(realm, socialLink);
    }
    String cacheKey = getUserByFederatedIdentityCacheKey(realm.getId(), socialLink);
    if (invalidations.contains(cacheKey)) {
        return getDelegate().getUserByFederatedIdentity(realm, socialLink);
    }
    UserListQuery query = cache.get(cacheKey, UserListQuery.class);
    String userId = null;
    if (query == null) {
        Long loaded = cache.getCurrentRevision(cacheKey);
        UserModel model = getDelegate().getUserByFederatedIdentity(realm, socialLink);
        if (model == null)
            return null;
        userId = model.getId();
        if (invalidations.contains(userId))
            return model;
        if (managedUsers.containsKey(userId))
            return managedUsers.get(userId);
        UserModel adapter = getUserAdapter(realm, userId, loaded, model);
        if (adapter instanceof UserAdapter) {
            query = new UserListQuery(loaded, cacheKey, realm, model.getId());
            cache.addRevisioned(query, startupRevision);
        }
        managedUsers.put(userId, adapter);
        return adapter;
    } else {
        userId = query.getUsers().iterator().next();
        if (invalidations.contains(userId)) {
            invalidations.add(cacheKey);
            return getDelegate().getUserByFederatedIdentity(realm, socialLink);
        }
        return getUserById(realm, userId);
    }
}
Also used : UserModel(org.keycloak.models.UserModel) CachedUserModel(org.keycloak.models.cache.CachedUserModel) UserListQuery(org.keycloak.models.cache.infinispan.entities.UserListQuery)

Example 4 with UserListQuery

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

the class UserCacheSession method findServiceAccount.

public UserModel findServiceAccount(ClientModel client) {
    String username = ServiceAccountConstants.SERVICE_ACCOUNT_USER_PREFIX + client.getClientId();
    logger.tracev("getServiceAccount: {0}", username);
    username = username.toLowerCase();
    RealmModel realm = client.getRealm();
    if (realmInvalidations.contains(realm.getId())) {
        logger.tracev("realmInvalidations");
        return getDelegate().getServiceAccount(client);
    }
    String cacheKey = getUserByUsernameCacheKey(realm.getId(), username);
    if (invalidations.contains(cacheKey)) {
        logger.tracev("invalidations");
        return getDelegate().getServiceAccount(client);
    }
    UserListQuery query = cache.get(cacheKey, UserListQuery.class);
    String userId = null;
    if (query == null) {
        logger.tracev("query null");
        Long loaded = cache.getCurrentRevision(cacheKey);
        UserModel model = getDelegate().getServiceAccount(client);
        if (model == null) {
            logger.tracev("model from delegate null");
            return null;
        }
        userId = model.getId();
        if (invalidations.contains(userId))
            return model;
        if (managedUsers.containsKey(userId)) {
            logger.tracev("return managed user");
            return managedUsers.get(userId);
        }
        UserModel adapter = getUserAdapter(realm, userId, loaded, model);
        if (adapter instanceof UserAdapter) {
            // this was cached, so we can cache query too
            query = new UserListQuery(loaded, cacheKey, realm, model.getId());
            cache.addRevisioned(query, startupRevision);
        }
        managedUsers.put(userId, adapter);
        return adapter;
    } else {
        userId = query.getUsers().iterator().next();
        if (invalidations.contains(userId)) {
            logger.tracev("invalidated cache return delegate");
            return getDelegate().getUserByUsername(realm, username);
        }
        logger.trace("return getUserById");
        return getUserById(realm, userId);
    }
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) CachedUserModel(org.keycloak.models.cache.CachedUserModel) UserListQuery(org.keycloak.models.cache.infinispan.entities.UserListQuery)

Aggregations

UserModel (org.keycloak.models.UserModel)4 CachedUserModel (org.keycloak.models.cache.CachedUserModel)4 UserListQuery (org.keycloak.models.cache.infinispan.entities.UserListQuery)4 RealmModel (org.keycloak.models.RealmModel)1