Search in sources :

Example 1 with FederatedUserConsentClientScopeEntity

use of org.keycloak.storage.jpa.entity.FederatedUserConsentClientScopeEntity in project keycloak by keycloak.

the class JpaUserFederatedStorageProvider method updateGrantedConsentEntity.

// Update roles and protocolMappers to given consentEntity from the consentModel
private void updateGrantedConsentEntity(FederatedUserConsentEntity consentEntity, UserConsentModel consentModel) {
    Collection<FederatedUserConsentClientScopeEntity> grantedClientScopeEntities = consentEntity.getGrantedClientScopes();
    Collection<FederatedUserConsentClientScopeEntity> scopesToRemove = new HashSet<>(grantedClientScopeEntities);
    for (ClientScopeModel clientScope : consentModel.getGrantedClientScopes()) {
        FederatedUserConsentClientScopeEntity grantedClientScopeEntity = new FederatedUserConsentClientScopeEntity();
        grantedClientScopeEntity.setUserConsent(consentEntity);
        grantedClientScopeEntity.setScopeId(clientScope.getId());
        // Check if it's already there
        if (!grantedClientScopeEntities.contains(grantedClientScopeEntity)) {
            em.persist(grantedClientScopeEntity);
            em.flush();
            grantedClientScopeEntities.add(grantedClientScopeEntity);
        } else {
            scopesToRemove.remove(grantedClientScopeEntity);
        }
    }
    // Those mappers were no longer on consentModel and will be removed
    for (FederatedUserConsentClientScopeEntity toRemove : scopesToRemove) {
        grantedClientScopeEntities.remove(toRemove);
        em.remove(toRemove);
    }
    consentEntity.setLastUpdatedDate(Time.currentTimeMillis());
    em.flush();
}
Also used : FederatedUserConsentClientScopeEntity(org.keycloak.storage.jpa.entity.FederatedUserConsentClientScopeEntity) ClientScopeModel(org.keycloak.models.ClientScopeModel) HashSet(java.util.HashSet)

Example 2 with FederatedUserConsentClientScopeEntity

use of org.keycloak.storage.jpa.entity.FederatedUserConsentClientScopeEntity 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)

Aggregations

ClientScopeModel (org.keycloak.models.ClientScopeModel)2 FederatedUserConsentClientScopeEntity (org.keycloak.storage.jpa.entity.FederatedUserConsentClientScopeEntity)2 HashSet (java.util.HashSet)1 ClientModel (org.keycloak.models.ClientModel)1 UserConsentModel (org.keycloak.models.UserConsentModel)1 StorageId (org.keycloak.storage.StorageId)1