roles-privileges-payload-plugin 1.0.2 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -1
- package/dist/collections/roles.d.ts +32 -0
- package/dist/collections/roles.js +122 -0
- package/dist/collections/roles.js.map +1 -0
- package/dist/components/PrivilegesSelect.d.ts +19 -0
- package/dist/components/PrivilegesSelect.js +471 -0
- package/dist/components/PrivilegesSelect.js.map +1 -0
- package/dist/exports/client.d.ts +2 -0
- package/dist/exports/client.js +3 -0
- package/dist/exports/client.js.map +1 -0
- package/dist/exports/rsc.d.ts +1 -0
- package/dist/exports/rsc.js +3 -0
- package/dist/exports/rsc.js.map +1 -0
- package/dist/exports/types.d.ts +3 -0
- package/dist/exports/types.js +5 -0
- package/dist/exports/types.js.map +1 -0
- package/dist/exports/utilities.d.ts +6 -0
- package/dist/exports/utilities.js +14 -0
- package/dist/exports/utilities.js.map +1 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +179 -0
- package/dist/index.js.map +1 -0
- package/dist/translations/index.d.ts +7 -0
- package/dist/translations/index.js +50 -0
- package/dist/translations/index.js.map +1 -0
- package/dist/translations/languages/en.d.ts +2 -0
- package/dist/translations/languages/en.js +76 -0
- package/dist/translations/languages/en.js.map +1 -0
- package/dist/translations/languages/fr.d.ts +2 -0
- package/dist/translations/languages/fr.js +76 -0
- package/dist/translations/languages/fr.js.map +1 -0
- package/dist/translations/types.d.ts +67 -0
- package/dist/translations/types.js +3 -0
- package/dist/translations/types.js.map +1 -0
- package/dist/utils/createCustomPrivilege.d.ts +89 -0
- package/dist/utils/createCustomPrivilege.js +77 -0
- package/dist/utils/createCustomPrivilege.js.map +1 -0
- package/dist/utils/generateGlobalPrivileges.d.ts +48 -0
- package/dist/utils/generateGlobalPrivileges.js +133 -0
- package/dist/utils/generateGlobalPrivileges.js.map +1 -0
- package/dist/utils/generatePrivileges.d.ts +51 -0
- package/dist/utils/generatePrivileges.js +162 -0
- package/dist/utils/generatePrivileges.js.map +1 -0
- package/dist/utils/privilegesAccess.d.ts +71 -0
- package/dist/utils/privilegesAccess.js +144 -0
- package/dist/utils/privilegesAccess.js.map +1 -0
- package/dist/utils/seedSuperAdminRole.d.ts +6 -0
- package/dist/utils/seedSuperAdminRole.js +60 -0
- package/dist/utils/seedSuperAdminRole.js.map +1 -0
- package/package.json +18 -15
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import type { GlobalPrivilege } from './generateGlobalPrivileges.js';
|
|
2
|
+
import type { Privilege } from './generatePrivileges.js';
|
|
3
|
+
/**
|
|
4
|
+
* Storage for custom privileges that should appear in the UI
|
|
5
|
+
* Organized by collection/global slug
|
|
6
|
+
*/
|
|
7
|
+
export declare const customPrivilegesRegistry: Map<string, {
|
|
8
|
+
slug: string;
|
|
9
|
+
label: Record<string, string>;
|
|
10
|
+
type: "collection" | "global";
|
|
11
|
+
privileges: Record<string, Privilege | GlobalPrivilege>;
|
|
12
|
+
}>;
|
|
13
|
+
/**
|
|
14
|
+
* Configuration for creating a custom privilege
|
|
15
|
+
*/
|
|
16
|
+
export type CustomPrivilegeConfig = {
|
|
17
|
+
/**
|
|
18
|
+
* Unique key for the privilege (e.g., 'posts-publish', 'users-approve')
|
|
19
|
+
*/
|
|
20
|
+
privilegeKey: string;
|
|
21
|
+
/**
|
|
22
|
+
* Labels for the privilege in different languages
|
|
23
|
+
* @example { en: 'Publish Posts', fr: 'Publier les articles' }
|
|
24
|
+
*/
|
|
25
|
+
label: Record<string, string>;
|
|
26
|
+
/**
|
|
27
|
+
* Descriptions for the privilege in different languages
|
|
28
|
+
* @example { en: 'Ability to publish posts', fr: 'Capacité de publier des articles' }
|
|
29
|
+
*/
|
|
30
|
+
description: Record<string, string>;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Register a custom privilege to appear in the admin UI
|
|
34
|
+
*
|
|
35
|
+
* @param collectionOrGlobalSlug - The slug of the collection or global this privilege belongs to
|
|
36
|
+
* @param config - The privilege configuration
|
|
37
|
+
* @param options - Additional options
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* import { registerCustomPrivilege } from '@payload-enchants/roles-privileges-payload-plugin'
|
|
42
|
+
*
|
|
43
|
+
* // In your payload.config.ts, before the plugin initialization
|
|
44
|
+
* registerCustomPrivilege('posts', {
|
|
45
|
+
* privilegeKey: 'posts-publish',
|
|
46
|
+
* label: { en: 'Publish Posts', fr: 'Publier les articles' },
|
|
47
|
+
* description: { en: 'Ability to publish posts', fr: 'Capacité de publier des articles' },
|
|
48
|
+
* }, {
|
|
49
|
+
* type: 'collection',
|
|
50
|
+
* groupLabel: { en: 'Posts', fr: 'Articles' },
|
|
51
|
+
* })
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export declare const registerCustomPrivilege: (collectionOrGlobalSlug: string, config: CustomPrivilegeConfig, options?: {
|
|
55
|
+
type?: "collection" | "global";
|
|
56
|
+
groupLabel?: Record<string, string>;
|
|
57
|
+
}) => Privilege;
|
|
58
|
+
/**
|
|
59
|
+
* Register multiple custom privileges at once to appear in the admin UI
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* import { registerCustomPrivileges } from '@payload-enchants/roles-privileges-payload-plugin'
|
|
64
|
+
*
|
|
65
|
+
* registerCustomPrivileges('posts', [
|
|
66
|
+
* {
|
|
67
|
+
* privilegeKey: 'posts-publish',
|
|
68
|
+
* label: { en: 'Publish Posts', fr: 'Publier les articles' },
|
|
69
|
+
* description: { en: 'Publish posts', fr: 'Publier des articles' },
|
|
70
|
+
* },
|
|
71
|
+
* {
|
|
72
|
+
* privilegeKey: 'posts-feature',
|
|
73
|
+
* label: { en: 'Feature Posts', fr: 'Mettre en vedette les articles' },
|
|
74
|
+
* description: { en: 'Feature posts on homepage', fr: 'Mettre en vedette des articles' },
|
|
75
|
+
* },
|
|
76
|
+
* ], {
|
|
77
|
+
* type: 'collection',
|
|
78
|
+
* groupLabel: { en: 'Posts', fr: 'Articles' },
|
|
79
|
+
* })
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
export declare const registerCustomPrivileges: (collectionOrGlobalSlug: string, configs: CustomPrivilegeConfig[], options?: {
|
|
83
|
+
type?: "collection" | "global";
|
|
84
|
+
groupLabel?: Record<string, string>;
|
|
85
|
+
}) => Privilege[];
|
|
86
|
+
/**
|
|
87
|
+
* Type alias for GlobalPrivilege (for consistency)
|
|
88
|
+
*/
|
|
89
|
+
export type CustomGlobalPrivilege = GlobalPrivilege;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Storage for custom privileges that should appear in the UI
|
|
3
|
+
* Organized by collection/global slug
|
|
4
|
+
*/ export const customPrivilegesRegistry = new Map();
|
|
5
|
+
/**
|
|
6
|
+
* Register a custom privilege to appear in the admin UI
|
|
7
|
+
*
|
|
8
|
+
* @param collectionOrGlobalSlug - The slug of the collection or global this privilege belongs to
|
|
9
|
+
* @param config - The privilege configuration
|
|
10
|
+
* @param options - Additional options
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import { registerCustomPrivilege } from '@payload-enchants/roles-privileges-payload-plugin'
|
|
15
|
+
*
|
|
16
|
+
* // In your payload.config.ts, before the plugin initialization
|
|
17
|
+
* registerCustomPrivilege('posts', {
|
|
18
|
+
* privilegeKey: 'posts-publish',
|
|
19
|
+
* label: { en: 'Publish Posts', fr: 'Publier les articles' },
|
|
20
|
+
* description: { en: 'Ability to publish posts', fr: 'Capacité de publier des articles' },
|
|
21
|
+
* }, {
|
|
22
|
+
* type: 'collection',
|
|
23
|
+
* groupLabel: { en: 'Posts', fr: 'Articles' },
|
|
24
|
+
* })
|
|
25
|
+
* ```
|
|
26
|
+
*/ export const registerCustomPrivilege = (collectionOrGlobalSlug, config, options)=>{
|
|
27
|
+
const privilege = {
|
|
28
|
+
privilegeKey: config.privilegeKey,
|
|
29
|
+
label: config.label,
|
|
30
|
+
description: config.description,
|
|
31
|
+
isCustom: true
|
|
32
|
+
};
|
|
33
|
+
// Get or create the group for this collection/global
|
|
34
|
+
let group = customPrivilegesRegistry.get(collectionOrGlobalSlug);
|
|
35
|
+
if (!group) {
|
|
36
|
+
group = {
|
|
37
|
+
slug: collectionOrGlobalSlug,
|
|
38
|
+
label: options?.groupLabel || {
|
|
39
|
+
_default: collectionOrGlobalSlug
|
|
40
|
+
},
|
|
41
|
+
type: options?.type || 'collection',
|
|
42
|
+
privileges: {}
|
|
43
|
+
};
|
|
44
|
+
customPrivilegesRegistry.set(collectionOrGlobalSlug, group);
|
|
45
|
+
}
|
|
46
|
+
// Add the privilege to the group
|
|
47
|
+
group.privileges[config.privilegeKey] = privilege;
|
|
48
|
+
return privilege;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Register multiple custom privileges at once to appear in the admin UI
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```typescript
|
|
55
|
+
* import { registerCustomPrivileges } from '@payload-enchants/roles-privileges-payload-plugin'
|
|
56
|
+
*
|
|
57
|
+
* registerCustomPrivileges('posts', [
|
|
58
|
+
* {
|
|
59
|
+
* privilegeKey: 'posts-publish',
|
|
60
|
+
* label: { en: 'Publish Posts', fr: 'Publier les articles' },
|
|
61
|
+
* description: { en: 'Publish posts', fr: 'Publier des articles' },
|
|
62
|
+
* },
|
|
63
|
+
* {
|
|
64
|
+
* privilegeKey: 'posts-feature',
|
|
65
|
+
* label: { en: 'Feature Posts', fr: 'Mettre en vedette les articles' },
|
|
66
|
+
* description: { en: 'Feature posts on homepage', fr: 'Mettre en vedette des articles' },
|
|
67
|
+
* },
|
|
68
|
+
* ], {
|
|
69
|
+
* type: 'collection',
|
|
70
|
+
* groupLabel: { en: 'Posts', fr: 'Articles' },
|
|
71
|
+
* })
|
|
72
|
+
* ```
|
|
73
|
+
*/ export const registerCustomPrivileges = (collectionOrGlobalSlug, configs, options)=>{
|
|
74
|
+
return configs.map((config)=>registerCustomPrivilege(collectionOrGlobalSlug, config, options));
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
//# sourceMappingURL=createCustomPrivilege.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/utils/createCustomPrivilege.ts"],"sourcesContent":["import type { GlobalPrivilege } from './generateGlobalPrivileges.js'\nimport type { Privilege } from './generatePrivileges.js'\n\n/**\n * Storage for custom privileges that should appear in the UI\n * Organized by collection/global slug\n */\nexport const customPrivilegesRegistry = new Map<\n string,\n {\n slug: string\n label: Record<string, string>\n type: 'collection' | 'global'\n privileges: Record<string, Privilege | GlobalPrivilege>\n }\n>()\n\n/**\n * Configuration for creating a custom privilege\n */\nexport type CustomPrivilegeConfig = {\n /**\n * Unique key for the privilege (e.g., 'posts-publish', 'users-approve')\n */\n privilegeKey: string\n /**\n * Labels for the privilege in different languages\n * @example { en: 'Publish Posts', fr: 'Publier les articles' }\n */\n label: Record<string, string>\n /**\n * Descriptions for the privilege in different languages\n * @example { en: 'Ability to publish posts', fr: 'Capacité de publier des articles' }\n */\n description: Record<string, string>\n}\n\n/**\n * Register a custom privilege to appear in the admin UI\n *\n * @param collectionOrGlobalSlug - The slug of the collection or global this privilege belongs to\n * @param config - The privilege configuration\n * @param options - Additional options\n *\n * @example\n * ```typescript\n * import { registerCustomPrivilege } from '@payload-enchants/roles-privileges-payload-plugin'\n *\n * // In your payload.config.ts, before the plugin initialization\n * registerCustomPrivilege('posts', {\n * privilegeKey: 'posts-publish',\n * label: { en: 'Publish Posts', fr: 'Publier les articles' },\n * description: { en: 'Ability to publish posts', fr: 'Capacité de publier des articles' },\n * }, {\n * type: 'collection',\n * groupLabel: { en: 'Posts', fr: 'Articles' },\n * })\n * ```\n */\nexport const registerCustomPrivilege = (\n collectionOrGlobalSlug: string,\n config: CustomPrivilegeConfig,\n options?: {\n type?: 'collection' | 'global'\n groupLabel?: Record<string, string>\n },\n): Privilege => {\n const privilege: Privilege = {\n privilegeKey: config.privilegeKey,\n label: config.label,\n description: config.description,\n isCustom: true,\n }\n\n // Get or create the group for this collection/global\n let group = customPrivilegesRegistry.get(collectionOrGlobalSlug)\n\n if (!group) {\n group = {\n slug: collectionOrGlobalSlug,\n label: options?.groupLabel || { _default: collectionOrGlobalSlug },\n type: options?.type || 'collection',\n privileges: {},\n }\n customPrivilegesRegistry.set(collectionOrGlobalSlug, group)\n }\n\n // Add the privilege to the group\n group.privileges[config.privilegeKey] = privilege\n\n return privilege\n}\n\n/**\n * Register multiple custom privileges at once to appear in the admin UI\n *\n * @example\n * ```typescript\n * import { registerCustomPrivileges } from '@payload-enchants/roles-privileges-payload-plugin'\n *\n * registerCustomPrivileges('posts', [\n * {\n * privilegeKey: 'posts-publish',\n * label: { en: 'Publish Posts', fr: 'Publier les articles' },\n * description: { en: 'Publish posts', fr: 'Publier des articles' },\n * },\n * {\n * privilegeKey: 'posts-feature',\n * label: { en: 'Feature Posts', fr: 'Mettre en vedette les articles' },\n * description: { en: 'Feature posts on homepage', fr: 'Mettre en vedette des articles' },\n * },\n * ], {\n * type: 'collection',\n * groupLabel: { en: 'Posts', fr: 'Articles' },\n * })\n * ```\n */\nexport const registerCustomPrivileges = (\n collectionOrGlobalSlug: string,\n configs: CustomPrivilegeConfig[],\n options?: {\n type?: 'collection' | 'global'\n groupLabel?: Record<string, string>\n },\n): Privilege[] => {\n return configs.map((config) => registerCustomPrivilege(collectionOrGlobalSlug, config, options))\n}\n\n/**\n * Type alias for GlobalPrivilege (for consistency)\n */\nexport type CustomGlobalPrivilege = GlobalPrivilege\n"],"names":["customPrivilegesRegistry","Map","registerCustomPrivilege","collectionOrGlobalSlug","config","options","privilege","privilegeKey","label","description","isCustom","group","get","slug","groupLabel","_default","type","privileges","set","registerCustomPrivileges","configs","map"],"mappings":"AAGA;;;CAGC,GACD,OAAO,MAAMA,2BAA2B,IAAIC,MAQzC;AAsBH;;;;;;;;;;;;;;;;;;;;;CAqBC,GACD,OAAO,MAAMC,0BAA0B,CACrCC,wBACAC,QACAC;IAKA,MAAMC,YAAuB;QAC3BC,cAAcH,OAAOG,YAAY;QACjCC,OAAOJ,OAAOI,KAAK;QACnBC,aAAaL,OAAOK,WAAW;QAC/BC,UAAU;IACZ;IAEA,qDAAqD;IACrD,IAAIC,QAAQX,yBAAyBY,GAAG,CAACT;IAEzC,IAAI,CAACQ,OAAO;QACVA,QAAQ;YACNE,MAAMV;YACNK,OAAOH,SAASS,cAAc;gBAAEC,UAAUZ;YAAuB;YACjEa,MAAMX,SAASW,QAAQ;YACvBC,YAAY,CAAC;QACf;QACAjB,yBAAyBkB,GAAG,CAACf,wBAAwBQ;IACvD;IAEA,iCAAiC;IACjCA,MAAMM,UAAU,CAACb,OAAOG,YAAY,CAAC,GAAGD;IAExC,OAAOA;AACT,EAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;CAuBC,GACD,OAAO,MAAMa,2BAA2B,CACtChB,wBACAiB,SACAf;IAKA,OAAOe,QAAQC,GAAG,CAAC,CAACjB,SAAWF,wBAAwBC,wBAAwBC,QAAQC;AACzF,EAAC"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { GlobalConfig } from 'payload';
|
|
2
|
+
/**
|
|
3
|
+
* Available privilege types for all global operations
|
|
4
|
+
*/
|
|
5
|
+
export type GlobalPrivilegeType = 'read' | 'readDrafts' | 'readVersions' | 'update';
|
|
6
|
+
/**
|
|
7
|
+
* Interface for a single privilege
|
|
8
|
+
*/
|
|
9
|
+
export interface GlobalPrivilege {
|
|
10
|
+
privilegeKey: string;
|
|
11
|
+
label: Record<string, string>;
|
|
12
|
+
description: Record<string, string>;
|
|
13
|
+
isCustom?: boolean;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Interface for a global's privileges
|
|
17
|
+
*/
|
|
18
|
+
export interface GlobalPrivileges {
|
|
19
|
+
globalSlug: string;
|
|
20
|
+
globalLabel: Record<string, string>;
|
|
21
|
+
description: Record<string, string>;
|
|
22
|
+
privileges: {
|
|
23
|
+
read: GlobalPrivilege;
|
|
24
|
+
readDrafts: GlobalPrivilege;
|
|
25
|
+
readVersions: GlobalPrivilege;
|
|
26
|
+
update: GlobalPrivilege;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Map to store all generated global privileges by global slug
|
|
31
|
+
*/
|
|
32
|
+
export declare const allGlobalPrivilegesMap: Map<string, GlobalPrivileges>;
|
|
33
|
+
/**
|
|
34
|
+
* Generate privilege key from global slug and operation
|
|
35
|
+
*/
|
|
36
|
+
export declare const generateGlobalPrivilegeKey: (globalSlug: string, operation: GlobalPrivilegeType) => string;
|
|
37
|
+
/**
|
|
38
|
+
* Generate read and update privileges for a global
|
|
39
|
+
*/
|
|
40
|
+
export declare const generateGlobalPrivileges: (global: GlobalConfig) => GlobalPrivileges;
|
|
41
|
+
/**
|
|
42
|
+
* Get all global privilege keys
|
|
43
|
+
*/
|
|
44
|
+
export declare const getAllGlobalPrivilegeKeys: () => string[];
|
|
45
|
+
/**
|
|
46
|
+
* Get all global privileges as a flat array
|
|
47
|
+
*/
|
|
48
|
+
export declare const getAllGlobalPrivileges: () => GlobalPrivilege[];
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { translations } from '../translations/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Map to store all generated global privileges by global slug
|
|
4
|
+
*/ export const allGlobalPrivilegesMap = new Map();
|
|
5
|
+
/**
|
|
6
|
+
* Generate privilege key from global slug and operation
|
|
7
|
+
*/ export const generateGlobalPrivilegeKey = (globalSlug, operation)=>{
|
|
8
|
+
return `${globalSlug}-${operation}`;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Capitalize first letter of a string
|
|
12
|
+
*/ const capitalize = (str)=>{
|
|
13
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Get label from global config or use slug
|
|
17
|
+
*/ const getGlobalLabel = (global)=>{
|
|
18
|
+
if (global.label) {
|
|
19
|
+
if (typeof global.label === 'string') {
|
|
20
|
+
// Return the string for all languages (will be used as fallback)
|
|
21
|
+
return {
|
|
22
|
+
_default: global.label
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
if (typeof global.label === 'object' && !Array.isArray(global.label)) {
|
|
26
|
+
return global.label;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
// Default to slug
|
|
30
|
+
return {
|
|
31
|
+
_default: global.slug
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Get operation prefix translations for each language
|
|
36
|
+
*/ const getGlobalOperationPrefix = (operation, lang)=>{
|
|
37
|
+
const langTranslations = translations[lang] || translations.en;
|
|
38
|
+
const key = `privilege-prefix-global-${operation}`;
|
|
39
|
+
return langTranslations['plugin-roles-privileges'][key] || translations.en['plugin-roles-privileges'][key];
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Generate operation labels based on operation type
|
|
43
|
+
*/ const getGlobalOperationLabels = (operation, label)=>{
|
|
44
|
+
const result = {};
|
|
45
|
+
// Get all available languages from label
|
|
46
|
+
const languages = Object.keys(label);
|
|
47
|
+
for (const lang of languages){
|
|
48
|
+
const labelText = label[lang];
|
|
49
|
+
const prefix = getGlobalOperationPrefix(operation, lang);
|
|
50
|
+
result[lang] = `${prefix} ${labelText}`;
|
|
51
|
+
}
|
|
52
|
+
return result;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Get operation description templates for each language
|
|
56
|
+
*/ const getGlobalOperationDescriptionTemplate = (operation, lang)=>{
|
|
57
|
+
const langTranslations = translations[lang] || translations.en;
|
|
58
|
+
const key = `privilege-template-global-${operation}`;
|
|
59
|
+
return langTranslations['plugin-roles-privileges'][key] || translations.en['plugin-roles-privileges'][key];
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Generate operation descriptions based on operation type
|
|
63
|
+
*/ const getGlobalOperationDescriptions = (operation, label)=>{
|
|
64
|
+
const result = {};
|
|
65
|
+
// Get all available languages
|
|
66
|
+
const languages = Object.keys(label);
|
|
67
|
+
for (const lang of languages){
|
|
68
|
+
const labelText = label[lang].toLowerCase();
|
|
69
|
+
const template = getGlobalOperationDescriptionTemplate(operation, lang);
|
|
70
|
+
result[lang] = template.replace('{label}', labelText);
|
|
71
|
+
}
|
|
72
|
+
return result;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* Generate a single privilege for a global operation
|
|
76
|
+
*/ const generateGlobalPrivilege = (globalSlug, operation, label)=>{
|
|
77
|
+
return {
|
|
78
|
+
privilegeKey: generateGlobalPrivilegeKey(globalSlug, operation),
|
|
79
|
+
label: getGlobalOperationLabels(operation, label),
|
|
80
|
+
description: getGlobalOperationDescriptions(operation, label)
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
/**
|
|
84
|
+
* Generate read and update privileges for a global
|
|
85
|
+
*/ export const generateGlobalPrivileges = (global)=>{
|
|
86
|
+
const label = getGlobalLabel(global);
|
|
87
|
+
const description = {};
|
|
88
|
+
const languages = Object.keys(label);
|
|
89
|
+
for (const lang of languages){
|
|
90
|
+
const labelText = label[lang].toLowerCase();
|
|
91
|
+
const langTranslations = translations[lang] || translations.en;
|
|
92
|
+
const template = langTranslations['plugin-roles-privileges']['privilege-global-description'] || translations.en['plugin-roles-privileges']['privilege-global-description'];
|
|
93
|
+
description[lang] = template.replace('{label}', labelText);
|
|
94
|
+
}
|
|
95
|
+
const globalPrivileges = {
|
|
96
|
+
globalSlug: global.slug,
|
|
97
|
+
globalLabel: label,
|
|
98
|
+
description,
|
|
99
|
+
privileges: {
|
|
100
|
+
read: generateGlobalPrivilege(global.slug, 'read', label),
|
|
101
|
+
readDrafts: generateGlobalPrivilege(global.slug, 'readDrafts', label),
|
|
102
|
+
readVersions: generateGlobalPrivilege(global.slug, 'readVersions', label),
|
|
103
|
+
update: generateGlobalPrivilege(global.slug, 'update', label)
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
// Store in the map
|
|
107
|
+
allGlobalPrivilegesMap.set(global.slug, globalPrivileges);
|
|
108
|
+
return globalPrivileges;
|
|
109
|
+
};
|
|
110
|
+
/**
|
|
111
|
+
* Get all global privilege keys
|
|
112
|
+
*/ export const getAllGlobalPrivilegeKeys = ()=>{
|
|
113
|
+
const keys = [];
|
|
114
|
+
allGlobalPrivilegesMap.forEach((globalPrivileges)=>{
|
|
115
|
+
Object.values(globalPrivileges.privileges).forEach((privilege)=>{
|
|
116
|
+
keys.push(privilege.privilegeKey);
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
return keys;
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* Get all global privileges as a flat array
|
|
123
|
+
*/ export const getAllGlobalPrivileges = ()=>{
|
|
124
|
+
const privileges = [];
|
|
125
|
+
allGlobalPrivilegesMap.forEach((globalPrivileges)=>{
|
|
126
|
+
Object.values(globalPrivileges.privileges).forEach((privilege)=>{
|
|
127
|
+
privileges.push(privilege);
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
return privileges;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
//# sourceMappingURL=generateGlobalPrivileges.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/utils/generateGlobalPrivileges.ts"],"sourcesContent":["import type { GlobalConfig } from 'payload'\nimport { translations } from '../translations/index.js'\n\n/**\n * Available privilege types for all global operations\n */\nexport type GlobalPrivilegeType = 'read' | 'readDrafts' | 'readVersions' | 'update'\n\n/**\n * Interface for a single privilege\n */\nexport interface GlobalPrivilege {\n privilegeKey: string\n label: Record<string, string>\n description: Record<string, string>\n isCustom?: boolean\n}\n\n/**\n * Interface for a global's privileges\n */\nexport interface GlobalPrivileges {\n globalSlug: string\n globalLabel: Record<string, string>\n description: Record<string, string>\n privileges: {\n read: GlobalPrivilege\n readDrafts: GlobalPrivilege\n readVersions: GlobalPrivilege\n update: GlobalPrivilege\n }\n}\n\n/**\n * Map to store all generated global privileges by global slug\n */\nexport const allGlobalPrivilegesMap = new Map<string, GlobalPrivileges>()\n\n/**\n * Generate privilege key from global slug and operation\n */\nexport const generateGlobalPrivilegeKey = (\n globalSlug: string,\n operation: GlobalPrivilegeType,\n): string => {\n return `${globalSlug}-${operation}`\n}\n\n/**\n * Capitalize first letter of a string\n */\nconst capitalize = (str: string): string => {\n return str.charAt(0).toUpperCase() + str.slice(1)\n}\n\n/**\n * Get label from global config or use slug\n */\nconst getGlobalLabel = (global: GlobalConfig): Record<string, string> => {\n if (global.label) {\n if (typeof global.label === 'string') {\n // Return the string for all languages (will be used as fallback)\n return { _default: global.label }\n }\n if (typeof global.label === 'object' && !Array.isArray(global.label)) {\n return global.label as Record<string, string>\n }\n }\n\n // Default to slug\n return { _default: global.slug }\n}\n\n/**\n * Get operation prefix translations for each language\n */\nconst getGlobalOperationPrefix = (operation: GlobalPrivilegeType, lang: string): string => {\n const langTranslations = translations[lang] || translations.en\n const key =\n `privilege-prefix-global-${operation}` as keyof (typeof langTranslations)['plugin-roles-privileges']\n return (\n (langTranslations['plugin-roles-privileges'][key] as string) ||\n (translations.en['plugin-roles-privileges'][key] as string)\n )\n}\n\n/**\n * Generate operation labels based on operation type\n */\nconst getGlobalOperationLabels = (\n operation: GlobalPrivilegeType,\n label: Record<string, string>,\n): Record<string, string> => {\n const result: Record<string, string> = {}\n\n // Get all available languages from label\n const languages = Object.keys(label)\n\n for (const lang of languages) {\n const labelText = label[lang]\n const prefix = getGlobalOperationPrefix(operation, lang)\n result[lang] = `${prefix} ${labelText}`\n }\n\n return result\n}\n\n/**\n * Get operation description templates for each language\n */\nconst getGlobalOperationDescriptionTemplate = (\n operation: GlobalPrivilegeType,\n lang: string,\n): string => {\n const langTranslations = translations[lang] || translations.en\n const key =\n `privilege-template-global-${operation}` as keyof (typeof langTranslations)['plugin-roles-privileges']\n return (\n (langTranslations['plugin-roles-privileges'][key] as string) ||\n (translations.en['plugin-roles-privileges'][key] as string)\n )\n}\n\n/**\n * Generate operation descriptions based on operation type\n */\nconst getGlobalOperationDescriptions = (\n operation: GlobalPrivilegeType,\n label: Record<string, string>,\n): Record<string, string> => {\n const result: Record<string, string> = {}\n\n // Get all available languages\n const languages = Object.keys(label)\n\n for (const lang of languages) {\n const labelText = label[lang].toLowerCase()\n const template = getGlobalOperationDescriptionTemplate(operation, lang)\n result[lang] = template.replace('{label}', labelText)\n }\n\n return result\n}\n\n/**\n * Generate a single privilege for a global operation\n */\nconst generateGlobalPrivilege = (\n globalSlug: string,\n operation: GlobalPrivilegeType,\n label: Record<string, string>,\n): GlobalPrivilege => {\n return {\n privilegeKey: generateGlobalPrivilegeKey(globalSlug, operation),\n label: getGlobalOperationLabels(operation, label),\n description: getGlobalOperationDescriptions(operation, label),\n }\n}\n\n/**\n * Generate read and update privileges for a global\n */\nexport const generateGlobalPrivileges = (global: GlobalConfig): GlobalPrivileges => {\n const label = getGlobalLabel(global)\n\n const description: Record<string, string> = {}\n const languages = Object.keys(label)\n for (const lang of languages) {\n const labelText = label[lang].toLowerCase()\n const langTranslations = translations[lang] || translations.en\n const template =\n (langTranslations['plugin-roles-privileges']['privilege-global-description'] as string) ||\n (translations.en['plugin-roles-privileges']['privilege-global-description'] as string)\n description[lang] = template.replace('{label}', labelText)\n }\n\n const globalPrivileges: GlobalPrivileges = {\n globalSlug: global.slug,\n globalLabel: label,\n description,\n privileges: {\n read: generateGlobalPrivilege(global.slug, 'read', label),\n readDrafts: generateGlobalPrivilege(global.slug, 'readDrafts', label),\n readVersions: generateGlobalPrivilege(global.slug, 'readVersions', label),\n update: generateGlobalPrivilege(global.slug, 'update', label),\n },\n }\n\n // Store in the map\n allGlobalPrivilegesMap.set(global.slug, globalPrivileges)\n\n return globalPrivileges\n}\n\n/**\n * Get all global privilege keys\n */\nexport const getAllGlobalPrivilegeKeys = (): string[] => {\n const keys: string[] = []\n allGlobalPrivilegesMap.forEach((globalPrivileges) => {\n Object.values(globalPrivileges.privileges).forEach((privilege) => {\n keys.push(privilege.privilegeKey)\n })\n })\n return keys\n}\n\n/**\n * Get all global privileges as a flat array\n */\nexport const getAllGlobalPrivileges = (): GlobalPrivilege[] => {\n const privileges: GlobalPrivilege[] = []\n allGlobalPrivilegesMap.forEach((globalPrivileges) => {\n Object.values(globalPrivileges.privileges).forEach((privilege) => {\n privileges.push(privilege)\n })\n })\n return privileges\n}\n"],"names":["translations","allGlobalPrivilegesMap","Map","generateGlobalPrivilegeKey","globalSlug","operation","capitalize","str","charAt","toUpperCase","slice","getGlobalLabel","global","label","_default","Array","isArray","slug","getGlobalOperationPrefix","lang","langTranslations","en","key","getGlobalOperationLabels","result","languages","Object","keys","labelText","prefix","getGlobalOperationDescriptionTemplate","getGlobalOperationDescriptions","toLowerCase","template","replace","generateGlobalPrivilege","privilegeKey","description","generateGlobalPrivileges","globalPrivileges","globalLabel","privileges","read","readDrafts","readVersions","update","set","getAllGlobalPrivilegeKeys","forEach","values","privilege","push","getAllGlobalPrivileges"],"mappings":"AACA,SAASA,YAAY,QAAQ,2BAA0B;AAgCvD;;CAEC,GACD,OAAO,MAAMC,yBAAyB,IAAIC,MAA+B;AAEzE;;CAEC,GACD,OAAO,MAAMC,6BAA6B,CACxCC,YACAC;IAEA,OAAO,GAAGD,WAAW,CAAC,EAAEC,WAAW;AACrC,EAAC;AAED;;CAEC,GACD,MAAMC,aAAa,CAACC;IAClB,OAAOA,IAAIC,MAAM,CAAC,GAAGC,WAAW,KAAKF,IAAIG,KAAK,CAAC;AACjD;AAEA;;CAEC,GACD,MAAMC,iBAAiB,CAACC;IACtB,IAAIA,OAAOC,KAAK,EAAE;QAChB,IAAI,OAAOD,OAAOC,KAAK,KAAK,UAAU;YACpC,iEAAiE;YACjE,OAAO;gBAAEC,UAAUF,OAAOC,KAAK;YAAC;QAClC;QACA,IAAI,OAAOD,OAAOC,KAAK,KAAK,YAAY,CAACE,MAAM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B;IACvC,MAAMpB,OAAiB,EAAE;IACzB1B,uBAAuB+C,OAAO,CAAC,CAACT;QAC9Bb,OAAOuB,MAAM,CAACV,iBAAiBE,UAAU,EAAEO,OAAO,CAAC,CAACE;YAClDvB,KAAKwB,IAAI,CAACD,UAAUd,YAAY;QAClC;IACF;IACA,OAAOT;AACT,EAAC;AAED;;CAEC,GACD,OAAO,MAAMyB,yBAAyB;IACpC,MAAMX,aAAgC,EAAE;IACxCxC,uBAAuB+C,OAAO,CAAC,CAACT;QAC9Bb,OAAOuB,MAAM,CAACV,iBAAiBE,UAAU,EAAEO,OAAO,CAAC,CAACE;YAClDT,WAAWU,IAAI,CAACD;QAClB;IACF;IACA,OAAOT;AACT,EAAC"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { CollectionConfig } from 'payload';
|
|
2
|
+
/**
|
|
3
|
+
* Available privilege types for all collection operations
|
|
4
|
+
*/
|
|
5
|
+
export type PrivilegeType = 'admin' | 'create' | 'read' | 'readVersions' | 'update' | 'delete' | 'unlock';
|
|
6
|
+
/**
|
|
7
|
+
* Interface for a single privilege
|
|
8
|
+
*/
|
|
9
|
+
export interface Privilege {
|
|
10
|
+
privilegeKey: string;
|
|
11
|
+
label: Record<string, string>;
|
|
12
|
+
description: Record<string, string>;
|
|
13
|
+
isCustom?: boolean;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Interface for a collection's privileges
|
|
17
|
+
*/
|
|
18
|
+
export interface CollectionPrivileges {
|
|
19
|
+
collectionSlug: string;
|
|
20
|
+
collectionLabel: Record<string, string>;
|
|
21
|
+
description: Record<string, string>;
|
|
22
|
+
privileges: {
|
|
23
|
+
admin: Privilege;
|
|
24
|
+
create: Privilege;
|
|
25
|
+
read: Privilege;
|
|
26
|
+
readVersions: Privilege;
|
|
27
|
+
update: Privilege;
|
|
28
|
+
delete: Privilege;
|
|
29
|
+
unlock: Privilege;
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Map to store all generated privileges by collection slug
|
|
34
|
+
*/
|
|
35
|
+
export declare const allPrivilegesMap: Map<string, CollectionPrivileges>;
|
|
36
|
+
/**
|
|
37
|
+
* Generate privilege key from collection slug and operation
|
|
38
|
+
*/
|
|
39
|
+
export declare const generatePrivilegeKey: (collectionSlug: string, operation: PrivilegeType) => string;
|
|
40
|
+
/**
|
|
41
|
+
* Generate all CRUD privileges for a collection
|
|
42
|
+
*/
|
|
43
|
+
export declare const generateCollectionPrivileges: (collection: CollectionConfig) => CollectionPrivileges;
|
|
44
|
+
/**
|
|
45
|
+
* Get all privilege keys as a union type
|
|
46
|
+
*/
|
|
47
|
+
export declare const getAllPrivilegeKeys: () => string[];
|
|
48
|
+
/**
|
|
49
|
+
* Get all privileges as a flat array
|
|
50
|
+
*/
|
|
51
|
+
export declare const getAllPrivileges: () => Privilege[];
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { translations } from '../translations/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Map to store all generated privileges by collection slug
|
|
4
|
+
*/ export const allPrivilegesMap = new Map();
|
|
5
|
+
/**
|
|
6
|
+
* Generate privilege key from collection slug and operation
|
|
7
|
+
*/ export const generatePrivilegeKey = (collectionSlug, operation)=>{
|
|
8
|
+
return `${collectionSlug}-${operation}`;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Capitalize first letter of a string
|
|
12
|
+
*/ const capitalize = (str)=>{
|
|
13
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Get singular label from collection config or use slug
|
|
17
|
+
*/ const getSingularLabel = (collection)=>{
|
|
18
|
+
if (collection.labels?.singular) {
|
|
19
|
+
if (typeof collection.labels.singular === 'string') {
|
|
20
|
+
// Return the string for all languages (will be used as fallback)
|
|
21
|
+
return {
|
|
22
|
+
_default: collection.labels.singular
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
if (typeof collection.labels.singular === 'object' && !Array.isArray(collection.labels.singular)) {
|
|
26
|
+
return collection.labels.singular;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
// Default to slug
|
|
30
|
+
return {
|
|
31
|
+
_default: collection.slug
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Get plural label from collection config or use slug
|
|
36
|
+
*/ const getPluralLabel = (collection)=>{
|
|
37
|
+
if (collection.labels?.plural) {
|
|
38
|
+
if (typeof collection.labels.plural === 'string') {
|
|
39
|
+
// Return the string for all languages (will be used as fallback)
|
|
40
|
+
return {
|
|
41
|
+
_default: collection.labels.plural
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
if (typeof collection.labels.plural === 'object' && !Array.isArray(collection.labels.plural)) {
|
|
45
|
+
return collection.labels.plural;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
// Default to slug
|
|
49
|
+
return {
|
|
50
|
+
_default: collection.slug
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Get operation prefix translations for each language
|
|
55
|
+
*/ const getOperationPrefix = (operation, lang)=>{
|
|
56
|
+
const langTranslations = translations[lang] || translations.en;
|
|
57
|
+
const key = `privilege-prefix-${operation}`;
|
|
58
|
+
return langTranslations['plugin-roles-privileges'][key] || translations.en['plugin-roles-privileges'][key];
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* Generate operation labels based on operation type
|
|
62
|
+
*/ const getOperationLabels = (operation, singularLabel)=>{
|
|
63
|
+
const result = {};
|
|
64
|
+
// Get all available languages from singularLabel
|
|
65
|
+
const languages = Object.keys(singularLabel);
|
|
66
|
+
for (const lang of languages){
|
|
67
|
+
const label = singularLabel[lang];
|
|
68
|
+
const prefix = getOperationPrefix(operation, lang);
|
|
69
|
+
result[lang] = operation === 'readVersions' ? `${prefix} ${label}` : `${prefix} ${label}`;
|
|
70
|
+
}
|
|
71
|
+
return result;
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* Get operation description templates for each language
|
|
75
|
+
*/ const getOperationDescriptionTemplate = (operation, lang)=>{
|
|
76
|
+
const langTranslations = translations[lang] || translations.en;
|
|
77
|
+
const templateKey = `privilege-template-${operation}`;
|
|
78
|
+
const pluralKey = `privilege-template-${operation}-plural`;
|
|
79
|
+
const template = langTranslations['plugin-roles-privileges'][templateKey] || translations.en['plugin-roles-privileges'][templateKey];
|
|
80
|
+
const usePlural = langTranslations['plugin-roles-privileges'][pluralKey] === 'true';
|
|
81
|
+
return {
|
|
82
|
+
template,
|
|
83
|
+
usePlural
|
|
84
|
+
};
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* Generate operation descriptions based on operation type
|
|
88
|
+
*/ const getOperationDescriptions = (operation, singularLabel, pluralLabel)=>{
|
|
89
|
+
const result = {};
|
|
90
|
+
// Get all available languages
|
|
91
|
+
const languages = Object.keys(singularLabel);
|
|
92
|
+
for (const lang of languages){
|
|
93
|
+
const { template, usePlural } = getOperationDescriptionTemplate(operation, lang);
|
|
94
|
+
const labelToUse = usePlural ? pluralLabel[lang] : singularLabel[lang];
|
|
95
|
+
result[lang] = template.replace('{label}', labelToUse.toLowerCase());
|
|
96
|
+
}
|
|
97
|
+
return result;
|
|
98
|
+
};
|
|
99
|
+
/**
|
|
100
|
+
* Generate a single privilege for a collection operation
|
|
101
|
+
*/ const generatePrivilege = (collectionSlug, operation, singularLabel, pluralLabel)=>{
|
|
102
|
+
return {
|
|
103
|
+
privilegeKey: generatePrivilegeKey(collectionSlug, operation),
|
|
104
|
+
label: getOperationLabels(operation, singularLabel),
|
|
105
|
+
description: getOperationDescriptions(operation, singularLabel, pluralLabel)
|
|
106
|
+
};
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* Generate all CRUD privileges for a collection
|
|
110
|
+
*/ export const generateCollectionPrivileges = (collection)=>{
|
|
111
|
+
const singularLabel = getSingularLabel(collection);
|
|
112
|
+
const pluralLabel = getPluralLabel(collection);
|
|
113
|
+
const description = {};
|
|
114
|
+
const languages = Object.keys(pluralLabel);
|
|
115
|
+
for (const lang of languages){
|
|
116
|
+
const plural = pluralLabel[lang].toLowerCase();
|
|
117
|
+
const langTranslations = translations[lang] || translations.en;
|
|
118
|
+
const template = langTranslations['plugin-roles-privileges']['privilege-collection-description'] || translations.en['plugin-roles-privileges']['privilege-collection-description'];
|
|
119
|
+
description[lang] = template.replace('{label}', plural);
|
|
120
|
+
}
|
|
121
|
+
const collectionPrivileges = {
|
|
122
|
+
collectionSlug: collection.slug,
|
|
123
|
+
collectionLabel: pluralLabel,
|
|
124
|
+
description,
|
|
125
|
+
privileges: {
|
|
126
|
+
admin: generatePrivilege(collection.slug, 'admin', singularLabel, pluralLabel),
|
|
127
|
+
create: generatePrivilege(collection.slug, 'create', singularLabel, pluralLabel),
|
|
128
|
+
read: generatePrivilege(collection.slug, 'read', singularLabel, pluralLabel),
|
|
129
|
+
readVersions: generatePrivilege(collection.slug, 'readVersions', singularLabel, pluralLabel),
|
|
130
|
+
update: generatePrivilege(collection.slug, 'update', singularLabel, pluralLabel),
|
|
131
|
+
delete: generatePrivilege(collection.slug, 'delete', singularLabel, pluralLabel),
|
|
132
|
+
unlock: generatePrivilege(collection.slug, 'unlock', singularLabel, pluralLabel)
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
// Store in the map
|
|
136
|
+
allPrivilegesMap.set(collection.slug, collectionPrivileges);
|
|
137
|
+
return collectionPrivileges;
|
|
138
|
+
};
|
|
139
|
+
/**
|
|
140
|
+
* Get all privilege keys as a union type
|
|
141
|
+
*/ export const getAllPrivilegeKeys = ()=>{
|
|
142
|
+
const keys = [];
|
|
143
|
+
allPrivilegesMap.forEach((collectionPrivileges)=>{
|
|
144
|
+
Object.values(collectionPrivileges.privileges).forEach((privilege)=>{
|
|
145
|
+
keys.push(privilege.privilegeKey);
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
return keys;
|
|
149
|
+
};
|
|
150
|
+
/**
|
|
151
|
+
* Get all privileges as a flat array
|
|
152
|
+
*/ export const getAllPrivileges = ()=>{
|
|
153
|
+
const privileges = [];
|
|
154
|
+
allPrivilegesMap.forEach((collectionPrivileges)=>{
|
|
155
|
+
Object.values(collectionPrivileges.privileges).forEach((privilege)=>{
|
|
156
|
+
privileges.push(privilege);
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
return privileges;
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
//# sourceMappingURL=generatePrivileges.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/utils/generatePrivileges.ts"],"sourcesContent":["import type { CollectionConfig } from 'payload'\nimport { translations } from '../translations/index.js'\n\n/**\n * Available privilege types for all collection operations\n */\nexport type PrivilegeType =\n | 'admin'\n | 'create'\n | 'read'\n | 'readVersions'\n | 'update'\n | 'delete'\n | 'unlock'\n\n/**\n * Interface for a single privilege\n */\nexport interface Privilege {\n privilegeKey: string\n label: Record<string, string>\n description: Record<string, string>\n isCustom?: boolean\n}\n\n/**\n * Interface for a collection's privileges\n */\nexport interface CollectionPrivileges {\n collectionSlug: string\n collectionLabel: Record<string, string>\n description: Record<string, string>\n privileges: {\n admin: Privilege\n create: Privilege\n read: Privilege\n readVersions: Privilege\n update: Privilege\n delete: Privilege\n unlock: Privilege\n }\n}\n\n/**\n * Map to store all generated privileges by collection slug\n */\nexport const allPrivilegesMap = new Map<string, CollectionPrivileges>()\n\n/**\n * Generate privilege key from collection slug and operation\n */\nexport const generatePrivilegeKey = (collectionSlug: string, operation: PrivilegeType): string => {\n return `${collectionSlug}-${operation}`\n}\n\n/**\n * Capitalize first letter of a string\n */\nconst capitalize = (str: string): string => {\n return str.charAt(0).toUpperCase() + str.slice(1)\n}\n\n/**\n * Get singular label from collection config or use slug\n */\nconst getSingularLabel = (collection: CollectionConfig): Record<string, string> => {\n if (collection.labels?.singular) {\n if (typeof collection.labels.singular === 'string') {\n // Return the string for all languages (will be used as fallback)\n return { _default: collection.labels.singular }\n }\n if (\n typeof collection.labels.singular === 'object' &&\n !Array.isArray(collection.labels.singular)\n ) {\n return collection.labels.singular as Record<string, string>\n }\n }\n\n // Default to slug\n return { _default: collection.slug }\n}\n\n/**\n * Get plural label from collection config or use slug\n */\nconst getPluralLabel = (collection: CollectionConfig): Record<string, string> => {\n if (collection.labels?.plural) {\n if (typeof collection.labels.plural === 'string') {\n // Return the string for all languages (will be used as fallback)\n return { _default: collection.labels.plural }\n }\n if (typeof collection.labels.plural === 'object' && !Array.isArray(collection.labels.plural)) {\n return collection.labels.plural as Record<string, string>\n }\n }\n\n // Default to slug\n return { _default: collection.slug }\n}\n\n/**\n * Get operation prefix translations for each language\n */\nconst getOperationPrefix = (operation: PrivilegeType, lang: string): string => {\n const langTranslations = translations[lang] || translations.en\n const key =\n `privilege-prefix-${operation}` as keyof (typeof langTranslations)['plugin-roles-privileges']\n return (\n (langTranslations['plugin-roles-privileges'][key] as string) ||\n (translations.en['plugin-roles-privileges'][key] as string)\n )\n}\n\n/**\n * Generate operation labels based on operation type\n */\nconst getOperationLabels = (\n operation: PrivilegeType,\n singularLabel: Record<string, string>,\n): Record<string, string> => {\n const result: Record<string, string> = {}\n\n // Get all available languages from singularLabel\n const languages = Object.keys(singularLabel)\n\n for (const lang of languages) {\n const label = singularLabel[lang]\n const prefix = getOperationPrefix(operation, lang)\n result[lang] = operation === 'readVersions' ? `${prefix} ${label}` : `${prefix} ${label}`\n }\n\n return result\n}\n\n/**\n * Get operation description templates for each language\n */\nconst getOperationDescriptionTemplate = (\n operation: PrivilegeType,\n lang: string,\n): { template: string; usePlural: boolean } => {\n const langTranslations = translations[lang] || translations.en\n const templateKey =\n `privilege-template-${operation}` as keyof (typeof langTranslations)['plugin-roles-privileges']\n const pluralKey =\n `privilege-template-${operation}-plural` as keyof (typeof langTranslations)['plugin-roles-privileges']\n\n const template =\n (langTranslations['plugin-roles-privileges'][templateKey] as string) ||\n (translations.en['plugin-roles-privileges'][templateKey] as string)\n const usePlural = (langTranslations['plugin-roles-privileges'][pluralKey] as string) === 'true'\n\n return { template, usePlural }\n}\n\n/**\n * Generate operation descriptions based on operation type\n */\nconst getOperationDescriptions = (\n operation: PrivilegeType,\n singularLabel: Record<string, string>,\n pluralLabel: Record<string, string>,\n): Record<string, string> => {\n const result: Record<string, string> = {}\n\n // Get all available languages\n const languages = Object.keys(singularLabel)\n\n for (const lang of languages) {\n const { template, usePlural } = getOperationDescriptionTemplate(operation, lang)\n const labelToUse = usePlural ? pluralLabel[lang] : singularLabel[lang]\n result[lang] = template.replace('{label}', labelToUse.toLowerCase())\n }\n\n return result\n}\n\n/**\n * Generate a single privilege for a collection operation\n */\nconst generatePrivilege = (\n collectionSlug: string,\n operation: PrivilegeType,\n singularLabel: Record<string, string>,\n pluralLabel: Record<string, string>,\n): Privilege => {\n return {\n privilegeKey: generatePrivilegeKey(collectionSlug, operation),\n label: getOperationLabels(operation, singularLabel),\n description: getOperationDescriptions(operation, singularLabel, pluralLabel),\n }\n}\n\n/**\n * Generate all CRUD privileges for a collection\n */\nexport const generateCollectionPrivileges = (\n collection: CollectionConfig,\n): CollectionPrivileges => {\n const singularLabel = getSingularLabel(collection)\n const pluralLabel = getPluralLabel(collection)\n\n const description: Record<string, string> = {}\n const languages = Object.keys(pluralLabel)\n for (const lang of languages) {\n const plural = pluralLabel[lang].toLowerCase()\n const langTranslations = translations[lang] || translations.en\n const template =\n (langTranslations['plugin-roles-privileges']['privilege-collection-description'] as string) ||\n (translations.en['plugin-roles-privileges']['privilege-collection-description'] as string)\n description[lang] = template.replace('{label}', plural)\n }\n\n const collectionPrivileges: CollectionPrivileges = {\n collectionSlug: collection.slug,\n collectionLabel: pluralLabel,\n description,\n privileges: {\n admin: generatePrivilege(collection.slug, 'admin', singularLabel, pluralLabel),\n create: generatePrivilege(collection.slug, 'create', singularLabel, pluralLabel),\n read: generatePrivilege(collection.slug, 'read', singularLabel, pluralLabel),\n readVersions: generatePrivilege(collection.slug, 'readVersions', singularLabel, pluralLabel),\n update: generatePrivilege(collection.slug, 'update', singularLabel, pluralLabel),\n delete: generatePrivilege(collection.slug, 'delete', singularLabel, pluralLabel),\n unlock: generatePrivilege(collection.slug, 'unlock', singularLabel, pluralLabel),\n },\n }\n\n // Store in the map\n allPrivilegesMap.set(collection.slug, collectionPrivileges)\n\n return collectionPrivileges\n}\n\n/**\n * Get all privilege keys as a union type\n */\nexport const getAllPrivilegeKeys = (): string[] => {\n const keys: string[] = []\n allPrivilegesMap.forEach((collectionPrivileges) => {\n Object.values(collectionPrivileges.privileges).forEach((privilege) => {\n keys.push(privilege.privilegeKey)\n })\n })\n return keys\n}\n\n/**\n * Get all privileges as a flat array\n */\nexport const getAllPrivileges = (): Privilege[] => {\n const privileges: Privilege[] = []\n allPrivilegesMap.forEach((collectionPrivileges) => {\n Object.values(collectionPrivileges.privileges).forEach((privilege) => {\n privileges.push(privilege)\n })\n })\n return privileges\n}\n"],"names":["translations","allPrivilegesMap","Map","generatePrivilegeKey","collectionSlug","operation","capitalize","str","charAt","toUpperCase","slice","getSingularLabel","collection","labels","singular","_default","Array","isArray","slug","getPluralLabel","plural","getOperationPrefix","lang","langTranslations","en","key","getOperationLabels","singularLabel","result","languages","Object","keys","label","prefix","getOperationDescriptionTemplate","templateKey","pluralKey","template","usePlural","getOperationDescriptions","pluralLabel","labelToUse","replace","toLowerCase","generatePrivilege","privilegeKey","description","generateCollectionPrivileges","collectionPrivileges","collectionLabel","privileges","admin","create","read","readVersions","update","delete","unlock","set","getAllPrivilegeKeys","forEach","values","privilege","push","getAllPrivileges"],"mappings":"AACA,SAASA,YAAY,QAAQ,2BAA0B;AA0CvD;;CAEC,GACD,OAAO,MAAMC,mBAAmB,IAAIC,MAAmC;AAEvE;;CAEC,GACD,OAAO,MAAMC,uBAAuB,CAACC,gBAAwBC;IAC3D,OAAO,GAAGD,eAAe,CAAC,EAAEC,WAAW;AACzC,EAAC;AAED;;CAEC,GACD,MAAMC,a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