Tutorial: buttons and pages per role
In this tutorial some users see buttons, pages and menu entries that others don't. You give users roles, define authorization schemes that check them, and attach the schemes to components. Then you make the database agree, with row level security.
It builds on the Tasks app of the Tasks tutorial (ann and bob, the My tasks report on page 1 and the form on page 2). Run examples/tasks-app.sql if you don't have it yet. Conditions on the data (a button only while a task is open) are in the previous tutorial; this one is about who the user is.
The plan:
| Who | May |
|---|---|
| everyone | see and edit their own tasks |
lead | also see everyone's tasks on a page All tasks |
admin | also delete tasks; sees a Delete that others don't |
Step 1: give users roles
Roles are free text per application. Open Shared Components → Access control of Tasks, and set the roles:
| Account | Roles |
|---|---|
ann | lead, admin |
bob | (none) |
(Or from the account: Users → ann → Application access.) The builder lists the roles the application checks under the roles field; until you create the schemes below that list is empty.
Roles are read at sign-in. Changing someone's roles ends their sessions in that application, so the change applies at their next sign-in.
In SQL, for a migration or a script:
update meta.app_access x set roles = '{lead,admin}'
from meta.app a, meta.account u
where x.app_id = a.id and x.account_id = u.id and a.alias = 'tasks' and u.username = 'ann';Step 2: authorization schemes
An authorization scheme is a named rule. Under Shared Components → Authorization schemes create two:
| Name | Type | Value | Error message |
|---|---|---|---|
ADMIN | role | admin | Only administrators can do this. |
LEAD | sql | meta.has_role('lead') or meta.has_role('admin') | Only team leads can see all tasks. |
A role scheme passes when the user has that role; a sql scheme passes when the expression is true, so it can combine roles or look at data. Names are capitals, digits and _. Each scheme is evaluated at most once per request.
insert into meta.authz_scheme (app_id, name, type, value, error_message)
select a.id, x.name, x.type, x.value, x.msg from meta.app a, (values
('ADMIN', 'role', 'admin', 'Only administrators can do this.'),
('LEAD', 'sql', 'meta.has_role(''lead'') or meta.has_role(''admin'')', 'Only team leads can see all tasks.')
) x(name, type, value, msg) where a.alias = 'tasks';Step 3: a button only administrators see
On page 2 select the DELETE button and set Authorization (in its Security group) to ADMIN.
Sign in as bob and open a task: no Delete. Sign in as ann: Delete is there. The button keeps its condition (:P2_ID is not null); a button is shown only when both its condition and its authorization pass.
A hidden button is also a refused button: when someone without the role sends the DELETE request anyway (a saved URL, the developer tools), pgkiln answers 403 and logs it. You don't need to repeat the check in a process.
Want the opposite, a button only for users without a role? Prefix the scheme with !: !ADMIN shows a Request deletion button to everyone except administrators.
Step 4: a page and its menu entry for leads
Create a page 3 All tasks with a report on tasks.task (Create page → Report), with the source:
select owner, title, priority, due_date, done from tasks.task order by owner, done, due_date nulls lastSet the page's Authorization to LEAD. Now:
bobdoesn't see All tasks in the menu: navigation entries and links to a page the user may not open are hidden;- if
bobtypes/a/tasks/3, he gets a 403 page with the scheme's error message Only team leads can see all tasks.
The same authz property exists on regions, items, processes, dynamic actions, navigation entries and menu button entries. What happens when the scheme fails:
| On | Effect |
|---|---|
| Page | 403 page with the error message; links and menu entries to it are hidden |
| Region | Not rendered, with its items and buttons |
| Item | Not rendered, and can't be set by a submit |
| Button | Not rendered, and can't be pressed |
| Process | Skipped |
| Dynamic action | Not sent to the browser, and refused by the server |
Step 5: the database must agree
Open All tasks as ann: it only shows her own tasks. That is the row level security policy of the Tasks tutorial (owner = meta.app_user()): the page is allowed, but the database still decides which rows exist for this user. That is how it should be: an authorization scheme protects the screen, a policy protects the data. Let leads read every task, and keep writing to the owner:
drop policy own_tasks on tasks.task;
create policy read_tasks on tasks.task for select
using (owner = meta.app_user() or meta.has_role('lead') or meta.has_role('admin'));
create policy insert_tasks on tasks.task for insert
with check (owner = meta.app_user());
create policy update_tasks on tasks.task for update
using (owner = meta.app_user()) with check (owner = meta.app_user());
create policy delete_tasks on tasks.task for delete
using (owner = meta.app_user() or meta.has_role('admin'));meta.has_role() sees the same roles as the authorization schemes, in every query the application runs. Put this in a migration of your own project, as the Tasks tutorial advises.
Now ann sees everyone's tasks on page 3, an administrator can delete any task, and bob can't delete one even if he found a way to send the request: the database refuses it.
Step 6: visible to all, but grey for some
Sometimes users should see that a button exists without being able to use it (Approve for a clerk). Keep the button visible to everyone, grey it out in the browser for users without the role, and enforce the role on the server in the process:
- A dynamic action on page 2: event
load, actiondisable, affected itemsCOMPLETE, and the Server-side condition typesqlwithnot meta.has_role('lead'). The dynamic action is only on the page for users without the role. - The process that the button runs (Complete task): Authorization
LEAD. A user without the role who enables the button with the developer tools only gets the page back: the process is skipped.
Usually hiding (step 3) is the clearer choice; grey out when users ask why they can't find the button.
Where to go next
- Roles from your identity provider's groups: map them under Shared Components → Access control → Identity-provider groups → roles (chapter 8).
- A second factor before a sensitive page: a scheme with Second step within (minutes) (step-up).
- Users per table column and value: data security per table.
- The HR example has the schemes
ADMINandMANAGER(examples/hr/hr.sql): page 3's Give 10% raise is only for managers, Delete only for administrators.