-
Notifications
You must be signed in to change notification settings - Fork 7
Create queries for custom views, reports and queries #7650
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
labkey-klum
wants to merge
15
commits into
develop
Choose a base branch
from
fb_customView_query
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
78132b3
Create and expose a custom views table in the new query schema
labkey-klum e4928dd
Display column for flag field
labkey-klum f081417
Remove old manageViews action, including the delete action which is n…
labkey-klum a1af478
Unit test for CustomViewsTable
labkey-klum 6dc5bc2
Selenium test for custom views query.
labkey-klum ea4913e
split flags field, replace legacy forms, use QUS for CRUD
labkey-klum b5e767f
Merge branch 'develop' into fb_customView_query
labkey-klum a45b6c7
Delete unused actions.
labkey-klum be3547e
Reports and Queries Tables.
labkey-klum 96ac8a2
Merge branch 'develop' into fb_customView_query
labkey-klum 172f326
automation for new tables.
labkey-klum 5d6c2dc
claude code feedback
labkey-klum f4095cf
Merge branch 'develop' into fb_customView_query
labkey-klum 54bd3b9
cross container delete
labkey-klum ddb40b0
cross folder issues
labkey-klum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,266 @@ | ||
| /* | ||
| * Copyright (c) 2008-2026 LabKey Corporation | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.labkey.core.query; | ||
|
|
||
| import org.apache.logging.log4j.Logger; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.jetbrains.annotations.Nullable; | ||
| import org.junit.AfterClass; | ||
| import org.junit.Assert; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.Test; | ||
| import org.labkey.api.collections.CaseInsensitiveHashMap; | ||
| 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.JdbcType; | ||
| import org.labkey.api.data.SQLFragment; | ||
| import org.labkey.api.data.TableInfo; | ||
| import org.labkey.api.query.BatchValidationException; | ||
| import org.labkey.api.query.DefaultQueryUpdateService; | ||
| import org.labkey.api.query.DetailsURL; | ||
| import org.labkey.api.query.ExprColumn; | ||
| import org.labkey.api.query.FieldKey; | ||
| import org.labkey.api.query.FilteredTable; | ||
| import org.labkey.api.query.QueryService; | ||
| import org.labkey.api.query.QueryUpdateService; | ||
| import org.labkey.api.query.column.BuiltInColumnTypes; | ||
| import org.labkey.api.reports.ReportService; | ||
| import org.labkey.api.reports.report.QueryReport; | ||
| import org.labkey.api.reports.report.ReportDescriptor; | ||
| import org.labkey.api.reports.report.ReportUrls; | ||
| import org.labkey.api.security.User; | ||
| import org.labkey.api.security.UserPrincipal; | ||
| import org.labkey.api.security.permissions.AdminPermission; | ||
| import org.labkey.api.security.permissions.DeletePermission; | ||
| import org.labkey.api.security.permissions.Permission; | ||
| import org.labkey.api.security.permissions.ReadPermission; | ||
| import org.labkey.api.util.ContainerContext; | ||
| import org.labkey.api.util.JunitUtil; | ||
| import org.labkey.api.util.PageFlowUtil; | ||
| import org.labkey.api.util.TestContext; | ||
| import org.labkey.api.util.logging.LogHelper; | ||
| import org.labkey.api.view.ActionURL; | ||
| import org.labkey.api.view.UnauthorizedException; | ||
| import org.labkey.api.writer.ContainerUser; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import static org.labkey.api.util.JunitUtil.deleteTestContainer; | ||
|
|
||
| public class ReportsTable extends FilteredTable<CoreQuerySchema> | ||
| { | ||
| public ReportsTable(@NotNull CoreQuerySchema userSchema, ContainerFilter cf) | ||
| { | ||
| super(CoreSchema.getInstance().getTableInfoReport(), userSchema, cf); | ||
|
|
||
| setName(CoreQuerySchema.REPORTS_TABLE_NAME); | ||
| setDescription("Contains a row for each report in the database. Available only to administrators."); | ||
|
|
||
| ReportUrls reportUrls = PageFlowUtil.urlProvider(ReportUrls.class); | ||
| ActionURL baseUrl = reportUrls.urlReportDetails(userSchema.getContainer(), null); | ||
| DetailsURL detailsURL = new DetailsURL(baseUrl, Map.of(ReportDescriptor.Prop.reportId.toString(), FieldKey.fromParts("RowId"))); | ||
| detailsURL.setContainerContext(new ContainerContext.FieldKeyContext(FieldKey.fromParts("ContainerId"))); | ||
| setDetailsURL(detailsURL); | ||
|
|
||
| wrapAllColumns(true); | ||
|
|
||
| var folderCol = getMutableColumnOrThrow(FieldKey.fromString("ContainerId")); | ||
| folderCol.setLabel("Folder"); | ||
| folderCol.setConceptURI(BuiltInColumnTypes.CONTAINERID_CONCEPT_URI); | ||
|
|
||
| getMutableColumnOrThrow("CreatedBy").setConceptURI(BuiltInColumnTypes.USERID_CONCEPT_URI); | ||
| getMutableColumnOrThrow("ModifiedBy").setConceptURI(BuiltInColumnTypes.USERID_CONCEPT_URI); | ||
| getMutableColumnOrThrow("ReportOwner").setConceptURI(BuiltInColumnTypes.USERID_CONCEPT_URI); | ||
|
|
||
| ColumnInfo flagsCol = getRealTable().getColumn("Flags"); | ||
| var hiddenCol = new ExprColumn(this, "Hidden", | ||
| new SQLFragment("(CASE WHEN (" + ExprColumn.STR_TABLE_ALIAS + ".Flags & " + ReportDescriptor.FLAG_HIDDEN + ") != 0") | ||
| .append(" THEN ").append(getSqlDialect().getBooleanTRUE()) | ||
| .append(" ELSE ").append(getSqlDialect().getBooleanFALSE()).append(" END)"), | ||
| JdbcType.BOOLEAN, flagsCol); | ||
| addColumn(hiddenCol); | ||
|
|
||
| var inheritableCol = new ExprColumn(this, "Inheritable", | ||
| new SQLFragment("(CASE WHEN (" + ExprColumn.STR_TABLE_ALIAS + ".Flags & " + ReportDescriptor.FLAG_INHERITABLE + ") != 0") | ||
| .append(" THEN ").append(getSqlDialect().getBooleanTRUE()) | ||
| .append(" ELSE ").append(getSqlDialect().getBooleanFALSE()).append(" END)"), | ||
| JdbcType.BOOLEAN, flagsCol); | ||
| addColumn(inheritableCol); | ||
|
|
||
| setDefaultVisibleColumns(List.of( | ||
| FieldKey.fromParts("ReportKey"), | ||
| FieldKey.fromParts("ContainerId"), | ||
| FieldKey.fromParts("Hidden"), | ||
| FieldKey.fromParts("Inheritable"), | ||
| FieldKey.fromParts("Created"), | ||
| FieldKey.fromParts("CreatedBy"), | ||
| FieldKey.fromParts("Modified"), | ||
| FieldKey.fromParts("ModifiedBy"), | ||
| FieldKey.fromParts("ReportOwner") | ||
| )); | ||
| } | ||
|
|
||
| @Override | ||
| protected String getContainerFilterColumn() | ||
| { | ||
| return "ContainerId"; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean hasPermission(@NotNull UserPrincipal user, @NotNull Class<? extends Permission> perm) | ||
| { | ||
| return (perm.equals(ReadPermission.class) || perm.equals(DeletePermission.class)) && getContainer().hasPermission(user, AdminPermission.class); | ||
| } | ||
|
|
||
| @Override | ||
| public QueryUpdateService getUpdateService() | ||
| { | ||
| return new ReportsUpdateService(this, CoreSchema.getInstance().getTableInfoReport()); | ||
| } | ||
|
|
||
| protected static class ReportsUpdateService extends DefaultQueryUpdateService | ||
| { | ||
| public ReportsUpdateService(TableInfo queryTable, TableInfo dbTable) | ||
| { | ||
| super(queryTable, dbTable); | ||
| } | ||
|
|
||
| @Override | ||
| protected Map<String, Object> deleteRow(User user, Container container, Map<String, Object> oldRowMap) | ||
| { | ||
| Integer id = (Integer) oldRowMap.get("rowId"); | ||
| Container c = getContainer(oldRowMap); | ||
| if (id != null && c != null) | ||
| { | ||
| var r = ReportService.get().getReport(c, id); | ||
| if (r != null) | ||
| { | ||
| if (r.hasPermission(user, c, DeletePermission.class)) | ||
| ReportService.get().deleteReport(ContainerUser.create(c, user), r); | ||
| else | ||
| throw new UnauthorizedException(String.format("You do not have permission to delete this report from folder : %s", c.getPath())); | ||
| } | ||
| } | ||
| return oldRowMap; | ||
| } | ||
|
|
||
| private @Nullable Container getContainer(Map<String, Object> row) | ||
| { | ||
| String containerId = (String) row.get("containerId"); | ||
| if (containerId != null) | ||
| return ContainerManager.getForId(containerId); | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
|
|
||
| public static class TestCase extends Assert | ||
| { | ||
| private static final Logger LOG = LogHelper.getLogger(ReportsTable.class, "Integration tests for the ReportsTable"); | ||
| private static User _user; | ||
| private static Container _container; | ||
|
|
||
| @BeforeClass | ||
| public static void setup() throws Exception | ||
| { | ||
| _container = JunitUtil.getTestContainer(); | ||
| _user = TestContext.get().getUser(); | ||
| } | ||
|
|
||
| @AfterClass | ||
| public static void cleanup() | ||
| { | ||
| deleteTestContainer(); | ||
| _container = null; | ||
| _user = null; | ||
| } | ||
|
|
||
| @Test | ||
| public void testReportsTableAdminOnlyAccess() | ||
| { | ||
| LOG.info("Validate Core.Reports is admin only"); | ||
|
|
||
| var schema = QueryService.get().getUserSchema(User.getAdminServiceUser(), _container, CoreQuerySchema.NAME); | ||
| assertNotNull("Expected admin access to the " + CoreQuerySchema.REPORTS_TABLE_NAME + " table", schema.getTable(CoreQuerySchema.REPORTS_TABLE_NAME)); | ||
|
|
||
| schema = QueryService.get().getUserSchema(User.getSearchUser(), _container, CoreQuerySchema.NAME); | ||
| assertNull("Expected admin access to the " + CoreQuerySchema.REPORTS_TABLE_NAME + " table", schema.getTable(CoreQuerySchema.REPORTS_TABLE_NAME)); | ||
| } | ||
|
|
||
| private QueryUpdateService ensureUpdateService(String tableName) | ||
| { | ||
| var schema = QueryService.get().getUserSchema(_user, _container, CoreQuerySchema.NAME); | ||
| var table = schema.getTable(tableName); | ||
| var qus = table.getUpdateService(); | ||
| assertNotNull("Expected update service for " + tableName, qus); | ||
|
|
||
| return qus; | ||
| } | ||
|
|
||
| @Test | ||
| public void testReportsApiAccess() throws Exception | ||
| { | ||
| var qus = ensureUpdateService(CoreQuerySchema.REPORTS_TABLE_NAME); | ||
|
|
||
| try | ||
| { | ||
| BatchValidationException errors = new BatchValidationException(); | ||
| Map<String, Object> row = CaseInsensitiveHashMap.of( | ||
| "reportKey", "foo/bar", | ||
| "hidden", true | ||
| ); | ||
| qus.insertRows(_user, _container, List.of(row), errors, null, null); | ||
| fail("Insert should have thrown UnauthorizedException"); | ||
| } | ||
| catch (UnauthorizedException e) | ||
| { | ||
| // expected | ||
| } | ||
|
|
||
| // Save a report through the service | ||
| var queryReport = ReportService.get().createReportInstance(QueryReport.TYPE); | ||
| var descriptor = queryReport.getDescriptor(); | ||
| descriptor.setReportName("custom query report"); | ||
|
|
||
| var identifier = ReportService.get().saveReportEx(ContainerUser.create(_container, _user), "reportKey", queryReport, true); | ||
| assertTrue("Unable to save a query report", identifier.getRowId() != 0); | ||
|
|
||
| var savedReport = identifier.getReport(ContainerUser.create(_container, _user)); | ||
| assertNotNull("Unable to retrieve a saved report", savedReport); | ||
|
|
||
| BatchValidationException errors = new BatchValidationException(); | ||
| Map<String, Object> row = CaseInsensitiveHashMap.of( | ||
| "rowId", savedReport.getDescriptor().getReportId().getRowId(), | ||
| "flags", 2 | ||
| ); | ||
|
|
||
| try | ||
| { | ||
| qus.updateRows(_user, _container, List.of(row), null, errors, null, null); | ||
| fail("Update should have thrown UnauthorizedException"); | ||
| } | ||
| catch (UnauthorizedException e) | ||
| { | ||
| // expected, delete the query | ||
| qus.deleteRows(_user, _container, List.of(row), null, null); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Totally fine for now. In the future, we might consider showing non-admins their own reports (and queries and views).