Fine-grained authorization
Why the workspace is the single place which decides what each user may see and do across all data sources - locales, layers, rows, fields, themes and editing - through roles and template scopes; read before designing roles or a workspace for restricted data.
Resource and sources
compose../../apps/xyz/mod/workspace/composeObj.jsauthorization../../apps/xyz/mod/workspace/authorization.jsquery../../apps/xyz/mod/query.jsgetlayer../../apps/xyz/mod/workspace/getLayer.js- ../../apps/xyz/mod/workspace/templates/location_get.js
- ../../apps/xyz/tests/mod/workspace/composeObj.test.mjs
- ../../apps/xyz/tests/mod/query.test.mjs
The workspace is the core of XYZ. It is one JSON document that declares every locale, layer, query template, infoj entry, theme and edit control an instance offers. Data at rest may sit in several databases or behind other services. Users never reach that data directly; they reach it only through the objects the workspace declares. Authorization is therefore defined once, in the workspace, rather than separately in each data source.
The XYZ API composes the workspace for each request and each user. Locales and layers are merged from templates, and whatever the user's roles do not allow is removed before the response leaves the server.[1] The client cannot ask for an object, field or query which composition removed for its user.
What a role controls
| Level | Workspace key | Effect for users without the role |
|---|---|---|
| Locale | locale.roles, locale.role | The locale is not listed and cannot be loaded. |
| Layer | layer.roles | The layer is not in the locale; its queries fail. |
| Template | role on a layer, locale or query template | The template is not merged; its content never reaches the user. |
| Rows | a role's filter.default | Layer queries whose template contains ${filter} have the filter added; the request cannot remove it. |
| Fields | roles on an infoj entry | The entry is removed, and location_get does not select its field. |
| Features | a role object merged into the layer | Themes, dataviews, entries or edit settings exist only for the role. |
| Admin queries | admin on a query template | Only admin users may run the query. |
Roles come from the user: the ACL or a login provider sets user.roles, see authentication. How roles objects gate and merge is described in roles; how scopes are decided, and how an authorization provider such as OpenFGA can replace the decision, in workspace authorization.
Scopes and roles objects
Two mechanisms work together:
- Template scopes describe who may use a template. A template with
roleadds that role to the scope of everything merged into it, and nested templates join their roles with dots, eg.client.region. Use scopes for an organisation's structure: a user withclient.regiongets the templates scopedclientandclient.region, but not those of another region. - Roles objects on a locale, layer or entry gate that object, or merge configuration for a role, eg. an extra infoj entry, a role filter or a theme. Use them to vary one object between user groups.
One layer template can serve several user groups this way. Each group sees its own rows, fields and themes, without the layer being copied.
How a request is checked
- The request is authenticated and the user, with their roles, is assigned to the request params.
getLocaleandgetLayercompose the locale or layer for the user and return an error when the user may not access it.[2]- The query API runs only named query templates. A query naming a layer composes that layer for the user first. A
tableparam must be one of the layer's tables. The layer'sfilter.defaultis added to templates which contain${filter}; the location templates (location_get,location_update,location_delete,location_new,location_field_value),mvt_geom,sql_table_insertandtable_schemahave none.location_getselects only the fields of the user's composed infoj.[3] - The query runs on the
dbsconnection of the template, the layer or the workspace, in that order. Different layers can read different databases under the same authorization rules.
Designing for restricted data
- Set
PRIVATEfor internal data. Without it, anonymous users receive every locale and layer which has noroles; see security practices. - Restrict rows with a role
filter.defaultand fields with entryroles; both are enforced by the server, not only hidden in the UI. A role filter applies only to templates containing${filter}: a user who knows an id can still get that location, and change or delete it where the layer is editable. Restrict those with layerroles, entryrolesand database privileges; see filter expressions. - Give editing its own layer, gated by an editor role, and a
DBS_*connection whose database role can only change the tables and columns that layer edits. Database privileges are the last line of defence for data at rest. - Keep sensitive SQL in query templates with a
roleoradmin, never in the client. - Treat the workspace as code: whoever can change it decides every user's access.
Pitfalls
- A role object on a locale or layer also gates it. Add
"*": truewhen the object is open to all users and the role only adds configuration. - A role filter must be
filter.default; other filter keys are client state and do not restrict queries. - Roles beginning with
!are not negations.