Skip to content

Commit cc0a9b7

Browse files
feat: document new self feature (#499)
Co-authored-by: Maria Ines Parnisari <maria.ines.parnisari@authzed.com>
1 parent 0170b12 commit cc0a9b7

File tree

3 files changed

+59
-27
lines changed

3 files changed

+59
-27
lines changed

app/spicedb/concepts/schema/page.mdx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,36 @@ In the above example, the user must be in the `member` relation for _all_ groups
417417
results for the arrow.
418418
</Callout>
419419

420+
### The `self` Keyword
421+
422+
_Available in SpiceDB v1.49.0_
423+
424+
A common piece of authorization logic is that a `user` should always be able to view themselves.
425+
Using the `self` keyword provides a simple way to express this:
426+
427+
```zed
428+
use self
429+
430+
definition user {
431+
relation viewer: user
432+
permission view = viewer + self
433+
}
434+
```
435+
436+
With the `self` keyword, any check for that permission that has the same subject and object (e.g. `user:alice#view@user:alice`) will evaluate to true.
437+
438+
This can be expressed without the `self` keyword as well:
439+
440+
```zed
441+
definition user {
442+
relation viewer: user
443+
permission view = viewer
444+
}
445+
```
446+
447+
But in order to express that a user should be able to view themselves, you'll need to write a corresponding relationship (e.g. `user:alice#viewer@user:alice`)
448+
for the check to evaluate to true. The `self` keyword requires less DB space and computation to achieve the same result.
449+
420450
### Naming Permissions
421451

422452
Permissions define a set of objects that can perform an action or have some attribute, and thus **permissions should be named as verbs or nouns**, read as `(is/can) {permission name} (the object)`.

app/spicedb/modeling/attributes/page.mdx

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,40 @@ SpiceDB is a Relationship Based Access control system.
99
This gives SpiceDB the flexibility to evaluate attributes for access control alongside more complicated access control logic like roles and/or relationships.
1010

1111
The sections below will provide practical examples for implementing various kinds of attributes in the SpiceDB schema language.
12-
Before reading this guide, it's recommended that you have some familiarity with the SpiceDB schema language. [These documents](/spicedb/modeling/developing-a-schema) are a good place to start.
12+
Before reading this guide, it's recommended that you have some familiarity with the SpiceDB schema language.
13+
[This guide](/spicedb/modeling/developing-a-schema) is a good place to start.
1314

1415
## Boolean Attributes
1516

1617
A boolean attribute is an attribute on an object that affects authorization by enabling or disabling an authorization setting.
1718
Boolean attributes can often be thought of as a toggle.
1819
Feature flag authorization can be enabled with boolean attributes.
1920

21+
### Loop Relationships
22+
23+
Loop relationships are one way to implement boolean attributes.
24+
25+
In the example below, there is a schema that enforces the following authorization logic: a user can
26+
only view a document if the user is related to the document as viewer and editing is enabled for the
27+
document (this is the same authorization logic used in the wildcard example below).
28+
29+
In the example below, to enable editing for a document, a loop relationship using the `edit_enabled` relation must be written.
30+
When a `document` is related to itself with the `edit_enabled` relation, that relation can be walked to itself
31+
(with the arrow) to determine who relates to the document as an `editor`.
32+
33+
In summary, a `user` has permission to edit a `document` if they are related to that document as an `editor`
34+
and that document is related to itself with `edit_enabled`.
35+
36+
<InlinePlayground reference="ifgMyLGN-Hjw" />
37+
38+
<Callout type="warning">
39+
There is no mechanism in the SpiceDB schema language that enforces that a
40+
relation be used as a loop relation. In order to avoid accidentally misusing a
41+
loop relation (e.g. relating an object to a different instance of the same
42+
type) it is recommended to implement client side logic that enforces only
43+
using the loop relation for its intended purpose.
44+
</Callout>
45+
2046
### Wildcards
2147

2248
[Wildcards](/spicedb/concepts/schema#wildcards) are a way to implement boolean attributes.
@@ -32,31 +58,7 @@ To enable document editing, you need to establish a relationship that connects a
3258
Wildcards are adequate for most binary attribute scenarios; however, wildcards
3359
are not currently supported by [Authzed
3460
Materialize](/authzed/concepts/authzed-materialize). Those who plan to use
35-
Materialize should use self relationships for binary attributes.
36-
</Callout>
37-
38-
### Self Relationships
39-
40-
Self relationships are another way to implement boolean attributes.
41-
Self relationships relate an object to itself.
42-
A self relationship is walked with an [arrow](/spicedb/concepts/schema#--arrow) back to an object's self.
43-
In practice, relating something to itself toggles something on.
44-
45-
In the example below, there is a schema that enforces the following authorization logic: a user can only view a document if the user is related to the document as viewer and editing is enabled for the document (this is the same authorization logic used in the wildcard example above).
46-
47-
In the example below, to enable editing for a document, a self relationship using the `edit_enabled` relation must be written.
48-
When a `document` is related to itself with the `edit_enabled` relation, that relation can be walked to itself (with the arrow) to determine who relates to the document as an `editor`.
49-
50-
In summary, a `user` has permission to edit a `document` if they are related to that document as an `editor` and that document is related to itself with `edit_enabled`.
51-
52-
<InlinePlayground reference="ifgMyLGN-Hjw" />
53-
54-
<Callout type="warning">
55-
There is no mechanism in the SpiceDB schema language that enforces that a
56-
relation be used as a self relation. In order to avoid accidentally misusing a
57-
self relation (e.g. relating an object to a different instance of the same
58-
type) it is recommended to implement client side logic that enforces only
59-
using the self relation for it's intended purpose.
61+
Materialize should use loop relationships for binary attributes.
6062
</Callout>
6163

6264
## Attribute Matching

components/playground.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export function InlinePlayground(props: {
44
}) {
55
let playgroundUrl = "https://play.authzed.com";
66
return (
7-
<div>
7+
<div className="my-6">
88
<iframe
99
style={{ width: "100%", border: "0px", height: "500px" }}
1010
src={`${playgroundUrl}/i/${props.reference}`}

0 commit comments

Comments
 (0)