Search in sources :

Example 1 with UserRegistrationProvider

use of org.keycloak.storage.user.UserRegistrationProvider in project keycloak by keycloak.

the class UserStorageManager method removeUser.

@Override
public boolean removeUser(RealmModel realm, UserModel user) {
    if (getFederatedStorage() != null)
        getFederatedStorage().preRemove(realm, user);
    StorageId storageId = new StorageId(user.getId());
    if (storageId.getProviderId() == null) {
        String federationLink = user.getFederationLink();
        boolean linkRemoved = federationLink == null || Optional.ofNullable(getStorageProviderInstance(realm, federationLink, UserRegistrationProvider.class)).map(provider -> provider.removeUser(realm, user)).orElse(false);
        return localStorage().removeUser(realm, user) && linkRemoved;
    }
    UserRegistrationProvider registry = getStorageProviderInstance(realm, storageId.getProviderId(), UserRegistrationProvider.class);
    if (registry == null) {
        throw new ModelException("Could not resolve UserRegistrationProvider: " + storageId.getProviderId());
    }
    return registry.removeUser(realm, user);
}
Also used : ModelException(org.keycloak.models.ModelException) UserRegistrationProvider(org.keycloak.storage.user.UserRegistrationProvider)

Example 2 with UserRegistrationProvider

use of org.keycloak.storage.user.UserRegistrationProvider in project keycloak by keycloak.

the class UserModelTest method testAddDirtyRemoveFederationUsersInTheSameGroupConcurrent.

@Test
@RequireProvider(UserStorageProvider.class)
public void testAddDirtyRemoveFederationUsersInTheSameGroupConcurrent() {
    final ConcurrentSkipListSet<String> userIds = new ConcurrentSkipListSet<>();
    String groupId = groupIds.get(0);
    registerUserFederationWithRealm();
    // Create users and let them join first group
    IntStream.range(0, 100).parallel().forEach(index -> inComittedTransaction(index, (session, i) -> {
        final RealmModel realm = session.realms().getRealm(realmId);
        final UserModel user = session.users().addUser(realm, "user-" + i);
        user.joinGroup(session.groups().getGroupById(realm, groupId));
        log.infof("Created user with id: %s", user.getId());
        userIds.add(user.getId());
        return null;
    }));
    // Remove users _from the federation_, simulates eg. user being removed from LDAP without Keycloak knowing
    withRealm(realmId, (session, realm) -> {
        UserStorageProvider instance = getUserFederationInstance(session, realm);
        log.debugf("Removing selected users from backend");
        IntStream.range(FIRST_DELETED_USER_INDEX, LAST_DELETED_USER_INDEX).forEach(j -> {
            final UserModel user = session.users().getUserByUsername(realm, "user-" + j);
            ((UserRegistrationProvider) instance).removeUser(realm, user);
        });
        return null;
    });
    IntStream.range(0, 7).parallel().forEach(index -> withRealm(realmId, (session, realm) -> {
        final GroupModel group = session.groups().getGroupById(realm, groupId);
        assertThat(session.users().getGroupMembersStream(realm, group).count(), is(100L - DELETED_USER_COUNT));
        return null;
    }));
    inComittedTransaction(session -> {
        // the cache manually.
        if (session.userCache() != null) {
            session.userCache().clear();
        }
        return null;
    });
    // Now delete the users, and count those that were not found to be deleted. This should be equal to the number
    // of users removed directly in the user federation.
    // Some of the transactions may fail due to conflicts as there are many parallel request, so repeat until all users are removed
    AtomicInteger notFoundUsers = new AtomicInteger();
    Set<String> remainingUserIds = new HashSet<>();
    do {
        userIds.stream().parallel().forEach(index -> inComittedTransaction(index, (session, userId) -> {
            final RealmModel realm = session.realms().getRealm(realmId);
            final UserModel user = session.users().getUserById(realm, userId);
            if (user != null) {
                log.debugf("Deleting user: %s", userId);
                session.users().removeUser(realm, user);
            } else {
                log.debugf("Failed deleting user: %s", userId);
                notFoundUsers.incrementAndGet();
            }
            return null;
        }, null, (session, userId) -> {
            log.debugf("Could not delete user %s", userId);
            remainingUserIds.add(userId);
        }));
        userIds.clear();
        userIds.addAll(remainingUserIds);
        remainingUserIds.clear();
    } while (!userIds.isEmpty());
    assertThat(notFoundUsers.get(), is(DELETED_USER_COUNT));
    withRealm(realmId, (session, realm) -> {
        final GroupModel group = session.groups().getGroupById(realm, groupId);
        assertThat(session.users().getGroupMembersStream(realm, group).collect(Collectors.toList()), Matchers.empty());
        return null;
    });
}
Also used : IntStream(java.util.stream.IntStream) Assume.assumeThat(org.junit.Assume.assumeThat) Constants(org.keycloak.models.Constants) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) UserModel(org.keycloak.models.UserModel) UserRegistrationProvider(org.keycloak.storage.user.UserRegistrationProvider) RealmProvider(org.keycloak.models.RealmProvider) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ComponentModel(org.keycloak.component.ComponentModel) Matchers.hasSize(org.hamcrest.Matchers.hasSize) GroupModel(org.keycloak.models.GroupModel) UserStorageProviderModel(org.keycloak.storage.UserStorageProviderModel) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) UserStorageProviderFactory(org.keycloak.storage.UserStorageProviderFactory) RealmModel(org.keycloak.models.RealmModel) UserStorageProvider(org.keycloak.storage.UserStorageProvider) KeycloakSession(org.keycloak.models.KeycloakSession) Set(java.util.Set) Matchers(org.hamcrest.Matchers) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors(java.util.stream.Collectors) UserProvider(org.keycloak.models.UserProvider) List(java.util.List) Matchers.hasItem(org.hamcrest.Matchers.hasItem) Assert.assertNull(org.junit.Assert.assertNull) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) Assert.assertFalse(org.junit.Assert.assertFalse) Matchers.is(org.hamcrest.Matchers.is) RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) UserStorageProvider(org.keycloak.storage.UserStorageProvider) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GroupModel(org.keycloak.models.GroupModel) UserRegistrationProvider(org.keycloak.storage.user.UserRegistrationProvider) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with UserRegistrationProvider

use of org.keycloak.storage.user.UserRegistrationProvider in project keycloak by keycloak.

the class UserModelTest method testAddDirtyRemoveFederationUser.

@Test
@RequireProvider(UserStorageProvider.class)
public void testAddDirtyRemoveFederationUser() {
    registerUserFederationWithRealm();
    withRealm(realmId, (session, realm) -> session.users().addUser(realm, "user-A"));
    // Remove user _from the federation_, simulates eg. user being removed from LDAP without Keycloak knowing
    withRealm(realmId, (session, realm) -> {
        final UserStorageProvider instance = getUserFederationInstance(session, realm);
        log.debugf("Removing selected users from backend");
        final UserModel user = session.users().getUserByUsername(realm, "user-A");
        ((UserRegistrationProvider) instance).removeUser(realm, user);
        return null;
    });
    withRealm(realmId, (session, realm) -> {
        if (session.userCache() != null) {
            session.userCache().clear();
        }
        final UserModel user = session.users().getUserByUsername(realm, "user-A");
        assertThat("User should not be found in the main store", user, Matchers.nullValue());
        return null;
    });
}
Also used : UserModel(org.keycloak.models.UserModel) UserStorageProvider(org.keycloak.storage.UserStorageProvider) UserRegistrationProvider(org.keycloak.storage.user.UserRegistrationProvider) Test(org.junit.Test)

Aggregations

UserRegistrationProvider (org.keycloak.storage.user.UserRegistrationProvider)3 Test (org.junit.Test)2 UserModel (org.keycloak.models.UserModel)2 UserStorageProvider (org.keycloak.storage.UserStorageProvider)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 ConcurrentSkipListSet (java.util.concurrent.ConcurrentSkipListSet)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)1 Matchers (org.hamcrest.Matchers)1 Matchers.hasItem (org.hamcrest.Matchers.hasItem)1 Matchers.hasSize (org.hamcrest.Matchers.hasSize)1 Matchers.is (org.hamcrest.Matchers.is)1 Assert.assertFalse (org.junit.Assert.assertFalse)1 Assert.assertNull (org.junit.Assert.assertNull)1 Assert.assertTrue (org.junit.Assert.assertTrue)1