Search in sources :

Example 16 with UserSessionPersisterProvider

use of org.keycloak.models.session.UserSessionPersisterProvider in project keycloak by keycloak.

the class PersistSessionsCommand method doRunCommand.

@Override
public void doRunCommand(KeycloakSession sess) {
    final int count = getIntArg(0);
    final int batchCount = getIntArg(1);
    int remaining = count;
    while (remaining > 0) {
        int createInThisBatch = Math.min(batchCount, remaining);
        createSessionsBatch(createInThisBatch);
        remaining = remaining - createInThisBatch;
    }
    // Write some summary
    KeycloakModelUtils.runJobInTransaction(sessionFactory, new KeycloakSessionTask() {

        @Override
        public void run(KeycloakSession session) {
            UserSessionPersisterProvider persister = session.getProvider(UserSessionPersisterProvider.class);
            log.info("Command finished. Total number of sessions in persister: " + persister.getUserSessionsCount(true));
        }
    });
}
Also used : UserSessionPersisterProvider(org.keycloak.models.session.UserSessionPersisterProvider) KeycloakSessionTask(org.keycloak.models.KeycloakSessionTask) KeycloakSession(org.keycloak.models.KeycloakSession)

Example 17 with UserSessionPersisterProvider

use of org.keycloak.models.session.UserSessionPersisterProvider in project keycloak by keycloak.

the class InfinispanUserSessionProvider method getUserSessionsStream.

protected Stream<UserSessionModel> getUserSessionsStream(final RealmModel realm, ClientModel client, Integer firstResult, Integer maxResults, final boolean offline) {
    if (offline && loadOfflineSessionsFromDatabase) {
        // fetch the actual offline user session count from the database
        UserSessionPersisterProvider persister = session.getProvider(UserSessionPersisterProvider.class);
        return persister.loadUserSessionsStream(realm, client, true, firstResult, maxResults);
    }
    final String clientUuid = client.getId();
    UserSessionPredicate predicate = UserSessionPredicate.create(realm.getId()).client(clientUuid);
    return getUserSessionModels(realm, firstResult, maxResults, offline, predicate);
}
Also used : UserSessionPersisterProvider(org.keycloak.models.session.UserSessionPersisterProvider) UserSessionPredicate(org.keycloak.models.sessions.infinispan.stream.UserSessionPredicate)

Example 18 with UserSessionPersisterProvider

use of org.keycloak.models.session.UserSessionPersisterProvider in project keycloak by keycloak.

the class InfinispanUserSessionProvider method getUserSessionsCount.

protected long getUserSessionsCount(RealmModel realm, ClientModel client, boolean offline) {
    if (offline && loadOfflineSessionsFromDatabase) {
        // fetch the actual offline user session count from the database
        UserSessionPersisterProvider persister = session.getProvider(UserSessionPersisterProvider.class);
        return persister.getUserSessionsCount(realm, client, true);
    }
    Cache<String, SessionEntityWrapper<UserSessionEntity>> cache = getCache(offline);
    cache = CacheDecorators.skipCacheLoaders(cache);
    final String clientUuid = client.getId();
    return cache.entrySet().stream().filter(UserSessionPredicate.create(realm.getId()).client(clientUuid)).count();
}
Also used : UserSessionPersisterProvider(org.keycloak.models.session.UserSessionPersisterProvider) SessionEntityWrapper(org.keycloak.models.sessions.infinispan.changes.SessionEntityWrapper)

Example 19 with UserSessionPersisterProvider

use of org.keycloak.models.session.UserSessionPersisterProvider in project keycloak by keycloak.

the class UserSessionPersisterProviderTest method testNoSessions.

// KEYCLOAK-1999
@Test
public void testNoSessions() {
    inComittedTransaction(session -> {
        UserSessionPersisterProvider persister = session.getProvider(UserSessionPersisterProvider.class);
        Stream<UserSessionModel> sessions = persister.loadUserSessionsStream(0, 1, true, "00000000-0000-0000-0000-000000000000");
        Assert.assertEquals(0, sessions.count());
    });
}
Also used : UserSessionModel(org.keycloak.models.UserSessionModel) UserSessionPersisterProvider(org.keycloak.models.session.UserSessionPersisterProvider) Test(org.junit.Test) KeycloakModelTest(org.keycloak.testsuite.model.KeycloakModelTest)

Example 20 with UserSessionPersisterProvider

use of org.keycloak.models.session.UserSessionPersisterProvider in project keycloak by keycloak.

the class UserSessionPersisterProviderTest method persistUserSession.

private void persistUserSession(KeycloakSession session, UserSessionModel userSession, boolean offline) {
    UserSessionPersisterProvider persister = session.getProvider(UserSessionPersisterProvider.class);
    persister.createUserSession(userSession, offline);
    for (AuthenticatedClientSessionModel clientSession : userSession.getAuthenticatedClientSessions().values()) {
        persister.createClientSession(clientSession, offline);
    }
}
Also used : UserSessionPersisterProvider(org.keycloak.models.session.UserSessionPersisterProvider) AuthenticatedClientSessionModel(org.keycloak.models.AuthenticatedClientSessionModel)

Aggregations

UserSessionPersisterProvider (org.keycloak.models.session.UserSessionPersisterProvider)20 UserSessionModel (org.keycloak.models.UserSessionModel)10 RealmModel (org.keycloak.models.RealmModel)7 Test (org.junit.Test)6 KeycloakModelTest (org.keycloak.testsuite.model.KeycloakModelTest)6 AuthenticatedClientSessionModel (org.keycloak.models.AuthenticatedClientSessionModel)4 KeycloakSession (org.keycloak.models.KeycloakSession)4 UserModel (org.keycloak.models.UserModel)4 LinkedList (java.util.LinkedList)3 Set (java.util.Set)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 SessionEntityWrapper (org.keycloak.models.sessions.infinispan.changes.SessionEntityWrapper)3 ResetTimeOffsetEvent (org.keycloak.models.utils.ResetTimeOffsetEvent)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 Time (org.keycloak.common.util.Time)2 ClientModel (org.keycloak.models.ClientModel)2