Search in sources :

Example 1 with FederatedIdentityEntity

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

the class JpaUserProvider method getFederatedIdentitiesStream.

@Override
public Stream<FederatedIdentityModel> getFederatedIdentitiesStream(RealmModel realm, UserModel user) {
    TypedQuery<FederatedIdentityEntity> query = em.createNamedQuery("findFederatedIdentityByUser", FederatedIdentityEntity.class);
    UserEntity userEntity = em.getReference(UserEntity.class, user.getId());
    query.setParameter("user", userEntity);
    return closing(query.getResultStream().map(entity -> new FederatedIdentityModel(entity.getIdentityProvider(), entity.getUserId(), entity.getUserName(), entity.getToken())).distinct());
}
Also used : FederatedIdentityModel(org.keycloak.models.FederatedIdentityModel) UserEntity(org.keycloak.models.jpa.entities.UserEntity) FederatedIdentityEntity(org.keycloak.models.jpa.entities.FederatedIdentityEntity)

Example 2 with FederatedIdentityEntity

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

the class JpaUserProvider method removeFederatedIdentity.

@Override
public boolean removeFederatedIdentity(RealmModel realm, UserModel user, String identityProvider) {
    FederatedIdentityEntity entity = findFederatedIdentity(user, identityProvider, LockModeType.PESSIMISTIC_WRITE);
    if (entity != null) {
        em.remove(entity);
        em.flush();
        return true;
    } else {
        return false;
    }
}
Also used : FederatedIdentityEntity(org.keycloak.models.jpa.entities.FederatedIdentityEntity)

Example 3 with FederatedIdentityEntity

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

the class JpaUserProvider method updateFederatedIdentity.

@Override
public void updateFederatedIdentity(RealmModel realm, UserModel federatedUser, FederatedIdentityModel federatedIdentityModel) {
    FederatedIdentityEntity federatedIdentity = findFederatedIdentity(federatedUser, federatedIdentityModel.getIdentityProvider(), LockModeType.PESSIMISTIC_WRITE);
    federatedIdentity.setToken(federatedIdentityModel.getToken());
    em.persist(federatedIdentity);
    em.flush();
}
Also used : FederatedIdentityEntity(org.keycloak.models.jpa.entities.FederatedIdentityEntity)

Example 4 with FederatedIdentityEntity

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

the class JpaUserProvider method findFederatedIdentity.

private FederatedIdentityEntity findFederatedIdentity(UserModel user, String identityProvider, LockModeType lockMode) {
    TypedQuery<FederatedIdentityEntity> query = em.createNamedQuery("findFederatedIdentityByUserAndProvider", FederatedIdentityEntity.class);
    UserEntity userEntity = em.getReference(UserEntity.class, user.getId());
    query.setParameter("user", userEntity);
    query.setParameter("identityProvider", identityProvider);
    query.setLockMode(lockMode);
    List<FederatedIdentityEntity> results = query.getResultList();
    return results.size() > 0 ? results.get(0) : null;
}
Also used : UserEntity(org.keycloak.models.jpa.entities.UserEntity) FederatedIdentityEntity(org.keycloak.models.jpa.entities.FederatedIdentityEntity)

Example 5 with FederatedIdentityEntity

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

the class JpaUserProvider method addFederatedIdentity.

@Override
public void addFederatedIdentity(RealmModel realm, UserModel user, FederatedIdentityModel identity) {
    FederatedIdentityEntity entity = new FederatedIdentityEntity();
    entity.setRealmId(realm.getId());
    entity.setIdentityProvider(identity.getIdentityProvider());
    entity.setUserId(identity.getUserId());
    entity.setUserName(identity.getUserName().toLowerCase());
    entity.setToken(identity.getToken());
    UserEntity userEntity = em.getReference(UserEntity.class, user.getId());
    entity.setUser(userEntity);
    em.persist(entity);
    em.flush();
}
Also used : UserEntity(org.keycloak.models.jpa.entities.UserEntity) FederatedIdentityEntity(org.keycloak.models.jpa.entities.FederatedIdentityEntity)

Aggregations

FederatedIdentityEntity (org.keycloak.models.jpa.entities.FederatedIdentityEntity)5 UserEntity (org.keycloak.models.jpa.entities.UserEntity)3 FederatedIdentityModel (org.keycloak.models.FederatedIdentityModel)1