Skip to content

Conversation

@rodmgwgu
Copy link

@rodmgwgu rodmgwgu commented Nov 20, 2025

Description

Related bug: openedx/openedx-authz#139

Adds validating publish permissions based on the new openedx-authz model.

Hook implementation mostly copied from frontend-app-admin-console

Context:

The new openedx-authz authorization model adds the possibility to have fine-grained permissions for different roles in the system. Currently, the MVP involves implementing these new roles over the Content Libraries only.

The new roles are:

  • Library Admin
  • Library Author
  • Library Contributor
  • Library User

The specific permissions for these roles can be found here.

The enforcement mechanism for MFEs as implemented in this PR is described here.

This change mainly concerns the new "Library Contributor" role, which should have permission to edit library content, but not to publish it. This was not taken into account on the previous permission model, because before, anyone with write permissions could publish. This change makes it possible to disable the Publish button for "Library Contributors" by using the new enforcement mechanism.

Screenshots:

Before changes, as a "Library Contributor" user, the Publish button was shown, but the action failed due to lack of permissions:

512953087-074bd6a3-5771-4e51-8bd4-027035ef96fa

After changes, the Publish button is hidden:

Screenshot 2025-11-20 at 4 37 28 p m

Supporting information

Project: RBAC AuthZ

Related bug: openedx/openedx-authz#139

The enforcement mechanism for MFEs as implemented in this PR is described here.

Testing instructions

  1. Create a new non-staff user via tutor with tutor dev do createuser nonstaff [email protected]
  2. Log in as an admin user to Studio
  3. Create a new Content Library (non-legacy Library)
  4. Add the "nonstaff" user to the Library team, as a "Library Contributor"
Screenshot 2025-11-24 at 10 10 13 a m
  1. In an incognito window, log in to studio as the "nonstaff" user
  2. Go to the direct url for the library details page
  3. You should be able to see the Library details page, but the Publish button should not be shown
Screenshot 2025-11-20 at 4 37 28 p m

Other information

I implemented the validateUserPermissions hook and it's related code as an independent module in src/authz, with the idea of externalizing it as a library in the near future, perhaps as a part of frontend-base?

Because of that, I'm not using the helpers in src/testUtils.tsx on the hook tests.

Concern: I'm mocking the validateUserPermissions API call (used by the validateUserPermissions hook) on the initializeMocks util, so it doesn't interfere with several tests that are checking against axios mocks without validating the request url. I'm not sure if this is the best practice or if there is a better way?

Best Practices Checklist

We're trying to move away from some deprecated patterns in this codebase. Please
check if your PR meets these recommendations before asking for a review:

  • Any new files are using TypeScript (.ts, .tsx).
  • Avoid propTypes and defaultProps in any new or modified code.
  • Tests should use the helpers in src/testUtils.tsx (specifically initializeMocks)
  • Do not add new fields to the Redux state/store. Use React Context to share state among multiple components.
  • Use React Query to load data from REST APIs. See any apiHooks.ts in this repo for examples.
  • All new i18n messages in messages.ts files have a description for translators to use.
  • Avoid using ../ in import paths. To import from parent folders, use @src, e.g. import { initializeMocks } from '@src/testUtils'; instead of from '../../../../testUtils'

@openedx-webhooks
Copy link

openedx-webhooks commented Nov 20, 2025

Thanks for the pull request, @rodmgwgu!

This repository is currently maintained by @bradenmacdonald.

Once you've gone through the following steps feel free to tag them in a comment and let them know that your changes are ready for engineering review.

🔘 Get product approval

If you haven't already, check this list to see if your contribution needs to go through the product review process.

  • If it does, you'll need to submit a product proposal for your contribution, and have it reviewed by the Product Working Group.
    • This process (including the steps you'll need to take) is documented here.
  • If it doesn't, simply proceed with the next step.
🔘 Provide context

To help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:

  • Dependencies

    This PR must be merged before / after / at the same time as ...

  • Blockers

    This PR is waiting for OEP-1234 to be accepted.

  • Timeline information

    This PR must be merged by XX date because ...

  • Partner information

    This is for a course on edx.org.

  • Supporting documentation
  • Relevant Open edX discussion forum threads
🔘 Get a green build

If one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green.


Where can I find more information?

If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources:

When can I expect my changes to be merged?

Our goal is to get community contributions seen and reviewed as efficiently as possible.

However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:

  • The size and impact of the changes that it introduces
  • The need for product review
  • Maintenance status of the parent repository

💡 As a result it may take up to several weeks or months to complete a review and merge your PR.

@codecov
Copy link

codecov bot commented Nov 24, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.86%. Comparing base (ef36156) to head (129e771).
⚠️ Report is 7 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2685      +/-   ##
==========================================
+ Coverage   94.85%   94.86%   +0.01%     
==========================================
  Files        1232     1237       +5     
  Lines       27899    27964      +65     
  Branches     6316     6152     -164     
==========================================
+ Hits        26464    26529      +65     
- Misses       1364     1377      +13     
+ Partials       71       58      -13     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mphilbrick211 mphilbrick211 added the mao-onboarding Reviewing this will help onboard devs from an Axim mission-aligned organization (MAO). label Nov 25, 2025
@mphilbrick211 mphilbrick211 moved this from Needs Triage to Waiting on Author in Contributions Nov 25, 2025
@rodmgwgu rodmgwgu changed the title draft: feat: Implement querying openedx-authz for publish permissions feat: Implement querying openedx-authz for publish permissions Nov 25, 2025
@rodmgwgu rodmgwgu marked this pull request as ready for review November 25, 2025 16:13
Copy link

@MaferMazu MaferMazu left a comment

Choose a reason for hiding this comment

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

Thanks @rodmgwgu, I tested in a Ulmo env, and it works as expected! ✨

The code looks good to me, but I would prefer someone with more frontend skills to help with a review as well.

Before
image
After
image

import { validateUserPermissions } from './api';

const adminConsoleQueryKeys = {
all: [appId] as const,
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure if the appId value follows the same pattern used for query keys, i have see mostly camelCase values.

Copy link
Author

Choose a reason for hiding this comment

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

changed, thanks!

const permissions = LIBRARY_PERMISSIONS.map(action => ({ action, scope: libraryId }));

const { isLoading: isLoadingUserPermissions, data: userPermissions } = useValidateUserPermissions(permissions);
const canPublish = userPermissions ? userPermissions[0]?.allowed : false;

Choose a reason for hiding this comment

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

Something we could improve is being explicit about the action we are requesting instead of using userPermissions[0].

Copy link
Author

Choose a reason for hiding this comment

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

According to the ADR, the API guarantees that the order of the response will match the requested permissions, that's why I'm not trying to match it explicitly.

Copy link

@MaferMazu MaferMazu Nov 28, 2025

Choose a reason for hiding this comment

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

I didn't know about the order 🤔

The suggestion was more about readability and clarity regarding the permission I am requesting, and I still think it is important. Since that index depends on the order of the elements in LIBRARY_PERMISSIONS, if that list grows, I don't think it will be clear enough to use only the indexes.

Copy link
Contributor

Choose a reason for hiding this comment

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

+1. Also, hard-coding 0 here means that this code would become wrong if someone else changed the order of the LIBRARY_PERMISSIONS constant. Which could definitely happen, and then there would be a security hole.

Choose a reason for hiding this comment

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

I am thinking about this issue: openedx/openedx-authz#144. I haven't refined it yet, but I would probably need to add more params to the request to see other permissions.

Copy link
Contributor

Choose a reason for hiding this comment

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

I would suggest an API like this (no change to the REST API or the internal arrays, just implement some helper logic in the hook to support this):

const possiblePermissions = {
  canPublish: CONTENT_LIBRARY_PERMISSIONS.PUBLISH_LIBRARY_CONTENT,
];

const {
  isLoading: isLoadingUserPermissions,
  data: userPermissions,
} = useScopedUserPermissions(possiblePermissions, { scope: libraryId });

// API is useScopedUserPermissions(actions object, extra fields to mix in);

const canPublish = userPermissions?.canPublish;
// or
const canPublish = userPermissions?.canPublish.allowed; // (this is more verbose, and requiring these creates security bugs whenever users forget to include `.allowed`, but if you know there will likely be other fields besides .allowed in the future, it's better to be more verbose now)

@MaferMazu MaferMazu linked an issue Nov 25, 2025 that may be closed by this pull request
@rodmgwgu rodmgwgu force-pushed the rod/authz-publish-permissions branch from a501e16 to f7c566f Compare November 25, 2025 22:59
@bradenmacdonald
Copy link
Contributor

I implemented the validateUserPermissions hook and it's related code as an independent module in src/authz, with the idea of externalizing it as a library in the near future, perhaps as a part of frontend-base?

Because of that, I'm not using the helpers in src/testUtils.tsx on the hook tests.

Sounds good. I think it would be great to have this in frontend-base.

Concern: I'm mocking the validateUserPermissions API call (used by the validateUserPermissions hook) on the initializeMocks util, so it doesn't interfere with several tests that are checking against axios mocks without validating the request url. I'm not sure if this is the best practice or if there is a better way?

Your approach seems fine, but we shouldn't have tests that break just because another axios call/mock was introduced. Were there many such cases? Usually axios mocks only mock one specific endpoint, and the asserts should be made on that same endpoint-specific mock.

* if (data[0].allowed) { ... }
*
*/
export const useValidateUserPermissions = (
Copy link
Contributor

Choose a reason for hiding this comment

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

This is minor/optional feedback about the name of this hook:

To me, "validate user permissions" sounds like an action, like it would throw an exception if the user doesn't have some permissions. But this is just fetching some data, not making an action.

I think "useScopedPermissions" or just "useUserPermissions" or something like that would better reflect that this is just getting the user permissions, but you still have to validate/check that they're allowed or not yourself.

Comment on lines +19 to +20
const LIBRARY_PERMISSIONS = [
CONTENT_LIBRARY_PERMISSIONS.PUBLISH_LIBRARY_CONTENT,
Copy link
Contributor

Choose a reason for hiding this comment

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

LIBRARY_PERMISSIONS and CONTENT_LIBRARY_PERMISSIONS are basically the same name, so it's not very clear how they are different.

const permissions = LIBRARY_PERMISSIONS.map(action => ({ action, scope: libraryId }));

const { isLoading: isLoadingUserPermissions, data: userPermissions } = useValidateUserPermissions(permissions);
const canPublish = userPermissions ? userPermissions[0]?.allowed : false;
Copy link
Contributor

Choose a reason for hiding this comment

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

I would suggest an API like this (no change to the REST API or the internal arrays, just implement some helper logic in the hook to support this):

const possiblePermissions = {
  canPublish: CONTENT_LIBRARY_PERMISSIONS.PUBLISH_LIBRARY_CONTENT,
];

const {
  isLoading: isLoadingUserPermissions,
  data: userPermissions,
} = useScopedUserPermissions(possiblePermissions, { scope: libraryId });

// API is useScopedUserPermissions(actions object, extra fields to mix in);

const canPublish = userPermissions?.canPublish;
// or
const canPublish = userPermissions?.canPublish.allowed; // (this is more verbose, and requiring these creates security bugs whenever users forget to include `.allowed`, but if you know there will likely be other fields besides .allowed in the future, it's better to be more verbose now)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mao-onboarding Reviewing this will help onboard devs from an Axim mission-aligned organization (MAO). open-source-contribution PR author is not from Axim or 2U

Projects

Status: Waiting on Author

Development

Successfully merging this pull request may close these issues.

Bug: Hide publish library button in Studio.

6 participants