Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -339,6 +340,9 @@ protected Map<String, Object> 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();
Expand Down
12 changes: 11 additions & 1 deletion assay/src/org/labkey/assay/AssayController.java
Original file line number Diff line number Diff line change
Expand Up @@ -1783,13 +1783,15 @@ public Object execute(AssayOperationConfirmationForm form, BindException errors)
Collection<Map<String, Object>> dataRows = new TableSelector(tableInfo, PageFlowUtil.set("rowid", "dataid"), filter, null).getMapCollection();
List<Long> permittedRowIds = new LongArrayList();
Set<Container> uniqueContainers = new HashSet<>();
Map<Long, Container> containersByRowId = new HashMap<>();
for (Map<String, Object> dataRow : dataRows)
{
Long rowId = MapUtils.getLong(dataRow,"rowid");
ExpData data = ExperimentService.get().getExpData(asLong(dataRow.get("dataid")));
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()))
Expand All @@ -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<String, Object> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1364,6 +1365,86 @@ protected Map<String, Object> getRow(User user, Container container, Map<String,
return getRow(user, container, keys, false);
}

@Override
public List<Map<String, Object>> deleteRows(User user, Container container, List<Map<String, Object>> keys, @Nullable Map<Enum, Object> configParameters, @Nullable Map<String, Object> 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<Map<String, Object>> keys)
throws BatchValidationException
{
Set<Long> rowIds = new HashSet<>();
Set<String> lsids = new HashSet<>();
Set<String> names = new HashSet<>();
List<Object> keyValues = new ArrayList<>(keys.size());
for (Map<String, Object> key : keys)
{
Map<String, Object> 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<Object, Container> 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 <T> void selectDataContainers(Column keyColumn, Collection<T> values, boolean scopeToDataClass, Map<Object, Container> 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<Map<String, Object>> getRows(User user, Container container, List<Map<String, Object>> keys)
throws SQLException, InvalidKeyException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -790,6 +791,9 @@ protected Map<String, Object> deleteRow(User user, Container container, Map<Stri
{
List<Long> 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;
Expand All @@ -809,18 +813,29 @@ public List<Map<String, Object>> deleteRows(User user, Container container, List
else
{
List<Long> 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<String, Object> map : getMaterialMaps(keys))
List<Map<String, Object>> materialMaps = getMaterialMaps(keys);
for (int i = 0; i < materialMaps.size(); i++)
{
Map<String, Object> map = materialMaps.get(i);
if (map == null)
throw new QueryUpdateServiceException("No Sample Type Material found for RowID or LSID");

Long rowId = getMaterialRowId(map);
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))
{
Expand All @@ -831,6 +846,9 @@ public List<Map<String, Object>> 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);
}
Expand Down Expand Up @@ -873,6 +891,27 @@ public List<Map<String, Object>> 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<String, Object> 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<String, Object> keys, boolean useSampleType)
{
Long rowId = getMaterialRowId(keys);
Expand Down