@paramira/access 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +33 -2
- package/package.json +30 -3
- package/src/index.js +193 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 The paramira authors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,34 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @paramira/access
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Who may do what on a site (paramira-web design 0003, ADR-0018). Roles come
|
|
4
|
+
from the account's identity-provider groups (`paramira-web-<role>`); a person
|
|
5
|
+
can hold several and their permissions add up; anything not granted is
|
|
6
|
+
refused. No dependencies.
|
|
7
|
+
|
|
8
|
+
```js
|
|
9
|
+
import { assert, can, rolesOf } from '@paramira/access'
|
|
10
|
+
|
|
11
|
+
rolesOf(session) // ['community', 'editor']
|
|
12
|
+
can(session, 'entry.publish') // true for an editor
|
|
13
|
+
can(session, 'request.read', { owner }) // anyone signed in: their own requests only
|
|
14
|
+
assert(session, 'people.assign', { role: 'admin' }) // throws: 403, never admin
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
`session` is `{ user: { pk, groups } }` (what `@paramira/nuxt-org-auth`
|
|
18
|
+
keeps); signed out is `null`. Every signed-in account is **community**.
|
|
19
|
+
|
|
20
|
+
| Role | May |
|
|
21
|
+
|---|---|
|
|
22
|
+
| (signed out) | read |
|
|
23
|
+
| community | read; suggest a fix; discuss and report; propose, like and vote on ideas; ask for help and read their own requests; use the teams they are in (what they may do there is their team role's, `@paramira/teams`); propose, confirm and report dictionary words (ADR-0055) |
|
|
24
|
+
| contributor | enter the admin; write, edit, translate, send for review; review translations; comment; add media; the glossary; assess ideas (feasible or not); the dictionary: edit, review proposals, import, the grammar profile, recordings |
|
|
25
|
+
| editor | the contributor's writing; review, publish, return, archive, hide and show, restore; decide on suggestions; remove media; moderate discussions; review new ideas; the dictionary: the contributor's, and hide and show, archive, publish documented terms (ADR-0056) |
|
|
26
|
+
| customer | help agreed (ADR-0041): create their community's team (ADR-0042) |
|
|
27
|
+
| speaker | known to speak the language, given by an admin (readfine ADR-0061): the dictionary's community permissions, answer and correct checks (`lexicon.check`), record learner trials (`lexicon.trial`, contributors too) |
|
|
28
|
+
| admin | enter the admin; settings, the tools catalog, people and roles (never admin itself), all requests and moving them along, every team and its limits, the log; assess and plan ideas |
|
|
29
|
+
|
|
30
|
+
`assert` throws an `AccessError` with `statusCode` 401 (signed out) or 403.
|
|
31
|
+
The tests write the whole table out separately (`test/access.test.js`): a
|
|
32
|
+
change to a role changes both.
|
|
33
|
+
|
|
34
|
+
MIT.
|
package/package.json
CHANGED
|
@@ -1,6 +1,33 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@paramira/access",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Who may do what on a site: roles from identity-provider groups, permissions that add up, deny by default",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./src/index.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./src/index.js"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"src",
|
|
13
|
+
"README.md",
|
|
14
|
+
"LICENSE"
|
|
15
|
+
],
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "https://codeberg.org/phuman/paramira-web.git",
|
|
19
|
+
"directory": "packages/access"
|
|
20
|
+
},
|
|
21
|
+
"engines": {
|
|
22
|
+
"node": ">=22.13"
|
|
23
|
+
},
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"vitest": "4.1.12"
|
|
29
|
+
},
|
|
30
|
+
"scripts": {
|
|
31
|
+
"test": "vitest run"
|
|
32
|
+
}
|
|
6
33
|
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
// @paramira/access: who may do what on the site (design 0003, ADR-0018).
|
|
2
|
+
// Roles come from the account's identity-provider groups; a person can hold
|
|
3
|
+
// several and their permissions add up; anything not granted is refused.
|
|
4
|
+
// Every server handler asks `can()` (or `assert()`) before acting.
|
|
5
|
+
|
|
6
|
+
export const GROUP_PREFIX = 'paramira-web-'
|
|
7
|
+
export const ROLES = ['community', 'contributor', 'editor', 'customer', 'speaker', 'admin', 'treasurer']
|
|
8
|
+
|
|
9
|
+
/** What each role may do. Signed-out visitors may only read. */
|
|
10
|
+
export const PERMISSIONS = {
|
|
11
|
+
anyone: [
|
|
12
|
+
'content.read'
|
|
13
|
+
],
|
|
14
|
+
community: [
|
|
15
|
+
'content.read',
|
|
16
|
+
'suggestion.create',
|
|
17
|
+
'discussion.post',
|
|
18
|
+
'discussion.report',
|
|
19
|
+
'idea.create',
|
|
20
|
+
'idea.like',
|
|
21
|
+
'idea.vote',
|
|
22
|
+
'request.create',
|
|
23
|
+
'request.read',
|
|
24
|
+
// Their own teams: what they may do there is the team role's (@paramira/teams).
|
|
25
|
+
'team.use',
|
|
26
|
+
// The dictionary (design 0013, ADR-0055): propose, confirm, report.
|
|
27
|
+
'lexicon.propose',
|
|
28
|
+
'lexicon.confirm',
|
|
29
|
+
'lexicon.report'
|
|
30
|
+
],
|
|
31
|
+
contributor: [
|
|
32
|
+
'admin.enter',
|
|
33
|
+
'entry.create',
|
|
34
|
+
'entry.edit',
|
|
35
|
+
'entry.translate',
|
|
36
|
+
'entry.submit',
|
|
37
|
+
'translation.review',
|
|
38
|
+
'comment.create',
|
|
39
|
+
'media.add',
|
|
40
|
+
'glossary.edit',
|
|
41
|
+
'idea.assess',
|
|
42
|
+
'lexicon.edit',
|
|
43
|
+
'lexicon.review',
|
|
44
|
+
'lexicon.import',
|
|
45
|
+
'lexicon.profile',
|
|
46
|
+
'lexicon.record',
|
|
47
|
+
// Record a learner trial an observer watched (readfine ADR-0064).
|
|
48
|
+
'lexicon.trial',
|
|
49
|
+
// The home carousel (design 0016, ADR-0072): cards point to published items.
|
|
50
|
+
'carousel.edit'
|
|
51
|
+
],
|
|
52
|
+
editor: [
|
|
53
|
+
'admin.enter',
|
|
54
|
+
'entry.create',
|
|
55
|
+
'entry.edit',
|
|
56
|
+
'entry.translate',
|
|
57
|
+
'entry.submit',
|
|
58
|
+
'translation.review',
|
|
59
|
+
'comment.create',
|
|
60
|
+
'media.add',
|
|
61
|
+
'media.remove',
|
|
62
|
+
'glossary.edit',
|
|
63
|
+
'entry.review',
|
|
64
|
+
'entry.publish',
|
|
65
|
+
'entry.return',
|
|
66
|
+
'entry.archive',
|
|
67
|
+
'entry.hide',
|
|
68
|
+
'entry.rename',
|
|
69
|
+
'entry.restore',
|
|
70
|
+
'redirect.edit',
|
|
71
|
+
'links.read',
|
|
72
|
+
'suggestion.decide',
|
|
73
|
+
'discussion.moderate',
|
|
74
|
+
'idea.review',
|
|
75
|
+
'lexicon.edit',
|
|
76
|
+
'lexicon.review',
|
|
77
|
+
'lexicon.import',
|
|
78
|
+
'lexicon.profile',
|
|
79
|
+
'lexicon.record',
|
|
80
|
+
// Hide and restore, archive, and publish documented terms (ADR-0056).
|
|
81
|
+
'lexicon.hide',
|
|
82
|
+
'lexicon.archive',
|
|
83
|
+
'lexicon.document',
|
|
84
|
+
'carousel.edit'
|
|
85
|
+
],
|
|
86
|
+
// Help agreed (ADR-0041): they may make their community's team (ADR-0042).
|
|
87
|
+
customer: [
|
|
88
|
+
'team.create'
|
|
89
|
+
],
|
|
90
|
+
// Someone known to speak the language (readfine design 0014, ADR-0061):
|
|
91
|
+
// an admin gives it by hand. Speakers answer and correct the dictionary's
|
|
92
|
+
// checks and record learner trials; they also get the community role's
|
|
93
|
+
// dictionary permissions, so the role works on its own. Contributors and
|
|
94
|
+
// editors don't check with their own role: someone can hold both.
|
|
95
|
+
speaker: [
|
|
96
|
+
'lexicon.propose',
|
|
97
|
+
'lexicon.confirm',
|
|
98
|
+
'lexicon.report',
|
|
99
|
+
'lexicon.check',
|
|
100
|
+
'lexicon.trial'
|
|
101
|
+
],
|
|
102
|
+
admin: [
|
|
103
|
+
'admin.enter',
|
|
104
|
+
'settings.edit',
|
|
105
|
+
'tools.edit',
|
|
106
|
+
'people.read',
|
|
107
|
+
'people.assign',
|
|
108
|
+
'request.read',
|
|
109
|
+
'request.act',
|
|
110
|
+
'team.read',
|
|
111
|
+
'team.act',
|
|
112
|
+
'log.read',
|
|
113
|
+
'status.read',
|
|
114
|
+
'people.suspend',
|
|
115
|
+
'people.clear',
|
|
116
|
+
'visits.read',
|
|
117
|
+
'chat.moderate',
|
|
118
|
+
'invite.read',
|
|
119
|
+
'invite.create',
|
|
120
|
+
'invite.cancel',
|
|
121
|
+
'idea.assess',
|
|
122
|
+
'idea.plan',
|
|
123
|
+
'carousel.edit'
|
|
124
|
+
],
|
|
125
|
+
// The association's treasurer and at most one deputy (ADR-0069): given
|
|
126
|
+
// only in Authentik, like admin. Reads paid requests and confirms their
|
|
127
|
+
// payments; writes no quote and no invoice.
|
|
128
|
+
treasurer: [
|
|
129
|
+
'admin.enter',
|
|
130
|
+
'request.read',
|
|
131
|
+
'payment.read',
|
|
132
|
+
'payment.confirm',
|
|
133
|
+
'paidSupport.report'
|
|
134
|
+
]
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Roles an admin may give or take from the site; admin and treasurer only in the identity provider. */
|
|
138
|
+
export const ASSIGNABLE = ['community', 'contributor', 'editor', 'customer', 'speaker']
|
|
139
|
+
|
|
140
|
+
export class AccessError extends Error {
|
|
141
|
+
constructor (status, message) {
|
|
142
|
+
super(message)
|
|
143
|
+
this.statusCode = status
|
|
144
|
+
this.status = status
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* The roles of a session (`{ user: { pk, groups } }` from
|
|
150
|
+
* @paramira/nuxt-org-auth), or [] when signed out. Every signed-in account is
|
|
151
|
+
* community, whether or not its group says so.
|
|
152
|
+
*/
|
|
153
|
+
export function rolesOf (session) {
|
|
154
|
+
if (!session?.user) return []
|
|
155
|
+
const fromGroups = (session.user.groups ?? [])
|
|
156
|
+
.filter((g) => typeof g === 'string' && g.startsWith(GROUP_PREFIX))
|
|
157
|
+
.map((g) => g.slice(GROUP_PREFIX.length))
|
|
158
|
+
.filter((r) => ROLES.includes(r))
|
|
159
|
+
return ROLES.filter((r) => r === 'community' || fromGroups.includes(r))
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** Everything the session may do. */
|
|
163
|
+
export function permissionsOf (session) {
|
|
164
|
+
const granted = new Set(PERMISSIONS.anyone)
|
|
165
|
+
for (const role of rolesOf(session)) for (const p of PERMISSIONS[role]) granted.add(p)
|
|
166
|
+
return granted
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* May the session do `action` (on `resource`)?
|
|
171
|
+
*
|
|
172
|
+
* - `request.read`: anyone signed in reads their own requests
|
|
173
|
+
* (`resource.owner` is their account); an admin or a treasurer reads all
|
|
174
|
+
* (the site shows a treasurer only the paid ones).
|
|
175
|
+
* - `people.assign`: only the assignable roles (`resource.role`), never admin.
|
|
176
|
+
*/
|
|
177
|
+
export function can (session, action, resource = {}) {
|
|
178
|
+
const granted = permissionsOf(session)
|
|
179
|
+
if (!granted.has(action)) return false
|
|
180
|
+
const roles = rolesOf(session)
|
|
181
|
+
if (action === 'request.read' && !roles.includes('admin') && !roles.includes('treasurer')) {
|
|
182
|
+
return resource.owner !== undefined && resource.owner !== null && String(resource.owner) === String(session.user.pk)
|
|
183
|
+
}
|
|
184
|
+
if (action === 'people.assign') return ASSIGNABLE.includes(resource.role)
|
|
185
|
+
return true
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** Throws 401 (signed out) or 403 unless `can()`. */
|
|
189
|
+
export function assert (session, action, resource) {
|
|
190
|
+
if (can(session, action, resource)) return
|
|
191
|
+
if (!session?.user) throw new AccessError(401, 'sign in first')
|
|
192
|
+
throw new AccessError(403, `not allowed: ${action}`)
|
|
193
|
+
}
|