Search in sources :

Example 31 with StorageId

use of org.keycloak.storage.StorageId in project keycloak by keycloak.

the class JpaUserFederatedStorageProvider method persistAttributeValue.

private void persistAttributeValue(RealmModel realm, String userId, String name, String value) {
    FederatedUserAttributeEntity attr = new FederatedUserAttributeEntity();
    attr.setId(KeycloakModelUtils.generateId());
    attr.setName(name);
    attr.setValue(value);
    attr.setUserId(userId);
    attr.setRealmId(realm.getId());
    attr.setStorageProviderId(new StorageId(userId).getProviderId());
    em.persist(attr);
}
Also used : FederatedUserAttributeEntity(org.keycloak.storage.jpa.entity.FederatedUserAttributeEntity) StorageId(org.keycloak.storage.StorageId)

Example 32 with StorageId

use of org.keycloak.storage.StorageId in project keycloak by keycloak.

the class JpaUserFederatedStorageProvider method joinGroup.

@Override
public void joinGroup(RealmModel realm, String userId, GroupModel group) {
    createIndex(realm, userId);
    FederatedUserGroupMembershipEntity entity = new FederatedUserGroupMembershipEntity();
    entity.setUserId(userId);
    entity.setStorageProviderId(new StorageId(userId).getProviderId());
    entity.setGroupId(group.getId());
    entity.setRealmId(realm.getId());
    em.persist(entity);
}
Also used : FederatedUserGroupMembershipEntity(org.keycloak.storage.jpa.entity.FederatedUserGroupMembershipEntity) StorageId(org.keycloak.storage.StorageId)

Example 33 with StorageId

use of org.keycloak.storage.StorageId in project keycloak by keycloak.

the class JpaUserFederatedStorageProvider method toConsentModel.

private UserConsentModel toConsentModel(RealmModel realm, FederatedUserConsentEntity entity) {
    if (entity == null) {
        return null;
    }
    StorageId clientStorageId = null;
    if (entity.getClientId() == null) {
        clientStorageId = new StorageId(entity.getClientStorageProvider(), entity.getExternalClientId());
    } else {
        clientStorageId = new StorageId(entity.getClientId());
    }
    ClientModel client = realm.getClientById(clientStorageId.getId());
    UserConsentModel model = new UserConsentModel(client);
    model.setCreatedDate(entity.getCreatedDate());
    model.setLastUpdatedDate(entity.getLastUpdatedDate());
    Collection<FederatedUserConsentClientScopeEntity> grantedClientScopeEntities = entity.getGrantedClientScopes();
    if (grantedClientScopeEntities != null) {
        for (FederatedUserConsentClientScopeEntity grantedClientScope : grantedClientScopeEntities) {
            ClientScopeModel grantedClientScopeModel = realm.getClientScopeById(grantedClientScope.getScopeId());
            if (grantedClientScopeModel == null) {
                grantedClientScopeModel = realm.getClientById(grantedClientScope.getScopeId());
            }
            if (grantedClientScopeModel != null) {
                model.addGrantedClientScope(grantedClientScopeModel);
            }
        }
    }
    return model;
}
Also used : ClientModel(org.keycloak.models.ClientModel) FederatedUserConsentClientScopeEntity(org.keycloak.storage.jpa.entity.FederatedUserConsentClientScopeEntity) ClientScopeModel(org.keycloak.models.ClientScopeModel) StorageId(org.keycloak.storage.StorageId) UserConsentModel(org.keycloak.models.UserConsentModel)

Example 34 with StorageId

use of org.keycloak.storage.StorageId in project keycloak by keycloak.

the class JpaUserFederatedStorageProvider method preRemove.

@Override
public void preRemove(RealmModel realm, ClientModel client) {
    StorageId clientStorageId = new StorageId(client.getId());
    if (clientStorageId.isLocal()) {
        em.createNamedQuery("deleteFederatedUserConsentClientScopesByClient").setParameter("clientId", client.getId()).executeUpdate();
        em.createNamedQuery("deleteFederatedUserConsentsByClient").setParameter("clientId", client.getId()).executeUpdate();
    } else {
        em.createNamedQuery("deleteFederatedUserConsentClientScopesByExternalClient").setParameter("clientStorageProvider", clientStorageId.getProviderId()).setParameter("externalClientId", clientStorageId.getExternalId()).executeUpdate();
        em.createNamedQuery("deleteFederatedUserConsentsByExternalClient").setParameter("clientStorageProvider", clientStorageId.getProviderId()).setParameter("externalClientId", clientStorageId.getExternalId()).executeUpdate();
    }
}
Also used : StorageId(org.keycloak.storage.StorageId)

Example 35 with StorageId

use of org.keycloak.storage.StorageId in project keycloak by keycloak.

the class JpaUserSessionPersisterProvider method getUserSessionsCount.

@Override
public int getUserSessionsCount(RealmModel realm, ClientModel clientModel, boolean offline) {
    String offlineStr = offlineToString(offline);
    Query query;
    StorageId clientStorageId = new StorageId(clientModel.getId());
    if (clientStorageId.isLocal()) {
        query = em.createNamedQuery("findClientSessionsCountByClient");
        query.setParameter("clientId", clientModel.getId());
    } else {
        query = em.createNamedQuery("findClientSessionsCountByExternalClient");
        query.setParameter("clientStorageProvider", clientStorageId.getProviderId());
        query.setParameter("externalClientId", clientStorageId.getExternalId());
    }
    // Note, that realm is unused here, since the clientModel id already determines the offline user-sessions bound to an owning realm.
    query.setParameter("offline", offlineStr);
    Number n = (Number) query.getSingleResult();
    return n.intValue();
}
Also used : TypedQuery(javax.persistence.TypedQuery) PaginationUtils.paginateQuery(org.keycloak.models.jpa.PaginationUtils.paginateQuery) Query(javax.persistence.Query) StorageId(org.keycloak.storage.StorageId)

Aggregations

StorageId (org.keycloak.storage.StorageId)44 UserModel (org.keycloak.models.UserModel)7 RealmModel (org.keycloak.models.RealmModel)6 ComponentModel (org.keycloak.component.ComponentModel)5 ClientModel (org.keycloak.models.ClientModel)5 ClientScopeModel (org.keycloak.models.ClientScopeModel)5 List (java.util.List)3 Objects (java.util.Objects)3 Stream (java.util.stream.Stream)3 Test (org.junit.Test)3 KeycloakSession (org.keycloak.models.KeycloakSession)3 ModelException (org.keycloak.models.ModelException)3 UserConsentModel (org.keycloak.models.UserConsentModel)3 Collection (java.util.Collection)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Set (java.util.Set)2 Function (java.util.function.Function)2 Predicate (java.util.function.Predicate)2