Skip to content

Well table contextual role support #6818

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
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
13 changes: 13 additions & 0 deletions api/src/org/labkey/api/assay/plate/AssayPlateMetadataService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@
import org.labkey.api.gwt.client.model.GWTDomain;
import org.labkey.api.gwt.client.model.GWTPropertyDescriptor;
import org.labkey.api.qc.DataLoaderSettings;
import org.labkey.api.query.QuerySchema;
import org.labkey.api.query.UserSchema;
import org.labkey.api.query.ValidationException;
import org.labkey.api.security.User;
import org.labkey.api.security.roles.Role;
import org.labkey.api.services.ServiceRegistry;
import org.labkey.vfs.FileLike;

import java.util.List;
import java.util.Map;
import java.util.Set;

public interface AssayPlateMetadataService
{
Expand Down Expand Up @@ -178,4 +182,13 @@ void applyHitSelectionCriteria(
TableInfo resultsTable,
List<Integer> runIds
) throws ValidationException;

/**
* Should only be used to get a local instance of a plate schema where a contextual role might be involved. Schemas created this way are not cached,
* and all other usages should retrieve schemas from the QueryService.
* <p>
* This method would be better placed in the PlateService interface, but it would have required moving that (and other) classes into API.
*/
@NotNull
UserSchema getPlateSchema(QuerySchema schema, Set<Role> contextualRoles);
}
11 changes: 9 additions & 2 deletions api/src/org/labkey/api/exp/query/SamplesSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.labkey.api.query.QuerySettings;
import org.labkey.api.query.QueryView;
import org.labkey.api.query.SchemaKey;
import org.labkey.api.query.UserSchema;
import org.labkey.api.security.User;
import org.labkey.api.security.permissions.ReadPermission;
import org.labkey.api.security.roles.ReaderRole;
Expand All @@ -56,7 +57,7 @@
import java.util.Map;
import java.util.Set;

public class SamplesSchema extends AbstractExpSchema
public class SamplesSchema extends AbstractExpSchema implements UserSchema.HasContextualRoles
{
public static final String SCHEMA_NAME = "samples";
public static final String STUDY_LINKED_SCHEMA_NAME = "~~STUDY.LINKED.SAMPLE~~";
Expand Down Expand Up @@ -107,7 +108,7 @@ public SamplesSchema(QuerySchema schema)
setDefaultSchema(schema.getDefaultSchema());
}

public SamplesSchema(QuerySchema schema, boolean studyLinkedSamples)
private SamplesSchema(QuerySchema schema, boolean studyLinkedSamples)
{
this(schema.getUser(), schema.getContainer());
setDefaultSchema(schema.getDefaultSchema());
Expand All @@ -126,6 +127,12 @@ public SamplesSchema(User user, Container container)
this(SchemaKey.fromParts(SCHEMA_NAME), user, container);
}

@Override
public @NotNull Set<Role> getContextualRoles()
{
return contextualRoles;
}

@Override
public boolean canReadSchema()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,15 @@
import org.labkey.api.qc.DataState;
import org.labkey.api.query.BatchValidationException;
import org.labkey.api.query.FieldKey;
import org.labkey.api.query.QuerySchema;
import org.labkey.api.query.QueryService;
import org.labkey.api.query.QueryUpdateService;
import org.labkey.api.query.RuntimeValidationException;
import org.labkey.api.query.UserSchema;
import org.labkey.api.query.ValidationException;
import org.labkey.api.reader.DataLoader;
import org.labkey.api.security.User;
import org.labkey.api.security.roles.Role;
import org.labkey.api.util.FileUtil;
import org.labkey.api.util.JunitUtil;
import org.labkey.api.util.Pair;
Expand All @@ -93,6 +96,7 @@
import org.labkey.api.view.ActionURL;
import org.labkey.assay.TSVProtocolSchema;
import org.labkey.assay.plate.model.WellBean;
import org.labkey.assay.plate.query.PlateSchema;
import org.labkey.assay.plate.query.PlateTable;
import org.labkey.assay.plate.query.WellTable;
import org.labkey.assay.query.AssayDbSchema;
Expand Down Expand Up @@ -1485,6 +1489,12 @@ public String format(FieldKey fieldKey)
return StringUtils.join(parts, " and ");
}

@Override
public UserSchema getPlateSchema(QuerySchema querySchema, Set<Role> contextualRoles)
{
return new PlateSchema(querySchema, contextualRoles);
}

private static class PlateMetadataImportHelper extends SimpleAssayDataImportHelper
{
private final Map<Integer, Map<Position, Lsid>> _wellPositionMap; // map of plate position to well table
Expand Down
27 changes: 26 additions & 1 deletion assay/src/org/labkey/assay/plate/query/PlateSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.labkey.assay.plate.query;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.labkey.api.collections.CaseInsensitiveTreeSet;
import org.labkey.api.data.Container;
Expand All @@ -25,13 +26,16 @@
import org.labkey.api.query.DefaultSchema;
import org.labkey.api.query.QuerySchema;
import org.labkey.api.query.SimpleUserSchema;
import org.labkey.api.query.UserSchema;
import org.labkey.api.security.User;
import org.labkey.api.security.permissions.ReadPermission;
import org.labkey.api.security.roles.Role;
import org.labkey.assay.query.AssayDbSchema;

import java.util.List;
import java.util.Set;

public class PlateSchema extends SimpleUserSchema
public class PlateSchema extends SimpleUserSchema implements UserSchema.HasContextualRoles
{
public static final String SCHEMA_NAME = "plate";
private static final String DESCRIPTION = "Contains data about defined plates";
Expand All @@ -48,12 +52,21 @@ public class PlateSchema extends SimpleUserSchema
AmountUnitsTable.NAME
));

Set<Role> contextualRoles = Set.of();

public PlateSchema(User user, Container container)
{
super(SCHEMA_NAME, DESCRIPTION, user, container, AssayDbSchema.getInstance().getSchema(),
null, AVAILABLE_TABLES, null);
}

public PlateSchema(QuerySchema schema, Set<Role> contextualRoles)
{
this(schema.getUser(), schema.getContainer());
setDefaultSchema(schema.getDefaultSchema());
this.contextualRoles = contextualRoles;
}

@Override
public Set<String> getTableNames()
{
Expand Down Expand Up @@ -85,6 +98,18 @@ public TableInfo createTable(String name, ContainerFilter cf)
return null;
}

@Override
public @NotNull Set<Role> getContextualRoles()
{
return contextualRoles;
}

@Override
public boolean canReadSchema()
{
return super.canReadSchema() || getContainer().hasPermission(getUser(), ReadPermission.class, contextualRoles) ;
}

public static TableInfo getPlateSetTable(Container container, User user, @Nullable ContainerFilter cf)
{
PlateSchema plateSchema = new PlateSchema(user, container);
Expand Down
5 changes: 5 additions & 0 deletions assay/src/org/labkey/assay/plate/query/WellTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.labkey.api.security.permissions.DeletePermission;
import org.labkey.api.security.permissions.InsertPermission;
import org.labkey.api.security.permissions.Permission;
import org.labkey.api.security.permissions.ReadPermission;
import org.labkey.api.util.UnexpectedException;
import org.labkey.assay.plate.PlateManager;
import org.labkey.assay.plate.TsvPlateLayoutHandler;
Expand Down Expand Up @@ -374,6 +375,10 @@ public boolean hasPermission(@NotNull UserPrincipal user, @NotNull Class<? exten
{
if (!_allowInsertDelete && (InsertPermission.class.equals(perm) || DeletePermission.class.equals(perm)))
return false;

if (perm.equals(ReadPermission.class))
return _userSchema.getContainer().hasPermission(user, perm, _userSchema.getContextualRoles());

return super.hasPermission(user, perm);
}

Expand Down
34 changes: 31 additions & 3 deletions experiment/src/org/labkey/experiment/api/ExpMaterialTableImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,36 @@
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.labkey.api.assay.plate.AssayPlateMetadataService;
import org.labkey.api.audit.AuditHandler;
import org.labkey.api.cache.BlockingCache;
import org.labkey.api.cache.CacheManager;
import org.labkey.api.collections.CaseInsensitiveHashMap;
import org.labkey.api.collections.CaseInsensitiveHashSet;
import org.labkey.api.compliance.TableRules;
import org.labkey.api.compliance.TableRulesManager;
import org.labkey.api.data.*;
import org.labkey.api.data.ColumnHeaderType;
import org.labkey.api.data.ColumnInfo;
import org.labkey.api.data.Container;
import org.labkey.api.data.ContainerFilter;
import org.labkey.api.data.ContainerManager;
import org.labkey.api.data.CoreSchema;
import org.labkey.api.data.DataColumn;
import org.labkey.api.data.DataRegion;
import org.labkey.api.data.DbSchema;
import org.labkey.api.data.DbScope;
import org.labkey.api.data.DisplayColumn;
import org.labkey.api.data.DisplayColumnFactory;
import org.labkey.api.data.ImportAliasable;
import org.labkey.api.data.JdbcType;
import org.labkey.api.data.MaterializedQueryHelper;
import org.labkey.api.data.MutableColumnInfo;
import org.labkey.api.data.PHI;
import org.labkey.api.data.RenderContext;
import org.labkey.api.data.SQLFragment;
import org.labkey.api.data.Sort;
import org.labkey.api.data.TableInfo;
import org.labkey.api.data.UnionContainerFilter;
import org.labkey.api.dataiterator.DataIteratorBuilder;
import org.labkey.api.dataiterator.DataIteratorContext;
import org.labkey.api.dataiterator.LoggingDataIterator;
Expand Down Expand Up @@ -120,7 +142,7 @@
import static org.labkey.api.exp.query.ExpMaterialTable.Column.AvailableAliquotCount;
import static org.labkey.api.exp.query.ExpMaterialTable.Column.AvailableAliquotVolume;
import static org.labkey.api.util.StringExpressionFactory.AbstractStringExpression.NullValueBehavior.NullResult;
import static org.labkey.experiment.api.SampleTypeServiceImpl.SampleChangeType.*;
import static org.labkey.experiment.api.SampleTypeServiceImpl.SampleChangeType.schema;

public class ExpMaterialTableImpl extends ExpRunItemTableImpl<ExpMaterialTable.Column> implements ExpMaterialTable
{
Expand Down Expand Up @@ -780,8 +802,14 @@ public void addQueryFieldKeys(Set<FieldKey> keys)
if (InventoryService.get() != null && (st == null || !st.isMedia()))
defaultCols.addAll(InventoryService.get().addInventoryStatusColumns(st == null ? null : st.getMetricUnit(), this, getContainer(), _userSchema.getUser()));

UserSchema plateUserSchema = QueryService.get().getUserSchema(_userSchema.getUser(), getContainer(), "plate");
SQLFragment sql;
UserSchema plateUserSchema;
// Issue 53194 : this would be the case for linked to study samples
if (_userSchema instanceof UserSchema.HasContextualRoles samplesSchema && !samplesSchema.getContextualRoles().isEmpty())
plateUserSchema = AssayPlateMetadataService.get().getPlateSchema(_userSchema, samplesSchema.getContextualRoles());
else
plateUserSchema = QueryService.get().getUserSchema(_userSchema.getUser(), _userSchema.getContainer(), "plate");

if (plateUserSchema != null && plateUserSchema.getTable("Well") != null)
{
String rowIdField = ExprColumn.STR_TABLE_ALIAS + "." + Column.RowId.name();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,25 @@
import org.labkey.api.audit.TransactionAuditProvider;
import org.labkey.api.cache.Cache;
import org.labkey.api.cache.CacheManager;
import org.labkey.api.data.*;
import org.labkey.api.data.AuditConfigurable;
import org.labkey.api.data.Container;
import org.labkey.api.data.ContainerFilter;
import org.labkey.api.data.ContainerManager;
import org.labkey.api.data.DbSchema;
import org.labkey.api.data.DbScope;
import org.labkey.api.data.DbSequence;
import org.labkey.api.data.DbSequenceManager;
import org.labkey.api.data.JdbcType;
import org.labkey.api.data.NameGenerator;
import org.labkey.api.data.Parameter;
import org.labkey.api.data.ParameterMapStatement;
import org.labkey.api.data.RuntimeSQLException;
import org.labkey.api.data.SQLFragment;
import org.labkey.api.data.SimpleFilter;
import org.labkey.api.data.SqlExecutor;
import org.labkey.api.data.SqlSelector;
import org.labkey.api.data.TableInfo;
import org.labkey.api.data.TableSelector;
import org.labkey.api.data.dialect.SqlDialect;
import org.labkey.api.data.measurement.Measurement;
import org.labkey.api.defaults.DefaultValueService;
Expand Down
1 change: 0 additions & 1 deletion study/src/org/labkey/study/query/SampleDatasetTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ private TableInfo getSamplesTable()
_sampleType = sampleType;

UserSchema userSchema = QueryService.get().getUserSchema(_userSchema.getUser(), sampleType.getContainer(), SamplesSchema.SCHEMA_NAME);

// Hide 'linked' column for Sample Type Datasets
if (userSchema instanceof SamplesSchema samplesSchema)
{
Expand Down