Search in sources :

Example 1 with RoleEntity

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

the class JpaRealmProvider method removeRole.

@Override
public boolean removeRole(RoleModel role) {
    RealmModel realm;
    if (role.getContainer() instanceof RealmModel) {
        realm = (RealmModel) role.getContainer();
    } else if (role.getContainer() instanceof ClientModel) {
        realm = ((ClientModel) role.getContainer()).getRealm();
    } else {
        throw new IllegalStateException("RoleModel's container isn not instance of either RealmModel or ClientModel");
    }
    session.users().preRemove(realm, role);
    RoleEntity roleEntity = em.getReference(RoleEntity.class, role.getId());
    if (roleEntity == null || !roleEntity.getRealmId().equals(realm.getId())) {
        // Throw model exception to ensure transaction rollback and revert previous operations (removing default roles) as well
        throw new ModelException("Role not found or trying to remove role from incorrect realm");
    }
    String compositeRoleTable = JpaUtils.getTableNameForNativeQuery("COMPOSITE_ROLE", em);
    em.createNativeQuery("delete from " + compositeRoleTable + " where CHILD_ROLE = :role").setParameter("role", roleEntity).executeUpdate();
    em.createNamedQuery("deleteClientScopeRoleMappingByRole").setParameter("role", roleEntity).executeUpdate();
    em.flush();
    em.remove(roleEntity);
    session.getKeycloakSessionFactory().publish(roleRemovedEvent(role));
    em.flush();
    return true;
}
Also used : RealmModel(org.keycloak.models.RealmModel) RoleEntity(org.keycloak.models.jpa.entities.RoleEntity) ClientModel(org.keycloak.models.ClientModel) ModelException(org.keycloak.models.ModelException)

Example 2 with RoleEntity

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

the class JpaRealmProvider method getRoleById.

@Override
public RoleModel getRoleById(RealmModel realm, String id) {
    RoleEntity entity = em.find(RoleEntity.class, id);
    if (entity == null)
        return null;
    if (!realm.getId().equals(entity.getRealmId()))
        return null;
    RoleAdapter adapter = new RoleAdapter(session, realm, em, entity);
    return adapter;
}
Also used : RoleEntity(org.keycloak.models.jpa.entities.RoleEntity)

Example 3 with RoleEntity

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

the class JpaRealmProvider method addRealmRole.

@Override
public RoleModel addRealmRole(RealmModel realm, String id, String name) {
    if (getRealmRole(realm, name) != null) {
        throw new ModelDuplicateException();
    }
    RoleEntity entity = new RoleEntity();
    entity.setId(id);
    entity.setName(name);
    entity.setRealmId(realm.getId());
    em.persist(entity);
    em.flush();
    RoleAdapter adapter = new RoleAdapter(session, realm, em, entity);
    return adapter;
}
Also used : RoleEntity(org.keycloak.models.jpa.entities.RoleEntity) ModelDuplicateException(org.keycloak.models.ModelDuplicateException)

Example 4 with RoleEntity

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

the class JpaRealmProvider method addClientRole.

@Override
public RoleModel addClientRole(ClientModel client, String id, String name) {
    if (getClientRole(client, name) != null) {
        throw new ModelDuplicateException();
    }
    RoleEntity roleEntity = new RoleEntity();
    roleEntity.setId(id);
    roleEntity.setName(name);
    roleEntity.setRealmId(client.getRealm().getId());
    roleEntity.setClientId(client.getId());
    roleEntity.setClientRole(true);
    em.persist(roleEntity);
    RoleAdapter adapter = new RoleAdapter(session, client.getRealm(), em, roleEntity);
    return adapter;
}
Also used : RoleEntity(org.keycloak.models.jpa.entities.RoleEntity) ModelDuplicateException(org.keycloak.models.ModelDuplicateException)

Example 5 with RoleEntity

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

the class RoleAdapter method addCompositeRole.

@Override
public void addCompositeRole(RoleModel role) {
    RoleEntity entity = toRoleEntity(role);
    for (RoleEntity composite : getEntity().getCompositeRoles()) {
        if (composite.equals(entity))
            return;
    }
    getEntity().getCompositeRoles().add(entity);
}
Also used : RoleEntity(org.keycloak.models.jpa.entities.RoleEntity)

Aggregations

RoleEntity (org.keycloak.models.jpa.entities.RoleEntity)6 ModelDuplicateException (org.keycloak.models.ModelDuplicateException)2 ClientModel (org.keycloak.models.ClientModel)1 ModelException (org.keycloak.models.ModelException)1 RealmModel (org.keycloak.models.RealmModel)1