@paramira/access 0.0.0-stage → 0.1.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.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +32 -2
  3. package/package.json +30 -3
  4. package/src/index.js +164 -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,33 @@
1
- # Temporary Holding Version
1
+ # @paramira/access
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
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
+ | 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 |
28
+
29
+ `assert` throws an `AccessError` with `statusCode` 401 (signed out) or 403.
30
+ The tests write the whole table out separately (`test/access.test.js`): a
31
+ change to a role changes both.
32
+
33
+ MIT.
package/package.json CHANGED
@@ -1,6 +1,33 @@
1
1
  {
2
2
  "name": "@paramira/access",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.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,164 @@
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', 'admin']
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
+ ],
48
+ editor: [
49
+ 'admin.enter',
50
+ 'entry.create',
51
+ 'entry.edit',
52
+ 'entry.translate',
53
+ 'entry.submit',
54
+ 'translation.review',
55
+ 'comment.create',
56
+ 'media.add',
57
+ 'media.remove',
58
+ 'glossary.edit',
59
+ 'entry.review',
60
+ 'entry.publish',
61
+ 'entry.return',
62
+ 'entry.archive',
63
+ 'entry.hide',
64
+ 'entry.rename',
65
+ 'entry.restore',
66
+ 'redirect.edit',
67
+ 'links.read',
68
+ 'suggestion.decide',
69
+ 'discussion.moderate',
70
+ 'idea.review',
71
+ 'lexicon.edit',
72
+ 'lexicon.review',
73
+ 'lexicon.import',
74
+ 'lexicon.profile',
75
+ 'lexicon.record',
76
+ // Hide and restore, archive, and publish documented terms (ADR-0056).
77
+ 'lexicon.hide',
78
+ 'lexicon.archive',
79
+ 'lexicon.document'
80
+ ],
81
+ // Help agreed (ADR-0041): they may make their community's team (ADR-0042).
82
+ customer: [
83
+ 'team.create'
84
+ ],
85
+ admin: [
86
+ 'admin.enter',
87
+ 'settings.edit',
88
+ 'tools.edit',
89
+ 'people.read',
90
+ 'people.assign',
91
+ 'request.read',
92
+ 'request.act',
93
+ 'team.read',
94
+ 'team.act',
95
+ 'log.read',
96
+ 'status.read',
97
+ 'people.suspend',
98
+ 'people.clear',
99
+ 'visits.read',
100
+ 'chat.moderate',
101
+ 'invite.read',
102
+ 'invite.create',
103
+ 'invite.cancel',
104
+ 'idea.assess',
105
+ 'idea.plan'
106
+ ]
107
+ }
108
+
109
+ /** Roles an admin may give or take from the site; admin only in the identity provider. */
110
+ export const ASSIGNABLE = ['community', 'contributor', 'editor', 'customer']
111
+
112
+ export class AccessError extends Error {
113
+ constructor (status, message) {
114
+ super(message)
115
+ this.statusCode = status
116
+ this.status = status
117
+ }
118
+ }
119
+
120
+ /**
121
+ * The roles of a session (`{ user: { pk, groups } }` from
122
+ * @paramira/nuxt-org-auth), or [] when signed out. Every signed-in account is
123
+ * community, whether or not its group says so.
124
+ */
125
+ export function rolesOf (session) {
126
+ if (!session?.user) return []
127
+ const fromGroups = (session.user.groups ?? [])
128
+ .filter((g) => typeof g === 'string' && g.startsWith(GROUP_PREFIX))
129
+ .map((g) => g.slice(GROUP_PREFIX.length))
130
+ .filter((r) => ROLES.includes(r))
131
+ return ROLES.filter((r) => r === 'community' || fromGroups.includes(r))
132
+ }
133
+
134
+ /** Everything the session may do. */
135
+ export function permissionsOf (session) {
136
+ const granted = new Set(PERMISSIONS.anyone)
137
+ for (const role of rolesOf(session)) for (const p of PERMISSIONS[role]) granted.add(p)
138
+ return granted
139
+ }
140
+
141
+ /**
142
+ * May the session do `action` (on `resource`)?
143
+ *
144
+ * - `request.read`: anyone signed in reads their own requests
145
+ * (`resource.owner` is their account); an admin reads all.
146
+ * - `people.assign`: only the assignable roles (`resource.role`), never admin.
147
+ */
148
+ export function can (session, action, resource = {}) {
149
+ const granted = permissionsOf(session)
150
+ if (!granted.has(action)) return false
151
+ const roles = rolesOf(session)
152
+ if (action === 'request.read' && !roles.includes('admin')) {
153
+ return resource.owner !== undefined && resource.owner !== null && String(resource.owner) === String(session.user.pk)
154
+ }
155
+ if (action === 'people.assign') return ASSIGNABLE.includes(resource.role)
156
+ return true
157
+ }
158
+
159
+ /** Throws 401 (signed out) or 403 unless `can()`. */
160
+ export function assert (session, action, resource) {
161
+ if (can(session, action, resource)) return
162
+ if (!session?.user) throw new AccessError(401, 'sign in first')
163
+ throw new AccessError(403, `not allowed: ${action}`)
164
+ }