Search in sources :

Example 1 with GroupEntity

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

the class JpaRealmProvider method createGroup.

@Override
public GroupModel createGroup(RealmModel realm, String id, String name, GroupModel toParent) {
    if (id == null) {
        id = KeycloakModelUtils.generateId();
    } else if (GroupEntity.TOP_PARENT_ID.equals(id)) {
        // maybe it's impossible but better ensure this doesn't happen
        throw new ModelException("The ID of the new group is equals to the tag used for top level groups");
    }
    GroupEntity groupEntity = new GroupEntity();
    groupEntity.setId(id);
    groupEntity.setName(name);
    groupEntity.setRealm(realm.getId());
    groupEntity.setParentId(toParent == null ? GroupEntity.TOP_PARENT_ID : toParent.getId());
    em.persist(groupEntity);
    em.flush();
    return new GroupAdapter(realm, em, groupEntity);
}
Also used : ModelException(org.keycloak.models.ModelException) GroupEntity(org.keycloak.models.jpa.entities.GroupEntity)

Example 2 with GroupEntity

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

the class JpaRealmProvider method getGroupById.

@Override
public GroupModel getGroupById(RealmModel realm, String id) {
    GroupEntity groupEntity = em.find(GroupEntity.class, id);
    if (groupEntity == null)
        return null;
    if (!groupEntity.getRealm().equals(realm.getId()))
        return null;
    GroupAdapter adapter = new GroupAdapter(realm, em, groupEntity);
    return adapter;
}
Also used : GroupEntity(org.keycloak.models.jpa.entities.GroupEntity)

Example 3 with GroupEntity

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

the class JpaRealmProvider method removeGroup.

@Override
public boolean removeGroup(RealmModel realm, GroupModel group) {
    if (group == null) {
        return false;
    }
    GroupModel.GroupRemovedEvent event = new GroupModel.GroupRemovedEvent() {

        @Override
        public RealmModel getRealm() {
            return realm;
        }

        @Override
        public GroupModel getGroup() {
            return group;
        }

        @Override
        public KeycloakSession getKeycloakSession() {
            return session;
        }
    };
    session.getKeycloakSessionFactory().publish(event);
    session.users().preRemove(realm, group);
    realm.removeDefaultGroup(group);
    group.getSubGroupsStream().forEach(realm::removeGroup);
    GroupEntity groupEntity = em.find(GroupEntity.class, group.getId(), LockModeType.PESSIMISTIC_WRITE);
    if ((groupEntity == null) || (!groupEntity.getRealm().equals(realm.getId()))) {
        return false;
    }
    em.createNamedQuery("deleteGroupRoleMappingsByGroup").setParameter("group", groupEntity).executeUpdate();
    em.remove(groupEntity);
    return true;
}
Also used : GroupEntity(org.keycloak.models.jpa.entities.GroupEntity) GroupModel(org.keycloak.models.GroupModel)

Aggregations

GroupEntity (org.keycloak.models.jpa.entities.GroupEntity)3 GroupModel (org.keycloak.models.GroupModel)1 ModelException (org.keycloak.models.ModelException)1