@foundrykit/permissions-plugin 0.0.0-stage → 0.1.1
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/README.md +107 -2
- package/dist/access.d.ts +38 -0
- package/dist/access.js +119 -0
- package/dist/access.js.map +1 -0
- package/dist/audit.d.ts +25 -0
- package/dist/audit.js +95 -0
- package/dist/audit.js.map +1 -0
- package/dist/collections/PermissionOverrides.d.ts +6 -0
- package/dist/collections/PermissionOverrides.js +152 -0
- package/dist/collections/PermissionOverrides.js.map +1 -0
- package/dist/collections/Roles.d.ts +6 -0
- package/dist/collections/Roles.js +106 -0
- package/dist/collections/Roles.js.map +1 -0
- package/dist/components/NavLinks.d.ts +3 -0
- package/dist/components/NavLinks.js +20 -0
- package/dist/components/NavLinks.js.map +1 -0
- package/dist/components/PublishButtonGate.d.ts +6 -0
- package/dist/components/PublishButtonGate.js +12 -0
- package/dist/components/PublishButtonGate.js.map +1 -0
- package/dist/components/RolesMatrix.d.ts +3 -0
- package/dist/components/RolesMatrix.js +324 -0
- package/dist/components/RolesMatrix.js.map +1 -0
- package/dist/components/UsersTable.d.ts +3 -0
- package/dist/components/UsersTable.js +390 -0
- package/dist/components/UsersTable.js.map +1 -0
- package/dist/components/api.d.ts +2 -0
- package/dist/components/api.js +17 -0
- package/dist/components/api.js.map +1 -0
- package/dist/components/index.scss +335 -0
- package/dist/endpoints/resetToRole.d.ts +4 -0
- package/dist/endpoints/resetToRole.js +53 -0
- package/dist/endpoints/resetToRole.js.map +1 -0
- package/dist/exports/client.d.ts +1 -0
- package/dist/exports/client.js +3 -0
- package/dist/exports/client.js.map +1 -0
- package/dist/exports/rsc.d.ts +4 -0
- package/dist/exports/rsc.js +6 -0
- package/dist/exports/rsc.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +194 -0
- package/dist/index.js.map +1 -0
- package/dist/registry.d.ts +5 -0
- package/dist/registry.js +87 -0
- package/dist/registry.js.map +1 -0
- package/dist/registry.test.d.ts +1 -0
- package/dist/resolve.d.ts +21 -0
- package/dist/resolve.js +33 -0
- package/dist/resolve.js.map +1 -0
- package/dist/resolve.test.d.ts +1 -0
- package/dist/rules.d.ts +5 -0
- package/dist/rules.js +20 -0
- package/dist/rules.js.map +1 -0
- package/dist/types.d.ts +59 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist/users.d.ts +3 -0
- package/dist/users.js +142 -0
- package/dist/users.js.map +1 -0
- package/dist/views/PermissionsLayout.d.ts +11 -0
- package/dist/views/PermissionsLayout.js +69 -0
- package/dist/views/PermissionsLayout.js.map +1 -0
- package/dist/views/RolesView.d.ts +2 -0
- package/dist/views/RolesView.js +20 -0
- package/dist/views/RolesView.js.map +1 -0
- package/dist/views/UsersView.d.ts +2 -0
- package/dist/views/UsersView.js +14 -0
- package/dist/views/UsersView.js.map +1 -0
- package/dist/views/data.d.ts +36 -0
- package/dist/views/data.js +58 -0
- package/dist/views/data.js.map +1 -0
- package/package.json +107 -4
package/dist/registry.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
export const MANAGE_PERMISSION = 'permissions.manage';
|
|
2
|
+
const STANDARD_LABELS = {
|
|
3
|
+
create: 'Create',
|
|
4
|
+
delete: 'Delete',
|
|
5
|
+
edit: 'Edit',
|
|
6
|
+
publish: 'Publish',
|
|
7
|
+
read: 'Read'
|
|
8
|
+
};
|
|
9
|
+
const hasDrafts = (entity)=>typeof entity.versions === 'object' && Boolean(entity.versions?.drafts);
|
|
10
|
+
const defaultActions = (area, collections, globals)=>{
|
|
11
|
+
if (area.collection) {
|
|
12
|
+
const collection = collections.find((c)=>c.slug === area.collection);
|
|
13
|
+
if (!collection) {
|
|
14
|
+
throw new Error(`permissions: area "${area.slug}" maps to unknown collection "${area.collection}"`);
|
|
15
|
+
}
|
|
16
|
+
const actions = [
|
|
17
|
+
'read',
|
|
18
|
+
'create',
|
|
19
|
+
'edit',
|
|
20
|
+
'delete'
|
|
21
|
+
];
|
|
22
|
+
return hasDrafts(collection) ? [
|
|
23
|
+
...actions,
|
|
24
|
+
'publish'
|
|
25
|
+
] : actions;
|
|
26
|
+
}
|
|
27
|
+
if (area.global) {
|
|
28
|
+
const global = globals.find((g)=>g.slug === area.global);
|
|
29
|
+
if (!global) {
|
|
30
|
+
throw new Error(`permissions: area "${area.slug}" maps to unknown global "${area.global}"`);
|
|
31
|
+
}
|
|
32
|
+
return hasDrafts(global) ? [
|
|
33
|
+
'read',
|
|
34
|
+
'edit',
|
|
35
|
+
'publish'
|
|
36
|
+
] : [
|
|
37
|
+
'read',
|
|
38
|
+
'edit'
|
|
39
|
+
];
|
|
40
|
+
}
|
|
41
|
+
return [];
|
|
42
|
+
};
|
|
43
|
+
/** Turn the site's area config into the full list of `area.action` permissions. */ export const buildRegistry = (areas, collections, globals)=>{
|
|
44
|
+
const seen = new Set();
|
|
45
|
+
const registry = [
|
|
46
|
+
...areas,
|
|
47
|
+
{
|
|
48
|
+
slug: 'permissions',
|
|
49
|
+
customActions: {
|
|
50
|
+
manage: 'Manage roles and permissions'
|
|
51
|
+
},
|
|
52
|
+
label: 'Permissions'
|
|
53
|
+
}
|
|
54
|
+
].map((area)=>{
|
|
55
|
+
if (area.slug.includes('.')) {
|
|
56
|
+
throw new Error(`permissions: area slug "${area.slug}" can't contain "."`);
|
|
57
|
+
}
|
|
58
|
+
if (seen.has(area.slug)) {
|
|
59
|
+
throw new Error(`permissions: area "${area.slug}" is registered twice`);
|
|
60
|
+
}
|
|
61
|
+
seen.add(area.slug);
|
|
62
|
+
const standard = (area.actions ?? defaultActions(area, collections, globals)).map((action)=>({
|
|
63
|
+
action,
|
|
64
|
+
label: STANDARD_LABELS[action]
|
|
65
|
+
}));
|
|
66
|
+
const custom = Object.entries(area.customActions ?? {}).map(([action, label])=>({
|
|
67
|
+
action,
|
|
68
|
+
label
|
|
69
|
+
}));
|
|
70
|
+
return {
|
|
71
|
+
slug: area.slug,
|
|
72
|
+
label: area.label ?? area.slug,
|
|
73
|
+
permissions: [
|
|
74
|
+
...standard,
|
|
75
|
+
...custom
|
|
76
|
+
].map(({ action, label })=>({
|
|
77
|
+
action,
|
|
78
|
+
area: area.slug,
|
|
79
|
+
key: `${area.slug}.${action}`,
|
|
80
|
+
label
|
|
81
|
+
}))
|
|
82
|
+
};
|
|
83
|
+
});
|
|
84
|
+
return registry;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
//# sourceMappingURL=registry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/registry.ts"],"sourcesContent":["import type { CollectionConfig, GlobalConfig } from 'payload'\n\nimport type { Area, AreaConfig, StandardAction } from './types.js'\n\nexport const MANAGE_PERMISSION = 'permissions.manage'\n\nconst STANDARD_LABELS: Record<StandardAction, string> = {\n create: 'Create',\n delete: 'Delete',\n edit: 'Edit',\n publish: 'Publish',\n read: 'Read',\n}\n\nconst hasDrafts = (entity: CollectionConfig | GlobalConfig) =>\n typeof entity.versions === 'object' && Boolean(entity.versions?.drafts)\n\nconst defaultActions = (\n area: AreaConfig,\n collections: CollectionConfig[],\n globals: GlobalConfig[],\n): StandardAction[] => {\n if (area.collection) {\n const collection = collections.find((c) => c.slug === area.collection)\n if (!collection) {\n throw new Error(\n `permissions: area \"${area.slug}\" maps to unknown collection \"${area.collection}\"`,\n )\n }\n const actions: StandardAction[] = ['read', 'create', 'edit', 'delete']\n return hasDrafts(collection) ? [...actions, 'publish'] : actions\n }\n if (area.global) {\n const global = globals.find((g) => g.slug === area.global)\n if (!global) {\n throw new Error(`permissions: area \"${area.slug}\" maps to unknown global \"${area.global}\"`)\n }\n return hasDrafts(global) ? ['read', 'edit', 'publish'] : ['read', 'edit']\n }\n return []\n}\n\n/** Turn the site's area config into the full list of `area.action` permissions. */\nexport const buildRegistry = (\n areas: AreaConfig[],\n collections: CollectionConfig[],\n globals: GlobalConfig[],\n): Area[] => {\n const seen = new Set<string>()\n const registry = [\n ...areas,\n {\n slug: 'permissions',\n customActions: { manage: 'Manage roles and permissions' },\n label: 'Permissions',\n },\n ].map((area): Area => {\n if (area.slug.includes('.')) {\n throw new Error(`permissions: area slug \"${area.slug}\" can't contain \".\"`)\n }\n if (seen.has(area.slug)) {\n throw new Error(`permissions: area \"${area.slug}\" is registered twice`)\n }\n seen.add(area.slug)\n\n const standard = (area.actions ?? defaultActions(area, collections, globals)).map((action) => ({\n action,\n label: STANDARD_LABELS[action],\n }))\n const custom = Object.entries(area.customActions ?? {}).map(([action, label]) => ({\n action,\n label,\n }))\n\n return {\n slug: area.slug,\n label: area.label ?? area.slug,\n permissions: [...standard, ...custom].map(({ action, label }) => ({\n action,\n area: area.slug,\n key: `${area.slug}.${action}`,\n label,\n })),\n }\n })\n return registry\n}\n"],"names":["MANAGE_PERMISSION","STANDARD_LABELS","create","delete","edit","publish","read","hasDrafts","entity","versions","Boolean","drafts","defaultActions","area","collections","globals","collection","find","c","slug","Error","actions","global","g","buildRegistry","areas","seen","Set","registry","customActions","manage","label","map","includes","has","add","standard","action","custom","Object","entries","permissions","key"],"mappings":"AAIA,OAAO,MAAMA,oBAAoB,qBAAoB;AAErD,MAAMC,kBAAkD;IACtDC,QAAQ;IACRC,QAAQ;IACRC,MAAM;IACNC,SAAS;IACTC,MAAM;AACR;AAEA,MAAMC,YAAY,CAACC,SACjB,OAAOA,OAAOC,QAAQ,KAAK,YAAYC,QAAQF,OAAOC,QAAQ,EAAEE;AAElE,MAAMC,iBAAiB,CACrBC,MACAC,aACAC;IAEA,IAAIF,KAAKG,UAAU,EAAE;QACnB,MAAMA,aAAaF,YAAYG,IAAI,CAAC,CAACC,IAAMA,EAAEC,IAAI,KAAKN,KAAKG,UAAU;QACrE,IAAI,CAACA,YAAY;YACf,MAAM,IAAII,MACR,CAAC,mBAAmB,EAAEP,KAAKM,IAAI,CAAC,8BAA8B,EAAEN,KAAKG,UAAU,CAAC,CAAC,CAAC;QAEtF;QACA,MAAMK,UAA4B;YAAC;YAAQ;YAAU;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AAC,CAAC,CAACK,QAAQN,MAAM,GAAM,CAAA;gBAChFM;gBACAN;YACF,CAAA;QAEA,OAAO;YACLZ,MAAMN,KAAKM,IAAI;YACfY,OAAOlB,KAAKkB,KAAK,IAAIlB,KAAKM,IAAI;YAC9BsB,aAAa;mBAAIL;mBAAaE;aAAO,CAACN,GAAG,CAAC,CAAC,EAAEK,MAAM,EAAEN,KAAK,EAAE,GAAM,CAAA;oBAChEM;oBACAxB,MAAMA,KAAKM,IAAI;oBACfuB,KAAK,GAAG7B,KAAKM,IAAI,CAAC,CAAC,EAAEkB,QAAQ;oBAC7BN;gBACF,CAAA;QACF;IACF;IACA,OAAOH;AACT,EAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Override } from './types.js';
|
|
2
|
+
export type PermissionSource = 'override' | 'role' | 'superAdmin';
|
|
3
|
+
export type ResolvedPermission = {
|
|
4
|
+
allowed: boolean;
|
|
5
|
+
source: PermissionSource;
|
|
6
|
+
};
|
|
7
|
+
type Input = {
|
|
8
|
+
overrides: Override[];
|
|
9
|
+
rolePermissions: string[];
|
|
10
|
+
superAdmin: boolean;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Super admin → allowed. Deny override → denied. Allow override → allowed.
|
|
14
|
+
* Otherwise whatever the role says. Shared by the access checks and the admin
|
|
15
|
+
* UI so the two can't disagree.
|
|
16
|
+
*/
|
|
17
|
+
export declare const resolvePermission: (key: string, input: Input) => ResolvedPermission;
|
|
18
|
+
export declare const resolvePermissions: (keys: string[], input: Input) => Set<string>;
|
|
19
|
+
/** Overrides that say the same as the role, which a role change leaves pointless. */
|
|
20
|
+
export declare const redundantOverrides: <T extends Override>(overrides: T[], rolePermissions: string[]) => T[];
|
|
21
|
+
export {};
|
package/dist/resolve.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Super admin → allowed. Deny override → denied. Allow override → allowed.
|
|
3
|
+
* Otherwise whatever the role says. Shared by the access checks and the admin
|
|
4
|
+
* UI so the two can't disagree.
|
|
5
|
+
*/ export const resolvePermission = (key, input)=>{
|
|
6
|
+
if (input.superAdmin) {
|
|
7
|
+
return {
|
|
8
|
+
allowed: true,
|
|
9
|
+
source: 'superAdmin'
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
const overrides = input.overrides.filter((o)=>o.permission === key);
|
|
13
|
+
if (overrides.some((o)=>o.effect === 'deny')) {
|
|
14
|
+
return {
|
|
15
|
+
allowed: false,
|
|
16
|
+
source: 'override'
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
if (overrides.length) {
|
|
20
|
+
return {
|
|
21
|
+
allowed: true,
|
|
22
|
+
source: 'override'
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
return {
|
|
26
|
+
allowed: input.rolePermissions.includes(key),
|
|
27
|
+
source: 'role'
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
export const resolvePermissions = (keys, input)=>new Set(keys.filter((key)=>resolvePermission(key, input).allowed));
|
|
31
|
+
/** Overrides that say the same as the role, which a role change leaves pointless. */ export const redundantOverrides = (overrides, rolePermissions)=>overrides.filter((o)=>o.effect === 'allow' === rolePermissions.includes(o.permission));
|
|
32
|
+
|
|
33
|
+
//# sourceMappingURL=resolve.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/resolve.ts"],"sourcesContent":["import type { Override } from './types.js'\n\nexport type PermissionSource = 'override' | 'role' | 'superAdmin'\n\nexport type ResolvedPermission = {\n allowed: boolean\n source: PermissionSource\n}\n\ntype Input = {\n overrides: Override[]\n rolePermissions: string[]\n superAdmin: boolean\n}\n\n/**\n * Super admin → allowed. Deny override → denied. Allow override → allowed.\n * Otherwise whatever the role says. Shared by the access checks and the admin\n * UI so the two can't disagree.\n */\nexport const resolvePermission = (key: string, input: Input): ResolvedPermission => {\n if (input.superAdmin) {\n return { allowed: true, source: 'superAdmin' }\n }\n const overrides = input.overrides.filter((o) => o.permission === key)\n if (overrides.some((o) => o.effect === 'deny')) {\n return { allowed: false, source: 'override' }\n }\n if (overrides.length) {\n return { allowed: true, source: 'override' }\n }\n return { allowed: input.rolePermissions.includes(key), source: 'role' }\n}\n\nexport const resolvePermissions = (keys: string[], input: Input): Set<string> =>\n new Set(keys.filter((key) => resolvePermission(key, input).allowed))\n\n/** Overrides that say the same as the role, which a role change leaves pointless. */\nexport const redundantOverrides = <T extends Override>(\n overrides: T[],\n rolePermissions: string[],\n): T[] => overrides.filter((o) => (o.effect === 'allow') === rolePermissions.includes(o.permission))\n"],"names":["resolvePermission","key","input","superAdmin","allowed","source","overrides","filter","o","permission","some","effect","length","rolePermissions","includes","resolvePermissions","keys","Set","redundantOverrides"],"mappings":"AAeA;;;;CAIC,GACD,OAAO,MAAMA,oBAAoB,CAACC,KAAaC;IAC7C,IAAIA,MAAMC,UAAU,EAAE;QACpB,OAAO;YAAEC,SAAS;YAAMC,QAAQ;QAAa;IAC/C;IACA,MAAMC,YAAYJ,MAAMI,SAAS,CAACC,MAAM,CAAC,CAACC,IAAMA,EAAEC,UAAU,KAAKR;IACjE,IAAIK,UAAUI,IAAI,CAAC,CAACF,IAAMA,EAAEG,MAAM,KAAK,SAAS;QAC9C,OAAO;YAAEP,SAAS;YAAOC,QAAQ;QAAW;IAC9C;IACA,IAAIC,UAAUM,MAAM,EAAE;QACpB,OAAO;YAAER,SAAS;YAAMC,QAAQ;QAAW;IAC7C;IACA,OAAO;QAAED,SAASF,MAAMW,eAAe,CAACC,QAAQ,CAACb;QAAMI,QAAQ;IAAO;AACxE,EAAC;AAED,OAAO,MAAMU,qBAAqB,CAACC,MAAgBd,QACjD,IAAIe,IAAID,KAAKT,MAAM,CAAC,CAACN,MAAQD,kBAAkBC,KAAKC,OAAOE,OAAO,GAAE;AAEtE,mFAAmF,GACnF,OAAO,MAAMc,qBAAqB,CAChCZ,WACAO,kBACQP,UAAUC,MAAM,CAAC,CAACC,IAAM,AAACA,EAAEG,MAAM,KAAK,YAAaE,gBAAgBC,QAAQ,CAACN,EAAEC,UAAU,GAAE"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/rules.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { PayloadRequest } from 'payload';
|
|
2
|
+
/** Nobody changes their own role or overrides. */
|
|
3
|
+
export declare const assertNotSelf: (req: PayloadRequest, userID: unknown) => void;
|
|
4
|
+
/** Nobody grants a permission they don't hold themselves. */
|
|
5
|
+
export declare const assertCanGrant: (req: PayloadRequest, keys: string[]) => Promise<void>;
|
package/dist/rules.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { APIError } from 'payload';
|
|
2
|
+
import { getPermissions, isCMSUser } from './access.js';
|
|
3
|
+
const forbid = (message)=>new APIError(message, 403, undefined, true);
|
|
4
|
+
/** Nobody changes their own role or overrides. */ export const assertNotSelf = (req, userID)=>{
|
|
5
|
+
if (isCMSUser(req.payload, req.user) && String(req.user.id) === String(userID)) {
|
|
6
|
+
throw forbid("You can't change your own permissions.");
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
/** Nobody grants a permission they don't hold themselves. */ export const assertCanGrant = async (req, keys)=>{
|
|
10
|
+
if (!isCMSUser(req.payload, req.user) || !keys.length) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
const held = await getPermissions(req);
|
|
14
|
+
const missing = keys.filter((key)=>!held.has(key));
|
|
15
|
+
if (missing.length) {
|
|
16
|
+
throw forbid(`You can't grant permissions you don't hold: ${missing.join(', ')}`);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
//# sourceMappingURL=rules.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/rules.ts"],"sourcesContent":["import type { PayloadRequest } from 'payload'\n\nimport { APIError } from 'payload'\n\nimport { getPermissions, isCMSUser } from './access.js'\n\nconst forbid = (message: string) => new APIError(message, 403, undefined, true)\n\n/** Nobody changes their own role or overrides. */\nexport const assertNotSelf = (req: PayloadRequest, userID: unknown) => {\n if (isCMSUser(req.payload, req.user) && String(req.user.id) === String(userID)) {\n throw forbid(\"You can't change your own permissions.\")\n }\n}\n\n/** Nobody grants a permission they don't hold themselves. */\nexport const assertCanGrant = async (req: PayloadRequest, keys: string[]) => {\n if (!isCMSUser(req.payload, req.user) || !keys.length) {\n return\n }\n const held = await getPermissions(req)\n const missing = keys.filter((key) => !held.has(key))\n if (missing.length) {\n throw forbid(`You can't grant permissions you don't hold: ${missing.join(', ')}`)\n }\n}\n"],"names":["APIError","getPermissions","isCMSUser","forbid","message","undefined","assertNotSelf","req","userID","payload","user","String","id","assertCanGrant","keys","length","held","missing","filter","key","has","join"],"mappings":"AAEA,SAASA,QAAQ,QAAQ,UAAS;AAElC,SAASC,cAAc,EAAEC,SAAS,QAAQ,cAAa;AAEvD,MAAMC,SAAS,CAACC,UAAoB,IAAIJ,SAASI,SAAS,KAAKC,WAAW;AAE1E,gDAAgD,GAChD,OAAO,MAAMC,gBAAgB,CAACC,KAAqBC;IACjD,IAAIN,UAAUK,IAAIE,OAAO,EAAEF,IAAIG,IAAI,KAAKC,OAAOJ,IAAIG,IAAI,CAACE,EAAE,MAAMD,OAAOH,SAAS;QAC9E,MAAML,OAAO;IACf;AACF,EAAC;AAED,2DAA2D,GAC3D,OAAO,MAAMU,iBAAiB,OAAON,KAAqBO;IACxD,IAAI,CAACZ,UAAUK,IAAIE,OAAO,EAAEF,IAAIG,IAAI,KAAK,CAACI,KAAKC,MAAM,EAAE;QACrD;IACF;IACA,MAAMC,OAAO,MAAMf,eAAeM;IAClC,MAAMU,UAAUH,KAAKI,MAAM,CAAC,CAACC,MAAQ,CAACH,KAAKI,GAAG,CAACD;IAC/C,IAAIF,QAAQF,MAAM,EAAE;QAClB,MAAMZ,OAAO,CAAC,4CAA4C,EAAEc,QAAQI,IAAI,CAAC,OAAO;IAClF;AACF,EAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { CollectionSlug } from 'payload';
|
|
2
|
+
export type StandardAction = 'create' | 'delete' | 'edit' | 'publish' | 'read';
|
|
3
|
+
/**
|
|
4
|
+
* A section of the CMS. Usually maps to a collection or global, sometimes to a
|
|
5
|
+
* custom feature with only custom actions (e.g. CRM resends).
|
|
6
|
+
*/
|
|
7
|
+
export type AreaConfig = {
|
|
8
|
+
/**
|
|
9
|
+
* Standard actions this area exposes. Defaults to every action the mapped
|
|
10
|
+
* collection/global supports (publish only when drafts are enabled), or none
|
|
11
|
+
* for an unmapped area. An action left out is denied for CMS users.
|
|
12
|
+
*/
|
|
13
|
+
actions?: StandardAction[];
|
|
14
|
+
/** Collection whose access this area controls. */
|
|
15
|
+
collection?: string;
|
|
16
|
+
/** Extra non-CRUD actions, keyed by action slug, e.g. `{ resend: 'Resend CRM sends' }`. */
|
|
17
|
+
customActions?: Record<string, string>;
|
|
18
|
+
/** Global whose access this area controls. */
|
|
19
|
+
global?: string;
|
|
20
|
+
/** Defaults to the slug. */
|
|
21
|
+
label?: string;
|
|
22
|
+
/** First half of every permission key in this area; must not contain a dot. */
|
|
23
|
+
slug: string;
|
|
24
|
+
};
|
|
25
|
+
export type PermissionsPluginConfig = {
|
|
26
|
+
/** The areas this site has. Only these show up in the matrix. */
|
|
27
|
+
areas?: AreaConfig[];
|
|
28
|
+
disabled?: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Umi staff. Super admins hold every permission and sit outside roles and
|
|
31
|
+
* overrides, so they can't be locked out through the UI. Defaults to nobody.
|
|
32
|
+
*/
|
|
33
|
+
isSuperAdmin?: (user: Record<string, unknown>) => boolean;
|
|
34
|
+
};
|
|
35
|
+
export type Permission = {
|
|
36
|
+
action: string;
|
|
37
|
+
area: string;
|
|
38
|
+
/** `area.action` */
|
|
39
|
+
key: string;
|
|
40
|
+
label: string;
|
|
41
|
+
};
|
|
42
|
+
export type Area = {
|
|
43
|
+
label: string;
|
|
44
|
+
permissions: Permission[];
|
|
45
|
+
slug: string;
|
|
46
|
+
};
|
|
47
|
+
export type OverrideEffect = 'allow' | 'deny';
|
|
48
|
+
export type Override = {
|
|
49
|
+
effect: OverrideEffect;
|
|
50
|
+
id?: number | string;
|
|
51
|
+
permission: string;
|
|
52
|
+
};
|
|
53
|
+
/** What the plugin stores on `config.custom.permissions` for runtime helpers. */
|
|
54
|
+
export type PluginState = {
|
|
55
|
+
isSuperAdmin: (user: Record<string, unknown>) => boolean;
|
|
56
|
+
keys: string[];
|
|
57
|
+
registry: Area[];
|
|
58
|
+
usersSlug: CollectionSlug;
|
|
59
|
+
};
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/types.ts"],"sourcesContent":["import type { CollectionSlug } from 'payload'\n\nexport type StandardAction = 'create' | 'delete' | 'edit' | 'publish' | 'read'\n\n/**\n * A section of the CMS. Usually maps to a collection or global, sometimes to a\n * custom feature with only custom actions (e.g. CRM resends).\n */\nexport type AreaConfig = {\n /**\n * Standard actions this area exposes. Defaults to every action the mapped\n * collection/global supports (publish only when drafts are enabled), or none\n * for an unmapped area. An action left out is denied for CMS users.\n */\n actions?: StandardAction[]\n /** Collection whose access this area controls. */\n collection?: string\n /** Extra non-CRUD actions, keyed by action slug, e.g. `{ resend: 'Resend CRM sends' }`. */\n customActions?: Record<string, string>\n /** Global whose access this area controls. */\n global?: string\n /** Defaults to the slug. */\n label?: string\n /** First half of every permission key in this area; must not contain a dot. */\n slug: string\n}\n\nexport type PermissionsPluginConfig = {\n /** The areas this site has. Only these show up in the matrix. */\n areas?: AreaConfig[]\n disabled?: boolean\n /**\n * Umi staff. Super admins hold every permission and sit outside roles and\n * overrides, so they can't be locked out through the UI. Defaults to nobody.\n */\n isSuperAdmin?: (user: Record<string, unknown>) => boolean\n}\n\nexport type Permission = {\n action: string\n area: string\n /** `area.action` */\n key: string\n label: string\n}\n\nexport type Area = {\n label: string\n permissions: Permission[]\n slug: string\n}\n\nexport type OverrideEffect = 'allow' | 'deny'\n\nexport type Override = {\n effect: OverrideEffect\n id?: number | string\n permission: string\n}\n\n/** What the plugin stores on `config.custom.permissions` for runtime helpers. */\nexport type PluginState = {\n isSuperAdmin: (user: Record<string, unknown>) => boolean\n keys: string[]\n registry: Area[]\n usersSlug: CollectionSlug\n}\n"],"names":[],"mappings":"AA4DA,+EAA+E,GAC/E,WAKC"}
|
package/dist/users.d.ts
ADDED
package/dist/users.js
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { getOverrides, getRolePermissions, hasPermission, isCMSUser, markActive, OVERRIDES_SLUG, relationID, ROLES_SLUG } from './access.js';
|
|
2
|
+
import { PART_OF_CHANGE, writeAudit } from './audit.js';
|
|
3
|
+
import { MANAGE_PERMISSION } from './registry.js';
|
|
4
|
+
import { redundantOverrides } from './resolve.js';
|
|
5
|
+
import { assertCanGrant, assertNotSelf } from './rules.js';
|
|
6
|
+
const roleName = async (req, id)=>{
|
|
7
|
+
if (id === null) {
|
|
8
|
+
return null;
|
|
9
|
+
}
|
|
10
|
+
const { docs } = await req.payload.find({
|
|
11
|
+
collection: ROLES_SLUG,
|
|
12
|
+
depth: 0,
|
|
13
|
+
limit: 1,
|
|
14
|
+
overrideAccess: true,
|
|
15
|
+
req,
|
|
16
|
+
where: {
|
|
17
|
+
id: {
|
|
18
|
+
equals: id
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
return docs[0]?.name ?? null;
|
|
23
|
+
};
|
|
24
|
+
/** Adds the role field, last-active stamp and role-change rules to the users collection. */ export const extendUsersCollection = (users)=>{
|
|
25
|
+
for (const name of [
|
|
26
|
+
'role',
|
|
27
|
+
'lastActiveAt'
|
|
28
|
+
]){
|
|
29
|
+
if (users.fields.some((field)=>'name' in field && field.name === name)) {
|
|
30
|
+
throw new Error(`permissions: the "${users.slug}" collection already has a "${name}" field`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return {
|
|
34
|
+
...users,
|
|
35
|
+
fields: [
|
|
36
|
+
...users.fields,
|
|
37
|
+
{
|
|
38
|
+
name: 'role',
|
|
39
|
+
type: 'relationship',
|
|
40
|
+
access: {
|
|
41
|
+
create: ({ req })=>hasPermission(req, MANAGE_PERMISSION),
|
|
42
|
+
update: async ({ id, req })=>!(isCMSUser(req.payload, req.user) && String(req.user.id) === String(id)) && hasPermission(req, MANAGE_PERMISSION)
|
|
43
|
+
},
|
|
44
|
+
admin: {
|
|
45
|
+
position: 'sidebar'
|
|
46
|
+
},
|
|
47
|
+
index: true,
|
|
48
|
+
relationTo: ROLES_SLUG
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
name: 'lastActiveAt',
|
|
52
|
+
type: 'date',
|
|
53
|
+
admin: {
|
|
54
|
+
date: {
|
|
55
|
+
pickerAppearance: 'dayAndTime'
|
|
56
|
+
},
|
|
57
|
+
description: 'Last login or edit.',
|
|
58
|
+
position: 'sidebar',
|
|
59
|
+
readOnly: true
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
],
|
|
63
|
+
hooks: {
|
|
64
|
+
...users.hooks,
|
|
65
|
+
afterChange: [
|
|
66
|
+
...users.hooks?.afterChange ?? [],
|
|
67
|
+
async ({ doc, operation, previousDoc, req })=>{
|
|
68
|
+
const was = operation === 'create' ? null : relationID(previousDoc?.role);
|
|
69
|
+
const now = relationID(doc.role);
|
|
70
|
+
if (String(was) === String(now)) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
const rolePermissions = await getRolePermissions(req.payload, now, req);
|
|
74
|
+
const dropped = redundantOverrides(await getOverrides(req.payload, doc.id, req), rolePermissions);
|
|
75
|
+
if (dropped.length) {
|
|
76
|
+
await req.payload.delete({
|
|
77
|
+
collection: OVERRIDES_SLUG,
|
|
78
|
+
context: {
|
|
79
|
+
[PART_OF_CHANGE]: true
|
|
80
|
+
},
|
|
81
|
+
overrideAccess: true,
|
|
82
|
+
req,
|
|
83
|
+
where: {
|
|
84
|
+
id: {
|
|
85
|
+
in: dropped.map((o)=>o.id)
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
const fromName = await roleName(req, was);
|
|
91
|
+
const toName = await roleName(req, now);
|
|
92
|
+
await writeAudit(req, {
|
|
93
|
+
action: 'user.role',
|
|
94
|
+
after: {
|
|
95
|
+
droppedOverrides: dropped.map(({ effect, permission })=>({
|
|
96
|
+
effect,
|
|
97
|
+
permission
|
|
98
|
+
})),
|
|
99
|
+
role: toName
|
|
100
|
+
},
|
|
101
|
+
before: {
|
|
102
|
+
role: fromName
|
|
103
|
+
},
|
|
104
|
+
role: now,
|
|
105
|
+
summary: `Role changed from ${fromName ?? 'none'} to ${toName ?? 'none'}`,
|
|
106
|
+
user: doc.id
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
],
|
|
110
|
+
afterLogin: [
|
|
111
|
+
...users.hooks?.afterLogin ?? [],
|
|
112
|
+
({ req, user })=>markActive(req, {
|
|
113
|
+
...user,
|
|
114
|
+
collection: users.slug
|
|
115
|
+
})
|
|
116
|
+
],
|
|
117
|
+
beforeChange: [
|
|
118
|
+
...users.hooks?.beforeChange ?? [],
|
|
119
|
+
async ({ data, originalDoc, req })=>{
|
|
120
|
+
if (data.role === undefined) {
|
|
121
|
+
return data;
|
|
122
|
+
}
|
|
123
|
+
const now = relationID(data.role);
|
|
124
|
+
if (String(relationID(originalDoc?.role)) === String(now)) {
|
|
125
|
+
return data;
|
|
126
|
+
}
|
|
127
|
+
if (originalDoc) {
|
|
128
|
+
assertNotSelf(req, originalDoc.id);
|
|
129
|
+
}
|
|
130
|
+
if (isCMSUser(req.payload, req.user)) {
|
|
131
|
+
// Read outside the transaction: Payload runs beforeChange work concurrently
|
|
132
|
+
// and Mongo rejects parallel operations racing to start a transaction.
|
|
133
|
+
await assertCanGrant(req, await getRolePermissions(req.payload, now));
|
|
134
|
+
}
|
|
135
|
+
return data;
|
|
136
|
+
}
|
|
137
|
+
]
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
//# sourceMappingURL=users.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/users.ts"],"sourcesContent":["import type { CollectionConfig } from 'payload'\n\nimport {\n getOverrides,\n getRolePermissions,\n hasPermission,\n isCMSUser,\n markActive,\n OVERRIDES_SLUG,\n relationID,\n ROLES_SLUG,\n} from './access.js'\nimport { PART_OF_CHANGE, writeAudit } from './audit.js'\nimport { MANAGE_PERMISSION } from './registry.js'\nimport { redundantOverrides } from './resolve.js'\nimport { assertCanGrant, assertNotSelf } from './rules.js'\n\nconst roleName = async (req: Parameters<typeof writeAudit>[0], id: null | number | string) => {\n if (id === null) {\n return null\n }\n const { docs } = await req.payload.find({\n collection: ROLES_SLUG,\n depth: 0,\n limit: 1,\n overrideAccess: true,\n req,\n where: { id: { equals: id } },\n })\n return (docs[0]?.name as string | undefined) ?? null\n}\n\n/** Adds the role field, last-active stamp and role-change rules to the users collection. */\nexport const extendUsersCollection = (users: CollectionConfig): CollectionConfig => {\n for (const name of ['role', 'lastActiveAt']) {\n if (users.fields.some((field) => 'name' in field && field.name === name)) {\n throw new Error(`permissions: the \"${users.slug}\" collection already has a \"${name}\" field`)\n }\n }\n\n return {\n ...users,\n fields: [\n ...users.fields,\n {\n name: 'role',\n type: 'relationship',\n access: {\n create: ({ req }) => hasPermission(req, MANAGE_PERMISSION),\n update: async ({ id, req }) =>\n !(isCMSUser(req.payload, req.user) && String(req.user.id) === String(id)) &&\n hasPermission(req, MANAGE_PERMISSION),\n },\n admin: { position: 'sidebar' },\n index: true,\n relationTo: ROLES_SLUG,\n },\n {\n name: 'lastActiveAt',\n type: 'date',\n admin: {\n date: { pickerAppearance: 'dayAndTime' },\n description: 'Last login or edit.',\n position: 'sidebar',\n readOnly: true,\n },\n },\n ],\n hooks: {\n ...users.hooks,\n afterChange: [\n ...(users.hooks?.afterChange ?? []),\n async ({ doc, operation, previousDoc, req }) => {\n const was = operation === 'create' ? null : relationID(previousDoc?.role)\n const now = relationID(doc.role)\n if (String(was) === String(now)) {\n return\n }\n\n const rolePermissions = await getRolePermissions(req.payload, now, req)\n const dropped = redundantOverrides(\n await getOverrides(req.payload, doc.id, req),\n rolePermissions,\n )\n if (dropped.length) {\n await req.payload.delete({\n collection: OVERRIDES_SLUG,\n context: { [PART_OF_CHANGE]: true },\n overrideAccess: true,\n req,\n where: { id: { in: dropped.map((o) => o.id) } },\n })\n }\n\n const fromName = await roleName(req, was)\n const toName = await roleName(req, now)\n await writeAudit(req, {\n action: 'user.role',\n after: {\n droppedOverrides: dropped.map(({ effect, permission }) => ({ effect, permission })),\n role: toName,\n },\n before: { role: fromName },\n role: now,\n summary: `Role changed from ${fromName ?? 'none'} to ${toName ?? 'none'}`,\n user: doc.id,\n })\n },\n ],\n afterLogin: [\n ...(users.hooks?.afterLogin ?? []),\n ({ req, user }) => markActive(req, { ...user, collection: users.slug }),\n ],\n beforeChange: [\n ...(users.hooks?.beforeChange ?? []),\n async ({ data, originalDoc, req }) => {\n if (data.role === undefined) {\n return data\n }\n const now = relationID(data.role)\n if (String(relationID(originalDoc?.role)) === String(now)) {\n return data\n }\n if (originalDoc) {\n assertNotSelf(req, originalDoc.id)\n }\n if (isCMSUser(req.payload, req.user)) {\n // Read outside the transaction: Payload runs beforeChange work concurrently\n // and Mongo rejects parallel operations racing to start a transaction.\n await assertCanGrant(req, await getRolePermissions(req.payload, now))\n }\n return data\n },\n ],\n },\n }\n}\n"],"names":["getOverrides","getRolePermissions","hasPermission","isCMSUser","markActive","OVERRIDES_SLUG","relationID","ROLES_SLUG","PART_OF_CHANGE","writeAudit","MANAGE_PERMISSION","redundantOverrides","assertCanGrant","assertNotSelf","roleName","req","id","docs","payload","find","collection","depth","limit","overrideAccess","where","equals","name","extendUsersCollection","users","fields","some","field","Error","slug","type","access","create","update","user","String","admin","position","index","relationTo","date","pickerAppearance","description","readOnly","hooks","afterChange","doc","operation","previousDoc","was","role","now","rolePermissions","dropped","length","delete","context","in","map","o","fromName","toName","action","after","droppedOverrides","effect","permission","before","summary","afterLogin","beforeChange","data","originalDoc","undefined"],"mappings":"AAEA,SACEA,YAAY,EACZC,kBAAkB,EAClBC,aAAa,EACbC,SAAS,EACTC,UAAU,EACVC,cAAc,EACdC,UAAU,EACVC,UAAU,QACL,cAAa;AACpB,SAASC,cAAc,EAAEC,UAAU,QAAQ,aAAY;AACvD,SAASC,iBAAiB,QAAQ,gBAAe;AACjD,SAASC,kBAAkB,QAAQ,eAAc;AACjD,SAASC,cAAc,EAAEC,aAAa,QAAQ,aAAY;AAE1D,MAAMC,WAAW,OAAOC,KAAuCC;IAC7D,IAAIA,OAAO,MAAM;QACf,OAAO;IACT;IACA,MAAM,EAAEC,IAAI,EAAE,GAAG,MAAMF,IAAIG,OAAO,CAACC,IAAI,CAAC;QACtCC,YAAYb;QACZc,OAAO;QACPC,OAAO;QACPC,gBAAgB;QAChBR;QACAS,OAAO;YAAER,IAAI;gBAAES,QAAQT;YAAG;QAAE;IAC9B;IACA,OAAO,AAACC,IAAI,CAAC,EAAE,EAAES,QAA+B;AAClD;AAEA,0FAA0F,GAC1F,OAAO,MAAMC,wBAAwB,CAACC;IACpC,KAAK,MAAMF,QAAQ;QAAC;QAAQ;KAAe,CAAE;QAC3C,IAAIE,MAAMC,MAAM,CAACC,IAAI,CAAC,CAACC,QAAU,UAAUA,SAASA,MAAML,IAAI,KAAKA,OAAO;YACxE,MAAM,IAAIM,MAAM,CAAC,kBAAkB,EAAEJ,MAAMK,IAAI,CAAC,4BAA4B,EAAEP,KAAK,OAAO,CAAC;QAC7F;IACF;IAEA,OAAO;QACL,GAAGE,KAAK;QACRC,QAAQ;eACHD,MAAMC,MAAM;YACf;gBACEH,MAAM;gBACNQ,MAAM;gBACNC,QAAQ;oBACNC,QAAQ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OoB;gBACT;aACD;QACH;IACF;AACF,EAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { AdminViewServerProps } from 'payload';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import '../components/index.scss';
|
|
4
|
+
type Props = {
|
|
5
|
+
/** Only called once the viewer is allowed in, so no data loads otherwise. */
|
|
6
|
+
render: () => Promise<React.ReactNode>;
|
|
7
|
+
tab: 'roles' | 'users';
|
|
8
|
+
} & AdminViewServerProps;
|
|
9
|
+
/** Admin chrome, tabs and the permissions.manage check shared by both views. */
|
|
10
|
+
export declare const PermissionsLayout: ({ initPageResult, params, render, searchParams, tab, }: Props) => Promise<import("react/jsx-runtime").JSX.Element>;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { DefaultTemplate } from '@payloadcms/next/templates';
|
|
3
|
+
import { Gutter, Link } from '@payloadcms/ui';
|
|
4
|
+
import { redirect } from 'next/navigation.js';
|
|
5
|
+
import React from 'react';
|
|
6
|
+
import { AUDIT_SLUG, hasPermission } from '../access.js';
|
|
7
|
+
import { MANAGE_PERMISSION } from '../registry.js';
|
|
8
|
+
import '../components/index.scss';
|
|
9
|
+
/** Admin chrome, tabs and the permissions.manage check shared by both views. */ export const PermissionsLayout = async ({ initPageResult, params, render, searchParams, tab })=>{
|
|
10
|
+
const { req } = initPageResult;
|
|
11
|
+
const adminRoute = req.payload.config.routes.admin;
|
|
12
|
+
if (!req.user) {
|
|
13
|
+
redirect(`${adminRoute}/login`);
|
|
14
|
+
}
|
|
15
|
+
const allowed = await hasPermission(req, MANAGE_PERMISSION);
|
|
16
|
+
const tabs = [
|
|
17
|
+
{
|
|
18
|
+
href: `${adminRoute}/permissions`,
|
|
19
|
+
key: 'roles',
|
|
20
|
+
label: 'Roles'
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
href: `${adminRoute}/permissions/users`,
|
|
24
|
+
key: 'users',
|
|
25
|
+
label: 'Users'
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
href: `${adminRoute}/collections/${AUDIT_SLUG}`,
|
|
29
|
+
key: 'audit',
|
|
30
|
+
label: 'Audit log'
|
|
31
|
+
}
|
|
32
|
+
];
|
|
33
|
+
return /*#__PURE__*/ _jsx(DefaultTemplate, {
|
|
34
|
+
i18n: req.i18n,
|
|
35
|
+
locale: initPageResult.locale,
|
|
36
|
+
params: params,
|
|
37
|
+
payload: req.payload,
|
|
38
|
+
permissions: initPageResult.permissions,
|
|
39
|
+
searchParams: searchParams,
|
|
40
|
+
user: req.user ?? undefined,
|
|
41
|
+
visibleEntities: initPageResult.visibleEntities,
|
|
42
|
+
children: /*#__PURE__*/ _jsxs(Gutter, {
|
|
43
|
+
className: "perm",
|
|
44
|
+
children: [
|
|
45
|
+
/*#__PURE__*/ _jsx("h1", {
|
|
46
|
+
children: "Permissions"
|
|
47
|
+
}),
|
|
48
|
+
allowed ? /*#__PURE__*/ _jsxs(_Fragment, {
|
|
49
|
+
children: [
|
|
50
|
+
/*#__PURE__*/ _jsx("nav", {
|
|
51
|
+
className: "perm__tabs",
|
|
52
|
+
children: tabs.map((t)=>/*#__PURE__*/ _jsx(Link, {
|
|
53
|
+
"aria-current": t.key === tab ? 'page' : undefined,
|
|
54
|
+
className: "perm__tab",
|
|
55
|
+
href: t.href,
|
|
56
|
+
children: t.label
|
|
57
|
+
}, t.key))
|
|
58
|
+
}),
|
|
59
|
+
await render()
|
|
60
|
+
]
|
|
61
|
+
}) : /*#__PURE__*/ _jsx("p", {
|
|
62
|
+
children: "You don't have permission to manage roles and permissions."
|
|
63
|
+
})
|
|
64
|
+
]
|
|
65
|
+
})
|
|
66
|
+
});
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
//# sourceMappingURL=PermissionsLayout.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/views/PermissionsLayout.tsx"],"sourcesContent":["import type { AdminViewServerProps } from 'payload'\n\nimport { DefaultTemplate } from '@payloadcms/next/templates'\nimport { Gutter, Link } from '@payloadcms/ui'\nimport { redirect } from 'next/navigation.js'\nimport React from 'react'\n\nimport { AUDIT_SLUG, hasPermission } from '../access.js'\nimport { MANAGE_PERMISSION } from '../registry.js'\nimport '../components/index.scss'\n\ntype Props = {\n /** Only called once the viewer is allowed in, so no data loads otherwise. */\n render: () => Promise<React.ReactNode>\n tab: 'roles' | 'users'\n} & AdminViewServerProps\n\n/** Admin chrome, tabs and the permissions.manage check shared by both views. */\nexport const PermissionsLayout = async ({\n initPageResult,\n params,\n render,\n searchParams,\n tab,\n}: Props) => {\n const { req } = initPageResult\n const adminRoute = req.payload.config.routes.admin\n if (!req.user) {\n redirect(`${adminRoute}/login`)\n }\n const allowed = await hasPermission(req, MANAGE_PERMISSION)\n\n const tabs = [\n { href: `${adminRoute}/permissions`, key: 'roles', label: 'Roles' },\n { href: `${adminRoute}/permissions/users`, key: 'users', label: 'Users' },\n { href: `${adminRoute}/collections/${AUDIT_SLUG}`, key: 'audit', label: 'Audit log' },\n ]\n\n return (\n <DefaultTemplate\n i18n={req.i18n}\n locale={initPageResult.locale}\n params={params}\n payload={req.payload}\n permissions={initPageResult.permissions}\n searchParams={searchParams}\n user={req.user ?? undefined}\n visibleEntities={initPageResult.visibleEntities}\n >\n <Gutter className=\"perm\">\n <h1>Permissions</h1>\n {allowed ? (\n <>\n <nav className=\"perm__tabs\">\n {tabs.map((t) => (\n <Link\n aria-current={t.key === tab ? 'page' : undefined}\n className=\"perm__tab\"\n href={t.href}\n key={t.key}\n >\n {t.label}\n </Link>\n ))}\n </nav>\n {await render()}\n </>\n ) : (\n <p>You don't have permission to manage roles and permissions.</p>\n )}\n </Gutter>\n </DefaultTemplate>\n )\n}\n"],"names":["DefaultTemplate","Gutter","Link","redirect","React","AUDIT_SLUG","hasPermission","MANAGE_PERMISSION","PermissionsLayout","initPageResult","params","render","searchParams","tab","req","adminRoute","payload","config","routes","admin","user","allowed","tabs","href","key","label","i18n","locale","permissions","undefined","visibleEntities","className","h1","nav","map","t","aria-current","p"],"mappings":";AAEA,SAASA,eAAe,QAAQ,6BAA4B;AAC5D,SAASC,MAAM,EAAEC,IAAI,QAAQ,iBAAgB;AAC7C,SAASC,QAAQ,QAAQ,qBAAoB;AAC7C,OAAOC,WAAW,QAAO;AAEzB,SAASC,UAAU,EAAEC,aAAa,QAAQ,eAAc;AACxD,SAASC,iBAAiB,QAAQ,iBAAgB;AAClD,OAAO,2BAA0B;AAQjC,8EAA8E,GAC9E,OAAO,MAAMC,oBAAoB,OAAO,EACtCC,cAAc,EACdC,MAAM,EACNC,MAAM,EACNC,YAAY,EACZC,GAAG,EACG;IACN,MAAM,EAAEC,GAAG,EAAE,GAAGL;IAChB,MAAMM,aAAaD,IAAIE,OAAO,CAACC,MAAM,CAACC,MAAM,CAACC,KAAK;IAClD,IAAI,CAACL,IAAIM,IAAI,EAAE;QACbjB,SAAS,GAAGY,WAAW,MAAM,CAAC;IAChC;IACA,MAAMM,UAAU,MAAMf,cAAcQ,KAAKP;IAEzC,MAAMe,OAAO;QACX;YAAEC,MAAM,GAAGR,WAAW,YAAY,CAAC;YAAES,KAAK;YAASC,OAAO;QAAQ;QAClE;YAAEF,MAAM,GAAGR,WAAW,kBAAkB,CAAC;YAAES,KAAK;YAASC,OAAO;QAAQ;QACxE;YAAEF,MAAM,GAAGR,WAAW,aAAa,EAAEV,YAAY;YAAEmB,KAAK;YAASC,OAAO;QAAY;KACrF;IAED,qBACE,KAACzB;QACC0B,MAAMZ,IAAIY,IAAI;QACdC,QAAQlB,eAAekB,MAAM;QAC7BjB,QAAQA;QACRM,SAASF,IAAIE,OAAO;QACpBY,aAAanB,eAAemB,WAAW;QACvChB,cAAcA;QACdQ,MAAMN,IAAIM,IAAI,IAAIS;QAClBC,iBAAiBrB,eAAeqB,eAAe;kBAE/C,cAAA,MAAC7B;YAAO8B,WAAU;;8BAChB,KAACC;8BAAG;;gBACHX,wBACC;;sCACE,KAACY;4BAAIF,WAAU;sCACZT,KAAKY,GAAG,CAAC,CAACC,kBACT,KAACjC;oCACCkC,gBAAcD,EAAEX,GAAG,KAAKX,MAAM,SAASgB;oCACvCE,WAAU;oCACVR,MAAMY,EAAEZ,IAAI;8CAGXY,EAAEV,KAAK;mCAFHU,EAAEX,GAAG;;wBAMf,MAAMb;;mCAGT,KAAC0B;8BAAE;;;;;AAKb,EAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { createElement as _createElement } from "react";
|
|
3
|
+
import React from 'react';
|
|
4
|
+
import { RolesMatrix } from '../components/RolesMatrix.js';
|
|
5
|
+
import { loadViewData } from './data.js';
|
|
6
|
+
import { PermissionsLayout } from './PermissionsLayout.js';
|
|
7
|
+
export const RolesView = (props)=>/*#__PURE__*/ _jsx(PermissionsLayout, {
|
|
8
|
+
...props,
|
|
9
|
+
render: async ()=>{
|
|
10
|
+
const data = await loadViewData(props.initPageResult.req);
|
|
11
|
+
// Keyed on the saved roles so a refresh after saving resets the draft.
|
|
12
|
+
return /*#__PURE__*/ _createElement(RolesMatrix, {
|
|
13
|
+
...data,
|
|
14
|
+
key: JSON.stringify(data.roles)
|
|
15
|
+
});
|
|
16
|
+
},
|
|
17
|
+
tab: "roles"
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
//# sourceMappingURL=RolesView.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/views/RolesView.tsx"],"sourcesContent":["import type { AdminViewServerProps } from 'payload'\n\nimport React from 'react'\n\nimport { RolesMatrix } from '../components/RolesMatrix.js'\nimport { loadViewData } from './data.js'\nimport { PermissionsLayout } from './PermissionsLayout.js'\n\nexport const RolesView = (props: AdminViewServerProps) => (\n <PermissionsLayout\n {...props}\n render={async () => {\n const data = await loadViewData(props.initPageResult.req)\n // Keyed on the saved roles so a refresh after saving resets the draft.\n return <RolesMatrix {...data} key={JSON.stringify(data.roles)} />\n }}\n tab=\"roles\"\n />\n)\n"],"names":["React","RolesMatrix","loadViewData","PermissionsLayout","RolesView","props","render","data","initPageResult","req","key","JSON","stringify","roles","tab"],"mappings":";;AAEA,OAAOA,WAAW,QAAO;AAEzB,SAASC,WAAW,QAAQ,+BAA8B;AAC1D,SAASC,YAAY,QAAQ,YAAW;AACxC,SAASC,iBAAiB,QAAQ,yBAAwB;AAE1D,OAAO,MAAMC,YAAY,CAACC,sBACxB,KAACF;QACE,GAAGE,KAAK;QACTC,QAAQ;YACN,MAAMC,OAAO,MAAML,aAAaG,MAAMG,cAAc,CAACC,GAAG;YACxD,uEAAuE;YACvE,qBAAO,eAACR;gBAAa,GAAGM,IAAI;gBAAEG,KAAKC,KAAKC,SAAS,CAACL,KAAKM,KAAK;;QAC9D;QACAC,KAAI;OAEP"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { UsersTable } from '../components/UsersTable.js';
|
|
4
|
+
import { loadViewData } from './data.js';
|
|
5
|
+
import { PermissionsLayout } from './PermissionsLayout.js';
|
|
6
|
+
export const UsersView = (props)=>/*#__PURE__*/ _jsx(PermissionsLayout, {
|
|
7
|
+
...props,
|
|
8
|
+
render: async ()=>/*#__PURE__*/ _jsx(UsersTable, {
|
|
9
|
+
...await loadViewData(props.initPageResult.req)
|
|
10
|
+
}),
|
|
11
|
+
tab: "users"
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
//# sourceMappingURL=UsersView.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/views/UsersView.tsx"],"sourcesContent":["import type { AdminViewServerProps } from 'payload'\n\nimport React from 'react'\n\nimport { UsersTable } from '../components/UsersTable.js'\nimport { loadViewData } from './data.js'\nimport { PermissionsLayout } from './PermissionsLayout.js'\n\nexport const UsersView = (props: AdminViewServerProps) => (\n <PermissionsLayout\n {...props}\n render={async () => <UsersTable {...await loadViewData(props.initPageResult.req)} />}\n tab=\"users\"\n />\n)\n"],"names":["React","UsersTable","loadViewData","PermissionsLayout","UsersView","props","render","initPageResult","req","tab"],"mappings":";AAEA,OAAOA,WAAW,QAAO;AAEzB,SAASC,UAAU,QAAQ,8BAA6B;AACxD,SAASC,YAAY,QAAQ,YAAW;AACxC,SAASC,iBAAiB,QAAQ,yBAAwB;AAE1D,OAAO,MAAMC,YAAY,CAACC,sBACxB,KAACF;QACE,GAAGE,KAAK;QACTC,QAAQ,wBAAY,KAACL;gBAAY,GAAG,MAAMC,aAAaG,MAAME,cAAc,CAACC,GAAG,CAAC;;QAChFC,KAAI;OAEP"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { PayloadRequest } from 'payload';
|
|
2
|
+
import type { Area, OverrideEffect } from '../types.js';
|
|
3
|
+
type ID = number | string;
|
|
4
|
+
export type ViewRole = {
|
|
5
|
+
id: ID;
|
|
6
|
+
name: string;
|
|
7
|
+
permissions: string[];
|
|
8
|
+
};
|
|
9
|
+
export type ViewUser = {
|
|
10
|
+
email: string;
|
|
11
|
+
id: ID;
|
|
12
|
+
label: string;
|
|
13
|
+
lastActiveAt: null | string;
|
|
14
|
+
role: ID | null;
|
|
15
|
+
superAdmin: boolean;
|
|
16
|
+
};
|
|
17
|
+
export type ViewOverride = {
|
|
18
|
+
effect: OverrideEffect;
|
|
19
|
+
id: ID;
|
|
20
|
+
permission: string;
|
|
21
|
+
user: ID;
|
|
22
|
+
};
|
|
23
|
+
/** Everything the permissions views need, serialisable for client components. */
|
|
24
|
+
export type ViewData = {
|
|
25
|
+
adminRoute: string;
|
|
26
|
+
apiRoute: string;
|
|
27
|
+
currentUserID: ID;
|
|
28
|
+
/** Permissions the viewer holds, and so can grant. */
|
|
29
|
+
held: string[];
|
|
30
|
+
overrides: ViewOverride[];
|
|
31
|
+
registry: Area[];
|
|
32
|
+
roles: ViewRole[];
|
|
33
|
+
users: ViewUser[];
|
|
34
|
+
};
|
|
35
|
+
export declare const loadViewData: (req: PayloadRequest) => Promise<ViewData>;
|
|
36
|
+
export {};
|