Search in sources :

Example 1 with UserConsentEntity

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

the class JpaUserProvider method revokeConsentForClient.

public boolean revokeConsentForClient(RealmModel realm, String userId, String clientId) {
    UserConsentEntity consentEntity = getGrantedConsentEntity(userId, clientId, LockModeType.PESSIMISTIC_WRITE);
    if (consentEntity == null)
        return false;
    em.remove(consentEntity);
    em.flush();
    return true;
}
Also used : UserConsentEntity(org.keycloak.models.jpa.entities.UserConsentEntity)

Example 2 with UserConsentEntity

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

the class JpaUserProvider method getGrantedConsentEntity.

private UserConsentEntity getGrantedConsentEntity(String userId, String clientId, LockModeType lockMode) {
    StorageId clientStorageId = new StorageId(clientId);
    String queryName = clientStorageId.isLocal() ? "userConsentByUserAndClient" : "userConsentByUserAndExternalClient";
    TypedQuery<UserConsentEntity> query = em.createNamedQuery(queryName, UserConsentEntity.class);
    query.setParameter("userId", userId);
    if (clientStorageId.isLocal()) {
        query.setParameter("clientId", clientId);
    } else {
        query.setParameter("clientStorageProvider", clientStorageId.getProviderId());
        query.setParameter("externalClientId", clientStorageId.getExternalId());
    }
    query.setLockMode(lockMode);
    List<UserConsentEntity> 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) UserConsentEntity(org.keycloak.models.jpa.entities.UserConsentEntity)

Example 3 with UserConsentEntity

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

the class JpaUserProvider method addConsent.

@Override
public void addConsent(RealmModel realm, String userId, UserConsentModel consent) {
    String clientId = consent.getClient().getId();
    UserConsentEntity consentEntity = getGrantedConsentEntity(userId, clientId, LockModeType.NONE);
    if (consentEntity != null) {
        throw new ModelDuplicateException("Consent already exists for client [" + clientId + "] and user [" + userId + "]");
    }
    long currentTime = Time.currentTimeMillis();
    consentEntity = new UserConsentEntity();
    consentEntity.setId(KeycloakModelUtils.generateId());
    consentEntity.setUser(em.getReference(UserEntity.class, userId));
    StorageId clientStorageId = new StorageId(clientId);
    if (clientStorageId.isLocal()) {
        consentEntity.setClientId(clientId);
    } else {
        consentEntity.setClientStorageProvider(clientStorageId.getProviderId());
        consentEntity.setExternalClientId(clientStorageId.getExternalId());
    }
    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) UserConsentEntity(org.keycloak.models.jpa.entities.UserConsentEntity) UserEntity(org.keycloak.models.jpa.entities.UserEntity)

Example 4 with UserConsentEntity

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

the class JpaUserProvider method updateConsent.

@Override
public void updateConsent(RealmModel realm, String userId, UserConsentModel consent) {
    String clientId = consent.getClient().getId();
    UserConsentEntity 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) UserConsentEntity(org.keycloak.models.jpa.entities.UserConsentEntity)

Aggregations

UserConsentEntity (org.keycloak.models.jpa.entities.UserConsentEntity)4 ModelException (org.keycloak.models.ModelException)2 StorageId (org.keycloak.storage.StorageId)2 ModelDuplicateException (org.keycloak.models.ModelDuplicateException)1 UserEntity (org.keycloak.models.jpa.entities.UserEntity)1