Search in sources :

Example 1 with FederatedUserConsentEntity

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

the class JpaUserFederatedStorageProvider method updateConsent.

@Override
public void updateConsent(RealmModel realm, String userId, UserConsentModel consent) {
    createIndex(realm, userId);
    String clientId = consent.getClient().getId();
    FederatedUserConsentEntity consentEntity = getGrantedConsentEntity(userId, clientId, LockModeType.PESSIMISTIC_WRITE);
    if (consentEntity == null) {
        throw new ModelException("Consent not found for client [" + clientId + "] and user [" + userId + "]");
    }
    updateGrantedConsentEntity(consentEntity, consent);
}
Also used : ModelException(org.keycloak.models.ModelException) FederatedUserConsentEntity(org.keycloak.storage.jpa.entity.FederatedUserConsentEntity)

Example 2 with FederatedUserConsentEntity

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

the class JpaUserFederatedStorageProvider method revokeConsentForClient.

@Override
public boolean revokeConsentForClient(RealmModel realm, String userId, String clientInternalId) {
    FederatedUserConsentEntity consentEntity = getGrantedConsentEntity(userId, clientInternalId, LockModeType.PESSIMISTIC_WRITE);
    if (consentEntity == null)
        return false;
    em.remove(consentEntity);
    em.flush();
    return true;
}
Also used : FederatedUserConsentEntity(org.keycloak.storage.jpa.entity.FederatedUserConsentEntity)

Example 3 with FederatedUserConsentEntity

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

the class JpaUserFederatedStorageProvider method getGrantedConsentEntity.

private FederatedUserConsentEntity getGrantedConsentEntity(String userId, String clientId, LockModeType lockMode) {
    StorageId clientStorageId = new StorageId(clientId);
    String queryName = clientStorageId.isLocal() ? "userFederatedConsentByUserAndClient" : "userFederatedConsentByUserAndExternalClient";
    TypedQuery<FederatedUserConsentEntity> query = em.createNamedQuery(queryName, FederatedUserConsentEntity.class);
    query.setLockMode(lockMode);
    query.setParameter("userId", userId);
    if (clientStorageId.isLocal()) {
        query.setParameter("clientId", clientId);
    } else {
        query.setParameter("clientStorageProvider", clientStorageId.getProviderId());
        query.setParameter("externalClientId", clientStorageId.getExternalId());
    }
    List<FederatedUserConsentEntity> results = query.getResultList();
    if (results.size() > 1) {
        throw new ModelException("More results found for user [" + userId + "] and client [" + clientId + "]");
    } else if (results.size() == 1) {
        return results.get(0);
    } else {
        return null;
    }
}
Also used : ModelException(org.keycloak.models.ModelException) StorageId(org.keycloak.storage.StorageId) FederatedUserConsentEntity(org.keycloak.storage.jpa.entity.FederatedUserConsentEntity)

Example 4 with FederatedUserConsentEntity

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

the class JpaUserFederatedStorageProvider method addConsent.

@Override
public void addConsent(RealmModel realm, String userId, UserConsentModel consent) {
    createIndex(realm, userId);
    String clientId = consent.getClient().getId();
    FederatedUserConsentEntity consentEntity = getGrantedConsentEntity(userId, clientId, LockModeType.NONE);
    if (consentEntity != null) {
        throw new ModelDuplicateException("Consent already exists for client [" + clientId + "] and user [" + userId + "]");
    }
    consentEntity = new FederatedUserConsentEntity();
    consentEntity.setId(KeycloakModelUtils.generateId());
    consentEntity.setUserId(userId);
    StorageId clientStorageId = new StorageId(clientId);
    if (clientStorageId.isLocal()) {
        consentEntity.setClientId(clientId);
    } else {
        consentEntity.setClientStorageProvider(clientStorageId.getProviderId());
        consentEntity.setExternalClientId(clientStorageId.getExternalId());
    }
    consentEntity.setRealmId(realm.getId());
    consentEntity.setStorageProviderId(new StorageId(userId).getProviderId());
    long currentTime = Time.currentTimeMillis();
    consentEntity.setCreatedDate(currentTime);
    consentEntity.setLastUpdatedDate(currentTime);
    em.persist(consentEntity);
    em.flush();
    updateGrantedConsentEntity(consentEntity, consent);
}
Also used : ModelDuplicateException(org.keycloak.models.ModelDuplicateException) StorageId(org.keycloak.storage.StorageId) FederatedUserConsentEntity(org.keycloak.storage.jpa.entity.FederatedUserConsentEntity)

Aggregations

FederatedUserConsentEntity (org.keycloak.storage.jpa.entity.FederatedUserConsentEntity)4 ModelException (org.keycloak.models.ModelException)2 StorageId (org.keycloak.storage.StorageId)2 ModelDuplicateException (org.keycloak.models.ModelDuplicateException)1