Fix the authorization table for org roles
Your task
An agent built your app's authorization middleware from a one-paragraph spec: admins can edit any org, members can edit only their own org, viewers are read only. The demo works, but demos only show what is allowed. So you write the spec out as a full table, including the cases it implies, and check every cell.
An assistant wrote authorization_matrix(roles, actions, same_org_flags), which decides each cell. It is in the editor. It has a bug. Find it and fix it.
Input and output:
roles,actions,same_org_flags: one request per position: the caller's role, the action, and whether the resource belongs to the caller's own org.- Return one true (allow) or false (deny) per entry of
roles, in order.
Rules the tests check:
- If
actionsorsame_org_flagshas no entry at a position, deny that request. - Compare roles and actions with whitespace trimmed from both ends, ignoring case.
- Only the actions
readandeditcan ever be allowed. Any other action, an empty one included, is denied. admin: may read and edit in any org.member: may read and edit only in their own org.viewer: may read only, and only in their own org.- Any other role, an empty one included, is denied.
Examples
authorization_matrix(roles: list[str], actions: list[str], same_org_flags: list[bool]) -> list[bool]Example 1
Input
authorization_matrix(["admin", "member", "viewer"], ["edit", "edit", "edit"], [False, True, True])Output
[True, True, False]Example 2
Input
authorization_matrix(["member", "viewer", "ghost"], ["edit", "read", "read"], [False, True, True])Output
[False, True, False]Example 3
Input
authorization_matrix(["admin"], ["delete"], [True])Output
[False]