Search in sources :

Example 1 with UiEntityAttributeContext

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

the class ValueBinder method bind.

public <V> ValueBinding<V> bind(HasValue<V> component, ValueSource<V> valueSource) {
    if (valueSource instanceof ApplicationContextAware) {
        ((ApplicationContextAware) valueSource).setApplicationContext(applicationContext);
    }
    ValueBindingImpl<V> binding = new ValueBindingImpl<>(component, valueSource);
    if (component instanceof Component.Editable) {
        ((Component.Editable) component).setEditable(!valueSource.isReadOnly());
    }
    if (valueSource instanceof EntityValueSource) {
        EntityValueSource entityValueSource = (EntityValueSource) valueSource;
        MetaPropertyPath metaPropertyPath = entityValueSource.getMetaPropertyPath();
        if (component instanceof Field) {
            initRequired((Field) component, metaPropertyPath);
            initBeanValidator((Field<?>) component, metaPropertyPath);
        }
        if (entityValueSource.isDataModelSecurityEnabled()) {
            UiEntityAttributeContext attributeContext = new UiEntityAttributeContext(metaPropertyPath);
            accessManager.applyRegisteredConstraints(attributeContext);
            if (component instanceof Component.Editable) {
                MetaClass metaClass = entityValueSource.getEntityMetaClass();
                boolean permittedIfEmbedded = true;
                if (entityValueSource instanceof ContainerValueSource) {
                    InstanceContainer<?> container = ((ContainerValueSource<?, ?>) entityValueSource).getContainer();
                    if (container instanceof Nested && metaClass != null && metadataTools.isJpaEmbeddable(metaClass)) {
                        String embeddedProperty = ((Nested) container).getProperty();
                        MetaClass masterMetaClass = ((Nested) container).getMaster().getEntityMetaClass();
                        UiEntityAttributeContext embeddedAttributeContext = new UiEntityAttributeContext(masterMetaClass, embeddedProperty);
                        accessManager.applyRegisteredConstraints(embeddedAttributeContext);
                        permittedIfEmbedded = embeddedAttributeContext.canModify();
                    }
                    if (permittedIfEmbedded && metaPropertyPath.length() > 1) {
                        for (MetaProperty property : metaPropertyPath.getMetaProperties()) {
                            if (metadataTools.isEmbedded(property)) {
                                UiEntityAttributeContext childAttributeContext = new UiEntityAttributeContext(property.getDomain(), property.getName());
                                accessManager.applyRegisteredConstraints(childAttributeContext);
                                if (!childAttributeContext.canModify()) {
                                    permittedIfEmbedded = false;
                                    break;
                                }
                            }
                        }
                    }
                }
                if (!attributeContext.canModify() || !permittedIfEmbedded) {
                    ((Component.Editable) component).setEditable(false);
                }
            }
            if (!attributeContext.canView()) {
                component.setVisible(false);
            }
        }
    }
    binding.bind();
    return binding;
}
Also used : EntityValueSource(io.jmix.ui.component.data.meta.EntityValueSource) ApplicationContextAware(org.springframework.context.ApplicationContextAware) UiEntityAttributeContext(io.jmix.ui.accesscontext.UiEntityAttributeContext) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) Nested(io.jmix.ui.model.Nested) Field(io.jmix.ui.component.Field) MetaClass(io.jmix.core.metamodel.model.MetaClass) MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Example 2 with UiEntityAttributeContext

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

the class AbstractTable method enableEditableColumns.

protected void enableEditableColumns(EntityTableItems<E> entityTableSource, Collection<MetaPropertyPath> propertyIds) {
    MetaClass metaClass = entityTableSource.getEntityMetaClass();
    List<MetaPropertyPath> editableColumns = new ArrayList<>(propertyIds.size());
    for (MetaPropertyPath propertyId : propertyIds) {
        UiEntityAttributeContext attributeContext = new UiEntityAttributeContext(metaClass, propertyId.toString());
        accessManager.applyRegisteredConstraints(attributeContext);
        if (!attributeContext.canModify()) {
            continue;
        }
        Column column = getColumn(propertyId.toString());
        if (column != null && BooleanUtils.isTrue(column.isEditable())) {
            com.vaadin.v7.ui.Table.ColumnGenerator generator = component.getColumnGenerator(column.getId());
            if (generator != null) {
                if (generator instanceof SystemTableColumnGenerator) {
                    // remove default generator
                    component.removeGeneratedColumn(propertyId);
                } else {
                    // do not edit generated columns
                    continue;
                }
            }
            editableColumns.add(propertyId);
        }
    }
    setEditableColumns(editableColumns);
}
Also used : JmixEnhancedTable(io.jmix.ui.widget.JmixEnhancedTable) KeyValueMetaClass(io.jmix.core.impl.keyvalue.KeyValueMetaClass) MetaClass(io.jmix.core.metamodel.model.MetaClass) UiEntityAttributeContext(io.jmix.ui.accesscontext.UiEntityAttributeContext) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath)

Example 3 with UiEntityAttributeContext

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

the class InspectorFormBuilder method addField.

/**
 * Adds field to the specified form.
 * If the field should be custom, adds it to the specified customFields collection
 * which can be used later to create fieldGenerators
 *
 * @param metaProperty meta property of the item's property which field is creating
 * @param form         field group to which created field will be added
 */
protected void addField(InstanceContainer container, Form form, MetaProperty metaProperty, boolean isReadonly) {
    MetaClass metaClass = container.getEntityMetaClass();
    Range range = metaProperty.getRange();
    boolean isRequired = isRequired(metaProperty);
    UiEntityAttributeContext attributeContext = new UiEntityAttributeContext(metaClass, metaProperty.getName());
    accessManager.applyRegisteredConstraints(attributeContext);
    if (!attributeContext.canView())
        return;
    if (range.isClass()) {
        UiEntityContext entityContext = new UiEntityContext(range.asClass());
        accessManager.applyRegisteredConstraints(entityContext);
        if (!entityContext.isViewPermitted()) {
            return;
        }
    }
    ValueSource valueSource = new ContainerValueSource<>(container, metaProperty.getName());
    ComponentGenerationContext componentContext = new ComponentGenerationContext(metaClass, metaProperty.getName());
    componentContext.setValueSource(valueSource);
    Field field = (Field) uiComponentsGenerator.generate(componentContext);
    if (requireTextArea(metaProperty, getItem(), MAX_TEXTFIELD_STRING_LENGTH)) {
        field = uiComponents.create(TextArea.NAME);
    }
    if (isBoolean(metaProperty)) {
        field = createBooleanField();
    }
    if (range.isClass()) {
        EntityPicker pickerField = uiComponents.create(EntityPicker.class);
        EntityLookupAction lookupAction = actions.create(EntityLookupAction.class);
        lookupAction.setScreenClass(EntityInspectorBrowser.class);
        lookupAction.setScreenOptionsSupplier(() -> new MapScreenOptions(ParamsMap.of("entity", metaProperty.getRange().asClass().getName())));
        lookupAction.setOpenMode(OpenMode.THIS_TAB);
        pickerField.addAction(lookupAction);
        pickerField.addAction(actions.create(EntityClearAction.class));
        field = pickerField;
    }
    field.setValueSource(valueSource);
    field.setCaption(getPropertyCaption(metaClass, metaProperty));
    field.setRequired(isRequired);
    isReadonly = isReadonly || (disabledProperties != null && disabledProperties.contains(metaProperty.getName()));
    if (range.isClass() && !metadataTools.isEmbedded(metaProperty)) {
        field.setEditable(metadataTools.isOwningSide(metaProperty) && !isReadonly);
    } else {
        field.setEditable(!isReadonly);
    }
    field.setWidth(fieldWidth);
    if (isRequired) {
        field.setRequiredMessage(messageTools.getDefaultRequiredMessage(metaClass, metaProperty.getName()));
    }
    form.add(field);
}
Also used : UiEntityContext(io.jmix.ui.accesscontext.UiEntityContext) UiEntityAttributeContext(io.jmix.ui.accesscontext.UiEntityAttributeContext) Range(io.jmix.core.metamodel.model.Range) ContainerValueSource(io.jmix.ui.component.data.value.ContainerValueSource) EntityClearAction(io.jmix.ui.action.entitypicker.EntityClearAction) MetaClass(io.jmix.core.metamodel.model.MetaClass) ContainerValueSource(io.jmix.ui.component.data.value.ContainerValueSource) ValueSource(io.jmix.ui.component.data.ValueSource) EntityLookupAction(io.jmix.ui.action.entitypicker.EntityLookupAction) MapScreenOptions(io.jmix.ui.screen.MapScreenOptions)

Example 4 with UiEntityAttributeContext

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

the class BaseEmbeddingStrategy method checkPermissions.

protected boolean checkPermissions(AttributeDefinition attributeDefinition, MetaClass entityMetaClass) {
    UiEntityAttributeContext uiEntityAttributeContext = new UiEntityAttributeContext(metadataTools.resolveMetaPropertyPath(entityMetaClass, DynAttrUtils.getPropertyFromAttributeCode(attributeDefinition.getCode())));
    accessManager.applyRegisteredConstraints(uiEntityAttributeContext);
    if (uiEntityAttributeContext.canView()) {
        if (attributeDefinition.getDataType() == AttributeType.ENTITY) {
            MetaClass referenceEntityClass = metadata.getClass(attributeDefinition.getJavaType());
            UiEntityContext uiEntityContext = new UiEntityContext(referenceEntityClass);
            accessManager.applyRegisteredConstraints(uiEntityContext);
            return uiEntityContext.isViewPermitted();
        }
        return true;
    }
    return false;
}
Also used : UiEntityContext(io.jmix.ui.accesscontext.UiEntityContext) MetaClass(io.jmix.core.metamodel.model.MetaClass) UiEntityAttributeContext(io.jmix.ui.accesscontext.UiEntityAttributeContext)

Example 5 with UiEntityAttributeContext

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

the class ExcludeAction method isPermitted.

@Override
protected boolean isPermitted() {
    if (target == null || !(target.getItems() instanceof ContainerDataUnit)) {
        return false;
    }
    ContainerDataUnit<E> containerDataUnit = (ContainerDataUnit) target.getItems();
    MetaClass metaClass = containerDataUnit.getEntityMetaClass();
    if (metaClass == null) {
        return false;
    }
    if (containerDataUnit.getContainer() instanceof Nested) {
        Nested nestedContainer = (Nested) containerDataUnit.getContainer();
        MetaClass masterMetaClass = nestedContainer.getMaster().getEntityMetaClass();
        MetaProperty metaProperty = masterMetaClass.getProperty(nestedContainer.getProperty());
        UiEntityAttributeContext attributeContext = new UiEntityAttributeContext(masterMetaClass, metaProperty.getName());
        accessManager.applyRegisteredConstraints(attributeContext);
        boolean attrPermitted = attributeContext.canModify();
        if (!attrPermitted) {
            return false;
        }
    }
    return super.isPermitted();
}
Also used : MetaClass(io.jmix.core.metamodel.model.MetaClass) UiEntityAttributeContext(io.jmix.ui.accesscontext.UiEntityAttributeContext) Nested(io.jmix.ui.model.Nested) ContainerDataUnit(io.jmix.ui.component.data.meta.ContainerDataUnit) MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Aggregations

UiEntityAttributeContext (io.jmix.ui.accesscontext.UiEntityAttributeContext)20 MetaClass (io.jmix.core.metamodel.model.MetaClass)13 UiEntityContext (io.jmix.ui.accesscontext.UiEntityContext)9 MetaPropertyPath (io.jmix.core.metamodel.model.MetaPropertyPath)7 KeyValueMetaClass (io.jmix.core.impl.keyvalue.KeyValueMetaClass)5 MetaProperty (io.jmix.core.metamodel.model.MetaProperty)5 Nested (io.jmix.ui.model.Nested)4 ValueSource (io.jmix.ui.component.data.ValueSource)3 ContainerDataUnit (io.jmix.ui.component.data.meta.ContainerDataUnit)3 EntityValueSource (io.jmix.ui.component.data.meta.EntityValueSource)3 Range (io.jmix.core.metamodel.model.Range)2 io.jmix.ui.component (io.jmix.ui.component)2 HasValueSource (io.jmix.ui.component.data.HasValueSource)2 ContainerValueSource (io.jmix.ui.component.data.value.ContainerValueSource)2 Component (com.vaadin.ui.Component)1 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