payload-access-plugin 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.
- package/LICENSE +21 -0
- package/README.md +220 -2
- package/dist/access/can.d.ts +3 -0
- package/dist/access/can.js +10 -0
- package/dist/access/can.js.map +1 -0
- package/dist/access/combine.d.ts +6 -0
- package/dist/access/combine.js +23 -0
- package/dist/access/combine.js.map +1 -0
- package/dist/access/fields.d.ts +7 -0
- package/dist/access/fields.js +41 -0
- package/dist/access/fields.js.map +1 -0
- package/dist/access/load.d.ts +6 -0
- package/dist/access/load.js +22 -0
- package/dist/access/load.js.map +1 -0
- package/dist/access/manage.d.ts +9 -0
- package/dist/access/manage.js +8 -0
- package/dist/access/manage.js.map +1 -0
- package/dist/access/resolve.d.ts +22 -0
- package/dist/access/resolve.js +106 -0
- package/dist/access/resolve.js.map +1 -0
- package/dist/access/wrap.d.ts +6 -0
- package/dist/access/wrap.js +120 -0
- package/dist/access/wrap.js.map +1 -0
- package/dist/admin/AccessView.d.ts +3 -0
- package/dist/admin/AccessView.js +66 -0
- package/dist/admin/AccessView.js.map +1 -0
- package/dist/admin/Matrix.d.ts +10 -0
- package/dist/admin/Matrix.js +383 -0
- package/dist/admin/Matrix.js.map +1 -0
- package/dist/admin/MatrixCell.d.ts +17 -0
- package/dist/admin/MatrixCell.js +42 -0
- package/dist/admin/MatrixCell.js.map +1 -0
- package/dist/admin/MatrixFilters.d.ts +18 -0
- package/dist/admin/MatrixFilters.js +91 -0
- package/dist/admin/MatrixFilters.js.map +1 -0
- package/dist/admin/NavLink.d.ts +3 -0
- package/dist/admin/NavLink.js +23 -0
- package/dist/admin/NavLink.js.map +1 -0
- package/dist/admin/NavLinkClient.d.ts +9 -0
- package/dist/admin/NavLinkClient.js +36 -0
- package/dist/admin/NavLinkClient.js.map +1 -0
- package/dist/admin/NewRoleDrawer.d.ts +14 -0
- package/dist/admin/NewRoleDrawer.js +226 -0
- package/dist/admin/NewRoleDrawer.js.map +1 -0
- package/dist/admin/RoleGrid.d.ts +21 -0
- package/dist/admin/RoleGrid.js +141 -0
- package/dist/admin/RoleGrid.js.map +1 -0
- package/dist/admin/RolePermissionsField.d.ts +17 -0
- package/dist/admin/RolePermissionsField.js +96 -0
- package/dist/admin/RolePermissionsField.js.map +1 -0
- package/dist/admin/css.d.js +2 -0
- package/dist/admin/css.d.js.map +1 -0
- package/dist/admin/model.d.ts +58 -0
- package/dist/admin/model.js +166 -0
- package/dist/admin/model.js.map +1 -0
- package/dist/admin/steps/FieldsGlobalsStep.d.ts +2 -0
- package/dist/admin/steps/FieldsGlobalsStep.js +21 -0
- package/dist/admin/steps/FieldsGlobalsStep.js.map +1 -0
- package/dist/admin/steps/NameStep.d.ts +7 -0
- package/dist/admin/steps/NameStep.js +74 -0
- package/dist/admin/steps/NameStep.js.map +1 -0
- package/dist/admin/steps/PermissionsStep.d.ts +2 -0
- package/dist/admin/steps/PermissionsStep.js +21 -0
- package/dist/admin/steps/PermissionsStep.js.map +1 -0
- package/dist/admin/steps/ReviewStep.d.ts +6 -0
- package/dist/admin/steps/ReviewStep.js +85 -0
- package/dist/admin/steps/ReviewStep.js.map +1 -0
- package/dist/admin/steps/StartFromStep.d.ts +10 -0
- package/dist/admin/steps/StartFromStep.js +63 -0
- package/dist/admin/steps/StartFromStep.js.map +1 -0
- package/dist/admin/steps/types.d.ts +18 -0
- package/dist/admin/steps/types.js +3 -0
- package/dist/admin/steps/types.js.map +1 -0
- package/dist/admin/styles.module.css +289 -0
- package/dist/collections/roles.d.ts +3 -0
- package/dist/collections/roles.js +80 -0
- package/dist/collections/roles.js.map +1 -0
- package/dist/core/decide.d.ts +3 -0
- package/dist/core/decide.js +9 -0
- package/dist/core/decide.js.map +1 -0
- package/dist/core/locks.d.ts +7 -0
- package/dist/core/locks.js +57 -0
- package/dist/core/locks.js.map +1 -0
- package/dist/core/merge.d.ts +4 -0
- package/dist/core/merge.js +64 -0
- package/dist/core/merge.js.map +1 -0
- package/dist/core/types.d.ts +34 -0
- package/dist/core/types.js +4 -0
- package/dist/core/types.js.map +1 -0
- package/dist/endpoints/me.d.ts +13 -0
- package/dist/endpoints/me.js +55 -0
- package/dist/endpoints/me.js.map +1 -0
- package/dist/endpoints/schema.d.ts +14 -0
- package/dist/endpoints/schema.js +39 -0
- package/dist/endpoints/schema.js.map +1 -0
- package/dist/exports/client.d.ts +7 -0
- package/dist/exports/client.js +9 -0
- package/dist/exports/client.js.map +1 -0
- package/dist/exports/rsc.d.ts +2 -0
- package/dist/exports/rsc.js +4 -0
- package/dist/exports/rsc.js.map +1 -0
- package/dist/fields/owner.d.ts +7 -0
- package/dist/fields/owner.js +32 -0
- package/dist/fields/owner.js.map +1 -0
- package/dist/fields/userRoles.d.ts +3 -0
- package/dist/fields/userRoles.js +17 -0
- package/dist/fields/userRoles.js.map +1 -0
- package/dist/hooks/guardrails.d.ts +17 -0
- package/dist/hooks/guardrails.js +255 -0
- package/dist/hooks/guardrails.js.map +1 -0
- package/dist/i18n.d.ts +74 -0
- package/dist/i18n.js +76 -0
- package/dist/i18n.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +122 -0
- package/dist/index.js.map +1 -0
- package/dist/options.d.ts +45 -0
- package/dist/options.js +203 -0
- package/dist/options.js.map +1 -0
- package/dist/schema.d.ts +6 -0
- package/dist/schema.js +78 -0
- package/dist/schema.js.map +1 -0
- package/dist/seed.d.ts +4 -0
- package/dist/seed.js +35 -0
- package/dist/seed.js.map +1 -0
- package/dist/slug.d.ts +3 -0
- package/dist/slug.js +3 -0
- package/dist/slug.js.map +1 -0
- package/dist/types.d.ts +66 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +113 -4
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { canManageRoles } from '../access/manage.js';
|
|
2
|
+
import { roleGuardrails, slugGuard } from '../hooks/guardrails.js';
|
|
3
|
+
import { translations } from '../i18n.js';
|
|
4
|
+
import { validatePermissions } from '../schema.js';
|
|
5
|
+
export const rolesCollection = (opts, overrides)=>{
|
|
6
|
+
const collection = {
|
|
7
|
+
slug: opts.rolesSlug,
|
|
8
|
+
access: {
|
|
9
|
+
create: ({ req })=>canManageRoles(req, opts),
|
|
10
|
+
delete: ({ req })=>canManageRoles(req, opts),
|
|
11
|
+
read: ({ req })=>Boolean(req.user),
|
|
12
|
+
update: ({ req })=>canManageRoles(req, opts)
|
|
13
|
+
},
|
|
14
|
+
admin: {
|
|
15
|
+
useAsTitle: 'name'
|
|
16
|
+
},
|
|
17
|
+
fields: [
|
|
18
|
+
{
|
|
19
|
+
name: 'name',
|
|
20
|
+
type: 'text',
|
|
21
|
+
required: true
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
name: 'slug',
|
|
25
|
+
type: 'text',
|
|
26
|
+
access: {
|
|
27
|
+
update: ()=>false
|
|
28
|
+
},
|
|
29
|
+
hooks: {
|
|
30
|
+
beforeValidate: [
|
|
31
|
+
slugGuard
|
|
32
|
+
]
|
|
33
|
+
},
|
|
34
|
+
index: true,
|
|
35
|
+
required: true,
|
|
36
|
+
unique: true
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
name: 'description',
|
|
40
|
+
type: 'textarea'
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
name: 'locked',
|
|
44
|
+
type: 'checkbox',
|
|
45
|
+
access: {
|
|
46
|
+
create: ()=>false,
|
|
47
|
+
update: ()=>false
|
|
48
|
+
},
|
|
49
|
+
admin: {
|
|
50
|
+
readOnly: true
|
|
51
|
+
},
|
|
52
|
+
defaultValue: false
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
name: 'permissions',
|
|
56
|
+
type: 'json',
|
|
57
|
+
admin: {
|
|
58
|
+
components: {
|
|
59
|
+
Field: {
|
|
60
|
+
// Translations only: the schema (locks, targets) is fetched from the gated endpoint.
|
|
61
|
+
clientProps: {
|
|
62
|
+
labels: translations(opts.i18n)
|
|
63
|
+
},
|
|
64
|
+
path: 'payload-access-plugin/client#RolePermissionsField'
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
validate: (value)=>validatePermissions(value, opts.targets)
|
|
69
|
+
}
|
|
70
|
+
],
|
|
71
|
+
hooks: roleGuardrails(opts),
|
|
72
|
+
versions: {
|
|
73
|
+
drafts: false,
|
|
74
|
+
maxPerDoc: 0
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
return overrides ? overrides(collection) : collection;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
//# sourceMappingURL=roles.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/collections/roles.ts"],"sourcesContent":["import type { CollectionConfig } from 'payload'\n\nimport type { NormalizedOptions } from '../options.js'\n\nimport { canManageRoles } from '../access/manage.js'\nimport { roleGuardrails, slugGuard } from '../hooks/guardrails.js'\nimport { translations } from '../i18n.js'\nimport { validatePermissions } from '../schema.js'\n\nexport const rolesCollection = (\n opts: NormalizedOptions,\n overrides?: (c: CollectionConfig) => CollectionConfig,\n): CollectionConfig => {\n const collection: CollectionConfig = {\n slug: opts.rolesSlug,\n access: {\n create: ({ req }) => canManageRoles(req, opts),\n delete: ({ req }) => canManageRoles(req, opts),\n read: ({ req }) => Boolean(req.user),\n update: ({ req }) => canManageRoles(req, opts),\n },\n admin: { useAsTitle: 'name' },\n fields: [\n { name: 'name', type: 'text', required: true },\n {\n name: 'slug',\n type: 'text',\n access: { update: () => false },\n hooks: { beforeValidate: [slugGuard] },\n index: true,\n required: true,\n unique: true,\n },\n { name: 'description', type: 'textarea' },\n {\n name: 'locked',\n type: 'checkbox',\n access: { create: () => false, update: () => false },\n admin: { readOnly: true },\n defaultValue: false,\n },\n {\n name: 'permissions',\n type: 'json',\n admin: {\n components: {\n Field: {\n // Translations only: the schema (locks, targets) is fetched from the gated endpoint.\n clientProps: { labels: translations(opts.i18n) },\n path: 'payload-access-plugin/client#RolePermissionsField',\n },\n },\n },\n validate: (value: unknown) => validatePermissions(value, opts.targets),\n },\n ],\n hooks: roleGuardrails(opts),\n versions: { drafts: false, maxPerDoc: 0 },\n }\n return overrides ? overrides(collection) : collection\n}\n"],"names":["canManageRoles","roleGuardrails","slugGuard","translations","validatePermissions","rolesCollection","opts","overrides","collection","slug","rolesSlug","access","create","req","delete","read","Boolean","user","update","admin","useAsTitle","fields","name","type","required","hooks","beforeValidate","index","unique","readOnly","defaultValue","components","Field","clientProps","labels","i18n","path","validate","value","targets","versions","drafts","maxPerDoc"],"mappings":"AAIA,SAASA,cAAc,QAAQ,sBAAqB;AACpD,SAASC,cAAc,EAAEC,SAAS,QAAQ,yBAAwB;AAClE,SAASC,YAAY,QAAQ,aAAY;AACzC,SAASC,mBAAmB,QAAQ,eAAc;AAElD,OAAO,MAAMC,kBAAkB,CAC7BC,MACAC;IAEA,MAAMC,aAA+B;QACnCC,MAAMH,KAAKI,SAAS;QACpBC,QAAQ;YACNC,QAAQ,CAAC,EAAEC,GAAG,EAAE,GAAKb,eAAea,KAAKP;YACzCQ,QAAQ,CAAC,EAAED,GAAG,EAAE,GAAKb,eAAea,KAAKP;YACzCS,MAAM,CAAC,EAAEF,GAAG,EAAE,GAAKG,QAAQH,IAAII,IAAI;YACnCC,QAAQ,CAAC,EAAEL,GAAG,EAAE,GAAKb,eAAea,KAAKP;QAC3C;QACAa,OAAO;YAAEC,YAAY;QAAO;QAC5BC,QAAQ;YACN;gBAAEC,MAAM;gBAAQC,MAAM;gBAAQC,UAAU;YAAK;YAC7C;gBACEF,MAAM;gBACNC,MAAM;gBACNZ,QAAQ;oBAAEO,QAAQ,IAAM;gBAAM;gBAC9BO,OAAO;oBAAEC,gBAAgB;wBAACxB;qBAAU;gBAAC;gBACrCyB,OAAO;gBACPH,UAAU;gBACVI,QAAQ;YACV;YACA;gBAAEN,MAAM;gBAAeC,MAAM;YAAW;YACxC;gBACED,MAAM;gBACNC,MAAM;gBACNZ,QAAQ;oBAAEC,QAAQ,IAAM;oBAAOM,QAAQ,IAAM;gBAAM;gBACnDC,OAAO;oBAAEU,UAAU;gBAAK;gBACxBC,cAAc;YAChB;YACA;gBACER,MAAM;gBACNC,MAAM;gBACNJ,OAAO;oBACLY,YAAY;wBACVC,OAAO;4BACL,qFAAqF;4BACrFC,aAAa;gCAAEC,QAAQ/B,aAAaG,KAAK6B,IAAI;4BAAE;4BAC/CC,MAAM;wBACR;oBACF;gBACF;gBACAC,UAAU,CAACC,QAAmBlC,oBAAoBkC,OAAOhC,KAAKiC,OAAO;YACvE;SACD;QACDd,OAAOxB,eAAeK;QACtBkC,UAAU;YAAEC,QAAQ;YAAOC,WAAW;QAAE;IAC1C;IACA,OAAOnC,YAAYA,UAAUC,cAAcA;AAC7C,EAAC"}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { ResolvedPermissions, Scope, TargetKind } from './types.js';
|
|
2
|
+
/** The scope for an action on a target. Unknown targets or actions are 'none'. */
|
|
3
|
+
export declare const decide: (resolved: ResolvedPermissions, kind: TargetKind, slug: string, action: string) => Scope;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { scopeKey } from './types.js';
|
|
2
|
+
/** The scope for an action on a target. Unknown targets or actions are 'none'. */ export const decide = (resolved, kind, slug, action)=>{
|
|
3
|
+
if (resolved.superAdmin) {
|
|
4
|
+
return 'all';
|
|
5
|
+
}
|
|
6
|
+
return resolved.scopes.get(scopeKey(kind, slug, action)) ?? 'none';
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
//# sourceMappingURL=decide.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/core/decide.ts"],"sourcesContent":["import type { ResolvedPermissions, Scope, TargetKind } from './types.js'\n\nimport { scopeKey } from './types.js'\n\n/** The scope for an action on a target. Unknown targets or actions are 'none'. */\nexport const decide = (\n resolved: ResolvedPermissions,\n kind: TargetKind,\n slug: string,\n action: string,\n): Scope => {\n if (resolved.superAdmin) {\n return 'all'\n }\n return resolved.scopes.get(scopeKey(kind, slug, action)) ?? 'none'\n}\n"],"names":["scopeKey","decide","resolved","kind","slug","action","superAdmin","scopes","get"],"mappings":"AAEA,SAASA,QAAQ,QAAQ,aAAY;AAErC,gFAAgF,GAChF,OAAO,MAAMC,SAAS,CACpBC,UACAC,MACAC,MACAC;IAEA,IAAIH,SAASI,UAAU,EAAE;QACvB,OAAO;IACT;IACA,OAAOJ,SAASK,MAAM,CAACC,GAAG,CAACR,SAASG,MAAMC,MAAMC,YAAY;AAC9D,EAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { KnownTargets, Lock, ResolvedPermissions } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Applies locks in array order on top of role data. Locks win; later locks override earlier ones.
|
|
4
|
+
* Also prunes stale role data (unknown targets, actions, fields), so callers always run
|
|
5
|
+
* mergeRoles then applyLocks before decide.
|
|
6
|
+
*/
|
|
7
|
+
export declare const applyLocks: (resolved: ResolvedPermissions, roleSlugs: string[], locks: Lock[], targets: KnownTargets) => ResolvedPermissions;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { scopeKey } from './types.js';
|
|
2
|
+
const lockScope = {
|
|
3
|
+
allow: 'all',
|
|
4
|
+
deny: 'none',
|
|
5
|
+
own: 'own'
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Applies locks in array order on top of role data. Locks win; later locks override earlier ones.
|
|
9
|
+
* Also prunes stale role data (unknown targets, actions, fields), so callers always run
|
|
10
|
+
* mergeRoles then applyLocks before decide.
|
|
11
|
+
*/ export const applyLocks = (resolved, roleSlugs, locks, targets)=>{
|
|
12
|
+
const scopes = new Map();
|
|
13
|
+
for (const [key, scope] of resolved.scopes){
|
|
14
|
+
const [kind, slug, ...rest] = key.split(':');
|
|
15
|
+
const group = kind === 'collection' ? targets.collections : targets.globals;
|
|
16
|
+
if (Object.hasOwn(group, slug) && group[slug].includes(rest.join(':'))) {
|
|
17
|
+
scopes.set(key, scope);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
const fields = new Map();
|
|
21
|
+
for (const [key, rule] of resolved.fields){
|
|
22
|
+
const [slug, ...rest] = key.split(':');
|
|
23
|
+
if (Object.hasOwn(targets.fields, slug) && targets.fields[slug].includes(rest.join(':'))) {
|
|
24
|
+
fields.set(key, rule);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
for (const lock of locks){
|
|
28
|
+
if (lock.roles !== '*' && !lock.roles.some((slug)=>roleSlugs.includes(slug))) {
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
const scope = lockScope[lock.value];
|
|
32
|
+
for (const [kind, group] of [
|
|
33
|
+
[
|
|
34
|
+
'collection',
|
|
35
|
+
targets.collections
|
|
36
|
+
],
|
|
37
|
+
[
|
|
38
|
+
'global',
|
|
39
|
+
targets.globals
|
|
40
|
+
]
|
|
41
|
+
]){
|
|
42
|
+
for (const [slug, actions] of Object.entries(group)){
|
|
43
|
+
if ((lock.target === '*' || lock.target === slug) && actions.includes(lock.action)) {
|
|
44
|
+
scopes.set(scopeKey(kind, slug, lock.action), scope);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
...resolved,
|
|
51
|
+
custom: new Set(resolved.custom),
|
|
52
|
+
fields,
|
|
53
|
+
scopes
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
//# sourceMappingURL=locks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/core/locks.ts"],"sourcesContent":["import type {\n FieldRule,\n KnownTargets,\n Lock,\n ResolvedPermissions,\n Scope,\n TargetKind,\n} from './types.js'\n\nimport { scopeKey } from './types.js'\n\nconst lockScope: Record<Lock['value'], Scope> = { allow: 'all', deny: 'none', own: 'own' }\n\n/**\n * Applies locks in array order on top of role data. Locks win; later locks override earlier ones.\n * Also prunes stale role data (unknown targets, actions, fields), so callers always run\n * mergeRoles then applyLocks before decide.\n */\nexport const applyLocks = (\n resolved: ResolvedPermissions,\n roleSlugs: string[],\n locks: Lock[],\n targets: KnownTargets,\n): ResolvedPermissions => {\n const scopes = new Map<string, Scope>()\n for (const [key, scope] of resolved.scopes) {\n const [kind, slug, ...rest] = key.split(':')\n const group = kind === 'collection' ? targets.collections : targets.globals\n if (Object.hasOwn(group, slug) && group[slug].includes(rest.join(':'))) {\n scopes.set(key, scope)\n }\n }\n const fields = new Map<string, FieldRule>()\n for (const [key, rule] of resolved.fields) {\n const [slug, ...rest] = key.split(':')\n if (Object.hasOwn(targets.fields, slug) && targets.fields[slug].includes(rest.join(':'))) {\n fields.set(key, rule)\n }\n }\n\n for (const lock of locks) {\n if (lock.roles !== '*' && !lock.roles.some((slug) => roleSlugs.includes(slug))) {\n continue\n }\n const scope = lockScope[lock.value]\n for (const [kind, group] of [\n ['collection', targets.collections],\n ['global', targets.globals],\n ] as [TargetKind, Record<string, string[]>][]) {\n for (const [slug, actions] of Object.entries(group)) {\n if ((lock.target === '*' || lock.target === slug) && actions.includes(lock.action)) {\n scopes.set(scopeKey(kind, slug, lock.action), scope)\n }\n }\n }\n }\n\n return { ...resolved, custom: new Set(resolved.custom), fields, scopes }\n}\n"],"names":["scopeKey","lockScope","allow","deny","own","applyLocks","resolved","roleSlugs","locks","targets","scopes","Map","key","scope","kind","slug","rest","split","group","collections","globals","Object","hasOwn","includes","join","set","fields","rule","lock","roles","some","value","actions","entries","target","action","custom","Set"],"mappings":"AASA,SAASA,QAAQ,QAAQ,aAAY;AAErC,MAAMC,YAA0C;IAAEC,OAAO;IAAOC,MAAM;IAAQC,KAAK;AAAM;AAEzF;;;;CAIC,GACD,OAAO,MAAMC,aAAa,CACxBC,UACAC,WACAC,OACAC;IAEA,MAAMC,SAAS,IAAIC;IACnB,KAAK,MAAM,CAACC,KAAKC,MAAM,IAAIP,SAASI,MAAM,CAAE;QAC1C,MAAM,CAACI,MAAMC,MAAM,GAAGC,KAAK,GAAGJ,IAAIK,KAAK,CAAC;QACxC,MAAMC,QAAQJ,SAAS,eAAeL,QAAQU,WAAW,GAAGV,QAAQW,OAAO;QAC3E,IAAIC,OAAOC,MAAM,CAACJ,OAAOH,SAASG,KAAK,CAACH,KAAK,CAACQ,QAAQ,CAACP,KAAKQ,IAAI,CAAC,OAAO;YACtEd,OAAOe,GAAG,CAACb,KAAKC;QAClB;IACF;IACA,MAAMa,SAAS,IAAIf;IACnB,KAAK,MAAM,CAACC,KAAKe,KAAK,IAAIrB,SAASoB,MAAM,CAAE;QACzC,MAAM,CAACX,MAAM,GAAGC,KAAK,GAAGJ,IAAIK,KAAK,CAAC;QAClC,IAAII,OAAOC,MAAM,CAACb,QAAQiB,MAAM,EAAEX,SAASN,QAAQiB,MAAM,CAACX,KAAK,CAACQ,QAAQ,CAACP,KAAKQ,IAAI,CAAC,OAAO;YACxFE,OAAOD,GAAG,CAACb,KAAKe;QAClB;IACF;IAEA,KAAK,MAAMC,QAAQpB,MAAO;QACxB,IAAIoB,KAAKC,KAAK,KAAK,OAAO,CAACD,KAAKC,KAAK,CAACC,IAAI,CAAC,CAACf,OAASR,UAAUgB,QAAQ,CAACR,QAAQ;YAC9E;QACF;QACA,MAAMF,QAAQZ,SAAS,CAAC2B,KAAKG,KAAK,CAAC;QACnC,KAAK,MAAM,CAACjB,MAAMI,MAAM,IAAI;YAC1B;gBAAC;gBAAcT,QAAQU,WAAW;aAAC;YACnC;gBAAC;gBAAUV,QAAQW,OAAO;aAAC;SAC5B,CAA8C;YAC7C,KAAK,MAAM,CAACL,MAAMiB,QAAQ,IAAIX,OAAOY,OAAO,CAACf,OAAQ;gBACnD,IAAI,AAACU,CAAAA,KAAKM,MAAM,KAAK,OAAON,KAAKM,MAAM,KAAKnB,IAAG,KAAMiB,QAAQT,QAAQ,CAACK,KAAKO,MAAM,GAAG;oBAClFzB,OAAOe,GAAG,CAACzB,SAASc,MAAMC,MAAMa,KAAKO,MAAM,GAAGtB;gBAChD;YACF;QACF;IACF;IAEA,OAAO;QAAE,GAAGP,QAAQ;QAAE8B,QAAQ,IAAIC,IAAI/B,SAAS8B,MAAM;QAAGV;QAAQhB;IAAO;AACzE,EAAC"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { FieldRule, ResolvedPermissions, RolePermissions } from './types.js';
|
|
2
|
+
/** Merges role permissions: the most permissive scope wins; a field rule survives only if every role has one. */
|
|
3
|
+
export declare const mergeRoles: (roles: RolePermissions[]) => ResolvedPermissions;
|
|
4
|
+
export declare const fieldRule: (resolved: ResolvedPermissions, slug: string, path: string) => FieldRule | undefined;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { fieldKey, scopeKey } from './types.js';
|
|
2
|
+
const rank = {
|
|
3
|
+
all: 2,
|
|
4
|
+
none: 0,
|
|
5
|
+
own: 1
|
|
6
|
+
};
|
|
7
|
+
/** Merges role permissions: the most permissive scope wins; a field rule survives only if every role has one. */ export const mergeRoles = (roles)=>{
|
|
8
|
+
const resolved = {
|
|
9
|
+
custom: new Set(),
|
|
10
|
+
fields: new Map(),
|
|
11
|
+
scopes: new Map(),
|
|
12
|
+
superAdmin: false
|
|
13
|
+
};
|
|
14
|
+
const fieldRules = new Map();
|
|
15
|
+
for (const role of roles){
|
|
16
|
+
for (const [kind, group] of [
|
|
17
|
+
[
|
|
18
|
+
'collection',
|
|
19
|
+
role.collections
|
|
20
|
+
],
|
|
21
|
+
[
|
|
22
|
+
'global',
|
|
23
|
+
role.globals
|
|
24
|
+
]
|
|
25
|
+
]){
|
|
26
|
+
for (const [slug, actions] of Object.entries(group ?? {})){
|
|
27
|
+
for (const [action, scope] of Object.entries(actions)){
|
|
28
|
+
if (!scope || !Object.hasOwn(rank, scope)) {
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
const key = scopeKey(kind, slug, action);
|
|
32
|
+
const current = resolved.scopes.get(key);
|
|
33
|
+
if (!current || rank[scope] > rank[current]) {
|
|
34
|
+
resolved.scopes.set(key, scope);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
for (const [slug, paths] of Object.entries(role.fields ?? {})){
|
|
40
|
+
for (const [path, rule] of Object.entries(paths)){
|
|
41
|
+
if (rule !== 'hidden' && rule !== 'readOnly') {
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
const key = fieldKey(slug, path);
|
|
45
|
+
fieldRules.set(key, [
|
|
46
|
+
...fieldRules.get(key) ?? [],
|
|
47
|
+
rule
|
|
48
|
+
]);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
for (const action of role.custom ?? []){
|
|
52
|
+
resolved.custom.add(action);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
for (const [key, rules] of fieldRules){
|
|
56
|
+
if (rules.length === roles.length) {
|
|
57
|
+
resolved.fields.set(key, rules.every((rule)=>rule === 'hidden') ? 'hidden' : 'readOnly');
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return resolved;
|
|
61
|
+
};
|
|
62
|
+
export const fieldRule = (resolved, slug, path)=>resolved.fields.get(fieldKey(slug, path));
|
|
63
|
+
|
|
64
|
+
//# sourceMappingURL=merge.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/core/merge.ts"],"sourcesContent":["import type { FieldRule, ResolvedPermissions, RolePermissions, Scope } from './types.js'\n\nimport { fieldKey, scopeKey } from './types.js'\n\nconst rank: Record<Scope, number> = { all: 2, none: 0, own: 1 }\n\n/** Merges role permissions: the most permissive scope wins; a field rule survives only if every role has one. */\nexport const mergeRoles = (roles: RolePermissions[]): ResolvedPermissions => {\n const resolved: ResolvedPermissions = {\n custom: new Set(),\n fields: new Map(),\n scopes: new Map(),\n superAdmin: false,\n }\n\n const fieldRules = new Map<string, FieldRule[]>()\n\n for (const role of roles) {\n for (const [kind, group] of [\n ['collection', role.collections],\n ['global', role.globals],\n ] as const) {\n for (const [slug, actions] of Object.entries(group ?? {})) {\n for (const [action, scope] of Object.entries(actions)) {\n if (!scope || !Object.hasOwn(rank, scope)) {\n continue\n }\n const key = scopeKey(kind, slug, action)\n const current = resolved.scopes.get(key)\n if (!current || rank[scope] > rank[current]) {\n resolved.scopes.set(key, scope)\n }\n }\n }\n }\n\n for (const [slug, paths] of Object.entries(role.fields ?? {})) {\n for (const [path, rule] of Object.entries(paths)) {\n if (rule !== 'hidden' && rule !== 'readOnly') {\n continue\n }\n const key = fieldKey(slug, path)\n fieldRules.set(key, [...(fieldRules.get(key) ?? []), rule])\n }\n }\n\n for (const action of role.custom ?? []) {\n resolved.custom.add(action)\n }\n }\n\n for (const [key, rules] of fieldRules) {\n if (rules.length === roles.length) {\n resolved.fields.set(key, rules.every((rule) => rule === 'hidden') ? 'hidden' : 'readOnly')\n }\n }\n\n return resolved\n}\n\nexport const fieldRule = (\n resolved: ResolvedPermissions,\n slug: string,\n path: string,\n): FieldRule | undefined => resolved.fields.get(fieldKey(slug, path))\n"],"names":["fieldKey","scopeKey","rank","all","none","own","mergeRoles","roles","resolved","custom","Set","fields","Map","scopes","superAdmin","fieldRules","role","kind","group","collections","globals","slug","actions","Object","entries","action","scope","hasOwn","key","current","get","set","paths","path","rule","add","rules","length","every","fieldRule"],"mappings":"AAEA,SAASA,QAAQ,EAAEC,QAAQ,QAAQ,aAAY;AAE/C,MAAMC,OAA8B;IAAEC,KAAK;IAAGC,MAAM;IAAGC,KAAK;AAAE;AAE9D,+GAA+G,GAC/G,OAAO,MAAMC,aAAa,CAACC;IACzB,MAAMC,WAAgC;QACpCC,QAAQ,IAAIC;QACZC,QAAQ,IAAIC;QACZC,QAAQ,IAAID;QACZE,YAAY;IACd;IAEA,MAAMC,aAAa,IAAIH;IAEvB,KAAK,MAAMI,QAAQT,MAAO;QACxB,KAAK,MAAM,CAACU,MAAMC,MAAM,IAAI;YAC1B;gBAAC;gBAAcF,KAAKG,WAAW;aAAC;YAChC;gBAAC;gBAAUH,KAAKI,OAAO;aAAC;SACzB,CAAW;YACV,KAAK,MAAM,CAACC,MAAMC,QAAQ,IAAIC,OAAOC,OAAO,CAACN,SAAS,CAAC,GAAI;gBACzD,KAAK,MAAM,CAACO,QAAQC,MAAM,IAAIH,OAAOC,OAAO,CAACF,SAAU;oBACrD,IAAI,CAACI,SAAS,CAACH,OAAOI,MAAM,CAACzB,MAAMwB,QAAQ;wBACzC;oBACF;oBACA,MAAME,MAAM3B,SAASgB,MAAMI,MAAMI;oBACjC,MAAMI,UAAUrB,SAASK,MAAM,CAACiB,GAAG,CAACF;oBACpC,IAAI,CAACC,WAAW3B,IAAI,CAACwB,MAAM,GAAGxB,IAAI,CAAC2B,QAAQ,EAAE;wBAC3CrB,SAASK,MAAM,CAACkB,GAAG,CAACH,KAAKF;oBAC3B;gBACF;YACF;QACF;QAEA,KAAK,MAAM,CAACL,MAAMW,MAAM,IAAIT,OAAOC,OAAO,CAACR,KAAKL,MAAM,IAAI,CAAC,GAAI;YAC7D,KAAK,MAAM,CAACsB,MAAMC,KAAK,IAAIX,OAAOC,OAAO,CAACQ,OAAQ;gBAChD,IAAIE,SAAS,YAAYA,SAAS,YAAY;oBAC5C;gBACF;gBACA,MAAMN,MAAM5B,SAASqB,MAAMY;gBAC3BlB,WAAWgB,GAAG,CAACH,KAAK;uBAAKb,WAAWe,GAAG,CAACF,QAAQ,EAAE;oBAAGM;iBAAK;YAC5D;QACF;QAEA,KAAK,MAAMT,UAAUT,KAAKP,MAAM,IAAI,EAAE,CAAE;YACtCD,SAASC,MAAM,CAAC0B,GAAG,CAACV;QACtB;IACF;IAEA,KAAK,MAAM,CAACG,KAAKQ,MAAM,IAAIrB,WAAY;QACrC,IAAIqB,MAAMC,MAAM,KAAK9B,MAAM8B,MAAM,EAAE;YACjC7B,SAASG,MAAM,CAACoB,GAAG,CAACH,KAAKQ,MAAME,KAAK,CAAC,CAACJ,OAASA,SAAS,YAAY,WAAW;QACjF;IACF;IAEA,OAAO1B;AACT,EAAC;AAED,OAAO,MAAM+B,YAAY,CACvB/B,UACAa,MACAY,OAC0BzB,SAASG,MAAM,CAACmB,GAAG,CAAC9B,SAASqB,MAAMY,OAAM"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export type Scope = 'all' | 'none' | 'own';
|
|
2
|
+
export type FieldRule = 'hidden' | 'readOnly';
|
|
3
|
+
export type RolePermissions = {
|
|
4
|
+
collections?: Record<string, Partial<Record<string, Scope>>>;
|
|
5
|
+
custom?: string[];
|
|
6
|
+
fields?: Record<string, Record<string, FieldRule>>;
|
|
7
|
+
globals?: Record<string, Partial<Record<string, Scope>>>;
|
|
8
|
+
};
|
|
9
|
+
export type Lock = {
|
|
10
|
+
action: string;
|
|
11
|
+
roles: '*' | string[];
|
|
12
|
+
/** A collection or global slug, or '*' for all. */
|
|
13
|
+
target: string;
|
|
14
|
+
value: 'allow' | 'deny' | 'own';
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Scope keys are `"<kind>:<slug>:<action>"` where kind is `collection` or `global`.
|
|
18
|
+
* Field keys are `"<slug>:<fieldPath>"`.
|
|
19
|
+
*/
|
|
20
|
+
export type ResolvedPermissions = {
|
|
21
|
+
custom: Set<string>;
|
|
22
|
+
fields: Map<string, FieldRule>;
|
|
23
|
+
scopes: Map<string, Scope>;
|
|
24
|
+
superAdmin: boolean;
|
|
25
|
+
};
|
|
26
|
+
/** Slug to allowed actions (or field paths for `fields`). */
|
|
27
|
+
export type KnownTargets = {
|
|
28
|
+
collections: Record<string, string[]>;
|
|
29
|
+
fields: Record<string, string[]>;
|
|
30
|
+
globals: Record<string, string[]>;
|
|
31
|
+
};
|
|
32
|
+
export type TargetKind = 'collection' | 'global';
|
|
33
|
+
export declare const scopeKey: (kind: TargetKind, slug: string, action: string) => string;
|
|
34
|
+
export declare const fieldKey: (slug: string, path: string) => string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/core/types.ts"],"sourcesContent":["export type Scope = 'all' | 'none' | 'own'\n\nexport type FieldRule = 'hidden' | 'readOnly'\n\nexport type RolePermissions = {\n collections?: Record<string, Partial<Record<string, Scope>>>\n custom?: string[]\n fields?: Record<string, Record<string, FieldRule>>\n globals?: Record<string, Partial<Record<string, Scope>>>\n}\n\nexport type Lock = {\n action: string\n roles: '*' | string[]\n /** A collection or global slug, or '*' for all. */\n target: string\n value: 'allow' | 'deny' | 'own'\n}\n\n/**\n * Scope keys are `\"<kind>:<slug>:<action>\"` where kind is `collection` or `global`.\n * Field keys are `\"<slug>:<fieldPath>\"`.\n */\nexport type ResolvedPermissions = {\n custom: Set<string>\n fields: Map<string, FieldRule>\n scopes: Map<string, Scope>\n superAdmin: boolean\n}\n\n/** Slug to allowed actions (or field paths for `fields`). */\nexport type KnownTargets = {\n collections: Record<string, string[]>\n fields: Record<string, string[]>\n globals: Record<string, string[]>\n}\n\nexport type TargetKind = 'collection' | 'global'\n\nexport const scopeKey = (kind: TargetKind, slug: string, action: string): string =>\n `${kind}:${slug}:${action}`\n\nexport const fieldKey = (slug: string, path: string): string => `${slug}:${path}`\n"],"names":["scopeKey","kind","slug","action","fieldKey","path"],"mappings":"AAuCA,OAAO,MAAMA,WAAW,CAACC,MAAkBC,MAAcC,SACvD,GAAGF,KAAK,CAAC,EAAEC,KAAK,CAAC,EAAEC,QAAQ,CAAA;AAE7B,OAAO,MAAMC,WAAW,CAACF,MAAcG,OAAyB,GAAGH,KAAK,CAAC,EAAEG,MAAM,CAAA"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Endpoint } from 'payload';
|
|
2
|
+
import type { FieldRule, Scope } from '../core/types.js';
|
|
3
|
+
import type { NormalizedOptions } from '../options.js';
|
|
4
|
+
export type MeResponse = {
|
|
5
|
+
collections: Record<string, Record<string, Scope>>;
|
|
6
|
+
custom: string[];
|
|
7
|
+
fields: Record<string, Record<string, FieldRule>>;
|
|
8
|
+
globals: Record<string, Record<string, Scope>>;
|
|
9
|
+
roles: string[];
|
|
10
|
+
superAdmin: boolean;
|
|
11
|
+
};
|
|
12
|
+
/** `GET /access-plugin/me`: the signed-in user's merged permissions, for front ends. */
|
|
13
|
+
export declare const meEndpoint: (opts: NormalizedOptions) => Endpoint;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { idsOf, loadRoles } from '../access/load.js';
|
|
2
|
+
import { getPermissions } from '../access/resolve.js';
|
|
3
|
+
import { decide } from '../core/decide.js';
|
|
4
|
+
import { fieldRule } from '../core/merge.js';
|
|
5
|
+
/** `GET /access-plugin/me`: the signed-in user's merged permissions, for front ends. */ export const meEndpoint = (opts)=>({
|
|
6
|
+
handler: async (req)=>{
|
|
7
|
+
if (!req.user) {
|
|
8
|
+
return Response.json({
|
|
9
|
+
errors: [
|
|
10
|
+
{
|
|
11
|
+
message: 'You must be signed in.'
|
|
12
|
+
}
|
|
13
|
+
]
|
|
14
|
+
}, {
|
|
15
|
+
status: 401
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
const resolved = await getPermissions(req, opts);
|
|
19
|
+
const roleDocs = await loadRoles(req, opts, idsOf(req.user.roles));
|
|
20
|
+
const scopes = (kind, targets)=>Object.fromEntries(Object.entries(targets).map(([slug, actions])=>[
|
|
21
|
+
slug,
|
|
22
|
+
Object.fromEntries(actions.map((action)=>[
|
|
23
|
+
action,
|
|
24
|
+
decide(resolved, kind, slug, action)
|
|
25
|
+
]))
|
|
26
|
+
]));
|
|
27
|
+
const fields = {};
|
|
28
|
+
if (!resolved.superAdmin) {
|
|
29
|
+
for (const [slug, paths] of Object.entries(opts.targets.fields)){
|
|
30
|
+
for (const path of paths){
|
|
31
|
+
const rule = fieldRule(resolved, slug, path);
|
|
32
|
+
if (rule) {
|
|
33
|
+
;
|
|
34
|
+
(fields[slug] ??= {})[path] = rule;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const body = {
|
|
40
|
+
collections: scopes('collection', opts.targets.collections),
|
|
41
|
+
custom: [
|
|
42
|
+
...resolved.custom
|
|
43
|
+
],
|
|
44
|
+
fields,
|
|
45
|
+
globals: scopes('global', opts.targets.globals),
|
|
46
|
+
roles: roleDocs.map((doc)=>doc.slug).filter((slug)=>Boolean(slug)),
|
|
47
|
+
superAdmin: resolved.superAdmin
|
|
48
|
+
};
|
|
49
|
+
return Response.json(body);
|
|
50
|
+
},
|
|
51
|
+
method: 'get',
|
|
52
|
+
path: '/access-plugin/me'
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
//# sourceMappingURL=me.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/endpoints/me.ts"],"sourcesContent":["import type { Endpoint } from 'payload'\n\nimport type { FieldRule, Scope } from '../core/types.js'\nimport type { NormalizedOptions } from '../options.js'\n\nimport { idsOf, loadRoles } from '../access/load.js'\nimport { getPermissions } from '../access/resolve.js'\nimport { decide } from '../core/decide.js'\nimport { fieldRule } from '../core/merge.js'\n\nexport type MeResponse = {\n collections: Record<string, Record<string, Scope>>\n custom: string[]\n fields: Record<string, Record<string, FieldRule>>\n globals: Record<string, Record<string, Scope>>\n roles: string[]\n superAdmin: boolean\n}\n\n/** `GET /access-plugin/me`: the signed-in user's merged permissions, for front ends. */\nexport const meEndpoint = (opts: NormalizedOptions): Endpoint => ({\n handler: async (req) => {\n if (!req.user) {\n return Response.json({ errors: [{ message: 'You must be signed in.' }] }, { status: 401 })\n }\n const resolved = await getPermissions(req, opts)\n const roleDocs = (await loadRoles(\n req,\n opts,\n idsOf((req.user as { roles?: unknown }).roles),\n )) as { slug?: string }[]\n\n const scopes = (kind: 'collection' | 'global', targets: Record<string, string[]>) =>\n Object.fromEntries(\n Object.entries(targets).map(([slug, actions]) => [\n slug,\n Object.fromEntries(\n actions.map((action) => [action, decide(resolved, kind, slug, action)]),\n ),\n ]),\n )\n\n const fields: MeResponse['fields'] = {}\n if (!resolved.superAdmin) {\n for (const [slug, paths] of Object.entries(opts.targets.fields)) {\n for (const path of paths) {\n const rule = fieldRule(resolved, slug, path)\n if (rule) {\n ;(fields[slug] ??= {})[path] = rule\n }\n }\n }\n }\n\n const body: MeResponse = {\n collections: scopes('collection', opts.targets.collections),\n custom: [...resolved.custom],\n fields,\n globals: scopes('global', opts.targets.globals),\n roles: roleDocs.map((doc) => doc.slug).filter((slug): slug is string => Boolean(slug)),\n superAdmin: resolved.superAdmin,\n }\n return Response.json(body)\n },\n method: 'get',\n path: '/access-plugin/me',\n})\n"],"names":["idsOf","loadRoles","getPermissions","decide","fieldRule","meEndpoint","opts","handler","req","user","Response","json","errors","message","status","resolved","roleDocs","roles","scopes","kind","targets","Object","fromEntries","entries","map","slug","actions","action","fields","superAdmin","paths","path","rule","body","collections","custom","globals","doc","filter","Boolean","method"],"mappings":"AAKA,SAASA,KAAK,EAAEC,SAAS,QAAQ,oBAAmB;AACpD,SAASC,cAAc,QAAQ,uBAAsB;AACrD,SAASC,MAAM,QAAQ,oBAAmB;AAC1C,SAASC,SAAS,QAAQ,mBAAkB;AAW5C,sFAAsF,GACtF,OAAO,MAAMC,aAAa,CAACC,OAAuC,CAAA;QAChEC,SAAS,OAAOC;YACd,IAAI,CAACA,IAAIC,IAAI,EAAE;gBACb,OAAOC,SAASC,IAAI,CAAC;oBAAEC,QAAQ;wBAAC;4BAAEC,SAAS;wBAAyB;qBAAE;gBAAC,GAAG;oBAAEC,QAAQ;gBAAI;YAC1F;YACA,MAAMC,WAAW,MAAMb,eAAeM,KAAKF;YAC3C,MAAMU,WAAY,MAAMf,UACtBO,KACAF,MACAN,MAAM,AAACQ,IAAIC,IAAI,CAAyBQ,KAAK;YAG/C,MAAMC,SAAS,CAACC,MAA+BC,UAC7CC,OAAOC,WAAW,CAChBD,OAAOE,OAAO,CAACH,SAASI,GAAG,CAAC,CAAC,CAACC,MAAMC,QAAQ,GAAK;wBAC/CD;wBACAJ,OAAOC,WAAW,CAChBI,QAAQF,GAAG,CAAC,CAACG,SAAW;gCAACA;gCAAQxB,OAAOY,UAAUI,MAAMM,MAAME;6BAAQ;qBAEzE;YAGL,MAAMC,SAA+B,CAAC;YACtC,IAAI,CAACb,SAASc,UAAU,EAAE;gBACxB,KAAK,MAAM,CAACJ,MAAMK,MAAM,IAAIT,OAAOE,OAAO,CAACjB,KAAKc,OAAO,CAACQ,MAAM,EAAG;oBAC/D,KAAK,MAAMG,QAAQD,MAAO;wBACxB,MAAME,OAAO5B,UAAUW,UAAUU,MAAMM;wBACvC,IAAIC,MAAM;;4BACNJ,CAAAA,MAAM,CAACH,KAAK,KAAK,CAAC,CAAA,CAAE,CAACM,KAAK,GAAGC;wBACjC;oBACF;gBACF;YACF;YAEA,MAAMC,OAAmB;gBACvBC,aAAahB,OAAO,cAAcZ,KAAKc,OAAO,CAACc,WAAW;gBAC1DC,QAAQ;uBAAIpB,SAASoB,MAAM;iBAAC;gBAC5BP;gBACAQ,SAASlB,OAAO,UAAUZ,KAAKc,OAAO,CAACgB,OAAO;gBAC9CnB,OAAOD,SAASQ,GAAG,CAAC,CAACa,MAAQA,IAAIZ,IAAI,EAAEa,MAAM,CAAC,CAACb,OAAyBc,QAAQd;gBAChFI,YAAYd,SAASc,UAAU;YACjC;YACA,OAAOnB,SAASC,IAAI,CAACsB;QACvB;QACAO,QAAQ;QACRT,MAAM;IACR,CAAA,EAAE"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Endpoint } from 'payload';
|
|
2
|
+
import type { Lock } from '../core/types.js';
|
|
3
|
+
import type { NormalizedOptions, TargetLabels } from '../options.js';
|
|
4
|
+
import type { CustomAction } from '../types.js';
|
|
5
|
+
export type SchemaResponse = {
|
|
6
|
+
customActions: CustomAction[];
|
|
7
|
+
locks: Lock[];
|
|
8
|
+
ownership: string[];
|
|
9
|
+
rolesSlug: string;
|
|
10
|
+
targets: TargetLabels;
|
|
11
|
+
};
|
|
12
|
+
export declare const buildSchema: (opts: NormalizedOptions) => SchemaResponse;
|
|
13
|
+
/** `GET /access-plugin/schema`: what the matrix screen shows. Only for `manageRoles`. */
|
|
14
|
+
export declare const schemaEndpoint: (opts: NormalizedOptions) => Endpoint;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { canManageRoles } from '../access/manage.js';
|
|
2
|
+
export const buildSchema = (opts)=>({
|
|
3
|
+
customActions: opts.customActions,
|
|
4
|
+
locks: opts.locks,
|
|
5
|
+
ownership: Object.keys(opts.ownership),
|
|
6
|
+
rolesSlug: opts.rolesSlug,
|
|
7
|
+
targets: opts.labels
|
|
8
|
+
});
|
|
9
|
+
/** `GET /access-plugin/schema`: what the matrix screen shows. Only for `manageRoles`. */ export const schemaEndpoint = (opts)=>({
|
|
10
|
+
handler: async (req)=>{
|
|
11
|
+
if (!req.user) {
|
|
12
|
+
return Response.json({
|
|
13
|
+
errors: [
|
|
14
|
+
{
|
|
15
|
+
message: 'You must be signed in.'
|
|
16
|
+
}
|
|
17
|
+
]
|
|
18
|
+
}, {
|
|
19
|
+
status: 401
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
if (!await canManageRoles(req, opts)) {
|
|
23
|
+
return Response.json({
|
|
24
|
+
errors: [
|
|
25
|
+
{
|
|
26
|
+
message: 'You are not allowed to manage roles.'
|
|
27
|
+
}
|
|
28
|
+
]
|
|
29
|
+
}, {
|
|
30
|
+
status: 403
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
return Response.json(buildSchema(opts));
|
|
34
|
+
},
|
|
35
|
+
method: 'get',
|
|
36
|
+
path: '/access-plugin/schema'
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
//# sourceMappingURL=schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/endpoints/schema.ts"],"sourcesContent":["import type { Endpoint } from 'payload'\n\nimport type { Lock } from '../core/types.js'\nimport type { NormalizedOptions, TargetLabels } from '../options.js'\nimport type { CustomAction } from '../types.js'\n\nimport { canManageRoles } from '../access/manage.js'\n\nexport type SchemaResponse = {\n customActions: CustomAction[]\n locks: Lock[]\n ownership: string[]\n rolesSlug: string\n targets: TargetLabels\n}\n\nexport const buildSchema = (opts: NormalizedOptions): SchemaResponse => ({\n customActions: opts.customActions,\n locks: opts.locks,\n ownership: Object.keys(opts.ownership),\n rolesSlug: opts.rolesSlug,\n targets: opts.labels,\n})\n\n/** `GET /access-plugin/schema`: what the matrix screen shows. Only for `manageRoles`. */\nexport const schemaEndpoint = (opts: NormalizedOptions): Endpoint => ({\n handler: async (req) => {\n if (!req.user) {\n return Response.json({ errors: [{ message: 'You must be signed in.' }] }, { status: 401 })\n }\n if (!(await canManageRoles(req, opts))) {\n return Response.json(\n { errors: [{ message: 'You are not allowed to manage roles.' }] },\n { status: 403 },\n )\n }\n return Response.json(buildSchema(opts))\n },\n method: 'get',\n path: '/access-plugin/schema',\n})\n"],"names":["canManageRoles","buildSchema","opts","customActions","locks","ownership","Object","keys","rolesSlug","targets","labels","schemaEndpoint","handler","req","user","Response","json","errors","message","status","method","path"],"mappings":"AAMA,SAASA,cAAc,QAAQ,sBAAqB;AAUpD,OAAO,MAAMC,cAAc,CAACC,OAA6C,CAAA;QACvEC,eAAeD,KAAKC,aAAa;QACjCC,OAAOF,KAAKE,KAAK;QACjBC,WAAWC,OAAOC,IAAI,CAACL,KAAKG,SAAS;QACrCG,WAAWN,KAAKM,SAAS;QACzBC,SAASP,KAAKQ,MAAM;IACtB,CAAA,EAAE;AAEF,uFAAuF,GACvF,OAAO,MAAMC,iBAAiB,CAACT,OAAuC,CAAA;QACpEU,SAAS,OAAOC;YACd,IAAI,CAACA,IAAIC,IAAI,EAAE;gBACb,OAAOC,SAASC,IAAI,CAAC;oBAAEC,QAAQ;wBAAC;4BAAEC,SAAS;wBAAyB;qBAAE;gBAAC,GAAG;oBAAEC,QAAQ;gBAAI;YAC1F;YACA,IAAI,CAAE,MAAMnB,eAAea,KAAKX,OAAQ;gBACtC,OAAOa,SAASC,IAAI,CAClB;oBAAEC,QAAQ;wBAAC;4BAAEC,SAAS;wBAAuC;qBAAE;gBAAC,GAChE;oBAAEC,QAAQ;gBAAI;YAElB;YACA,OAAOJ,SAASC,IAAI,CAACf,YAAYC;QACnC;QACAkB,QAAQ;QACRC,MAAM;IACR,CAAA,EAAE"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { Matrix } from '../admin/Matrix.js';
|
|
2
|
+
export { MatrixCell } from '../admin/MatrixCell.js';
|
|
3
|
+
export { MatrixFilters } from '../admin/MatrixFilters.js';
|
|
4
|
+
export { NavLinkClient } from '../admin/NavLinkClient.js';
|
|
5
|
+
export { NewRoleDrawer } from '../admin/NewRoleDrawer.js';
|
|
6
|
+
export { RoleGrid } from '../admin/RoleGrid.js';
|
|
7
|
+
export { RolePermissionsField } from '../admin/RolePermissionsField.js';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { Matrix } from '../admin/Matrix.js';
|
|
2
|
+
export { MatrixCell } from '../admin/MatrixCell.js';
|
|
3
|
+
export { MatrixFilters } from '../admin/MatrixFilters.js';
|
|
4
|
+
export { NavLinkClient } from '../admin/NavLinkClient.js';
|
|
5
|
+
export { NewRoleDrawer } from '../admin/NewRoleDrawer.js';
|
|
6
|
+
export { RoleGrid } from '../admin/RoleGrid.js';
|
|
7
|
+
export { RolePermissionsField } from '../admin/RolePermissionsField.js';
|
|
8
|
+
|
|
9
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/exports/client.ts"],"sourcesContent":["export { Matrix } from '../admin/Matrix.js'\nexport { MatrixCell } from '../admin/MatrixCell.js'\nexport { MatrixFilters } from '../admin/MatrixFilters.js'\nexport { NavLinkClient } from '../admin/NavLinkClient.js'\nexport { NewRoleDrawer } from '../admin/NewRoleDrawer.js'\nexport { RoleGrid } from '../admin/RoleGrid.js'\nexport { RolePermissionsField } from '../admin/RolePermissionsField.js'\n"],"names":["Matrix","MatrixCell","MatrixFilters","NavLinkClient","NewRoleDrawer","RoleGrid","RolePermissionsField"],"mappings":"AAAA,SAASA,MAAM,QAAQ,qBAAoB;AAC3C,SAASC,UAAU,QAAQ,yBAAwB;AACnD,SAASC,aAAa,QAAQ,4BAA2B;AACzD,SAASC,aAAa,QAAQ,4BAA2B;AACzD,SAASC,aAAa,QAAQ,4BAA2B;AACzD,SAASC,QAAQ,QAAQ,uBAAsB;AAC/C,SAASC,oBAAoB,QAAQ,mCAAkC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/exports/rsc.ts"],"sourcesContent":["export { AccessView } from '../admin/AccessView.js'\nexport { NavLink } from '../admin/NavLink.js'\n"],"names":["AccessView","NavLink"],"mappings":"AAAA,SAASA,UAAU,QAAQ,yBAAwB;AACnD,SAASC,OAAO,QAAQ,sBAAqB"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { RelationshipField } from 'payload';
|
|
2
|
+
import type { NormalizedOptions } from '../options.js';
|
|
3
|
+
/**
|
|
4
|
+
* The owner field behind the `own` scope: a read-only relationship to the first users
|
|
5
|
+
* collection, set from the signed-in user on create and kept as stored on update.
|
|
6
|
+
*/
|
|
7
|
+
export declare const ownerField: (name: string, opts: NormalizedOptions) => RelationshipField;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { asCollection } from '../slug.js';
|
|
2
|
+
/**
|
|
3
|
+
* The owner field behind the `own` scope: a read-only relationship to the first users
|
|
4
|
+
* collection, set from the signed-in user on create and kept as stored on update.
|
|
5
|
+
*/ export const ownerField = (name, opts)=>{
|
|
6
|
+
const usersSlug = opts.usersSlugs[0];
|
|
7
|
+
return {
|
|
8
|
+
name,
|
|
9
|
+
type: 'relationship',
|
|
10
|
+
admin: {
|
|
11
|
+
position: 'sidebar',
|
|
12
|
+
readOnly: true
|
|
13
|
+
},
|
|
14
|
+
hooks: {
|
|
15
|
+
beforeChange: [
|
|
16
|
+
({ operation, originalDoc, req, value })=>{
|
|
17
|
+
if (operation === 'create') {
|
|
18
|
+
if (!req.user) {
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
return req.user.collection === usersSlug ? req.user.id : undefined;
|
|
22
|
+
}
|
|
23
|
+
const stored = originalDoc?.[name];
|
|
24
|
+
return typeof stored === 'object' && stored !== null ? stored.id : stored;
|
|
25
|
+
}
|
|
26
|
+
]
|
|
27
|
+
},
|
|
28
|
+
relationTo: asCollection(usersSlug)
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
//# sourceMappingURL=owner.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/fields/owner.ts"],"sourcesContent":["import type { RelationshipField } from 'payload'\n\nimport type { NormalizedOptions } from '../options.js'\n\nimport { asCollection } from '../slug.js'\n\n/**\n * The owner field behind the `own` scope: a read-only relationship to the first users\n * collection, set from the signed-in user on create and kept as stored on update.\n */\nexport const ownerField = (name: string, opts: NormalizedOptions): RelationshipField => {\n const usersSlug = opts.usersSlugs[0]\n return {\n name,\n type: 'relationship',\n admin: { position: 'sidebar', readOnly: true },\n hooks: {\n beforeChange: [\n ({ operation, originalDoc, req, value }) => {\n if (operation === 'create') {\n if (!req.user) {\n return value\n }\n return req.user.collection === usersSlug ? req.user.id : undefined\n }\n const stored = originalDoc?.[name] as unknown\n return typeof stored === 'object' && stored !== null\n ? (stored as { id: unknown }).id\n : stored\n },\n ],\n },\n relationTo: asCollection(usersSlug),\n }\n}\n"],"names":["asCollection","ownerField","name","opts","usersSlug","usersSlugs","type","admin","position","readOnly","hooks","beforeChange","operation","originalDoc","req","value","user","collection","id","undefined","stored","relationTo"],"mappings":"AAIA,SAASA,YAAY,QAAQ,aAAY;AAEzC;;;CAGC,GACD,OAAO,MAAMC,aAAa,CAACC,MAAcC;IACvC,MAAMC,YAAYD,KAAKE,UAAU,CAAC,EAAE;IACpC,OAAO;QACLH;QACAI,MAAM;QACNC,OAAO;YAAEC,UAAU;YAAWC,UAAU;QAAK;QAC7CC,OAAO;YACLC,cAAc;gBACZ,CAAC,EAAEC,SAAS,EAAEC,WAAW,EAAEC,GAAG,EAAEC,KAAK,EAAE;oBACrC,IAAIH,cAAc,UAAU;wBAC1B,IAAI,CAACE,IAAIE,IAAI,EAAE;4BACb,OAAOD;wBACT;wBACA,OAAOD,IAAIE,IAAI,CAACC,UAAU,KAAKb,YAAYU,IAAIE,IAAI,CAACE,EAAE,GAAGC;oBAC3D;oBACA,MAAMC,SAASP,aAAa,CAACX,KAAK;oBAClC,OAAO,OAAOkB,WAAW,YAAYA,WAAW,OAC5C,AAACA,OAA2BF,EAAE,GAC9BE;gBACN;aACD;QACH;QACAC,YAAYrB,aAAaI;IAC3B;AACF,EAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { canManageRoles } from '../access/manage.js';
|
|
2
|
+
import { noUsersYet } from '../hooks/guardrails.js';
|
|
3
|
+
import { asCollection } from '../slug.js';
|
|
4
|
+
export const userRolesField = (opts)=>({
|
|
5
|
+
name: 'roles',
|
|
6
|
+
type: 'relationship',
|
|
7
|
+
access: {
|
|
8
|
+
// The first user of a fresh install may be given a super-admin role.
|
|
9
|
+
create: async ({ req })=>await canManageRoles(req, opts) || await noUsersYet(req, opts),
|
|
10
|
+
update: ({ req })=>canManageRoles(req, opts)
|
|
11
|
+
},
|
|
12
|
+
hasMany: true,
|
|
13
|
+
relationTo: asCollection(opts.rolesSlug),
|
|
14
|
+
saveToJWT: false
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
//# sourceMappingURL=userRoles.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/fields/userRoles.ts"],"sourcesContent":["import type { RelationshipField } from 'payload'\n\nimport type { NormalizedOptions } from '../options.js'\n\nimport { canManageRoles } from '../access/manage.js'\nimport { noUsersYet } from '../hooks/guardrails.js'\nimport { asCollection } from '../slug.js'\n\nexport const userRolesField = (opts: NormalizedOptions): RelationshipField => ({\n name: 'roles',\n type: 'relationship',\n access: {\n // The first user of a fresh install may be given a super-admin role.\n create: async ({ req }) => (await canManageRoles(req, opts)) || (await noUsersYet(req, opts)),\n update: ({ req }) => canManageRoles(req, opts),\n },\n hasMany: true,\n relationTo: asCollection(opts.rolesSlug),\n saveToJWT: false,\n})\n"],"names":["canManageRoles","noUsersYet","asCollection","userRolesField","opts","name","type","access","create","req","update","hasMany","relationTo","rolesSlug","saveToJWT"],"mappings":"AAIA,SAASA,cAAc,QAAQ,sBAAqB;AACpD,SAASC,UAAU,QAAQ,yBAAwB;AACnD,SAASC,YAAY,QAAQ,aAAY;AAEzC,OAAO,MAAMC,iBAAiB,CAACC,OAAgD,CAAA;QAC7EC,MAAM;QACNC,MAAM;QACNC,QAAQ;YACN,qEAAqE;YACrEC,QAAQ,OAAO,EAAEC,GAAG,EAAE,GAAK,AAAC,MAAMT,eAAeS,KAAKL,SAAW,MAAMH,WAAWQ,KAAKL;YACvFM,QAAQ,CAAC,EAAED,GAAG,EAAE,GAAKT,eAAeS,KAAKL;QAC3C;QACAO,SAAS;QACTC,YAAYV,aAAaE,KAAKS,SAAS;QACvCC,WAAW;IACb,CAAA,EAAE"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { CollectionBeforeChangeHook, CollectionBeforeDeleteHook, CollectionBeforeOperationHook, CollectionConfig, FieldHook, PayloadRequest } from 'payload';
|
|
2
|
+
import type { NormalizedOptions } from '../options.js';
|
|
3
|
+
/** Guardrails on the roles collection. Kept on even when the plugin is disabled. */
|
|
4
|
+
export declare const roleGuardrails: (opts: NormalizedOptions) => CollectionConfig["hooks"];
|
|
5
|
+
/**
|
|
6
|
+
* Role-field slug guard: the slug is fixed once created. Runs before field access, which then
|
|
7
|
+
* makes the field read-only in the admin UI.
|
|
8
|
+
*/
|
|
9
|
+
export declare const slugGuard: FieldHook;
|
|
10
|
+
/** True when no user exists yet in any users collection (the create-first-user case). */
|
|
11
|
+
export declare const noUsersYet: (req: PayloadRequest, opts: NormalizedOptions) => Promise<boolean>;
|
|
12
|
+
/** Guardrails on a users collection: default roles and the last super admin. */
|
|
13
|
+
export declare const userGuardrails: (opts: NormalizedOptions, slug: string) => {
|
|
14
|
+
beforeChange: CollectionBeforeChangeHook;
|
|
15
|
+
beforeDelete: CollectionBeforeDeleteHook;
|
|
16
|
+
beforeOperation: CollectionBeforeOperationHook;
|
|
17
|
+
};
|