roles-privileges-payload-plugin 1.0.2 → 1.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.
Files changed (51) hide show
  1. package/README.md +5 -1
  2. package/dist/collections/roles.d.ts +32 -0
  3. package/dist/collections/roles.js +122 -0
  4. package/dist/collections/roles.js.map +1 -0
  5. package/dist/components/PrivilegesSelect.d.ts +19 -0
  6. package/dist/components/PrivilegesSelect.js +471 -0
  7. package/dist/components/PrivilegesSelect.js.map +1 -0
  8. package/dist/exports/client.d.ts +2 -0
  9. package/dist/exports/client.js +3 -0
  10. package/dist/exports/client.js.map +1 -0
  11. package/dist/exports/rsc.d.ts +1 -0
  12. package/dist/exports/rsc.js +3 -0
  13. package/dist/exports/rsc.js.map +1 -0
  14. package/dist/exports/types.d.ts +3 -0
  15. package/dist/exports/types.js +5 -0
  16. package/dist/exports/types.js.map +1 -0
  17. package/dist/exports/utilities.d.ts +6 -0
  18. package/dist/exports/utilities.js +14 -0
  19. package/dist/exports/utilities.js.map +1 -0
  20. package/dist/index.d.ts +19 -0
  21. package/dist/index.js +179 -0
  22. package/dist/index.js.map +1 -0
  23. package/dist/roles-privileges-payload-plugin-1.1.1.tgz +0 -0
  24. package/dist/translations/index.d.ts +7 -0
  25. package/dist/translations/index.js +50 -0
  26. package/dist/translations/index.js.map +1 -0
  27. package/dist/translations/languages/en.d.ts +2 -0
  28. package/dist/translations/languages/en.js +76 -0
  29. package/dist/translations/languages/en.js.map +1 -0
  30. package/dist/translations/languages/fr.d.ts +2 -0
  31. package/dist/translations/languages/fr.js +76 -0
  32. package/dist/translations/languages/fr.js.map +1 -0
  33. package/dist/translations/types.d.ts +67 -0
  34. package/dist/translations/types.js +3 -0
  35. package/dist/translations/types.js.map +1 -0
  36. package/dist/utils/createCustomPrivilege.d.ts +89 -0
  37. package/dist/utils/createCustomPrivilege.js +77 -0
  38. package/dist/utils/createCustomPrivilege.js.map +1 -0
  39. package/dist/utils/generateGlobalPrivileges.d.ts +48 -0
  40. package/dist/utils/generateGlobalPrivileges.js +133 -0
  41. package/dist/utils/generateGlobalPrivileges.js.map +1 -0
  42. package/dist/utils/generatePrivileges.d.ts +51 -0
  43. package/dist/utils/generatePrivileges.js +162 -0
  44. package/dist/utils/generatePrivileges.js.map +1 -0
  45. package/dist/utils/privilegesAccess.d.ts +71 -0
  46. package/dist/utils/privilegesAccess.js +144 -0
  47. package/dist/utils/privilegesAccess.js.map +1 -0
  48. package/dist/utils/seedSuperAdminRole.d.ts +6 -0
  49. package/dist/utils/seedSuperAdminRole.js +60 -0
  50. package/dist/utils/seedSuperAdminRole.js.map +1 -0
  51. package/package.json +4 -1
@@ -0,0 +1,71 @@
1
+ import type { Access } from 'payload';
2
+ /**
3
+ * Check if user has specific privileges based on their roles
4
+ * @param privilegeArrays - Multiple arrays of privileges. Within each array is AND logic, between arrays is OR logic
5
+ * @returns Access function that checks if user has required privileges
6
+ * @example
7
+ * // User must have BOTH pages-create AND pages-read
8
+ * privilegesAccess([['pages-create', 'pages-read']])
9
+ *
10
+ * // User must have EITHER pages-create OR posts-create
11
+ * privilegesAccess([['pages-create'], ['posts-create']])
12
+ *
13
+ * // User must have (pages-create AND pages-read) OR (posts-create AND posts-read)
14
+ * privilegesAccess([['pages-create', 'pages-read'], ['posts-create', 'posts-read']])
15
+ */
16
+ export declare const privilegesAccess: (privilegeArrays: string[][]) => Access;
17
+ /**
18
+ * Check if user has specific privileges based on their roles (synchronous version for field access)
19
+ * @param privilegeArrays - Multiple arrays of privileges. Within each array is AND logic, between arrays is OR logic
20
+ * @param user - The user object from req
21
+ * @returns Boolean indicating if user has required privileges
22
+ * @example
23
+ * // User must have BOTH pages-create AND pages-read
24
+ * checkPrivileges([['pages-create', 'pages-read']], req.user)
25
+ *
26
+ * // User must have EITHER pages-create OR posts-create
27
+ * checkPrivileges([['pages-create'], ['posts-create']], req.user)
28
+ *
29
+ * // User must have (pages-create AND pages-read) OR (posts-create AND posts-read)
30
+ * checkPrivileges([['pages-create', 'pages-read'], ['posts-create', 'posts-read']], req.user)
31
+ */
32
+ export declare const checkPrivileges: (privilegeArrays: string[][], user: any) => boolean;
33
+ /**
34
+ * Check if user has a specific privilege (synchronous version for field access)
35
+ * @param privilegeKey - The privilege key to check
36
+ * @param user - The user object from req
37
+ * @returns Boolean indicating if user has the privilege
38
+ */
39
+ export declare const checkPrivilege: (privilegeKey: string, user: any) => boolean;
40
+ /**
41
+ * Create a simple single-privilege check access function
42
+ * @param privilegeKey - The privilege key to check
43
+ * @returns Access function that checks if user has the required privilege
44
+ */
45
+ export declare const hasPrivilege: (privilegeKey: string) => Access;
46
+ /**
47
+ * Combine privileges access with OR logic (user needs ANY of the privileges)
48
+ * @param privilegeKeys - Array of privilege keys where ANY must match
49
+ * @returns Access function that checks if user has any of the required privileges
50
+ */
51
+ export declare const hasAnyPrivilege: (...privilegeKeys: string[]) => Access;
52
+ /**
53
+ * Combine privileges access with AND logic (user needs ALL of the privileges)
54
+ * @param privilegeKeys - Array of privilege keys where ALL must match
55
+ * @returns Access function that checks if user has all of the required privileges
56
+ */
57
+ export declare const hasAllPrivileges: (...privilegeKeys: string[]) => Access;
58
+ /**
59
+ * Check if user has ANY of the privileges (OR logic, synchronous version for field access)
60
+ * @param user - The user object from req
61
+ * @param privilegeKeys - Array of privilege keys where ANY must match
62
+ * @returns Boolean indicating if user has any of the required privileges
63
+ */
64
+ export declare const checkAnyPrivilege: (user: any, ...privilegeKeys: string[]) => boolean;
65
+ /**
66
+ * Check if user has ALL of the privileges (AND logic, synchronous version for field access)
67
+ * @param user - The user object from req
68
+ * @param privilegeKeys - Array of privilege keys where ALL must match
69
+ * @returns Boolean indicating if user has all of the required privileges
70
+ */
71
+ export declare const checkAllPrivileges: (user: any, ...privilegeKeys: string[]) => boolean;
@@ -0,0 +1,144 @@
1
+ /**
2
+ * Check if user has specific privileges based on their roles
3
+ * @param privilegeArrays - Multiple arrays of privileges. Within each array is AND logic, between arrays is OR logic
4
+ * @returns Access function that checks if user has required privileges
5
+ * @example
6
+ * // User must have BOTH pages-create AND pages-read
7
+ * privilegesAccess([['pages-create', 'pages-read']])
8
+ *
9
+ * // User must have EITHER pages-create OR posts-create
10
+ * privilegesAccess([['pages-create'], ['posts-create']])
11
+ *
12
+ * // User must have (pages-create AND pages-read) OR (posts-create AND posts-read)
13
+ * privilegesAccess([['pages-create', 'pages-read'], ['posts-create', 'posts-read']])
14
+ */ export const privilegesAccess = (privilegeArrays)=>{
15
+ return ({ req: { user } })=>{
16
+ if (!user) {
17
+ return false;
18
+ }
19
+ // Get all privileges from user's roles
20
+ const userPrivileges = new Set();
21
+ if (user.roles && Array.isArray(user.roles)) {
22
+ for (const role of user.roles){
23
+ if (typeof role === 'object' && role !== null && 'privileges' in role) {
24
+ const rolePrivileges = role.privileges;
25
+ if (Array.isArray(rolePrivileges)) {
26
+ for (const priv of rolePrivileges){
27
+ if (typeof priv === 'object' && priv !== null && 'privilege' in priv) {
28
+ userPrivileges.add(priv.privilege);
29
+ }
30
+ }
31
+ }
32
+ }
33
+ }
34
+ }
35
+ // Check if user satisfies any of the privilege arrays (OR logic between arrays)
36
+ return privilegeArrays.some((privilegeArray)=>{
37
+ // Check if user has all privileges in this array (AND logic within array)
38
+ return privilegeArray.every((privilege)=>userPrivileges.has(privilege));
39
+ });
40
+ };
41
+ };
42
+ /**
43
+ * Check if user has specific privileges based on their roles (synchronous version for field access)
44
+ * @param privilegeArrays - Multiple arrays of privileges. Within each array is AND logic, between arrays is OR logic
45
+ * @param user - The user object from req
46
+ * @returns Boolean indicating if user has required privileges
47
+ * @example
48
+ * // User must have BOTH pages-create AND pages-read
49
+ * checkPrivileges([['pages-create', 'pages-read']], req.user)
50
+ *
51
+ * // User must have EITHER pages-create OR posts-create
52
+ * checkPrivileges([['pages-create'], ['posts-create']], req.user)
53
+ *
54
+ * // User must have (pages-create AND pages-read) OR (posts-create AND posts-read)
55
+ * checkPrivileges([['pages-create', 'pages-read'], ['posts-create', 'posts-read']], req.user)
56
+ */ export const checkPrivileges = (privilegeArrays, user)=>{
57
+ if (!user) {
58
+ return false;
59
+ }
60
+ // Get all privileges from user's roles
61
+ const userPrivileges = new Set();
62
+ if (user.roles && Array.isArray(user.roles)) {
63
+ for (const role of user.roles){
64
+ if (typeof role === 'object' && role !== null && 'privileges' in role) {
65
+ const rolePrivileges = role.privileges;
66
+ if (Array.isArray(rolePrivileges)) {
67
+ for (const priv of rolePrivileges){
68
+ if (typeof priv === 'object' && priv !== null && 'privilege' in priv) {
69
+ userPrivileges.add(priv.privilege);
70
+ }
71
+ }
72
+ }
73
+ }
74
+ }
75
+ }
76
+ // Check if user satisfies any of the privilege arrays (OR logic between arrays)
77
+ return privilegeArrays.some((privilegeArray)=>{
78
+ // Check if user has all privileges in this array (AND logic within array)
79
+ return privilegeArray.every((privilege)=>userPrivileges.has(privilege));
80
+ });
81
+ };
82
+ /**
83
+ * Check if user has a specific privilege (synchronous version for field access)
84
+ * @param privilegeKey - The privilege key to check
85
+ * @param user - The user object from req
86
+ * @returns Boolean indicating if user has the privilege
87
+ */ export const checkPrivilege = (privilegeKey, user)=>{
88
+ return checkPrivileges([
89
+ [
90
+ privilegeKey
91
+ ]
92
+ ], user);
93
+ };
94
+ /**
95
+ * Create a simple single-privilege check access function
96
+ * @param privilegeKey - The privilege key to check
97
+ * @returns Access function that checks if user has the required privilege
98
+ */ export const hasPrivilege = (privilegeKey)=>{
99
+ return privilegesAccess([
100
+ [
101
+ privilegeKey
102
+ ]
103
+ ]);
104
+ };
105
+ /**
106
+ * Combine privileges access with OR logic (user needs ANY of the privileges)
107
+ * @param privilegeKeys - Array of privilege keys where ANY must match
108
+ * @returns Access function that checks if user has any of the required privileges
109
+ */ export const hasAnyPrivilege = (...privilegeKeys)=>{
110
+ return privilegesAccess(privilegeKeys.map((key)=>[
111
+ key
112
+ ]));
113
+ };
114
+ /**
115
+ * Combine privileges access with AND logic (user needs ALL of the privileges)
116
+ * @param privilegeKeys - Array of privilege keys where ALL must match
117
+ * @returns Access function that checks if user has all of the required privileges
118
+ */ export const hasAllPrivileges = (...privilegeKeys)=>{
119
+ return privilegesAccess([
120
+ privilegeKeys
121
+ ]);
122
+ };
123
+ /**
124
+ * Check if user has ANY of the privileges (OR logic, synchronous version for field access)
125
+ * @param user - The user object from req
126
+ * @param privilegeKeys - Array of privilege keys where ANY must match
127
+ * @returns Boolean indicating if user has any of the required privileges
128
+ */ export const checkAnyPrivilege = (user, ...privilegeKeys)=>{
129
+ return checkPrivileges(privilegeKeys.map((key)=>[
130
+ key
131
+ ]), user);
132
+ };
133
+ /**
134
+ * Check if user has ALL of the privileges (AND logic, synchronous version for field access)
135
+ * @param user - The user object from req
136
+ * @param privilegeKeys - Array of privilege keys where ALL must match
137
+ * @returns Boolean indicating if user has all of the required privileges
138
+ */ export const checkAllPrivileges = (user, ...privilegeKeys)=>{
139
+ return checkPrivileges([
140
+ privilegeKeys
141
+ ], user);
142
+ };
143
+
144
+ //# sourceMappingURL=privilegesAccess.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/utils/privilegesAccess.ts"],"sourcesContent":["import type { Access, AccessArgs } from 'payload'\n\n/**\n * Check if user has specific privileges based on their roles\n * @param privilegeArrays - Multiple arrays of privileges. Within each array is AND logic, between arrays is OR logic\n * @returns Access function that checks if user has required privileges\n * @example\n * // User must have BOTH pages-create AND pages-read\n * privilegesAccess([['pages-create', 'pages-read']])\n *\n * // User must have EITHER pages-create OR posts-create\n * privilegesAccess([['pages-create'], ['posts-create']])\n *\n * // User must have (pages-create AND pages-read) OR (posts-create AND posts-read)\n * privilegesAccess([['pages-create', 'pages-read'], ['posts-create', 'posts-read']])\n */\nexport const privilegesAccess = (privilegeArrays: string[][]): Access => {\n return ({ req: { user } }: AccessArgs) => {\n if (!user) {\n return false\n }\n\n // Get all privileges from user's roles\n const userPrivileges = new Set<string>()\n\n if (user.roles && Array.isArray(user.roles)) {\n for (const role of user.roles) {\n if (typeof role === 'object' && role !== null && 'privileges' in role) {\n const rolePrivileges = role.privileges\n if (Array.isArray(rolePrivileges)) {\n for (const priv of rolePrivileges) {\n if (typeof priv === 'object' && priv !== null && 'privilege' in priv) {\n userPrivileges.add(priv.privilege as string)\n }\n }\n }\n }\n }\n }\n\n // Check if user satisfies any of the privilege arrays (OR logic between arrays)\n return privilegeArrays.some((privilegeArray) => {\n // Check if user has all privileges in this array (AND logic within array)\n return privilegeArray.every((privilege) => userPrivileges.has(privilege))\n })\n }\n}\n\n/**\n * Check if user has specific privileges based on their roles (synchronous version for field access)\n * @param privilegeArrays - Multiple arrays of privileges. Within each array is AND logic, between arrays is OR logic\n * @param user - The user object from req\n * @returns Boolean indicating if user has required privileges\n * @example\n * // User must have BOTH pages-create AND pages-read\n * checkPrivileges([['pages-create', 'pages-read']], req.user)\n *\n * // User must have EITHER pages-create OR posts-create\n * checkPrivileges([['pages-create'], ['posts-create']], req.user)\n *\n * // User must have (pages-create AND pages-read) OR (posts-create AND posts-read)\n * checkPrivileges([['pages-create', 'pages-read'], ['posts-create', 'posts-read']], req.user)\n */\nexport const checkPrivileges = (privilegeArrays: string[][], user: any): boolean => {\n if (!user) {\n return false\n }\n\n // Get all privileges from user's roles\n const userPrivileges = new Set<string>()\n\n if (user.roles && Array.isArray(user.roles)) {\n for (const role of user.roles) {\n if (typeof role === 'object' && role !== null && 'privileges' in role) {\n const rolePrivileges = role.privileges\n if (Array.isArray(rolePrivileges)) {\n for (const priv of rolePrivileges) {\n if (typeof priv === 'object' && priv !== null && 'privilege' in priv) {\n userPrivileges.add(priv.privilege as string)\n }\n }\n }\n }\n }\n }\n\n // Check if user satisfies any of the privilege arrays (OR logic between arrays)\n return privilegeArrays.some((privilegeArray) => {\n // Check if user has all privileges in this array (AND logic within array)\n return privilegeArray.every((privilege) => userPrivileges.has(privilege))\n })\n}\n\n/**\n * Check if user has a specific privilege (synchronous version for field access)\n * @param privilegeKey - The privilege key to check\n * @param user - The user object from req\n * @returns Boolean indicating if user has the privilege\n */\nexport const checkPrivilege = (privilegeKey: string, user: any): boolean => {\n return checkPrivileges([[privilegeKey]], user)\n}\n\n/**\n * Create a simple single-privilege check access function\n * @param privilegeKey - The privilege key to check\n * @returns Access function that checks if user has the required privilege\n */\nexport const hasPrivilege = (privilegeKey: string): Access => {\n return privilegesAccess([[privilegeKey]])\n}\n\n/**\n * Combine privileges access with OR logic (user needs ANY of the privileges)\n * @param privilegeKeys - Array of privilege keys where ANY must match\n * @returns Access function that checks if user has any of the required privileges\n */\nexport const hasAnyPrivilege = (...privilegeKeys: string[]): Access => {\n return privilegesAccess(privilegeKeys.map((key) => [key]))\n}\n\n/**\n * Combine privileges access with AND logic (user needs ALL of the privileges)\n * @param privilegeKeys - Array of privilege keys where ALL must match\n * @returns Access function that checks if user has all of the required privileges\n */\nexport const hasAllPrivileges = (...privilegeKeys: string[]): Access => {\n return privilegesAccess([privilegeKeys])\n}\n\n/**\n * Check if user has ANY of the privileges (OR logic, synchronous version for field access)\n * @param user - The user object from req\n * @param privilegeKeys - Array of privilege keys where ANY must match\n * @returns Boolean indicating if user has any of the required privileges\n */\nexport const checkAnyPrivilege = (user: any, ...privilegeKeys: string[]): boolean => {\n return checkPrivileges(\n privilegeKeys.map((key) => [key]),\n user,\n )\n}\n\n/**\n * Check if user has ALL of the privileges (AND logic, synchronous version for field access)\n * @param user - The user object from req\n * @param privilegeKeys - Array of privilege keys where ALL must match\n * @returns Boolean indicating if user has all of the required privileges\n */\nexport const checkAllPrivileges = (user: any, ...privilegeKeys: string[]): boolean => {\n return checkPrivileges([privilegeKeys], user)\n}\n"],"names":["privilegesAccess","privilegeArrays","req","user","userPrivileges","Set","roles","Array","isArray","role","rolePrivileges","privileges","priv","add","privilege","some","privilegeArray","every","has","checkPrivileges","checkPrivilege","privilegeKey","hasPrivilege","hasAnyPrivilege","privilegeKeys","map","key","hasAllPrivileges","checkAnyPrivilege","checkAllPrivileges"],"mappings":"AAEA;;;;;;;;;;;;;CAaC,GACD,OAAO,MAAMA,mBAAmB,CAACC;IAC/B,OAAO,CAAC,EAAEC,KAAK,EAAEC,IAAI,EAAE,EAAc;QACnC,IAAI,CAACA,MAAM;YACT,OAAO;QACT;QAEA,uCAAuC;QACvC,MAAMC,iBAAiB,IAAIC;QAE3B,IAAIF,KAAKG,KAAK,IAAIC,MAAMC,OAAO,CAACL,KAAKG,KAAK,GAAG;YAC3C,KAAK,MAAMG,QAAQN,KAAKG,KAAK,CAAE;gBAC7B,IAAI,OAAOG,SAAS,YAAYA,SAAS,QAAQ,gBAAgBA,MAAM;oBACrE,MAAMC,iBAAiBD,KAAKE,UAAU;oBACtC,IAAIJ,MAAMC,OAAO,CAACE,iBAAiB;wBACjC,KAAK,MAAME,QAAQF,eAAgB;4BACjC,IAAI,OAAOE,SAAS,YAAYA,SAAS,QAAQ,eAAeA,MAAM;gCACpER,eAAeS,GAAG,CAACD,KAAKE,SAAS;4BACnC;wBACF;oBACF;gBACF;YACF;QACF;QAEA,gFAAgF;QAChF,OAAOb,gBAAgBc,IAAI,CAAC,CAACC;YAC3B,0EAA0E;YAC1E,OAAOA,eAAeC,KAAK,CAAC,CAACH,YAAcV,eAAec,GAAG,CAACJ;QAChE;IACF;AACF,EAAC;AAED;;;;;;;;;;;;;;CAcC,GACD,OAAO,MAAMK,kBAAkB,CAAClB,iBAA6BE;IAC3D,IAAI,CAACA,MAAM;QACT,OAAO;IACT;IAEA,uCAAuC;IACvC,MAAMC,iBAAiB,IAAIC;IAE3B,IAAIF,KAAKG,KAAK,IAAIC,MAAMC,OAAO,CAACL,KAAKG,KAAK,GAAG;QAC3C,KAAK,MAAMG,QAAQN,KAAKG,KAAK,CAAE;YAC7B,IAAI,OAAOG,SAAS,YAAYA,SAAS,QAAQ,gBAAgBA,MAAM;gBACrE,MAAMC,iBAAiBD,KAAKE,UAAU;gBACtC,IAAIJ,MAAMC,OAAO,CAACE,iBAAiB;oBACjC,KAAK,MAAME,QAAQF,eAAgB;wBACjC,IAAI,OAAOE,SAAS,YAAYA,SAAS,QAAQ,eAAeA,MAAM;4BACpER,eAAeS,GAAG,CAACD,KAAKE,SAAS;wBACnC;oBACF;gBACF;YACF;QACF;IACF;IAEA,gFAAgF;IAChF,OAAOb,gBAAgBc,IAAI,CAAC,CAACC;QAC3B,0EAA0E;QAC1E,OAAOA,eAAeC,KAAK,CAAC,CAACH,YAAcV,eAAec,GAAG,CAACJ;IAChE;AACF,EAAC;AAED;;;;;CAKC,GACD,OAAO,MAAMM,iBAAiB,CAACC,cAAsBlB;IACnD,OAAOgB,gBAAgB;QAAC;YAACE;SAAa;KAAC,EAAElB;AAC3C,EAAC;AAED;;;;CAIC,GACD,OAAO,MAAMmB,eAAe,CAACD;IAC3B,OAAOrB,iBAAiB;QAAC;YAACqB;SAAa;KAAC;AAC1C,EAAC;AAED;;;;CAIC,GACD,OAAO,MAAME,kBAAkB,CAAC,GAAGC;IACjC,OAAOxB,iBAAiBwB,cAAcC,GAAG,CAAC,CAACC,MAAQ;YAACA;SAAI;AAC1D,EAAC;AAED;;;;CAIC,GACD,OAAO,MAAMC,mBAAmB,CAAC,GAAGH;IAClC,OAAOxB,iBAAiB;QAACwB;KAAc;AACzC,EAAC;AAED;;;;;CAKC,GACD,OAAO,MAAMI,oBAAoB,CAACzB,MAAW,GAAGqB;IAC9C,OAAOL,gBACLK,cAAcC,GAAG,CAAC,CAACC,MAAQ;YAACA;SAAI,GAChCvB;AAEJ,EAAC;AAED;;;;;CAKC,GACD,OAAO,MAAM0B,qBAAqB,CAAC1B,MAAW,GAAGqB;IAC/C,OAAOL,gBAAgB;QAACK;KAAc,EAAErB;AAC1C,EAAC"}
@@ -0,0 +1,6 @@
1
+ import type { Payload } from 'payload';
2
+ /**
3
+ * Seeds or updates the Super Admin role with all available privileges
4
+ * This ensures the Super Admin role always has access to all privileges in the system
5
+ */
6
+ export declare const seedSuperAdminRole: (payload: Payload) => Promise<void>;
@@ -0,0 +1,60 @@
1
+ import { getAllGlobalPrivileges } from './generateGlobalPrivileges.js';
2
+ import { getAllPrivileges } from './generatePrivileges.js';
3
+ /**
4
+ * Seeds or updates the Super Admin role with all available privileges
5
+ * This ensures the Super Admin role always has access to all privileges in the system
6
+ */ export const seedSuperAdminRole = async (payload)=>{
7
+ try {
8
+ // Get all available privileges from collections and globals
9
+ const collectionPrivileges = getAllPrivileges();
10
+ const globalPrivileges = getAllGlobalPrivileges();
11
+ const privilegesArray = [
12
+ ...collectionPrivileges.map((privilege)=>({
13
+ privilege: privilege.privilegeKey
14
+ })),
15
+ ...globalPrivileges.map((privilege)=>({
16
+ privilege: privilege.privilegeKey
17
+ }))
18
+ ] // Check if Super Admin role exists
19
+ ;
20
+ const existingRole = await payload.find({
21
+ collection: 'roles',
22
+ where: {
23
+ slug: {
24
+ equals: 'super-admin'
25
+ }
26
+ },
27
+ limit: 1
28
+ });
29
+ if (existingRole.docs.length > 0) {
30
+ // Update existing Super Admin role
31
+ await payload.update({
32
+ collection: 'roles',
33
+ id: existingRole.docs[0].id,
34
+ data: {
35
+ title: 'Super Admin',
36
+ slug: 'super-admin',
37
+ privileges: privilegesArray,
38
+ description: 'Super administrator with full system access and all privileges'
39
+ }
40
+ });
41
+ payload.logger.info('✅ Super Admin role updated with all privileges');
42
+ } else {
43
+ // Create new Super Admin role
44
+ await payload.create({
45
+ collection: 'roles',
46
+ data: {
47
+ title: 'Super Admin',
48
+ slug: 'super-admin',
49
+ privileges: privilegesArray,
50
+ description: 'Super administrator with full system access and all privileges'
51
+ }
52
+ });
53
+ payload.logger.info('✅ Super Admin role created with all privileges');
54
+ }
55
+ } catch (error) {
56
+ payload.logger.error('❌ Error seeding Super Admin role:', error);
57
+ }
58
+ };
59
+
60
+ //# sourceMappingURL=seedSuperAdminRole.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/utils/seedSuperAdminRole.ts"],"sourcesContent":["import type { Payload } from 'payload'\nimport { getAllGlobalPrivileges } from './generateGlobalPrivileges.js'\nimport { getAllPrivileges } from './generatePrivileges.js'\n\n/**\n * Seeds or updates the Super Admin role with all available privileges\n * This ensures the Super Admin role always has access to all privileges in the system\n */\nexport const seedSuperAdminRole = async (payload: Payload): Promise<void> => {\n try {\n // Get all available privileges from collections and globals\n const collectionPrivileges = getAllPrivileges()\n const globalPrivileges = getAllGlobalPrivileges()\n\n const privilegesArray = [\n ...collectionPrivileges.map((privilege) => ({\n privilege: privilege.privilegeKey,\n })),\n ...globalPrivileges.map((privilege) => ({\n privilege: privilege.privilegeKey,\n })),\n ] // Check if Super Admin role exists\n const existingRole = await payload.find({\n collection: 'roles',\n where: {\n slug: {\n equals: 'super-admin',\n },\n },\n limit: 1,\n })\n\n if (existingRole.docs.length > 0) {\n // Update existing Super Admin role\n await payload.update({\n collection: 'roles',\n id: existingRole.docs[0].id,\n data: {\n title: 'Super Admin',\n slug: 'super-admin',\n privileges: privilegesArray,\n description: 'Super administrator with full system access and all privileges',\n },\n })\n payload.logger.info('✅ Super Admin role updated with all privileges')\n } else {\n // Create new Super Admin role\n await payload.create({\n collection: 'roles',\n data: {\n title: 'Super Admin',\n slug: 'super-admin',\n privileges: privilegesArray,\n description: 'Super administrator with full system access and all privileges',\n },\n })\n payload.logger.info('✅ Super Admin role created with all privileges')\n }\n } catch (error) {\n payload.logger.error('❌ Error seeding Super Admin role:', error)\n }\n}\n"],"names":["getAllGlobalPrivileges","getAllPrivileges","seedSuperAdminRole","payload","collectionPrivileges","globalPrivileges","privilegesArray","map","privilege","privilegeKey","existingRole","find","collection","where","slug","equals","limit","docs","length","update","id","data","title","privileges","description","logger","info","create","error"],"mappings":"AACA,SAASA,sBAAsB,QAAQ,gCAA+B;AACtE,SAASC,gBAAgB,QAAQ,0BAAyB;AAE1D;;;CAGC,GACD,OAAO,MAAMC,qBAAqB,OAAOC;IACvC,IAAI;QACF,4DAA4D;QAC5D,MAAMC,uBAAuBH;QAC7B,MAAMI,mBAAmBL;QAEzB,MAAMM,kBAAkB;eACnBF,qBAAqBG,GAAG,CAAC,CAACC,YAAe,CAAA;oBAC1CA,WAAWA,UAAUC,YAAY;gBACnC,CAAA;eACGJ,iBAAiBE,GAAG,CAAC,CAACC,YAAe,CAAA;oBACtCA,WAAWA,UAAUC,YAAY;gBACnC,CAAA;SACD,CAAC,mCAAmC;;QACrC,MAAMC,eAAe,MAAMP,QAAQQ,IAAI,CAAC;YACtCC,YAAY;YACZC,OAAO;gBACLC,MAAM;oBACJC,QAAQ;gBACV;YACF;YACAC,OAAO;QACT;QAEA,IAAIN,aAAaO,IAAI,CAACC,MAAM,GAAG,GAAG;YAChC,mCAAmC;YACnC,MAAMf,QAAQgB,MAAM,CAAC;gBACnBP,YAAY;gBACZQ,IAAIV,aAAaO,IAAI,CAAC,EAAE,CAACG,EAAE;gBAC3BC,MAAM;oBACJC,OAAO;oBACPR,MAAM;oBACNS,YAAYjB;oBACZkB,aAAa;gBACf;YACF;YACArB,QAAQsB,MAAM,CAACC,IAAI,CAAC;QACtB,OAAO;YACL,8BAA8B;YAC9B,MAAMvB,QAAQwB,MAAM,CAAC;gBACnBf,YAAY;gBACZS,MAAM;oBACJC,OAAO;oBACPR,MAAM;oBACNS,YAAYjB;oBACZkB,aAAa;gBACf;YACF;YACArB,QAAQsB,MAAM,CAACC,IAAI,CAAC;QACtB;IACF,EAAE,OAAOE,OAAO;QACdzB,QAAQsB,MAAM,CAACG,KAAK,CAAC,qCAAqCA;IAC5D;AACF,EAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "roles-privileges-payload-plugin",
3
- "version": "1.0.2",
3
+ "version": "1.1.1",
4
4
  "description": "Automatic role-based access control (RBAC) plugin for Payload CMS that generates granular CRUD privileges for all collections with beautiful UI and zero configuration",
5
5
  "keywords": [
6
6
  "payload",
@@ -106,6 +106,9 @@
106
106
  "react": "19.2.1",
107
107
  "react-dom": "19.2.1",
108
108
  "rimraf": "3.0.2",
109
+ "semantic-release": "^24.2.0",
110
+ "@semantic-release/changelog": "^6.0.3",
111
+ "@semantic-release/git": "^10.0.1",
109
112
  "sharp": "0.34.2",
110
113
  "sort-package-json": "^2.10.0",
111
114
  "typescript": "5.7.3",