diff --git a/assay/api-src/org/labkey/api/assay/AssayResultUpdateService.java b/assay/api-src/org/labkey/api/assay/AssayResultUpdateService.java index 9619e023f3f..b56a90cb429 100644 --- a/assay/api-src/org/labkey/api/assay/AssayResultUpdateService.java +++ b/assay/api-src/org/labkey/api/assay/AssayResultUpdateService.java @@ -47,6 +47,7 @@ import org.labkey.api.query.FilteredTable; import org.labkey.api.query.InvalidKeyException; import org.labkey.api.query.QueryUpdateServiceException; +import org.labkey.api.query.RuntimeValidationException; import org.labkey.api.query.ValidationException; import org.labkey.api.security.User; import org.labkey.api.security.permissions.DeletePermission; @@ -339,6 +340,9 @@ protected Map deleteRow( { ExpRun run = getRun(oldRowMap, user, DeletePermission.class); + if (!container.allowRowMutationForContainer(run.getContainer())) + throw new RuntimeValidationException("Assay results being deleted are from a different container."); + AssayService.get().onBeforeAssayResultDelete(container, user, run, oldRowMap); TableInfo datatableInfo = this.getQueryTable(); diff --git a/assay/src/org/labkey/assay/AssayController.java b/assay/src/org/labkey/assay/AssayController.java index 2a92cbb79cb..0582ceaa4f9 100644 --- a/assay/src/org/labkey/assay/AssayController.java +++ b/assay/src/org/labkey/assay/AssayController.java @@ -1783,6 +1783,7 @@ public Object execute(AssayOperationConfirmationForm form, BindException errors) Collection> dataRows = new TableSelector(tableInfo, PageFlowUtil.set("rowid", "dataid"), filter, null).getMapCollection(); List permittedRowIds = new LongArrayList(); Set uniqueContainers = new HashSet<>(); + Map containersByRowId = new HashMap<>(); for (Map dataRow : dataRows) { Long rowId = MapUtils.getLong(dataRow,"rowid"); @@ -1790,6 +1791,7 @@ public Object execute(AssayOperationConfirmationForm form, BindException errors) if (data == null || data.getRun() == null) continue; Container c = data.getRun().getContainer(); + containersByRowId.put(rowId, c); if (c.hasPermission(getUser(), ReadPermission.class)) uniqueContainers.add(c); if (c.hasPermission(getUser(), AssayRunOperations.Edit.getPermissionClass())) @@ -1804,7 +1806,15 @@ public Object execute(AssayOperationConfirmationForm form, BindException errors) "path", (Object) c.getPath(), "permitted", permClass == null || c.hasPermission(getUser(), permClass) )).toList()); - response.put("allowed", allowedIds.stream().map(id -> Map.of("RowId", id)).toList()); + // GH Issue 1809: the client partitions the delete by ContainerPath; without it every row is sent to the request folder + response.put("allowed", allowedIds.stream().map(id -> { + Map rowMap = new HashMap<>(); + rowMap.put("RowId", id); + Container c = containersByRowId.get(id); + if (c != null && c.hasPermission(getUser(), ReadPermission.class)) + rowMap.put("ContainerPath", c.getPath()); + return rowMap; + }).toList()); response.put("notAllowed", new HashSet<>()); response.put("notPermitted", notPermitted); return success(response); diff --git a/experiment/src/org/labkey/experiment/api/ExpDataClassDataTableImpl.java b/experiment/src/org/labkey/experiment/api/ExpDataClassDataTableImpl.java index c1104f779a7..0e51089f021 100644 --- a/experiment/src/org/labkey/experiment/api/ExpDataClassDataTableImpl.java +++ b/experiment/src/org/labkey/experiment/api/ExpDataClassDataTableImpl.java @@ -32,6 +32,7 @@ import org.labkey.api.data.BaseColumnInfo; import org.labkey.api.data.ColumnHeaderType; import org.labkey.api.data.ColumnInfo; +import org.labkey.api.data.CompareType; import org.labkey.api.data.Container; import org.labkey.api.data.ContainerFilter; import org.labkey.api.data.ContainerManager; @@ -1364,6 +1365,86 @@ protected Map getRow(User user, Container container, Map> deleteRows(User user, Container container, List> keys, @Nullable Map configParameters, @Nullable Map extraScriptContext) + throws QueryUpdateServiceException, SQLException, InvalidKeyException, BatchValidationException + { + verifyNoCrossFolderDeletes(user, container, keys); + return super.deleteRows(user, container, keys, configParameters, extraScriptContext); + } + + /** + * GH Issue 1809: getRow() is scoped to the request folder, so a row in another folder would be dropped from the + * delete without a word. Report those rows instead. Delete only -- the shared lookup path keeps its old behavior. + */ + private void verifyNoCrossFolderDeletes(User user, Container container, List> keys) + throws BatchValidationException + { + Set rowIds = new HashSet<>(); + Set lsids = new HashSet<>(); + Set names = new HashSet<>(); + List keyValues = new ArrayList<>(keys.size()); + for (Map key : keys) + { + Map aliased = new CaseInsensitiveHashMap<>(key); + aliasColumns(_columnMapping, aliased); + Long rowId = (Long) JdbcType.BIGINT.convert(aliased.get(Column.RowId.name())); + String lsid = (String) JdbcType.VARCHAR.convert(aliased.get(Column.LSID.name())); + String name = (String) JdbcType.VARCHAR.convert(aliased.get(Name.name())); + if (null != rowId) + rowIds.add(rowId); + else if (null != lsid) + lsids.add(lsid); + else if (null != name) + names.add(name); + keyValues.add(rowId != null ? rowId : (lsid != null ? lsid : name)); + } + + // exp.data carries no container filter, so rows the user cannot read are found here and reported rather than dropped + Map containersByKey = new HashMap<>(); + selectDataContainers(Column.RowId, rowIds, false, containersByKey); + selectDataContainers(Column.LSID, lsids, false, containersByKey); + selectDataContainers(Column.Name, names, true, containersByKey); + + BatchValidationException errors = new BatchValidationException(); + for (int i = 0; i < keys.size(); i++) + { + // absent means no such row anywhere, which super.deleteRows already ignores + Container rowContainer = containersByKey.get(keyValues.get(i)); + if (null == rowContainer || container.equals(rowContainer)) + continue; + + // only name a folder the user is allowed to see + String where = rowContainer.hasPermission(user, getReadPermissionClass()) ? rowContainer.getPath() : "another folder"; + ValidationException vex = new ValidationException("Data is from a different folder: " + where + "."); + errors.addRowError(vex.fillIn(getQueryTable().getPublicSchemaName(), getQueryTable().getName(), keys.get(i), i)); + } + if (errors.hasErrors()) + throw errors; + } + + /** exp.data enforces UNIQUE(classId, name), so every key type resolves to at most one row. */ + private void selectDataContainers(Column keyColumn, Collection values, boolean scopeToDataClass, Map containersByKey) + { + if (values.isEmpty()) + return; + + SimpleFilter filter = new SimpleFilter(FieldKey.fromParts(keyColumn.name()), values, CompareType.IN); + if (scopeToDataClass) + filter.addCondition(FieldKey.fromParts("ClassId"), _dataClass.getRowId()); + + new TableSelector(ExperimentService.get().getTinfoData(), + CaseInsensitiveHashSet.of(keyColumn.name(), "Container"), filter, null) + .forEachMap(row -> { + Container c = ContainerManager.getForId((String) row.get("Container")); + if (null == c) + return; + // exp.data.RowId comes back as Integer; the keys were converted to Long + Object keyValue = Column.RowId == keyColumn ? JdbcType.BIGINT.convert(row.get(keyColumn.name())) : row.get(keyColumn.name()); + containersByKey.put(keyValue, c); + }); + } + @Override public List> getRows(User user, Container container, List> keys) throws SQLException, InvalidKeyException diff --git a/experiment/src/org/labkey/experiment/api/SampleTypeUpdateServiceDI.java b/experiment/src/org/labkey/experiment/api/SampleTypeUpdateServiceDI.java index ae54cdfcd84..4d5390938bf 100644 --- a/experiment/src/org/labkey/experiment/api/SampleTypeUpdateServiceDI.java +++ b/experiment/src/org/labkey/experiment/api/SampleTypeUpdateServiceDI.java @@ -96,6 +96,7 @@ import org.labkey.api.query.QueryService; import org.labkey.api.query.QueryUpdateService; import org.labkey.api.query.QueryUpdateServiceException; +import org.labkey.api.query.RuntimeValidationException; import org.labkey.api.query.UserSchema; import org.labkey.api.query.ValidationException; import org.labkey.api.reader.ColumnDescriptor; @@ -790,6 +791,9 @@ protected Map deleteRow(User user, Container container, Map id = new LinkedList<>(); Long rowId = getMaterialRowId(oldRowMap); + String crossFolderError = crossFolderError(container, oldRowMap); + if (crossFolderError != null) + throw new RuntimeValidationException(crossFolderError); id.add(rowId); ExperimentServiceImpl.get().deleteMaterialByRowIds(user, container, id, true, _sampleType, false, false); return oldRowMap; @@ -809,11 +813,14 @@ public List> deleteRows(User user, Container container, List else { List ids = new LinkedList<>(); + BatchValidationException errors = new BatchValidationException(); // Issue 40621 // adding input fields is expensive, skip input fields for delete since deleted samples are not surfaced on Timeline UI - for (Map map : getMaterialMaps(keys)) + List> materialMaps = getMaterialMaps(keys); + for (int i = 0; i < materialMaps.size(); i++) { + Map map = materialMaps.get(i); if (map == null) throw new QueryUpdateServiceException("No Sample Type Material found for RowID or LSID"); @@ -821,6 +828,14 @@ public List> deleteRows(User user, Container container, List if (rowId == null) throw new QueryUpdateServiceException("RowID is required to delete a Sample Type Material"); + String crossFolderError = crossFolderError(container, map); + if (crossFolderError != null) + { + ValidationException vex = new ValidationException(crossFolderError); + errors.addRowError(vex.fillIn(getQueryTable().getPublicSchemaName(), getQueryTable().getName(), keys.get(i), i)); + continue; + } + Long sampleStateId = MapUtils.getLong(map, SampleState.name()); if (!SampleStatusService.get().isOperationPermitted(getContainer(), sampleStateId, SampleTypeService.SampleOperations.Delete)) { @@ -831,6 +846,9 @@ public List> deleteRows(User user, Container container, List ids.add(rowId); result.add(map); } + if (errors.hasErrors()) + throw errors; + // TODO check if this handle attachments??? ExperimentServiceImpl.get().deleteMaterialByRowIds(user, container, ids, true, _sampleType, false, false); } @@ -873,6 +891,27 @@ public List> deleteRows(User user, Container container, List return MapUtils.getLong(row, RowId.name()); } + /** + * GH Issue 1809: rows resolve across containers, so the row's own folder must be checked before deleting it. + * Returns the error, or null when the row may be deleted from this folder. A folder that cannot be resolved is an + * error rather than a pass: this is the check the delete relies on. + */ + private @Nullable String crossFolderError(Container container, Map row) + { + Object folder = row.get(Folder.name()); + Container rowContainer = switch (folder) + { + case null -> null; + case Container c -> c; + default -> ContainerManager.getForId(folder.toString()); + }; + if (rowContainer == null) + return "Unable to determine the folder of the sample being deleted."; + if (!container.allowRowMutationForContainer(rowContainer)) + return "Samples being deleted are from a different folder: " + rowContainer.getPath() + "."; + return null; + } + private @Nullable Filter getMaterialFilter(Map keys, boolean useSampleType) { Long rowId = getMaterialRowId(keys);