Control who sees what
Access in MatterData is declared, not programmed. You write the rule on the page, and the engine puts it into the SQL that reads the rows. There is no place in an app where you can forget the check, because there is no place where you write it.
This guide covers three levels: who can open a page, which rows they see, and which columns.
Restrict a page to a role
Section titled “Restrict a page to a role”Every page declares the role that can open it. A page with no rule admits nobody.
page members "Members" { route: /members access: board
report of members "All members" { columns: name, email, joined_on }}Two reserved role names can be written in a rule. public admits anyone, including a visitor
with no account. authenticated admits anybody who holds at least one role in this
application.
access: publicauthenticated is not “has a session”. An organization can hold several applications, and a
login with no role in yours is anonymous in yours. Read
An organization, its applications and one login.
Three more names are reserved and are refused in a rule and in a declaration: owner,
anonymous, and tenant_admin. See Reserved names.
A signed-in person whose role the page does not admit gets a 403. A person who is not signed in is sent to the sign-in page. A row that the reader may not see is different: it answers 404, the same as a row that does not exist. See Security model.
Show a person only their own rows
Section titled “Show a person only their own rows”Add where to the rule. The condition is about a row of the page’s table.
page my_notes "My notes" { route: /notes access { office author where owner = :user.id }
report of notes "Notes" { columns: title, released, updated_at }}The office sees every note. An author sees the notes she owns. Both use one declaration and one URL.
A login can hold several roles. Every rule that names one of them applies, and the login sees
the rows that any of those rules admits. A login holding office and author here sees
every note, because the office rule has no where.
There is no :user.role. A login can hold several roles, so a role has no single value to
compare with. Name the roles in the rule instead, one line per role.
:user.id is the signed-in user. The engine resolves it once per request, before it renders
anything.
The condition becomes part of the query. It is not a filter applied to rows that were already read, so a row that the rule excludes never leaves the database.
Reach through a reference
Section titled “Reach through a reference”A condition can walk a reference, up to four steps.
access { teacher where course.teacher = :user.teacher_id}:user.teacher_id is the row that the login’s teacher profile points at. A profile is set
only while the login holds a role that the profile serves, and it is empty otherwise, so the
rule admits no row. A login can have one row per profile: a teacher who also holds guardian
has both :user.teacher_id and :user.guardian_id, and each rule reads its own.
Narrow one region further
Section titled “Narrow one region further”A region can carry its own rule. The region’s rule narrows the page’s rule and can never widen it.
page overview "Overview" { route: /overview access: authenticated
report of notes "Public notes" { columns: title where: released }
form of notes "Add a note" { access: office fields: title, body actions: save }}An author opens the page and sees the report. The form is absent for her, and not refused. A body that names the form anyway stores nothing.
Two regions of one record page
Section titled “Two regions of one record page”A record page can hold several regions that show the one row of the page’s table: form,
detail, versions and compare. Each of them that has its own access … where adds its row
condition to the lookup of the row. The engine joins the conditions with AND. A region that
the reader may not open adds nothing.
Suppose a form admits office where owner = :user.person_id and a detail admits
office where released. An office reader is admitted to both regions. The reader therefore
needs a row that is owned by them and is released. For every other row, the whole page answers 404.
The compiler warns when it finds two such regions with different conditions for one role. A region
with no condition of its own does not cause the warning.
Give a region with a different row condition its own page. A report, list or stats region is
not part of this lookup, because each of them reads its rows in a separate query under its own
rule.
Let only some roles delete
Section titled “Let only some roles delete”actions: gives an action to everybody who can use the form. To give delete to fewer
roles, add a delete block:
page post "Post" { route: /posts/{id} access: admin, editor
form of posts { fields: title, body actions: save, delete delete { access: admin } }}An editor saves the post and sees no delete button. A delete that an editor sends anyway is
refused, and the post stays. A where filter limits the rows: editor where not published
lets an editor delete a post only until it is published. A sheet takes a delete and an add
block, and a form’s duplicate block takes the same access line. See
Action blocks.
Hide a column from a role
Section titled “Hide a column from a role”Mark the column sensitive. After that, only a role whose rule grants the column can read it.
table grades "Grades" { display: student
student ref(students) required "Student" points int sensitive required "Points" remark text sensitive "Remark"}page grade_sheet "Grades" { route: /grades access { head_teacher { grants: grades.points, grades.remark } teacher where course.teacher = :user.teacher_id { grants: grades.points } }
report of grades "Grades" { columns: student, points, remark }}The head teacher reads both columns. The teacher reads the points and not the remark. For the teacher the remark is absent: no cell, no heading, and no name that a request can use.
The same grant decides writing. A form renders a control, and a sheet an editable cell, for a
granted column only. A request that names an ungranted column is ignored and logged.
A column derived from a sensitive column must be marked sensitive too. The compiler
refuses one that is not, because an aggregate over one row is that row’s value.
Name a set of rules once
Section titled “Name a set of rules once”When many pages admit the same roles, give the list a name with a right, and name the right
on each page.
right run_events "Run events" { board trainer}
page events "Events" { route: /events access: run_events
report of events "Events" { columns: starts_at, title }}The compiler writes the right’s rules out on every page that names it. A page can name rights
and roles together: access: run_events, treasurer.
A right can carry filters and grants, as an access block can. A filter is about the rows of
one table, so a right with a filter says which table with of, and can be named only where the
rule is about that table:
right work_on_posts of posts "Work on posts" { admin editor author where author = :user.person_id}Two sources that give one role different rules are a compile error, because the role would see more than either rule allows. For the full syntax and every refusal, read Access rules and rights.
Rules that apply everywhere
Section titled “Rules that apply everywhere”These hold for every app, and you do not write them.
- A menu shows only the pages that the reader can open. A
groupin a menu whose pages the reader cannot open is left out, with its label. See Navigation. - A row that the reader cannot see returns the same 404 as a row that does not exist.
- The row rule applies to writes. An invisible row cannot be updated or deleted.
- The row rule applies to exports. A CSV file contains the rows the reader can see.
- Every form that changes data carries a CSRF token.
- SQL is always parameterized. A column name that arrives in a request goes through an allowlist.
Make sure that the rules are right
Section titled “Make sure that the rules are right”Access is asserted by a generated matrix over every route and every role, including anonymous. A new route with no rule fails that test. When you change a rule, the matrix is where you see what changed.
For what the engine enforces around your rules, and the three places it needs your discipline, read Security model.