Search in sources :

Example 1 with AccessConstraint

use of io.jmix.core.constraint.AccessConstraint in project jmix by jmix-framework.

the class DataStoreCrudListener method beforeEntitySave.

@Override
public void beforeEntitySave(DataStoreBeforeEntitySaveEvent event) {
    SaveContext context = event.getSaveContext();
    Collection<AccessConstraint<?>> accessConstraints = context.getAccessConstraints();
    if (accessConstraints.isEmpty()) {
        return;
    }
    Map<MetaClass, CrudEntityContext> accessCache = new HashMap<>();
    for (Object entity : context.getEntitiesToSave()) {
        if (entity == null) {
            continue;
        }
        MetaClass metaClass = metadata.getClass(entity);
        CrudEntityContext entityContext = accessCache.computeIfAbsent(metaClass, key -> evaluateCrudAccess(key, accessConstraints));
        if (entityStates.isNew(entity)) {
            if (!entityContext.isCreatePermitted()) {
                throw new AccessDeniedException("entity", metaClass.getName(), "create");
            }
        } else if (!entityContext.isUpdatePermitted()) {
            throw new AccessDeniedException("entity", metaClass.getName(), "update");
        }
    }
    for (Object entity : context.getEntitiesToRemove()) {
        if (entity == null) {
            continue;
        }
        MetaClass metaClass = metadata.getClass(entity);
        CrudEntityContext entityContext = accessCache.computeIfAbsent(metaClass, key -> evaluateCrudAccess(key, accessConstraints));
        if (!entityContext.isDeletePermitted()) {
            throw new AccessDeniedException("entity", metaClass.getName(), "update");
        }
    }
}
Also used : AccessDeniedException(io.jmix.core.security.AccessDeniedException) MetaClass(io.jmix.core.metamodel.model.MetaClass) CrudEntityContext(io.jmix.core.accesscontext.CrudEntityContext) HashMap(java.util.HashMap) AccessConstraint(io.jmix.core.constraint.AccessConstraint)

Example 2 with AccessConstraint

use of io.jmix.core.constraint.AccessConstraint in project jmix by jmix-framework.

the class DynAttrManagerImpl method loadValues.

protected List<CategoryAttributeValue> loadValues(MetaClass metaClass, Collection<AccessConstraint<?>> accessConstraints, List<Object> entityIds) {
    List<CategoryAttributeValue> mainAttributeValues = findValuesByEntityIds(metaClass, entityIds);
    List<CategoryAttributeValue> entityValues = mainAttributeValues.stream().filter(v -> v.getObjectEntityValueId() != null).collect(Collectors.toList());
    List<CategoryAttributeValue> collectionValues = mainAttributeValues.stream().filter(v -> BooleanUtils.isTrue(v.getCategoryAttribute().getIsCollection())).collect(Collectors.toList());
    if (collectionValues.isEmpty()) {
        fetchEntityValues(accessConstraints, entityValues);
        return mainAttributeValues;
    } else {
        List<CategoryAttributeValue> reloadedCollectionValues = fetchCollectionValues(collectionValues);
        List<CategoryAttributeValue> values = new ArrayList<>(mainAttributeValues.size());
        for (CategoryAttributeValue value : reloadedCollectionValues) {
            if (value.getCategoryAttribute().getDataType() == AttributeType.ENTITY && value.getChildValues() != null) {
                for (CategoryAttributeValue child : value.getChildValues()) {
                    if (child.getDeleteTs() == null) {
                        entityValues.add(child);
                    }
                }
            }
        }
        fetchEntityValues(accessConstraints, entityValues);
        for (CategoryAttributeValue value : reloadedCollectionValues) {
            if (value.getChildValues() != null) {
                value.setTransientCollectionValue(value.getChildValues().stream().filter(v -> v.getDeleteTs() == null).map(CategoryAttributeValue::getValue).collect(Collectors.toList()));
            }
        }
        for (CategoryAttributeValue value : mainAttributeValues) {
            if (!reloadedCollectionValues.contains(value)) {
                values.add(value);
            }
        }
        values.addAll(reloadedCollectionValues);
        return values;
    }
}
Also used : EntitySystemAccess(io.jmix.core.entity.EntitySystemAccess) MetaClass(io.jmix.core.metamodel.model.MetaClass) java.util(java.util) io.jmix.dynattr(io.jmix.dynattr) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) io.jmix.core(io.jmix.core) CrudEntityContext(io.jmix.core.accesscontext.CrudEntityContext) BooleanUtils(org.apache.commons.lang3.BooleanUtils) Multimap(com.google.common.collect.Multimap) EntityValues(io.jmix.core.entity.EntityValues) BigDecimal(java.math.BigDecimal) HashMultimap(com.google.common.collect.HashMultimap) CategoryAttribute(io.jmix.dynattr.model.CategoryAttribute) CategoryAttributeValue(io.jmix.dynattr.model.CategoryAttributeValue) Nullable(javax.annotation.Nullable) Logger(org.slf4j.Logger) PersistenceHints(io.jmix.data.PersistenceHints) EntityManager(javax.persistence.EntityManager) Collectors(java.util.stream.Collectors) Component(org.springframework.stereotype.Component) Stream(java.util.stream.Stream) LocalDate(java.time.LocalDate) ReflectionHelper(io.jmix.core.common.util.ReflectionHelper) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) AccessConstraint(io.jmix.core.constraint.AccessConstraint) StoreAwareLocator(io.jmix.data.StoreAwareLocator) CategoryAttributeValue(io.jmix.dynattr.model.CategoryAttributeValue)

Example 3 with AccessConstraint

use of io.jmix.core.constraint.AccessConstraint in project jmix by jmix-framework.

the class DynAttrManagerImpl method doFetchValues.

protected void doFetchValues(MetaClass metaClass, Collection<Object> entities, Collection<AccessConstraint<?>> accessConstraints) {
    if (dynAttrMetadata.getAttributes(metaClass).isEmpty() || metadataTools.hasCompositePrimaryKey(metaClass) && !metadataTools.hasUuid(metaClass)) {
        for (Object entity : entities) {
            DynamicAttributesState state = new DynamicAttributesState(getEntityEntry(entity));
            addExtraState(entity, state);
        }
    } else {
        List<Object> ids = entities.stream().map(e -> referenceToEntitySupport.getReferenceId(e)).collect(Collectors.toList());
        Multimap<Object, CategoryAttributeValue> allAttributeValues = HashMultimap.create();
        List<Object> currentIds = new ArrayList<>();
        for (Object id : ids) {
            currentIds.add(id);
            if (currentIds.size() >= MAX_ENTITIES_FOR_ATTRIBUTE_VALUES_BATCH) {
                for (CategoryAttributeValue attributeValue : loadValues(metaClass, accessConstraints, currentIds)) {
                    allAttributeValues.put(attributeValue.getObjectEntityId(), attributeValue);
                }
                currentIds = new ArrayList<>();
            }
        }
        if (!currentIds.isEmpty()) {
            for (CategoryAttributeValue attributeValue : loadValues(metaClass, accessConstraints, currentIds)) {
                allAttributeValues.put(attributeValue.getObjectEntityId(), attributeValue);
            }
        }
        for (Object entity : entities) {
            Collection<CategoryAttributeValue> values = allAttributeValues.get(referenceToEntitySupport.getReferenceId(entity));
            DynamicAttributesState state = new DynamicAttributesState(getEntityEntry(entity));
            addExtraState(entity, state);
            Map<String, Object> map = new HashMap<>();
            if (values != null && !values.isEmpty()) {
                for (CategoryAttributeValue categoryAttributeValue : values) {
                    CategoryAttribute attribute = categoryAttributeValue.getCategoryAttribute();
                    if (attribute != null) {
                        map.put(attribute.getCode(), categoryAttributeValue.getValue());
                    }
                }
            }
            state.setDynamicAttributes(new DynamicAttributes(map));
        }
    }
}
Also used : EntitySystemAccess(io.jmix.core.entity.EntitySystemAccess) MetaClass(io.jmix.core.metamodel.model.MetaClass) java.util(java.util) io.jmix.dynattr(io.jmix.dynattr) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) io.jmix.core(io.jmix.core) CrudEntityContext(io.jmix.core.accesscontext.CrudEntityContext) BooleanUtils(org.apache.commons.lang3.BooleanUtils) Multimap(com.google.common.collect.Multimap) EntityValues(io.jmix.core.entity.EntityValues) BigDecimal(java.math.BigDecimal) HashMultimap(com.google.common.collect.HashMultimap) CategoryAttribute(io.jmix.dynattr.model.CategoryAttribute) CategoryAttributeValue(io.jmix.dynattr.model.CategoryAttributeValue) Nullable(javax.annotation.Nullable) Logger(org.slf4j.Logger) PersistenceHints(io.jmix.data.PersistenceHints) EntityManager(javax.persistence.EntityManager) Collectors(java.util.stream.Collectors) Component(org.springframework.stereotype.Component) Stream(java.util.stream.Stream) LocalDate(java.time.LocalDate) ReflectionHelper(io.jmix.core.common.util.ReflectionHelper) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) AccessConstraint(io.jmix.core.constraint.AccessConstraint) StoreAwareLocator(io.jmix.data.StoreAwareLocator) CategoryAttributeValue(io.jmix.dynattr.model.CategoryAttributeValue) CategoryAttribute(io.jmix.dynattr.model.CategoryAttribute)

Aggregations

CrudEntityContext (io.jmix.core.accesscontext.CrudEntityContext)3 AccessConstraint (io.jmix.core.constraint.AccessConstraint)3 MetaClass (io.jmix.core.metamodel.model.MetaClass)3 HashMultimap (com.google.common.collect.HashMultimap)2 Multimap (com.google.common.collect.Multimap)2 io.jmix.core (io.jmix.core)2 ReflectionHelper (io.jmix.core.common.util.ReflectionHelper)2 EntitySystemAccess (io.jmix.core.entity.EntitySystemAccess)2 EntityValues (io.jmix.core.entity.EntityValues)2 MetaProperty (io.jmix.core.metamodel.model.MetaProperty)2 PersistenceHints (io.jmix.data.PersistenceHints)2 StoreAwareLocator (io.jmix.data.StoreAwareLocator)2 io.jmix.dynattr (io.jmix.dynattr)2 CategoryAttribute (io.jmix.dynattr.model.CategoryAttribute)2 CategoryAttributeValue (io.jmix.dynattr.model.CategoryAttributeValue)2 BigDecimal (java.math.BigDecimal)2 LocalDate (java.time.LocalDate)2 java.util (java.util)2 Collectors (java.util.stream.Collectors)2 Stream (java.util.stream.Stream)2