Search in sources :

Example 1 with PersistentUserSessionAdapter

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

the class JpaUserSessionPersisterProvider method loadUserSessionsWithClientSessions.

private Stream<UserSessionModel> loadUserSessionsWithClientSessions(TypedQuery<PersistentUserSessionEntity> query, String offlineStr) {
    List<PersistentUserSessionAdapter> userSessionAdapters = closing(query.getResultStream().map(this::toAdapter).filter(Objects::nonNull)).collect(Collectors.toList());
    Map<String, PersistentUserSessionAdapter> sessionsById = userSessionAdapters.stream().collect(Collectors.toMap(UserSessionModel::getId, Function.identity()));
    Set<String> removedClientUUIDs = new HashSet<>();
    if (!sessionsById.isEmpty()) {
        String fromUserSessionId = userSessionAdapters.get(0).getId();
        String toUserSessionId = userSessionAdapters.get(userSessionAdapters.size() - 1).getId();
        TypedQuery<PersistentClientSessionEntity> queryClientSessions = em.createNamedQuery("findClientSessionsOrderedById", PersistentClientSessionEntity.class);
        queryClientSessions.setParameter("offline", offlineStr);
        queryClientSessions.setParameter("fromSessionId", fromUserSessionId);
        queryClientSessions.setParameter("toSessionId", toUserSessionId);
        closing(queryClientSessions.getResultStream()).forEach(clientSession -> {
            PersistentUserSessionAdapter userSession = sessionsById.get(clientSession.getUserSessionId());
            // check if we have a user session for the client session
            if (userSession != null) {
                boolean added = addClientSessionToAuthenticatedClientSessionsIfPresent(userSession, clientSession);
                if (!added) {
                    // client was removed in the meantime
                    removedClientUUIDs.add(clientSession.getClientId());
                }
            }
        });
    }
    for (String clientUUID : removedClientUUIDs) {
        onClientRemoved(clientUUID);
    }
    return userSessionAdapters.stream().map(UserSessionModel.class::cast);
}
Also used : PersistentUserSessionAdapter(org.keycloak.models.session.PersistentUserSessionAdapter) PersistentUserSessionModel(org.keycloak.models.session.PersistentUserSessionModel) UserSessionModel(org.keycloak.models.UserSessionModel) Objects(java.util.Objects) HashSet(java.util.HashSet)

Example 2 with PersistentUserSessionAdapter

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

the class JpaUserSessionPersisterProvider method createUserSession.

@Override
public void createUserSession(UserSessionModel userSession, boolean offline) {
    PersistentUserSessionAdapter adapter = new PersistentUserSessionAdapter(userSession);
    PersistentUserSessionModel model = adapter.getUpdatedModel();
    PersistentUserSessionEntity entity = new PersistentUserSessionEntity();
    entity.setUserSessionId(model.getUserSessionId());
    entity.setCreatedOn(model.getStarted());
    entity.setRealmId(adapter.getRealm().getId());
    entity.setUserId(adapter.getUser().getId());
    String offlineStr = offlineToString(offline);
    entity.setOffline(offlineStr);
    entity.setLastSessionRefresh(model.getLastSessionRefresh());
    entity.setData(model.getData());
    em.persist(entity);
    em.flush();
}
Also used : PersistentUserSessionAdapter(org.keycloak.models.session.PersistentUserSessionAdapter) PersistentUserSessionModel(org.keycloak.models.session.PersistentUserSessionModel)

Example 3 with PersistentUserSessionAdapter

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

the class JpaUserSessionPersisterProvider method toAdapter.

private PersistentUserSessionAdapter toAdapter(RealmModel realm, PersistentUserSessionEntity entity) {
    PersistentUserSessionModel model = new PersistentUserSessionModel();
    model.setUserSessionId(entity.getUserSessionId());
    model.setStarted(entity.getCreatedOn());
    model.setLastSessionRefresh(entity.getLastSessionRefresh());
    model.setData(entity.getData());
    model.setOffline(offlineFromString(entity.getOffline()));
    Map<String, AuthenticatedClientSessionModel> clientSessions = new HashMap<>();
    return new PersistentUserSessionAdapter(session, model, realm, entity.getUserId(), clientSessions);
}
Also used : PersistentUserSessionAdapter(org.keycloak.models.session.PersistentUserSessionAdapter) HashMap(java.util.HashMap) PersistentUserSessionModel(org.keycloak.models.session.PersistentUserSessionModel) AuthenticatedClientSessionModel(org.keycloak.models.AuthenticatedClientSessionModel)

Aggregations

PersistentUserSessionAdapter (org.keycloak.models.session.PersistentUserSessionAdapter)3 PersistentUserSessionModel (org.keycloak.models.session.PersistentUserSessionModel)3 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Objects (java.util.Objects)1 AuthenticatedClientSessionModel (org.keycloak.models.AuthenticatedClientSessionModel)1 UserSessionModel (org.keycloak.models.UserSessionModel)1