@foundrykit/permissions-plugin 0.0.0-stage → 0.1.2

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 (71) hide show
  1. package/README.md +110 -2
  2. package/dist/access.d.ts +38 -0
  3. package/dist/access.js +119 -0
  4. package/dist/access.js.map +1 -0
  5. package/dist/audit.d.ts +25 -0
  6. package/dist/audit.js +95 -0
  7. package/dist/audit.js.map +1 -0
  8. package/dist/collections/PermissionOverrides.d.ts +6 -0
  9. package/dist/collections/PermissionOverrides.js +152 -0
  10. package/dist/collections/PermissionOverrides.js.map +1 -0
  11. package/dist/collections/Roles.d.ts +6 -0
  12. package/dist/collections/Roles.js +106 -0
  13. package/dist/collections/Roles.js.map +1 -0
  14. package/dist/components/NavLinks.d.ts +3 -0
  15. package/dist/components/NavLinks.js +20 -0
  16. package/dist/components/NavLinks.js.map +1 -0
  17. package/dist/components/PublishButtonGate.d.ts +6 -0
  18. package/dist/components/PublishButtonGate.js +12 -0
  19. package/dist/components/PublishButtonGate.js.map +1 -0
  20. package/dist/components/RolesMatrix.d.ts +3 -0
  21. package/dist/components/RolesMatrix.js +324 -0
  22. package/dist/components/RolesMatrix.js.map +1 -0
  23. package/dist/components/UsersTable.d.ts +3 -0
  24. package/dist/components/UsersTable.js +390 -0
  25. package/dist/components/UsersTable.js.map +1 -0
  26. package/dist/components/api.d.ts +2 -0
  27. package/dist/components/api.js +17 -0
  28. package/dist/components/api.js.map +1 -0
  29. package/dist/components/index.scss +335 -0
  30. package/dist/endpoints/resetToRole.d.ts +4 -0
  31. package/dist/endpoints/resetToRole.js +85 -0
  32. package/dist/endpoints/resetToRole.js.map +1 -0
  33. package/dist/exports/client.d.ts +1 -0
  34. package/dist/exports/client.js +3 -0
  35. package/dist/exports/client.js.map +1 -0
  36. package/dist/exports/rsc.d.ts +4 -0
  37. package/dist/exports/rsc.js +6 -0
  38. package/dist/exports/rsc.js.map +1 -0
  39. package/dist/index.d.ts +7 -0
  40. package/dist/index.js +195 -0
  41. package/dist/index.js.map +1 -0
  42. package/dist/registry.d.ts +5 -0
  43. package/dist/registry.js +87 -0
  44. package/dist/registry.js.map +1 -0
  45. package/dist/registry.test.d.ts +1 -0
  46. package/dist/resolve.d.ts +21 -0
  47. package/dist/resolve.js +33 -0
  48. package/dist/resolve.js.map +1 -0
  49. package/dist/resolve.test.d.ts +1 -0
  50. package/dist/rules.d.ts +5 -0
  51. package/dist/rules.js +20 -0
  52. package/dist/rules.js.map +1 -0
  53. package/dist/types.d.ts +59 -0
  54. package/dist/types.js +3 -0
  55. package/dist/types.js.map +1 -0
  56. package/dist/users.d.ts +3 -0
  57. package/dist/users.js +161 -0
  58. package/dist/users.js.map +1 -0
  59. package/dist/views/PermissionsLayout.d.ts +11 -0
  60. package/dist/views/PermissionsLayout.js +69 -0
  61. package/dist/views/PermissionsLayout.js.map +1 -0
  62. package/dist/views/RolesView.d.ts +2 -0
  63. package/dist/views/RolesView.js +20 -0
  64. package/dist/views/RolesView.js.map +1 -0
  65. package/dist/views/UsersView.d.ts +2 -0
  66. package/dist/views/UsersView.js +14 -0
  67. package/dist/views/UsersView.js.map +1 -0
  68. package/dist/views/data.d.ts +36 -0
  69. package/dist/views/data.js +58 -0
  70. package/dist/views/data.js.map +1 -0
  71. package/package.json +107 -4
@@ -0,0 +1,335 @@
1
+ // Uses the admin theme's variables so it follows light/dark mode.
2
+
3
+ .perm {
4
+ padding-bottom: calc(var(--base) * 4);
5
+
6
+ table {
7
+ border-collapse: collapse;
8
+ width: 100%;
9
+ }
10
+
11
+ th,
12
+ td {
13
+ border-bottom: 1px solid var(--theme-elevation-100);
14
+ padding: calc(var(--base) * 0.4) calc(var(--base) * 0.5);
15
+ text-align: left;
16
+ vertical-align: middle;
17
+ }
18
+
19
+ thead th {
20
+ color: var(--theme-elevation-500);
21
+ font-weight: 500;
22
+ }
23
+
24
+ code {
25
+ color: var(--theme-elevation-500);
26
+ font-size: 0.85em;
27
+ }
28
+
29
+ input[type='checkbox'] {
30
+ accent-color: var(--theme-success-500);
31
+ cursor: pointer;
32
+ height: 16px;
33
+ width: 16px;
34
+
35
+ &:disabled {
36
+ cursor: not-allowed;
37
+ }
38
+ }
39
+
40
+ select,
41
+ input:not([type]) {
42
+ background: var(--theme-input-bg);
43
+ border: 1px solid var(--theme-elevation-150);
44
+ border-radius: var(--style-radius-s);
45
+ color: var(--theme-text);
46
+ padding: calc(var(--base) * 0.3) calc(var(--base) * 0.5);
47
+ }
48
+ }
49
+
50
+ .perm-matrix button:not(.btn),
51
+ .perm-users__name,
52
+ .perm-side__header button {
53
+ background: none;
54
+ border: 0;
55
+ color: inherit;
56
+ cursor: pointer;
57
+ font: inherit;
58
+ padding: 0;
59
+
60
+ &:disabled {
61
+ cursor: not-allowed;
62
+ opacity: 0.5;
63
+ }
64
+ }
65
+
66
+ .perm__tabs {
67
+ border-bottom: 1px solid var(--theme-elevation-100);
68
+ display: flex;
69
+ gap: calc(var(--base) * 1.5);
70
+ margin-bottom: calc(var(--base) * 1.5);
71
+ }
72
+
73
+ .perm__tab {
74
+ border-bottom: 2px solid transparent;
75
+ color: var(--theme-elevation-500);
76
+ padding: calc(var(--base) * 0.5) 0;
77
+ text-decoration: none;
78
+
79
+ &[aria-current='page'] {
80
+ border-color: var(--theme-text);
81
+ color: var(--theme-text);
82
+ }
83
+ }
84
+
85
+ .perm-matrix__add {
86
+ align-items: center;
87
+ display: flex;
88
+ gap: calc(var(--base) * 0.5);
89
+ margin-bottom: var(--base);
90
+
91
+ .btn {
92
+ margin: 0;
93
+ }
94
+ }
95
+
96
+ .perm-matrix__scroll {
97
+ overflow-x: auto;
98
+ }
99
+
100
+ .perm-matrix {
101
+ td {
102
+ text-align: center;
103
+ }
104
+
105
+ thead th:not(:first-child) {
106
+ min-width: 140px;
107
+ text-align: center;
108
+ }
109
+
110
+ tbody th {
111
+ font-weight: 400;
112
+ padding-left: calc(var(--base) * 1.5);
113
+ }
114
+ }
115
+
116
+ .perm-matrix__area {
117
+ background: var(--theme-elevation-50);
118
+
119
+ th {
120
+ font-weight: 600 !important;
121
+ padding-left: calc(var(--base) * 0.5) !important;
122
+ }
123
+ }
124
+
125
+ .perm-matrix__role-name {
126
+ font-weight: 600;
127
+ text-align: center;
128
+ width: 100%;
129
+ }
130
+
131
+ .perm-matrix__role-meta {
132
+ font-size: 0.85em;
133
+ margin-top: calc(var(--base) * 0.25);
134
+ }
135
+
136
+ .perm-matrix__role-actions {
137
+ display: flex;
138
+ font-size: 0.85em;
139
+ gap: calc(var(--base) * 0.5);
140
+ justify-content: center;
141
+ margin-top: calc(var(--base) * 0.25);
142
+
143
+ button {
144
+ text-decoration: underline;
145
+ }
146
+ }
147
+
148
+ .perm-matrix__save {
149
+ align-items: center;
150
+ background: var(--theme-elevation-0);
151
+ border: 1px solid var(--theme-elevation-150);
152
+ border-radius: var(--style-radius-m);
153
+ bottom: var(--base);
154
+ box-shadow: 0 4px 16px rgb(0 0 0 / 12%);
155
+ display: flex;
156
+ gap: var(--base);
157
+ justify-content: space-between;
158
+ margin-top: var(--base);
159
+ padding: calc(var(--base) * 0.75) var(--base);
160
+ position: sticky;
161
+
162
+ ul {
163
+ margin: 0;
164
+ padding-left: var(--base);
165
+ }
166
+
167
+ .btn {
168
+ margin: 0;
169
+ }
170
+ }
171
+
172
+ .perm-matrix__save-actions {
173
+ display: flex;
174
+ flex-shrink: 0;
175
+ gap: calc(var(--base) * 0.5);
176
+ }
177
+
178
+ .perm-users__filters {
179
+ align-items: center;
180
+ display: flex;
181
+ gap: calc(var(--base) * 1.5);
182
+ margin-bottom: var(--base);
183
+ }
184
+
185
+ .perm-users__name {
186
+ align-items: center;
187
+ display: inline-flex;
188
+ font-weight: 500;
189
+ gap: calc(var(--base) * 0.5);
190
+ text-align: left;
191
+
192
+ &:hover {
193
+ text-decoration: underline;
194
+ }
195
+ }
196
+
197
+ .perm-users__avatar {
198
+ align-items: center;
199
+ background: var(--theme-elevation-150);
200
+ border-radius: 50%;
201
+ display: inline-flex;
202
+ flex-shrink: 0;
203
+ font-size: 0.75em;
204
+ height: 28px;
205
+ justify-content: center;
206
+ width: 28px;
207
+ }
208
+
209
+ .perm-users__selected {
210
+ background: var(--theme-elevation-50);
211
+ }
212
+
213
+ .perm-side {
214
+ background: var(--theme-bg);
215
+ border-left: 1px solid var(--theme-elevation-150);
216
+ bottom: 0;
217
+ box-shadow: -8px 0 24px rgb(0 0 0 / 10%);
218
+ overflow-y: auto;
219
+ padding: 0 calc(var(--base) * 1.5);
220
+ position: fixed;
221
+ right: 0;
222
+ top: 0;
223
+ width: min(560px, 100vw);
224
+ z-index: 30;
225
+ }
226
+
227
+ .perm-side__header {
228
+ align-items: center;
229
+ background: var(--theme-bg);
230
+ border-bottom: 1px solid var(--theme-elevation-100);
231
+ display: flex;
232
+ justify-content: space-between;
233
+ padding: var(--base) 0;
234
+ position: sticky;
235
+ top: 0;
236
+ z-index: 1;
237
+
238
+ h3 {
239
+ margin: 0;
240
+ }
241
+ }
242
+
243
+ .perm-panel {
244
+ padding: var(--base) 0 calc(var(--base) * 2);
245
+
246
+ select {
247
+ background: var(--theme-input-bg);
248
+ border: 1px solid var(--theme-elevation-150);
249
+ border-radius: var(--style-radius-s);
250
+ color: var(--theme-text);
251
+ margin-left: calc(var(--base) * 0.5);
252
+ padding: calc(var(--base) * 0.3) calc(var(--base) * 0.5);
253
+ }
254
+
255
+ h4 {
256
+ margin: calc(var(--base) * 1.5) 0 calc(var(--base) * 0.5);
257
+ }
258
+
259
+ .btn {
260
+ margin: 0;
261
+ }
262
+ }
263
+
264
+ .perm-panel__toolbar {
265
+ align-items: center;
266
+ display: flex;
267
+ flex-wrap: wrap;
268
+ gap: var(--base);
269
+ justify-content: space-between;
270
+ margin-top: var(--base);
271
+ }
272
+
273
+ .perm-panel__row {
274
+ align-items: center;
275
+ border-bottom: 1px solid var(--theme-elevation-100);
276
+ display: flex;
277
+ gap: var(--base);
278
+ justify-content: space-between;
279
+ padding: calc(var(--base) * 0.4) 0;
280
+
281
+ small {
282
+ color: var(--theme-elevation-500);
283
+ }
284
+ }
285
+
286
+ .perm-panel__row--inherited > div:first-child {
287
+ color: var(--theme-elevation-500);
288
+ }
289
+
290
+ .perm-panel__states {
291
+ border: 1px solid var(--theme-elevation-150);
292
+ border-radius: var(--style-radius-s);
293
+ display: inline-flex;
294
+ flex-shrink: 0;
295
+ overflow: hidden;
296
+
297
+ button {
298
+ background: none;
299
+ border: 0;
300
+ color: var(--theme-elevation-600);
301
+ cursor: pointer;
302
+ font: inherit;
303
+ padding: calc(var(--base) * 0.25) calc(var(--base) * 0.6);
304
+
305
+ & + button {
306
+ border-left: 1px solid var(--theme-elevation-150);
307
+ }
308
+
309
+ &[aria-pressed='true'] {
310
+ background: var(--theme-elevation-800);
311
+ color: var(--theme-elevation-0);
312
+ opacity: 1;
313
+ }
314
+
315
+ &:disabled:not([aria-pressed='true']) {
316
+ cursor: not-allowed;
317
+ opacity: 0.4;
318
+ }
319
+ }
320
+ }
321
+
322
+ .perm-panel__history ul {
323
+ list-style: none;
324
+ margin: 0;
325
+ padding: 0;
326
+
327
+ li {
328
+ border-bottom: 1px solid var(--theme-elevation-100);
329
+ padding: calc(var(--base) * 0.4) 0;
330
+ }
331
+
332
+ small {
333
+ color: var(--theme-elevation-500);
334
+ }
335
+ }
@@ -0,0 +1,4 @@
1
+ import type { PayloadHandler } from 'payload';
2
+ export declare const RESET_PATH = "/permissions/reset/:id";
3
+ /** Clears every override on a user, logged as one "Reset to role" entry. */
4
+ export declare const resetToRoleHandler: PayloadHandler;
@@ -0,0 +1,85 @@
1
+ import { commitTransaction, initTransaction, killTransaction } from 'payload';
2
+ import { getOverrides, getState, hasPermission, OVERRIDES_SLUG } from '../access.js';
3
+ import { PART_OF_CHANGE, writeAudit } from '../audit.js';
4
+ import { MANAGE_PERMISSION } from '../registry.js';
5
+ import { assertCanGrant, assertNotSelf } from '../rules.js';
6
+ export const RESET_PATH = '/permissions/reset/:id';
7
+ /** Clears every override on a user, logged as one "Reset to role" entry. */ export const resetToRoleHandler = async (req)=>{
8
+ if (!await hasPermission(req, MANAGE_PERMISSION)) {
9
+ return Response.json({
10
+ errors: [
11
+ {
12
+ message: 'Forbidden'
13
+ }
14
+ ]
15
+ }, {
16
+ status: 403
17
+ });
18
+ }
19
+ // Look the user up so the id has the database's type (a number on Postgres, not the URL string).
20
+ const user = await req.payload.findByID({
21
+ id: req.routeParams?.id,
22
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion -- a union once a site generates types
23
+ collection: getState(req.payload).usersSlug,
24
+ depth: 0,
25
+ disableErrors: true,
26
+ overrideAccess: true,
27
+ req
28
+ }).catch(()=>null);
29
+ if (!user) {
30
+ return Response.json({
31
+ errors: [
32
+ {
33
+ message: 'Not Found'
34
+ }
35
+ ]
36
+ }, {
37
+ status: 404
38
+ });
39
+ }
40
+ const id = user.id;
41
+ assertNotSelf(req, id);
42
+ const overrides = await getOverrides(req.payload, id, req);
43
+ // Lifting a deny can hand the permission back, which counts as granting it.
44
+ await assertCanGrant(req, overrides.filter((o)=>o.effect === 'deny').map((o)=>o.permission));
45
+ if (overrides.length) {
46
+ // One transaction, so the overrides are never cleared without the audit entry.
47
+ const ownsTransaction = await initTransaction(req);
48
+ try {
49
+ await req.payload.delete({
50
+ collection: OVERRIDES_SLUG,
51
+ context: {
52
+ [PART_OF_CHANGE]: true
53
+ },
54
+ overrideAccess: true,
55
+ req,
56
+ where: {
57
+ user: {
58
+ equals: id
59
+ }
60
+ }
61
+ });
62
+ await writeAudit(req, {
63
+ action: 'user.reset',
64
+ after: [],
65
+ before: overrides.map(({ effect, permission })=>({
66
+ effect,
67
+ permission
68
+ })),
69
+ summary: `Reset to role: ${overrides.length} override${overrides.length === 1 ? '' : 's'} cleared`,
70
+ user: id
71
+ });
72
+ if (ownsTransaction) {
73
+ await commitTransaction(req);
74
+ }
75
+ } catch (err) {
76
+ await killTransaction(req);
77
+ throw err;
78
+ }
79
+ }
80
+ return Response.json({
81
+ removed: overrides.length
82
+ });
83
+ };
84
+
85
+ //# sourceMappingURL=resetToRole.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/endpoints/resetToRole.ts"],"sourcesContent":["import type { CollectionSlug, PayloadHandler } from 'payload'\n\nimport { commitTransaction, initTransaction, killTransaction } from 'payload'\n\nimport { getOverrides, getState, hasPermission, OVERRIDES_SLUG } from '../access.js'\nimport { PART_OF_CHANGE, writeAudit } from '../audit.js'\nimport { MANAGE_PERMISSION } from '../registry.js'\nimport { assertCanGrant, assertNotSelf } from '../rules.js'\n\nexport const RESET_PATH = '/permissions/reset/:id'\n\n/** Clears every override on a user, logged as one \"Reset to role\" entry. */\nexport const resetToRoleHandler: PayloadHandler = async (req) => {\n if (!(await hasPermission(req, MANAGE_PERMISSION))) {\n return Response.json({ errors: [{ message: 'Forbidden' }] }, { status: 403 })\n }\n // Look the user up so the id has the database's type (a number on Postgres, not the URL string).\n const user = await req.payload\n .findByID({\n id: req.routeParams?.id as string,\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion -- a union once a site generates types\n collection: getState(req.payload).usersSlug as CollectionSlug,\n depth: 0,\n disableErrors: true,\n overrideAccess: true,\n req,\n })\n .catch(() => null)\n if (!user) {\n return Response.json({ errors: [{ message: 'Not Found' }] }, { status: 404 })\n }\n const id = user.id\n assertNotSelf(req, id)\n\n const overrides = await getOverrides(req.payload, id, req)\n // Lifting a deny can hand the permission back, which counts as granting it.\n await assertCanGrant(\n req,\n overrides.filter((o) => o.effect === 'deny').map((o) => o.permission),\n )\n if (overrides.length) {\n // One transaction, so the overrides are never cleared without the audit entry.\n const ownsTransaction = await initTransaction(req)\n try {\n await req.payload.delete({\n collection: OVERRIDES_SLUG,\n context: { [PART_OF_CHANGE]: true },\n overrideAccess: true,\n req,\n where: { user: { equals: id } },\n })\n await writeAudit(req, {\n action: 'user.reset',\n after: [],\n before: overrides.map(({ effect, permission }) => ({ effect, permission })),\n summary: `Reset to role: ${overrides.length} override${overrides.length === 1 ? '' : 's'} cleared`,\n user: id,\n })\n if (ownsTransaction) {\n await commitTransaction(req)\n }\n } catch (err) {\n await killTransaction(req)\n throw err\n }\n }\n return Response.json({ removed: overrides.length })\n}\n"],"names":["commitTransaction","initTransaction","killTransaction","getOverrides","getState","hasPermission","OVERRIDES_SLUG","PART_OF_CHANGE","writeAudit","MANAGE_PERMISSION","assertCanGrant","assertNotSelf","RESET_PATH","resetToRoleHandler","req","Response","json","errors","message","status","user","payload","findByID","id","routeParams","collection","usersSlug","depth","disableErrors","overrideAccess","catch","overrides","filter","o","effect","map","permission","length","ownsTransaction","delete","context","where","equals","action","after","before","summary","err","removed"],"mappings":"AAEA,SAASA,iBAAiB,EAAEC,eAAe,EAAEC,eAAe,QAAQ,UAAS;AAE7E,SAASC,YAAY,EAAEC,QAAQ,EAAEC,aAAa,EAAEC,cAAc,QAAQ,eAAc;AACpF,SAASC,cAAc,EAAEC,UAAU,QAAQ,cAAa;AACxD,SAASC,iBAAiB,QAAQ,iBAAgB;AAClD,SAASC,cAAc,EAAEC,aAAa,QAAQ,cAAa;AAE3D,OAAO,MAAMC,aAAa,yBAAwB;AAElD,0EAA0E,GAC1E,OAAO,MAAMC,qBAAqC,OAAOC;IACvD,IAAI,CAAE,MAAMT,cAAcS,KAAKL,oBAAqB;QAClD,OAAOM,SAASC,IAAI,CAAC;YAAEC,QAAQ;gBAAC;oBAAEC,SAAS;gBAAY;aAAE;QAAC,GAAG;YAAEC,QAAQ;QAAI;IAC7E;IACA,iGAAiG;IACjG,MAAMC,OAAO,MAAMN,IAAIO,OAAO,CAC3BC,QAAQ,CAAC;QACRC,IAAIT,IAAIU,WAAW,EAAED;QACrB,mHAAmH;QACnHE,YAAYrB,SAASU,IAAIO,OAAO,EAAEK,SAAS;QAC3CC,OAAO;QACPC,eAAe;QACfC,gBAAgB;QAChBf;IACF,GACCgB,KAAK,CAAC,IAAM;IACf,IAAI,CAACV,MAAM;QACT,OAAOL,SAASC,IAAI,CAAC;YAAEC,QAAQ;gBAAC;oBAAEC,SAAS;gBAAY;aAAE;QAAC,GAAG;YAAEC,QAAQ;QAAI;IAC7E;IACA,MAAMI,KAAKH,KAAKG,EAAE;IAClBZ,cAAcG,KAAKS;IAEnB,MAAMQ,YAAY,MAAM5B,aAAaW,IAAIO,OAAO,EAAEE,IAAIT;IACtD,4EAA4E;IAC5E,MAAMJ,eACJI,KACAiB,UAAUC,MAAM,CAAC,CAACC,IAAMA,EAAEC,MAAM,KAAK,QAAQC,GAAG,CAAC,CAACF,IAAMA,EAAEG,UAAU;IAEtE,IAAIL,UAAUM,MAAM,EAAE;QACpB,+EAA+E;QAC/E,MAAMC,kBAAkB,MAAMrC,gBAAgBa;QAC9C,IAAI;YACF,MAAMA,IAAIO,OAAO,CAACkB,MAAM,CAAC;gBACvBd,YAAYnB;gBACZkC,SAAS;oBAAE,CAACjC,eAAe,EAAE;gBAAK;gBAClCsB,gBAAgB;gBAChBf;gBACA2B,OAAO;oBAAErB,MAAM;wBAAEsB,QAAQnB;oBAAG;gBAAE;YAChC;YACA,MAAMf,WAAWM,KAAK;gBACpB6B,QAAQ;gBACRC,OAAO,EAAE;gBACTC,QAAQd,UAAUI,GAAG,CAAC,CAAC,EAAED,MAAM,EAAEE,UAAU,EAAE,GAAM,CAAA;wBAAEF;wBAAQE;oBAAW,CAAA;gBACxEU,SAAS,CAAC,eAAe,EAAEf,UAAUM,MAAM,CAAC,SAAS,EAAEN,UAAUM,MAAM,KAAK,IAAI,KAAK,IAAI,QAAQ,CAAC;gBAClGjB,MAAMG;YACR;YACA,IAAIe,iBAAiB;gBACnB,MAAMtC,kBAAkBc;YAC1B;QACF,EAAE,OAAOiC,KAAK;YACZ,MAAM7C,gBAAgBY;YACtB,MAAMiC;QACR;IACF;IACA,OAAOhC,SAASC,IAAI,CAAC;QAAEgC,SAASjB,UAAUM,MAAM;IAAC;AACnD,EAAC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ export { };
2
+
3
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/exports/client.ts"],"sourcesContent":["export {}\n"],"names":[],"mappings":"AAAA,WAAS"}
@@ -0,0 +1,4 @@
1
+ export { NavLinks } from '../components/NavLinks.js';
2
+ export { PublishButtonGate } from '../components/PublishButtonGate.js';
3
+ export { RolesView } from '../views/RolesView.js';
4
+ export { UsersView } from '../views/UsersView.js';
@@ -0,0 +1,6 @@
1
+ export { NavLinks } from '../components/NavLinks.js';
2
+ export { PublishButtonGate } from '../components/PublishButtonGate.js';
3
+ export { RolesView } from '../views/RolesView.js';
4
+ export { UsersView } from '../views/UsersView.js';
5
+
6
+ //# sourceMappingURL=rsc.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/exports/rsc.ts"],"sourcesContent":["export { NavLinks } from '../components/NavLinks.js'\nexport { PublishButtonGate } from '../components/PublishButtonGate.js'\nexport { RolesView } from '../views/RolesView.js'\nexport { UsersView } from '../views/UsersView.js'\n"],"names":["NavLinks","PublishButtonGate","RolesView","UsersView"],"mappings":"AAAA,SAASA,QAAQ,QAAQ,4BAA2B;AACpD,SAASC,iBAAiB,QAAQ,qCAAoC;AACtE,SAASC,SAAS,QAAQ,wBAAuB;AACjD,SAASC,SAAS,QAAQ,wBAAuB"}
@@ -0,0 +1,7 @@
1
+ import type { Config } from 'payload';
2
+ import type { PermissionsPluginConfig } from './types.js';
3
+ export { AUDIT_SLUG, computePermissions, getPermissions, hasPermission, OVERRIDES_SLUG, requirePermission, ROLES_SLUG, userHasPermission, } from './access.js';
4
+ export { MANAGE_PERMISSION } from './registry.js';
5
+ export { redundantOverrides, resolvePermission, resolvePermissions } from './resolve.js';
6
+ export type { Area, AreaConfig, Permission, PermissionsPluginConfig, StandardAction, } from './types.js';
7
+ export declare const permissionsPlugin: (pluginOptions?: PermissionsPluginConfig) => (config: Config) => Config;
package/dist/index.js ADDED
@@ -0,0 +1,195 @@
1
+ import { APIError } from 'payload';
2
+ import { AUDIT_SLUG, gateAccess, hasPermission, isCMSUser, markActive, OVERRIDES_SLUG, ROLES_SLUG } from './access.js';
3
+ import { createAuditCollection } from './audit.js';
4
+ import { createOverridesCollection } from './collections/PermissionOverrides.js';
5
+ import { createRolesCollection } from './collections/Roles.js';
6
+ import { RESET_PATH, resetToRoleHandler } from './endpoints/resetToRole.js';
7
+ import { buildRegistry } from './registry.js';
8
+ import { extendUsersCollection } from './users.js';
9
+ export { AUDIT_SLUG, computePermissions, getPermissions, hasPermission, OVERRIDES_SLUG, requirePermission, ROLES_SLUG, userHasPermission } from './access.js';
10
+ export { MANAGE_PERMISSION } from './registry.js';
11
+ export { redundantOverrides, resolvePermission, resolvePermissions } from './resolve.js';
12
+ /** Publishing (moving `_status` to published) needs `area.publish` on top of create/edit. */ const guardPublish = (key)=>async ({ data, originalDoc, req })=>{
13
+ // Payload marks every draft save `_status: 'draft'` before hooks run, so anything else that
14
+ // asks to publish, or lands on a published document, changes what's live.
15
+ const publishing = data._status !== 'draft' && (data._status === 'published' || originalDoc?._status === 'published');
16
+ // Draft saves also send `_status: 'draft'`; only REST and GraphQL say which is which.
17
+ // REST parses `?draft=true` into a boolean before hooks run; GraphQL leaves the string.
18
+ // ponytail: local API unpublishes aren't checked, since hooks can't tell them from draft saves
19
+ const unpublishing = data._status === 'draft' && originalDoc?._status === 'published' && req.payloadAPI !== 'local' && String(req.query?.draft) !== 'true';
20
+ if (!publishing && !unpublishing) {
21
+ return data;
22
+ }
23
+ if (isCMSUser(req.payload, req.user) && !await hasPermission(req, key)) {
24
+ const verb = publishing ? 'publish' : 'unpublish';
25
+ throw new APIError(`You don't have permission to ${verb} this.`, 403, undefined, true);
26
+ }
27
+ return data;
28
+ };
29
+ const hasKey = (area, key)=>area.permissions.some((p)=>p.key === key);
30
+ const RSC = '@foundrykit/permissions-plugin/rsc';
31
+ /** Server component that hides the (un)publish button from people without `key`. */ const publishButton = (key, kind = 'publish')=>({
32
+ path: `${RSC}#PublishButtonGate`,
33
+ serverProps: {
34
+ kind,
35
+ permission: key
36
+ }
37
+ });
38
+ const gateCollection = (collection, area)=>({
39
+ ...collection,
40
+ access: {
41
+ ...collection.access,
42
+ create: gateAccess(`${area.slug}.create`, collection.access?.create),
43
+ delete: gateAccess(`${area.slug}.delete`, collection.access?.delete),
44
+ read: gateAccess(`${area.slug}.read`, collection.access?.read),
45
+ update: gateAccess(`${area.slug}.edit`, collection.access?.update)
46
+ },
47
+ admin: hasKey(area, `${area.slug}.publish`) ? {
48
+ ...collection.admin,
49
+ components: {
50
+ ...collection.admin?.components,
51
+ edit: {
52
+ PublishButton: publishButton(`${area.slug}.publish`),
53
+ UnpublishButton: publishButton(`${area.slug}.publish`, 'unpublish'),
54
+ ...collection.admin?.components?.edit
55
+ }
56
+ }
57
+ } : collection.admin,
58
+ hooks: {
59
+ ...collection.hooks,
60
+ afterChange: [
61
+ ...collection.hooks?.afterChange ?? [],
62
+ ({ req })=>markActive(req)
63
+ ],
64
+ afterDelete: [
65
+ ...collection.hooks?.afterDelete ?? [],
66
+ ({ req })=>markActive(req)
67
+ ],
68
+ beforeChange: hasKey(area, `${area.slug}.publish`) ? [
69
+ ...collection.hooks?.beforeChange ?? [],
70
+ guardPublish(`${area.slug}.publish`)
71
+ ] : collection.hooks?.beforeChange
72
+ }
73
+ });
74
+ const gateGlobal = (global, area)=>({
75
+ ...global,
76
+ access: {
77
+ ...global.access,
78
+ read: gateAccess(`${area.slug}.read`, global.access?.read),
79
+ update: gateAccess(`${area.slug}.edit`, global.access?.update)
80
+ },
81
+ admin: hasKey(area, `${area.slug}.publish`) ? {
82
+ ...global.admin,
83
+ components: {
84
+ ...global.admin?.components,
85
+ elements: {
86
+ PublishButton: publishButton(`${area.slug}.publish`),
87
+ UnpublishButton: publishButton(`${area.slug}.publish`, 'unpublish'),
88
+ ...global.admin?.components?.elements
89
+ }
90
+ }
91
+ } : global.admin,
92
+ hooks: {
93
+ ...global.hooks,
94
+ afterChange: [
95
+ ...global.hooks?.afterChange ?? [],
96
+ ({ req })=>markActive(req)
97
+ ],
98
+ beforeChange: hasKey(area, `${area.slug}.publish`) ? [
99
+ ...global.hooks?.beforeChange ?? [],
100
+ guardPublish(`${area.slug}.publish`)
101
+ ] : global.hooks?.beforeChange
102
+ }
103
+ });
104
+ export const permissionsPlugin = (pluginOptions = {})=>(config)=>{
105
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion -- a union once a site generates types
106
+ const usersSlug = config.admin?.user ?? 'users';
107
+ const collections = config.collections ?? [];
108
+ const globals = config.globals ?? [];
109
+ const areas = pluginOptions.areas ?? [];
110
+ if (!collections.some((c)=>c.slug === usersSlug)) {
111
+ throw new Error(`permissions: users collection "${usersSlug}" not found`);
112
+ }
113
+ for (const slug of [
114
+ ROLES_SLUG,
115
+ OVERRIDES_SLUG,
116
+ AUDIT_SLUG
117
+ ]){
118
+ if (collections.some((c)=>c.slug === slug)) {
119
+ throw new Error(`permissions: a "${slug}" collection already exists`);
120
+ }
121
+ }
122
+ const registry = buildRegistry(areas, collections, globals);
123
+ const areaFor = (key, slug)=>{
124
+ const configured = areas.find((a)=>a[key] === slug);
125
+ return configured && registry.find((a)=>a.slug === configured.slug);
126
+ };
127
+ config.custom = {
128
+ ...config.custom,
129
+ permissions: {
130
+ isSuperAdmin: pluginOptions.isSuperAdmin ?? (()=>false),
131
+ keys: registry.flatMap((a)=>a.permissions.map((p)=>p.key)),
132
+ registry,
133
+ usersSlug
134
+ }
135
+ };
136
+ config.collections = [
137
+ ...collections.map((collection)=>{
138
+ if (collection.slug === usersSlug) {
139
+ return extendUsersCollection(collection);
140
+ }
141
+ const area = !pluginOptions.disabled && areaFor('collection', collection.slug);
142
+ return area ? gateCollection(collection, area) : collection;
143
+ }),
144
+ createRolesCollection({
145
+ registry,
146
+ usersSlug
147
+ }),
148
+ createOverridesCollection({
149
+ registry,
150
+ usersSlug
151
+ }),
152
+ createAuditCollection(usersSlug)
153
+ ];
154
+ if (pluginOptions.disabled) {
155
+ return config;
156
+ }
157
+ config.globals = globals.map((global)=>{
158
+ const area = areaFor('global', global.slug);
159
+ return area ? gateGlobal(global, area) : global;
160
+ });
161
+ config.admin = {
162
+ ...config.admin,
163
+ components: {
164
+ ...config.admin?.components,
165
+ afterNavLinks: [
166
+ ...config.admin?.components?.afterNavLinks ?? [],
167
+ `${RSC}#NavLinks`
168
+ ],
169
+ views: {
170
+ ...config.admin?.components?.views,
171
+ permissionsRoles: {
172
+ Component: `${RSC}#RolesView`,
173
+ exact: true,
174
+ path: '/permissions'
175
+ },
176
+ permissionsUsers: {
177
+ Component: `${RSC}#UsersView`,
178
+ exact: true,
179
+ path: '/permissions/users'
180
+ }
181
+ }
182
+ }
183
+ };
184
+ config.endpoints = [
185
+ ...config.endpoints ?? [],
186
+ {
187
+ handler: resetToRoleHandler,
188
+ method: 'post',
189
+ path: RESET_PATH
190
+ }
191
+ ];
192
+ return config;
193
+ };
194
+
195
+ //# sourceMappingURL=index.js.map