Search in sources :

Example 21 with UiEntityContext

use of io.jmix.ui.accesscontext.UiEntityContext in project jmix by jmix-framework.

the class AbstractTable method setupColumnSettings.

protected void setupColumnSettings(EntityTableItems<E> entityTableSource) {
    MetaClass metaClass = entityTableSource.getEntityMetaClass();
    List<MetaPropertyPath> editableColumns = Collections.emptyList();
    for (Map.Entry<Object, Column<E>> entry : this.columns.entrySet()) {
        Object columnId = entry.getKey();
        Column<E> column = entry.getValue();
        String caption;
        if (column != null) {
            caption = getColumnCaption(columnId, column);
        } else {
            caption = StringUtils.capitalize(getColumnCaption(columnId));
        }
        setColumnHeader(columnId, caption);
        if (column != null) {
            if (column.isEditable() && (columnId instanceof MetaPropertyPath)) {
                MetaPropertyPath propertyPath = ((MetaPropertyPath) columnId);
                UiEntityAttributeContext attributeContext = new UiEntityAttributeContext(metaClass, propertyPath.toString());
                accessManager.applyRegisteredConstraints(attributeContext);
                if (attributeContext.canModify() && attributeContext.canView()) {
                    if (editableColumns.isEmpty()) {
                        editableColumns = new ArrayList<>();
                    }
                    editableColumns.add(propertyPath);
                } else {
                    log.info("Editable column '{}' is not permitted to read or update", propertyPath.toString());
                }
            }
            if (column.isCollapsed() && component.isColumnCollapsingAllowed()) {
                UiEntityAttributeContext attributeContext = new UiEntityAttributeContext(metaClass, columnId.toString());
                accessManager.applyRegisteredConstraints(attributeContext);
                if (!(columnId instanceof MetaPropertyPath) || attributeContext.canView()) {
                    component.setColumnCollapsed(column.getId(), true);
                }
            }
            if (column.getAggregation() != null) {
                checkAggregation(column.getAggregation());
                component.addContainerPropertyAggregation(column.getId(), WrapperUtils.convertAggregationType(column.getAggregation().getType()));
            }
        }
    }
    if (isEditable() && !editableColumns.isEmpty()) {
        UiEntityContext entityContext = new UiEntityContext(metaClass);
        accessManager.applyRegisteredConstraints(entityContext);
        if (entityContext.isViewPermitted() && entityContext.isEditPermitted()) {
            setEditableColumns(editableColumns);
        } else {
            log.info("Entity '{}' is not permitted to read or update", metaClass.getName());
        }
    }
}
Also used : UiEntityContext(io.jmix.ui.accesscontext.UiEntityContext) UiEntityAttributeContext(io.jmix.ui.accesscontext.UiEntityAttributeContext) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) KeyValueMetaClass(io.jmix.core.impl.keyvalue.KeyValueMetaClass) MetaClass(io.jmix.core.metamodel.model.MetaClass)

Example 22 with UiEntityContext

use of io.jmix.ui.accesscontext.UiEntityContext in project jmix by jmix-framework.

the class DataComponents method createCollectionContainer.

/**
 * Creates {@code CollectionPropertyContainer}.
 */
@SuppressWarnings("unchecked")
public <E> CollectionPropertyContainer<E> createCollectionContainer(Class<E> entityClass, InstanceContainer<?> masterContainer, String property) {
    CollectionPropertyContainerImpl<E> container = new CollectionPropertyContainerImpl<>(metadata.getClass(entityClass), masterContainer, property);
    autowire(container);
    container.setSorter(sorterFactory.createCollectionPropertyContainerSorter(container));
    UiEntityContext entityContext = new UiEntityContext(masterContainer.getEntityMetaClass());
    accessManager.applyRegisteredConstraints(entityContext);
    UiEntityAttributeContext attributeContext = new UiEntityAttributeContext(masterContainer.getEntityMetaClass(), property);
    accessManager.applyRegisteredConstraints(attributeContext);
    if (attributeContext.canView() && entityContext.isViewPermitted()) {
        masterContainer.addItemChangeListener(e -> {
            Object item = masterContainer.getItemOrNull();
            container.setItems(item != null ? EntityValues.getValue(item, property) : null);
        });
        masterContainer.addItemPropertyChangeListener(e -> {
            if (e.getProperty().equals(property)) {
                container.setDisconnectedItems((Collection<E>) e.getValue());
            }
        });
    }
    return container;
}
Also used : UiEntityContext(io.jmix.ui.accesscontext.UiEntityContext) UiEntityAttributeContext(io.jmix.ui.accesscontext.UiEntityAttributeContext)

Example 23 with UiEntityContext

use of io.jmix.ui.accesscontext.UiEntityContext in project jmix by jmix-framework.

the class DataComponents method createInstanceContainer.

/**
 * Creates {@code InstancePropertyContainer}.
 */
@SuppressWarnings("unchecked")
public <E> InstancePropertyContainer<E> createInstanceContainer(Class<E> entityClass, InstanceContainer<?> masterContainer, String property) {
    InstancePropertyContainerImpl<E> container = new InstancePropertyContainerImpl<>(metadata.getClass(entityClass), masterContainer, property);
    autowire(container);
    UiEntityContext entityContext = new UiEntityContext(masterContainer.getEntityMetaClass());
    accessManager.applyRegisteredConstraints(entityContext);
    UiEntityAttributeContext attributeContext = new UiEntityAttributeContext(masterContainer.getEntityMetaClass(), property);
    accessManager.applyRegisteredConstraints(attributeContext);
    if (entityContext.isViewPermitted() && attributeContext.canView()) {
        masterContainer.addItemChangeListener(e -> {
            Object item = masterContainer.getItemOrNull();
            container.setItem(item != null ? EntityValues.getValue(item, property) : null);
        });
        masterContainer.addItemPropertyChangeListener(e -> {
            if (e.getProperty().equals(property)) {
                container.setItem((E) e.getValue());
            }
        });
    }
    return container;
}
Also used : UiEntityContext(io.jmix.ui.accesscontext.UiEntityContext) UiEntityAttributeContext(io.jmix.ui.accesscontext.UiEntityAttributeContext)

Example 24 with UiEntityContext

use of io.jmix.ui.accesscontext.UiEntityContext in project jmix by jmix-framework.

the class ScreenNavigationHandler method createEditorScreenOptions.

@Nullable
protected Map<String, Object> createEditorScreenOptions(WindowInfo windowInfo, NavigationState requestedState, AppUI ui) {
    UrlChangeHandler urlChangeHandler = ui.getUrlChangeHandler();
    String idParam = MapUtils.isNotEmpty(requestedState.getParams()) ? // If no id was passed, open editor for creation
    requestedState.getParams().getOrDefault("id", NEW_ENTITY_ID) : NEW_ENTITY_ID;
    Class<?> entityClass = EditorTypeExtractor.extractEntityClass(windowInfo);
    if (entityClass == null) {
        return null;
    }
    MetaClass metaClass = metadata.getClass(entityClass);
    UiEntityContext entityContext = new UiEntityContext(metaClass);
    accessManager.applyRegisteredConstraints(entityContext);
    if (!entityContext.isViewPermitted()) {
        urlChangeHandler.revertNavigationState();
        throw new AccessDeniedException("entity", entityClass.getSimpleName(), "read");
    }
    if (NEW_ENTITY_ID.equals(idParam)) {
        if (!entityContext.isCreatePermitted()) {
            throw new AccessDeniedException("entity", entityClass.getSimpleName(), "create");
        }
        return ParamsMap.of("item", metadata.create(entityClass));
    }
    MetaProperty primaryKeyProperty = metadataTools.getPrimaryKeyProperty(metaClass);
    if (primaryKeyProperty == null) {
        throw new IllegalStateException(String.format("Entity %s has no primary key", metaClass.getName()));
    }
    Class<?> idType = primaryKeyProperty.getJavaType();
    Object id = UrlIdSerializer.deserializeId(idType, idParam);
    LoadContext<?> ctx = new LoadContext(metaClass);
    ctx.setId(id);
    ctx.setFetchPlan(fetchPlanRepository.getFetchPlan(metaClass, FetchPlan.INSTANCE_NAME));
    Object entity = dataManager.load(ctx);
    if (entity == null) {
        urlChangeHandler.revertNavigationState();
        throw new EntityAccessException(metaClass, id);
    }
    return ParamsMap.of("item", entity);
}
Also used : UiEntityContext(io.jmix.ui.accesscontext.UiEntityContext) AccessDeniedException(io.jmix.core.security.AccessDeniedException) MetaClass(io.jmix.core.metamodel.model.MetaClass) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) Nullable(javax.annotation.Nullable)

Aggregations

UiEntityContext (io.jmix.ui.accesscontext.UiEntityContext)24 MetaClass (io.jmix.core.metamodel.model.MetaClass)20 UiEntityAttributeContext (io.jmix.ui.accesscontext.UiEntityAttributeContext)9 EntityDataUnit (io.jmix.ui.component.data.meta.EntityDataUnit)9 InMemoryCrudEntityContext (io.jmix.core.accesscontext.InMemoryCrudEntityContext)3 MetaProperty (io.jmix.core.metamodel.model.MetaProperty)3 Range (io.jmix.core.metamodel.model.Range)2 ValueSource (io.jmix.ui.component.data.ValueSource)2 ContainerValueSource (io.jmix.ui.component.data.value.ContainerValueSource)2 AccessManager (io.jmix.core.AccessManager)1 EntityStates (io.jmix.core.EntityStates)1 MessageTools (io.jmix.core.MessageTools)1 Metadata (io.jmix.core.Metadata)1 ParamsMap (io.jmix.core.common.util.ParamsMap)1 EntityValues (io.jmix.core.entity.EntityValues)1 KeyValueMetaClass (io.jmix.core.impl.keyvalue.KeyValueMetaClass)1 ASSOCIATION (io.jmix.core.metamodel.model.MetaProperty.Type.ASSOCIATION)1 MetaPropertyPath (io.jmix.core.metamodel.model.MetaPropertyPath)1 AccessDeniedException (io.jmix.core.security.AccessDeniedException)1 PersistenceHints (io.jmix.data.PersistenceHints)1