Search in sources :

Example 16 with UserManager

use of org.keycloak.models.UserManager in project keycloak by keycloak.

the class ConcurrentTransactionsTest method tearDownRealm.

private void tearDownRealm(KeycloakSession session, String user1, String user2) {
    KeycloakSession currentSession = session;
    RealmModel realm = currentSession.realms().getRealmByName("original");
    UserModel realmUser1 = currentSession.users().getUserByUsername(realm, user1);
    UserModel realmUser2 = currentSession.users().getUserByUsername(realm, user2);
    UserManager um = new UserManager(currentSession);
    if (realmUser1 != null) {
        um.removeUser(realm, realmUser1);
    }
    if (realmUser2 != null) {
        um.removeUser(realm, realmUser2);
    }
    Assert.assertTrue(currentSession.realms().removeRealm(realm.getId()));
    Assert.assertThat(currentSession.realms().getRealm(realm.getId()), is(nullValue()));
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) UserManager(org.keycloak.models.UserManager) KeycloakSession(org.keycloak.models.KeycloakSession)

Example 17 with UserManager

use of org.keycloak.models.UserManager in project keycloak by keycloak.

the class AuthenticationSessionProviderTest method after.

@After
public void after() {
    testingClient.server().run(session -> {
        RealmModel realm = session.realms().getRealm("test");
        session.sessions().removeUserSessions(realm);
        UserModel user1 = session.users().getUserByUsername(realm, "user1");
        UserModel user2 = session.users().getUserByUsername(realm, "user2");
        UserManager um = new UserManager(session);
        if (user1 != null) {
            um.removeUser(realm, user1);
        }
        if (user2 != null) {
            um.removeUser(realm, user2);
        }
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) UserManager(org.keycloak.models.UserManager) After(org.junit.After)

Example 18 with UserManager

use of org.keycloak.models.UserManager in project keycloak by keycloak.

the class UserSessionInitializerTest method cleanEnvironment.

@Override
public void cleanEnvironment(KeycloakSession s) {
    RealmModel realm = s.realms().getRealm(realmId);
    s.sessions().removeUserSessions(realm);
    UserModel user1 = s.users().getUserByUsername(realm, "user1");
    UserModel user2 = s.users().getUserByUsername(realm, "user2");
    UserManager um = new UserManager(s);
    if (user1 != null) {
        um.removeUser(realm, user1);
    }
    if (user2 != null) {
        um.removeUser(realm, user2);
    }
    s.realms().removeRealm(realmId);
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) UserManager(org.keycloak.models.UserManager)

Example 19 with UserManager

use of org.keycloak.models.UserManager in project keycloak by keycloak.

the class UserSessionPersisterProviderTest method testOnUserRemoved.

@Test
public void testOnUserRemoved() {
    int started = Time.currentTime();
    AtomicReference<UserSessionModel[]> origSessionsAt = new AtomicReference<>();
    inComittedTransaction(session -> {
        // Create some sessions in infinispan
        UserSessionModel[] origSessions = createSessions(session, realmId);
        origSessionsAt.set(origSessions);
    });
    inComittedTransaction(session -> {
        RealmModel realm = session.realms().getRealm(realmId);
        UserSessionModel[] origSessions = origSessionsAt.get();
        // Persist 2 offline sessions of 2 users
        UserSessionModel userSession1 = session.sessions().getUserSession(realm, origSessions[1].getId());
        UserSessionModel userSession2 = session.sessions().getUserSession(realm, origSessions[2].getId());
        persistUserSession(session, userSession1, true);
        persistUserSession(session, userSession2, true);
    });
    inComittedTransaction(session -> {
        RealmModel realm = session.realms().getRealm(realmId);
        // Load offline sessions
        loadPersistedSessionsPaginated(session, true, 10, 1, 2);
        // Properly delete user and assert his offlineSession removed
        UserModel user1 = session.users().getUserByUsername(realm, "user1");
        new UserManager(session).removeUser(realm, user1);
    });
    inComittedTransaction(session -> {
        RealmModel realm = session.realms().getRealm(realmId);
        UserSessionPersisterProvider persister = session.getProvider(UserSessionPersisterProvider.class);
        Assert.assertEquals(1, persister.getUserSessionsCount(true));
        List<UserSessionModel> loadedSessions = loadPersistedSessionsPaginated(session, true, 10, 1, 1);
        UserSessionModel persistedSession = loadedSessions.get(0);
        assertSession(persistedSession, session.users().getUserByUsername(realm, "user2"), "127.0.0.3", started, started, "test-app");
        // KEYCLOAK-2431 Assert that userSessionPersister is resistent even to situation, when users are deleted "directly".
        // No exception will happen. However session will be still there
        UserModel user2 = session.users().getUserByUsername(realm, "user2");
        session.users().removeUser(realm, user2);
        loadedSessions = loadPersistedSessionsPaginated(session, true, 10, 1, 1);
        // Cleanup
        UserSessionModel userSession = loadedSessions.get(0);
        session.sessions().removeUserSession(realm, userSession);
        persister.removeUserSession(userSession.getId(), userSession.isOffline());
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) UserSessionModel(org.keycloak.models.UserSessionModel) UserSessionPersisterProvider(org.keycloak.models.session.UserSessionPersisterProvider) UserManager(org.keycloak.models.UserManager) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test) KeycloakModelTest(org.keycloak.testsuite.model.KeycloakModelTest)

Example 20 with UserManager

use of org.keycloak.models.UserManager in project keycloak by keycloak.

the class UserSessionProviderOfflineModelTest method cleanEnvironment.

@Override
public void cleanEnvironment(KeycloakSession s) {
    RealmModel realm = s.realms().getRealm(realmId);
    s.sessions().removeUserSessions(realm);
    UserModel user1 = s.users().getUserByUsername(realm, "user1");
    UserModel user2 = s.users().getUserByUsername(realm, "user2");
    UserManager um = new UserManager(s);
    if (user1 != null) {
        um.removeUser(realm, user1);
    }
    if (user2 != null) {
        um.removeUser(realm, user2);
    }
    s.realms().removeRealm(realmId);
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) UserManager(org.keycloak.models.UserManager)

Aggregations

UserManager (org.keycloak.models.UserManager)26 UserModel (org.keycloak.models.UserModel)25 RealmModel (org.keycloak.models.RealmModel)20 After (org.junit.After)7 KeycloakSession (org.keycloak.models.KeycloakSession)7 Test (org.junit.Test)6 RealmManager (org.keycloak.services.managers.RealmManager)5 AbstractTestRealmKeycloakTest (org.keycloak.testsuite.AbstractTestRealmKeycloakTest)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 ModelTest (org.keycloak.testsuite.arquillian.annotation.ModelTest)4 UserSessionModel (org.keycloak.models.UserSessionModel)3 ClientModel (org.keycloak.models.ClientModel)2 CachedUserModel (org.keycloak.models.cache.CachedUserModel)2 UserCache (org.keycloak.models.cache.UserCache)2 ClientManager (org.keycloak.services.managers.ClientManager)2 DELETE (javax.ws.rs.DELETE)1 NoCache (org.jboss.resteasy.annotations.cache.NoCache)1 EventBuilder (org.keycloak.events.EventBuilder)1 KeycloakContext (org.keycloak.models.KeycloakContext)1 ModelException (org.keycloak.models.ModelException)1