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");
}
}
}
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;
}
}
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));
}
}
}
Aggregations