Search in sources :

Example 6 with CredentialEntity

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

the class JpaUserCredentialStore method getStoredCredentialById.

@Override
public CredentialModel getStoredCredentialById(RealmModel realm, UserModel user, String id) {
    CredentialEntity entity = em.find(CredentialEntity.class, id);
    if (!checkCredentialEntity(entity, user))
        return null;
    CredentialModel model = toModel(entity);
    return model;
}
Also used : CredentialEntity(org.keycloak.models.jpa.entities.CredentialEntity) CredentialModel(org.keycloak.credential.CredentialModel)

Example 7 with CredentialEntity

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

the class JpaUserCredentialStore method removeCredentialEntity.

CredentialEntity removeCredentialEntity(RealmModel realm, UserModel user, String id) {
    CredentialEntity entity = em.find(CredentialEntity.class, id, LockModeType.PESSIMISTIC_WRITE);
    if (!checkCredentialEntity(entity, user))
        return null;
    int currentPriority = entity.getPriority();
    this.getStoredCredentialEntities(realm, user).forEach(cred -> {
        if (cred.getPriority() > currentPriority) {
            cred.setPriority(cred.getPriority() - PRIORITY_DIFFERENCE);
        }
    });
    em.remove(entity);
    em.flush();
    return entity;
}
Also used : CredentialEntity(org.keycloak.models.jpa.entities.CredentialEntity)

Example 8 with CredentialEntity

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

the class JpaUserCredentialStore method createCredentialEntity.

CredentialEntity createCredentialEntity(RealmModel realm, UserModel user, CredentialModel cred) {
    CredentialEntity entity = new CredentialEntity();
    String id = cred.getId() == null ? KeycloakModelUtils.generateId() : cred.getId();
    entity.setId(id);
    entity.setCreatedDate(cred.getCreatedDate());
    entity.setUserLabel(cred.getUserLabel());
    entity.setType(cred.getType());
    entity.setSecretData(cred.getSecretData());
    entity.setCredentialData(cred.getCredentialData());
    UserEntity userRef = em.getReference(UserEntity.class, user.getId());
    entity.setUser(userRef);
    // add in linkedlist to last position
    List<CredentialEntity> credentials = getStoredCredentialEntities(realm, user).collect(Collectors.toList());
    int priority = credentials.isEmpty() ? PRIORITY_DIFFERENCE : credentials.get(credentials.size() - 1).getPriority() + PRIORITY_DIFFERENCE;
    entity.setPriority(priority);
    em.persist(entity);
    return entity;
}
Also used : CredentialEntity(org.keycloak.models.jpa.entities.CredentialEntity) UserEntity(org.keycloak.models.jpa.entities.UserEntity)

Aggregations

CredentialEntity (org.keycloak.models.jpa.entities.CredentialEntity)8 UserEntity (org.keycloak.models.jpa.entities.UserEntity)4 CredentialModel (org.keycloak.credential.CredentialModel)1