Role-Gated Endpoints

Hard ⏱ 18 min 31% acceptance ★★★★★ 4.9
Write the decorator factory require_role(role) for functions whose first argument is a user dict with a 'roles' list: the wrapper raises PermissionError unless role is present, else delegates. @require_role('admin') then guards any endpoint-like function.

Examples

Example 1
Input
delete_all({'roles': ['admin']})
Output
runs normally
Explanation

The role check passed.

Example 2
Input
delete_all({'roles': ['viewer']})
Output
PermissionError
Explanation

Missing role blocks the call.

Constraints

  • Factory + decorator + wrapper structure.
  • The user dict is args[0].

Topics

Decoratorsauthorization

Companies

OktaAuth0Salesforce

Hints

Hint 1

role in user.get('roles', []).

Hint 2

Raise before delegating.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.