Search in sources :

Example 11 with UserCache

use of org.keycloak.models.cache.UserCache in project keycloak by keycloak.

the class UserCredentialStoreManager method removeStoredCredential.

@Override
public boolean removeStoredCredential(RealmModel realm, UserModel user, String id) {
    throwExceptionIfInvalidUser(user);
    boolean removalResult = getStoreForUser(user).removeStoredCredential(realm, user, id);
    UserCache userCache = session.userCache();
    if (userCache != null) {
        userCache.evict(realm, user);
    }
    return removalResult;
}
Also used : OnUserCache(org.keycloak.models.cache.OnUserCache) UserCache(org.keycloak.models.cache.UserCache)

Example 12 with UserCache

use of org.keycloak.models.cache.UserCache in project keycloak by keycloak.

the class UserCredentialStoreManager method updateCredentialLabel.

@Override
public void updateCredentialLabel(RealmModel realm, UserModel user, String credentialId, String userLabel) {
    throwExceptionIfInvalidUser(user);
    CredentialModel credential = getStoredCredentialById(realm, user, credentialId);
    credential.setUserLabel(userLabel);
    getStoreForUser(user).updateCredential(realm, user, credential);
    UserCache userCache = session.userCache();
    if (userCache != null) {
        userCache.evict(realm, user);
    }
}
Also used : OnUserCache(org.keycloak.models.cache.OnUserCache) UserCache(org.keycloak.models.cache.UserCache)

Example 13 with UserCache

use of org.keycloak.models.cache.UserCache in project keycloak by keycloak.

the class UserStorageManager method deleteInvalidUser.

protected void deleteInvalidUser(final RealmModel realm, final UserModel user) {
    String userId = user.getId();
    String userName = user.getUsername();
    UserCache userCache = session.userCache();
    if (userCache != null) {
        userCache.evict(realm, user);
    }
    // This needs to be running in separate transaction because removing the user may end up with throwing
    // PessimisticLockException which also rollbacks Jpa transaction, hence when it is running in current transaction
    // it will become not usable for all consequent jpa calls. It will end up with Transaction is in rolled back
    // state error
    runJobInTransaction(session.getKeycloakSessionFactory(), session -> {
        RealmModel realmModel = session.realms().getRealm(realm.getId());
        if (realmModel == null)
            return;
        UserModel deletedUser = session.userLocalStorage().getUserById(realmModel, userId);
        if (deletedUser != null) {
            try {
                new UserManager(session).removeUser(realmModel, deletedUser, session.userLocalStorage());
                logger.debugf("Removed invalid user '%s'", userName);
            } catch (ModelException ex) {
                // Ignore exception, possible cause may be concurrent deleteInvalidUser calls which means
                // ModelException exception may be ignored because users will be removed with next call or is
                // already removed
                logger.debugf(ex, "ModelException thrown during deleteInvalidUser with username '%s'", userName);
            }
        }
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) CachedUserModel(org.keycloak.models.cache.CachedUserModel) ModelException(org.keycloak.models.ModelException) UserManager(org.keycloak.models.UserManager) OnUserCache(org.keycloak.models.cache.OnUserCache) UserCache(org.keycloak.models.cache.UserCache)

Example 14 with UserCache

use of org.keycloak.models.cache.UserCache in project keycloak by keycloak.

the class MigrateTo1_4_0 method migrateUsers.

private void migrateUsers(KeycloakSession session, RealmModel realm) {
    session.userLocalStorage().getUsersStream(realm, false).forEach(user -> {
        String email = KeycloakModelUtils.toLowerCaseSafe(user.getEmail());
        if (email != null && !email.equals(user.getEmail())) {
            user.setEmail(email);
            UserCache userCache = session.userCache();
            if (userCache != null) {
                userCache.evict(realm, user);
            }
        }
    });
}
Also used : UserCache(org.keycloak.models.cache.UserCache)

Aggregations

UserCache (org.keycloak.models.cache.UserCache)14 OnUserCache (org.keycloak.models.cache.OnUserCache)5 UserModel (org.keycloak.models.UserModel)4 ComponentModel (org.keycloak.component.ComponentModel)3 KeycloakSessionFactory (org.keycloak.models.KeycloakSessionFactory)3 ModelException (org.keycloak.models.ModelException)3 PasswordPolicy (org.keycloak.models.PasswordPolicy)3 RealmModel (org.keycloak.models.RealmModel)3 UserCredentialModel (org.keycloak.models.UserCredentialModel)3 LDAPObject (org.keycloak.storage.ldap.idm.model.LDAPObject)3 List (java.util.List)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 BadRequestException (javax.ws.rs.BadRequestException)2 Test (org.junit.Test)2 PasswordHashProvider (org.keycloak.credential.hash.PasswordHashProvider)2 LDAPConstants (org.keycloak.models.LDAPConstants)2 UserManager (org.keycloak.models.UserManager)2 CachedUserModel (org.keycloak.models.cache.CachedUserModel)2 PasswordCredentialModel (org.keycloak.models.credential.PasswordCredentialModel)2