Skip to content

feat: add the endpoint to check a custom role name - #666

Merged
rodmgwgu merged 1 commit into
mainfrom
feat/custom-role-name-validation
Dec 2, 2026
Merged

rodmgwgu merged 1 commit into
mainfrom
feat/custom-role-name-validation

Conversation

@rodmgwgu

@rodmgwgu rodmgwgu commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Part of: #571
Needed for openedx/frontend-app-admin-console#293 (comment)

Problem

The create custom role wizard asks for the name on step 1 but only learns that the name is taken from the 409 of POST /api/authz/v1/roles/ on the last step, after the administrator has picked the permissions. No endpoint checks a name on its own, and GET /api/authz/v1/roles/?search= can't do it: it matches part of the translated name in the requested scope types, while the ADR 0036 rule compares the whole stored name across every resource type.

Approach

  • Added POST /api/authz/v1/roles/validate-name/ with {"display_name": "..."}. It always returns 200 with display_name (as sent), available, and reason (taken, invalid or null) and message. The shape follows POST /users/validate/, which the assign role wizard already uses.
  • Added api.validate_custom_role_name, which applies the name rules of create_custom_role without writing anything. Both share _clean_display_name, so they can't drift apart.
  • Limited to staff and superusers through PrivilegedWritePermission, as for create.
  • The check is not a reservation. Create still returns 409 if another role takes the name first, so the wizard must keep handling it.
  • Documented in a dated addendum to ADR 0036.

Manual testing instructions

As a staff user in the LMS:

  1. POST /api/authz/v1/roles/validate-name/ with {"display_name": "Library Designer"}. It returns available: true.
  2. POST with the stored name of a static role in another case, e.g. {"display_name": "LIBRARY AUTHOR"}. It returns available: false, reason: "taken".
  3. POST with {"display_name": " "}. It returns available: false, reason: "invalid".
  4. As a library admin who isn't staff, repeat step 1. It returns 403.

Unit tests: pytest src/openedx_authz/tests/api/test_custom_roles.py src/openedx_authz/tests/rest_api/test_views.py -k "TestValidateCustomRoleName or TestRoleNameValidationView".

Rollback plan

Revert this PR. No migration and no stored data.

Retro compatibility

No existing endpoint changes. create_custom_role behaves the same.

AI Usage

🤖 This change was made with assistance from Kiro, with human guidance on the decisions and validation against ADR 0036.


Merge checklist:

@openedx-webhooks openedx-webhooks added open-source-contribution PR author is not from Axim or 2U core contributor PR author is a Core Contributor (who may or may not have write access to this repo). labels Nov 27, 2026
@openedx-webhooks

openedx-webhooks commented Nov 27, 2026 •

Copy link
Copy Markdown

Thanks for the pull request, @rodmgwgu!

This repository is currently maintained by @openedx/committers-openedx-authz.

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.

Details
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.

@github-project-automation github-project-automation Bot moved this to Needs Triage in Contributions Nov 27, 2026
@rodmgwgu
rodmgwgu force-pushed the feat/custom-role-name-validation branch from b08e9a1 to 8add488 Compare November 27, 2026 21:42
@rodmgwgu
rodmgwgu added this pull request to stack #646 November 27, 2026 21:50
@rodmgwgu
rodmgwgu marked this pull request as ready for review November 27, 2026 21:50
@rodmgwgu
rodmgwgu force-pushed the feat/custom-role-name-validation branch from 8add488 to 80c0739 Compare November 27, 2026 21:51
@rodmgwgu
rodmgwgu force-pushed the feat/custom-role-name-validation branch from 80c0739 to db743ca Compare November 30, 2026 15:20
@rodmgwgu
rodmgwgu force-pushed the feat/custom-role-name-validation branch 2 times, most recently from 5e09b35 to 05fea51 Compare November 30, 2026 16:22

@jacobo-dominguez-wgu jacobo-dominguez-wgu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Tested locally on the admin console ui and postman and works as expected!

@BryanttV BryanttV left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks @rodmgwgu!

Two non-blocking notes on the name rules:

  1. (pre-existing) str.strip() keeps U+200B, so a zero-width space is reported as available. Names also aren't Unicode-normalized. Could we reject names with no visible characters and normalize to NFC, without banning joiners used by emoji and some scripts? A U+200B inside a name would need separate handling. Fine as a follow-up.

  2. (nit) DRF's CharField coerces numbers, so {"display_name": 123} returns 200 with "123", but the ADR says non-text values return 400 and the name is returned as sent. Since create also coerces, could we update the ADR (or use a strict string field in both) and add a numeric test case?

@rodmgwgu
rodmgwgu force-pushed the feat/custom-role-name-validation branch 2 times, most recently from 71e4a56 to 44333de Compare November 30, 2026 22:22
@rodmgwgu

Copy link
Copy Markdown
Contributor Author

Thanks @rodmgwgu!

Two non-blocking notes on the name rules:

  1. (pre-existing) str.strip() keeps U+200B, so a zero-width space is reported as available. Names also aren't Unicode-normalized. Could we reject names with no visible characters and normalize to NFC, without banning joiners used by emoji and some scripts? A U+200B inside a name would need separate handling. Fine as a follow-up.
  2. (nit) DRF's CharField coerces numbers, so {"display_name": 123} returns 200 with "123", but the ADR says non-text values return 400 and the name is returned as sent. Since create also coerces, could we update the ADR (or use a strict string field in both) and add a numeric test case?

Thanks @BryanttV, I applied both fixes in this PR.

@rodmgwgu
rodmgwgu force-pushed the feat/custom-role-name-validation branch from 44333de to 1a72398 Compare November 30, 2026 23:13

@BryanttV BryanttV left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for addressing my comments, LGTM!

@rodmgwgu
rodmgwgu force-pushed the feat/custom-role-name-validation branch 2 times, most recently from 7a4d57d to 9621955 Compare December 2, 2026 16:55
Base automatically changed from feat/custom-role-read-remove to main December 2, 2026 18:21
@rodmgwgu
rodmgwgu force-pushed the feat/custom-role-name-validation branch from 9621955 to f0f7d61 Compare December 2, 2026 18:21
@rodmgwgu
rodmgwgu merged commit 640fe45 into main Dec 2, 2026
7 checks passed
@rodmgwgu
rodmgwgu deleted the feat/custom-role-name-validation branch December 2, 2026 18:34
@github-project-automation github-project-automation Bot moved this from Needs Triage to Done in Contributions Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core contributor PR author is a Core Contributor (who may or may not have write access to this repo). open-source-contribution PR author is not from Axim or 2U

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants