Search in sources :

Example 26 with StorageId

use of org.keycloak.storage.StorageId in project keycloak by keycloak.

the class RoleStorageTest method testGetRoleById.

@Test
public void testGetRoleById() {
    String providerId = this.providerId;
    testingClient.server().run(session -> {
        RealmModel realm = session.realms().getRealmByName("test");
        StorageId storageId = new StorageId(providerId, "hardcoded-role");
        RoleModel hardcoded = realm.getRoleById(storageId.getId());
        assertNotNull(hardcoded);
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) RoleModel(org.keycloak.models.RoleModel) StorageId(org.keycloak.storage.StorageId) Test(org.junit.Test) AbstractTestRealmKeycloakTest(org.keycloak.testsuite.AbstractTestRealmKeycloakTest)

Example 27 with StorageId

use of org.keycloak.storage.StorageId in project keycloak by keycloak.

the class OpenshiftClientStorageProvider method getClientById.

@Override
public ClientModel getClientById(RealmModel realm, String id) {
    StorageId storageId = new StorageId(id);
    if (!storageId.getProviderId().equals(providerModel.getId()))
        return null;
    String clientId = storageId.getExternalId();
    return getClientByClientId(realm, clientId);
}
Also used : StorageId(org.keycloak.storage.StorageId)

Example 28 with StorageId

use of org.keycloak.storage.StorageId in project keycloak by keycloak.

the class JpaUserFederatedStorageProvider method createIndex.

/**
 * We create an entry so that its easy to iterate over all things in the database.  Specifically useful for export
 */
protected void createIndex(RealmModel realm, String userId) {
    if (em.find(FederatedUser.class, userId) == null) {
        FederatedUser fedUser = new FederatedUser();
        fedUser.setId(userId);
        fedUser.setRealmId(realm.getId());
        fedUser.setStorageProviderId(new StorageId(userId).getProviderId());
        em.persist(fedUser);
    }
}
Also used : FederatedUser(org.keycloak.storage.jpa.entity.FederatedUser) StorageId(org.keycloak.storage.StorageId)

Example 29 with StorageId

use of org.keycloak.storage.StorageId 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 30 with StorageId

use of org.keycloak.storage.StorageId 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

StorageId (org.keycloak.storage.StorageId)44 UserModel (org.keycloak.models.UserModel)7 RealmModel (org.keycloak.models.RealmModel)6 ComponentModel (org.keycloak.component.ComponentModel)5 ClientModel (org.keycloak.models.ClientModel)5 ClientScopeModel (org.keycloak.models.ClientScopeModel)5 List (java.util.List)3 Objects (java.util.Objects)3 Stream (java.util.stream.Stream)3 Test (org.junit.Test)3 KeycloakSession (org.keycloak.models.KeycloakSession)3 ModelException (org.keycloak.models.ModelException)3 UserConsentModel (org.keycloak.models.UserConsentModel)3 Collection (java.util.Collection)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Set (java.util.Set)2 Function (java.util.function.Function)2 Predicate (java.util.function.Predicate)2