Skip to content
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

Feat: add endpoint to check user access for sample and proposal #1327

Merged

Conversation

Junjiequan
Copy link
Contributor

@Junjiequan Junjiequan commented Jul 22, 2024

Description

This pull request introduces new endpoints to check user access for specific samples and proposals. It includes updates to the authorization mechanisms and related test cases to ensure proper access control based on user roles and permissions.

Additional:

  1. Added configurable test database environment variable is added for local testing.
  2. Replaced deprecated casl-factory AbilityBuilder with updated one.

Motivation

When a user opens a dataset detail page, the frontend makes requests to fetch the Sample and Proposal attached to the dataset, regardless of whether the user has permission to access them. By utilizing user access endpoints, we can pre-check if the user has access to the Sample or Proposal before making subsequent requests. This can reduce the collection of FALSY errors.

Changes

New Endpoints:

  • Added /samples/:id/authorization and /proposals/:pid/authorization endpoints to check if the user has access to specific samples and proposals.

Authorization Updates:

  • Modified CASL ability factory to include updated types and conditions for checking access permissions.
  • Updated policies guards to handle access control for the new endpoints.

Tests:

  • Added test cases for new endpoints in ProposalAuthorization.js and SampleAuthorization.js to validate access control for different user roles.

Tests included

  • Included for each change/fix?
  • Passing? (Merge will not be approved unless this is checked)

Documentation

  • swagger documentation updated [required]
  • official documentation updated [nice-to-have]

official documentation info

If you have updated the official documentation, please provide PR # and URL of the pages where the updates are included

@Junjiequan Junjiequan force-pushed the SWAP-4091-scicat-be-create-a-permission-check-endpoint-for branch from 07839f0 to d723ac9 Compare July 22, 2024 16:57
@Junjiequan Junjiequan changed the title Swap 4091 scicat be create a permission check endpoint for Feat: add endpoint to check user access for sample and proposal Jul 23, 2024
@Junjiequan
Copy link
Contributor Author

The decision on whether to allow users to update the isPublished status of proposals requires further investigation. In the proposalAuthorization test file, test cases for public proposals are temporarily commented out.

Copy link
Contributor

@nitrosx nitrosx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check my comments and let me know if we need to discuss

.env.example Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
src/proposals/proposals.controller.ts Outdated Show resolved Hide resolved
src/samples/samples.controller.ts Outdated Show resolved Hide resolved
test/ProposalAuthorization.js Outdated Show resolved Hide resolved
test/SampleAuthorization.js Outdated Show resolved Hide resolved
test/SampleAuthorization.js Outdated Show resolved Hide resolved
test/SampleAuthorization.js Outdated Show resolved Hide resolved
test/SampleAuthorization.js Outdated Show resolved Hide resolved
Comment on lines 6 to 7
const testDbName = process.env.MONGODB_TEST || "scicat-test";
const uri = `mongodb://localhost:27017/${testDbName}`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what is the correct solution, but I think that have a separate variable to set the dataset in testing will lead to confusion.
I think we should rely only on the value MONGODB_URI. If you are testing locally, you will change the value of MONGODB_URI according in order to use your local database.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should try to avoid relying on MONGODB_URI for testing.
If you by accident, run the test locally without changing the MONGODB_URI the test will wipe off all the local DB data

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your point, but by using the two environmental variables, we are forcing developers to change the database name in two places instead of one.
Also I feel that we are not minimizing the risk to wipe a local DB data. It is still very much present.

Copy link
Contributor Author

@Junjiequan Junjiequan Jul 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest to just hardcode test db as scicat-test-db in this file like the appUrl

Copy link
Contributor

@nitrosx nitrosx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Waiting for the changes on pretest

@nitrosx nitrosx merged commit 6acd472 into master Jul 24, 2024
7 checks passed
@nitrosx nitrosx deleted the SWAP-4091-scicat-be-create-a-permission-check-endpoint-for branch July 24, 2024 10:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants