Search in sources :

Example 1 with UserConsentClientScopeEntity

use of org.keycloak.models.jpa.entities.UserConsentClientScopeEntity in project keycloak by keycloak.

the class JpaUserProvider method toConsentModel.

private UserConsentModel toConsentModel(RealmModel realm, UserConsentEntity 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());
    if (client == null) {
        throw new ModelException("Client with id " + clientStorageId.getId() + " is not available");
    }
    UserConsentModel model = new UserConsentModel(client);
    model.setCreatedDate(entity.getCreatedDate());
    model.setLastUpdatedDate(entity.getLastUpdatedDate());
    Collection<UserConsentClientScopeEntity> grantedClientScopeEntities = entity.getGrantedClientScopes();
    if (grantedClientScopeEntities != null) {
        for (UserConsentClientScopeEntity grantedClientScope : grantedClientScopeEntities) {
            ClientScopeModel grantedClientScopeModel = KeycloakModelUtils.findClientScopeById(realm, client, grantedClientScope.getScopeId());
            if (grantedClientScopeModel != null) {
                model.addGrantedClientScope(grantedClientScopeModel);
            }
        }
    }
    return model;
}
Also used : ClientModel(org.keycloak.models.ClientModel) ModelException(org.keycloak.models.ModelException) ClientScopeModel(org.keycloak.models.ClientScopeModel) StorageId(org.keycloak.storage.StorageId) UserConsentClientScopeEntity(org.keycloak.models.jpa.entities.UserConsentClientScopeEntity) UserConsentModel(org.keycloak.models.UserConsentModel)

Example 2 with UserConsentClientScopeEntity

use of org.keycloak.models.jpa.entities.UserConsentClientScopeEntity in project keycloak by keycloak.

the class JpaUserProvider method updateGrantedConsentEntity.

// Update roles and protocolMappers to given consentEntity from the consentModel
private void updateGrantedConsentEntity(UserConsentEntity consentEntity, UserConsentModel consentModel) {
    Collection<UserConsentClientScopeEntity> grantedClientScopeEntities = consentEntity.getGrantedClientScopes();
    Collection<UserConsentClientScopeEntity> scopesToRemove = new HashSet<>(grantedClientScopeEntities);
    for (ClientScopeModel clientScope : consentModel.getGrantedClientScopes()) {
        UserConsentClientScopeEntity grantedClientScopeEntity = new UserConsentClientScopeEntity();
        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 client scopes were no longer on consentModel and will be removed
    for (UserConsentClientScopeEntity toRemove : scopesToRemove) {
        grantedClientScopeEntities.remove(toRemove);
        em.remove(toRemove);
    }
    consentEntity.setLastUpdatedDate(Time.currentTimeMillis());
    em.flush();
}
Also used : ClientScopeModel(org.keycloak.models.ClientScopeModel) UserConsentClientScopeEntity(org.keycloak.models.jpa.entities.UserConsentClientScopeEntity) HashSet(java.util.HashSet)

Aggregations

ClientScopeModel (org.keycloak.models.ClientScopeModel)2 UserConsentClientScopeEntity (org.keycloak.models.jpa.entities.UserConsentClientScopeEntity)2 HashSet (java.util.HashSet)1 ClientModel (org.keycloak.models.ClientModel)1 ModelException (org.keycloak.models.ModelException)1 UserConsentModel (org.keycloak.models.UserConsentModel)1 StorageId (org.keycloak.storage.StorageId)1