public boolean revokeConsentForClient(RealmModel realm, String userId, String clientId) { UserConsentEntity consentEntity = getGrantedConsentEntity(userId, clientId); if (consentEntity == null) return false; em.remove(consentEntity); em.flush(); return true; }
@Override public UserConsentModel getConsentByClient(RealmModel realm, String userId, String clientId) { UserConsentEntity entity = getGrantedConsentEntity(userId, clientId); return toConsentModel(realm, entity); }
@Override public void updateConsent(RealmModel realm, String userId, UserConsentModel consent) { String clientId = consent.getClient().getId(); UserConsentEntity consentEntity = getGrantedConsentEntity(userId, clientId); if (consentEntity == null) { throw new ModelException("Consent not found for client [" + clientId + "] and user [" + userId + "]"); } updateGrantedConsentEntity(consentEntity, consent); }
@Override public void addConsent(RealmModel realm, String userId, UserConsentModel consent) { String clientId = consent.getClient().getId(); UserConsentEntity consentEntity = getGrantedConsentEntity(userId, clientId); 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); }