@zealamic/payload-auth-rbac-plugin 1.0.0 → 1.0.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/dist/components/role-permission-matrix-client/default-data.js +2 -2
- package/dist/components/role-permission-matrix-client/default-data.js.map +1 -1
- package/dist/components/role-permission-matrix-client/index.js +2 -2
- package/dist/components/role-permission-matrix-client/index.js.map +1 -1
- package/dist/components/role-permission-matrix-client/matrix.module.scss +1 -4
- package/dist/components/role-permission-matrix-client/types.d.ts +4 -6
- package/dist/components/role-permission-matrix-client/types.js.map +1 -1
- package/docs/TRANSLATIONS.md +12 -5
- package/package.json +4 -27
- package/src/collections/permission-actions/default-data.ts +36 -0
- package/src/collections/permission-actions/index.ts +144 -0
- package/src/collections/permission-actions/types.ts +56 -0
- package/src/collections/permission-features/default-data.ts +30 -0
- package/src/collections/permission-features/index.ts +122 -0
- package/src/collections/permission-features/types.ts +47 -0
- package/src/collections/permissions/default-data.ts +38 -0
- package/src/collections/permissions/index.ts +160 -0
- package/src/collections/permissions/types.ts +57 -0
- package/src/collections/roles/default-data.ts +44 -0
- package/src/collections/roles/hooks/sync-permission-matrix-draft.ts +73 -0
- package/src/collections/roles/index.ts +178 -0
- package/src/collections/roles/types.ts +56 -0
- package/src/collections/roles-permissions/default-data.ts +28 -0
- package/src/collections/roles-permissions/index.ts +107 -0
- package/src/collections/roles-permissions/types.ts +42 -0
- package/src/collections/users/default-data.ts +19 -0
- package/src/collections/users/index.ts +148 -0
- package/src/collections/users/parent-path.ts +310 -0
- package/src/collections/users/types.ts +25 -0
- package/src/components/role-permission-matrix-client/default-data.ts +25 -0
- package/src/components/role-permission-matrix-client/index.tsx +369 -0
- package/src/components/role-permission-matrix-client/matrix.module.scss +66 -0
- package/src/components/role-permission-matrix-client/types.ts +16 -0
- package/src/endpoints/customEndpointHandler.ts +5 -0
- package/src/exports/client.ts +1 -0
- package/src/exports/rsc.ts +0 -0
- package/src/general-types.d.ts +5 -0
- package/src/index.ts +249 -0
- package/src/lib/constants/general.ts +1 -0
- package/src/lib/constants/index.ts +15 -0
- package/src/lib/constants/permission-action.ts +9 -0
- package/src/lib/constants/permission-feature.ts +4 -0
- package/src/lib/constants/permission.ts +4 -0
- package/src/lib/constants/role.ts +10 -0
- package/src/lib/constants/user.ts +1 -0
- package/src/lib/utils/access.ts +611 -0
- package/src/lib/utils/data.ts +7 -0
- package/src/lib/utils/fields.ts +62 -0
- package/src/lib/utils/index.ts +4 -0
- package/src/lib/utils/localization.ts +106 -0
- package/src/styles/variables.scss +1 -0
- package/src/types.ts +64 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import type { Config } from "payload";
|
|
2
|
+
import { permissionActionsDefaultTranslations } from "./collections/permission-actions/default-data.js";
|
|
3
|
+
import { getPermissionActionsCollection } from "./collections/permission-actions/index.js";
|
|
4
|
+
import { permissionFeaturesDefaultTranslations } from "./collections/permission-features/default-data.js";
|
|
5
|
+
import { getPermissionFeaturesCollection } from "./collections/permission-features/index.js";
|
|
6
|
+
import { permissionsDefaultTranslations } from "./collections/permissions/default-data.js";
|
|
7
|
+
import { getPermissionsCollection } from "./collections/permissions/index.js";
|
|
8
|
+
import { rolesDefaultTranslations } from "./collections/roles/default-data.js";
|
|
9
|
+
import { getRolesCollection } from "./collections/roles/index.js";
|
|
10
|
+
import { rolesPermissionsDefaultTranslations } from "./collections/roles-permissions/default-data.js";
|
|
11
|
+
import { getRolesPermissionsCollection } from "./collections/roles-permissions/index.js";
|
|
12
|
+
import { usersDefaultTranslations } from "./collections/users/default-data.js";
|
|
13
|
+
import { modifyUsersCollection } from "./collections/users/index.js";
|
|
14
|
+
import { rolePermissionMatrixClientDefaultTranslations } from "./components/role-permission-matrix-client/default-data.js";
|
|
15
|
+
|
|
16
|
+
import {
|
|
17
|
+
getAllTranslationsOfSpecificObject,
|
|
18
|
+
getMergedTranslations,
|
|
19
|
+
} from "./lib/utils/index.js";
|
|
20
|
+
import type {
|
|
21
|
+
PayloadAuthRbacPluginConfig,
|
|
22
|
+
PermissionActionsCollectionTranslations,
|
|
23
|
+
PermissionFeaturesCollectionTranslations,
|
|
24
|
+
PermissionsCollectionTranslations,
|
|
25
|
+
RolesCollectionTranslations,
|
|
26
|
+
RolesPermissionsCollectionTranslations,
|
|
27
|
+
UsersModificationTranslations,
|
|
28
|
+
} from "./types.js";
|
|
29
|
+
|
|
30
|
+
export * from "./lib/constants/index.js";
|
|
31
|
+
export * from "./lib/utils/index.js";
|
|
32
|
+
|
|
33
|
+
export const payloadAuthRbacPlugin =
|
|
34
|
+
(pluginOptions: PayloadAuthRbacPluginConfig) =>
|
|
35
|
+
(config: Config): Config => {
|
|
36
|
+
if (!config.collections) {
|
|
37
|
+
config.collections = [];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (!pluginOptions.translations) {
|
|
41
|
+
pluginOptions.translations = {};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
config.collections.push(
|
|
45
|
+
getPermissionActionsCollection({
|
|
46
|
+
...pluginOptions.collections?.permissionActions,
|
|
47
|
+
translations: getMergedTranslations({
|
|
48
|
+
defaultTranslations: permissionActionsDefaultTranslations,
|
|
49
|
+
translations:
|
|
50
|
+
getAllTranslationsOfSpecificObject<PermissionActionsCollectionTranslations>(
|
|
51
|
+
{
|
|
52
|
+
translations: pluginOptions.translations,
|
|
53
|
+
path: "collections.permissionActions",
|
|
54
|
+
},
|
|
55
|
+
),
|
|
56
|
+
}),
|
|
57
|
+
}),
|
|
58
|
+
);
|
|
59
|
+
config.collections.push(
|
|
60
|
+
getPermissionFeaturesCollection({
|
|
61
|
+
...pluginOptions.collections?.permissionFeatures,
|
|
62
|
+
translations: getMergedTranslations({
|
|
63
|
+
defaultTranslations: permissionFeaturesDefaultTranslations,
|
|
64
|
+
translations:
|
|
65
|
+
getAllTranslationsOfSpecificObject<PermissionFeaturesCollectionTranslations>(
|
|
66
|
+
{
|
|
67
|
+
translations: pluginOptions.translations,
|
|
68
|
+
path: "collections.permissionFeatures",
|
|
69
|
+
},
|
|
70
|
+
),
|
|
71
|
+
}),
|
|
72
|
+
}),
|
|
73
|
+
);
|
|
74
|
+
config.collections.push(
|
|
75
|
+
getPermissionsCollection({
|
|
76
|
+
...pluginOptions.collections?.permissions,
|
|
77
|
+
translations: getMergedTranslations({
|
|
78
|
+
defaultTranslations: permissionsDefaultTranslations,
|
|
79
|
+
translations:
|
|
80
|
+
getAllTranslationsOfSpecificObject<PermissionsCollectionTranslations>(
|
|
81
|
+
{
|
|
82
|
+
translations: pluginOptions.translations,
|
|
83
|
+
path: "collections.permissions",
|
|
84
|
+
},
|
|
85
|
+
),
|
|
86
|
+
}),
|
|
87
|
+
}),
|
|
88
|
+
);
|
|
89
|
+
config.collections.push(
|
|
90
|
+
getRolesCollection({
|
|
91
|
+
...pluginOptions.collections?.roles,
|
|
92
|
+
translations: getMergedTranslations({
|
|
93
|
+
defaultTranslations: rolesDefaultTranslations,
|
|
94
|
+
translations:
|
|
95
|
+
getAllTranslationsOfSpecificObject<RolesCollectionTranslations>({
|
|
96
|
+
translations: pluginOptions.translations,
|
|
97
|
+
path: "collections.roles",
|
|
98
|
+
}),
|
|
99
|
+
}),
|
|
100
|
+
}),
|
|
101
|
+
);
|
|
102
|
+
config.collections.push(
|
|
103
|
+
getRolesPermissionsCollection({
|
|
104
|
+
...pluginOptions.collections?.rolesPermissions,
|
|
105
|
+
translations: getMergedTranslations({
|
|
106
|
+
defaultTranslations: rolesPermissionsDefaultTranslations,
|
|
107
|
+
translations:
|
|
108
|
+
getAllTranslationsOfSpecificObject<RolesPermissionsCollectionTranslations>(
|
|
109
|
+
{
|
|
110
|
+
translations: pluginOptions.translations,
|
|
111
|
+
path: "collections.rolesPermissions",
|
|
112
|
+
},
|
|
113
|
+
),
|
|
114
|
+
}),
|
|
115
|
+
}),
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
if (pluginOptions.autoModifyUsersCollection !== false) {
|
|
119
|
+
config = modifyUsersCollection({
|
|
120
|
+
translations: getMergedTranslations({
|
|
121
|
+
defaultTranslations: usersDefaultTranslations,
|
|
122
|
+
translations:
|
|
123
|
+
getAllTranslationsOfSpecificObject<UsersModificationTranslations>({
|
|
124
|
+
translations: pluginOptions.translations,
|
|
125
|
+
path: "collections.users",
|
|
126
|
+
}),
|
|
127
|
+
}),
|
|
128
|
+
})(config);
|
|
129
|
+
}
|
|
130
|
+
// if (pluginOptions.collections) {
|
|
131
|
+
// for (const collectionSlug in pluginOptions.collections) {
|
|
132
|
+
// const collection = config.collections.find(
|
|
133
|
+
// (collection) => collection.slug === collectionSlug,
|
|
134
|
+
// );
|
|
135
|
+
|
|
136
|
+
// if (collection) {
|
|
137
|
+
// collection.fields.push({
|
|
138
|
+
// name: "addedByPlugin",
|
|
139
|
+
// type: "text",
|
|
140
|
+
// admin: {
|
|
141
|
+
// position: "sidebar",
|
|
142
|
+
// },
|
|
143
|
+
// });
|
|
144
|
+
// }
|
|
145
|
+
// }
|
|
146
|
+
// }
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* If the plugin is disabled, we still want to keep added collections/fields so the database schema is consistent which is important for migrations.
|
|
150
|
+
* If your plugin heavily modifies the database schema, you may want to remove this property.
|
|
151
|
+
*/
|
|
152
|
+
if (pluginOptions.disabled) {
|
|
153
|
+
return config;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (!config.endpoints) {
|
|
157
|
+
config.endpoints = [];
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (!config.admin) {
|
|
161
|
+
config.admin = {};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (!config.admin.components) {
|
|
165
|
+
config.admin.components = {};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// if (!config.admin.components.beforeDashboard) {
|
|
169
|
+
// config.admin.components.beforeDashboard = []
|
|
170
|
+
// }
|
|
171
|
+
|
|
172
|
+
// config.admin.components.beforeDashboard.push(
|
|
173
|
+
// `payload-auth-rbac-plugin/rsc#BeforeDashboardServer`,
|
|
174
|
+
// )
|
|
175
|
+
|
|
176
|
+
// config.endpoints.push({
|
|
177
|
+
// handler: customEndpointHandler,
|
|
178
|
+
// method: "get",
|
|
179
|
+
// path: "/my-plugin-endpoint",
|
|
180
|
+
// })
|
|
181
|
+
|
|
182
|
+
const incomingOnInit = config.onInit;
|
|
183
|
+
|
|
184
|
+
config.onInit = async (payload) => {
|
|
185
|
+
// Ensure we are executing any existing onInit functions before running our own.
|
|
186
|
+
if (incomingOnInit) {
|
|
187
|
+
await incomingOnInit(payload);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// const { totalDocs } = await payload.count({
|
|
191
|
+
// collection: "plugin-collection",
|
|
192
|
+
// where: {
|
|
193
|
+
// id: {
|
|
194
|
+
// equals: "seeded-by-plugin",
|
|
195
|
+
// },
|
|
196
|
+
// },
|
|
197
|
+
// });
|
|
198
|
+
|
|
199
|
+
// if (totalDocs === 0) {
|
|
200
|
+
// await payload.create({
|
|
201
|
+
// collection: "plugin-collection",
|
|
202
|
+
// data: {
|
|
203
|
+
// id: "seeded-by-plugin",
|
|
204
|
+
// },
|
|
205
|
+
// });
|
|
206
|
+
// }
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
if (!config.i18n) {
|
|
210
|
+
config.i18n = {};
|
|
211
|
+
}
|
|
212
|
+
const existingTranslations =
|
|
213
|
+
(config.i18n?.translations as
|
|
214
|
+
| Record<string, Record<string, unknown>>
|
|
215
|
+
| undefined) || {};
|
|
216
|
+
|
|
217
|
+
const defaultRBACTranslations = {
|
|
218
|
+
en: {
|
|
219
|
+
collections: {
|
|
220
|
+
permissionActions: permissionActionsDefaultTranslations.en,
|
|
221
|
+
permissionFeatures: permissionFeaturesDefaultTranslations.en,
|
|
222
|
+
permissions: permissionsDefaultTranslations.en,
|
|
223
|
+
roles: rolesDefaultTranslations.en,
|
|
224
|
+
rolesPermissions: rolesPermissionsDefaultTranslations.en,
|
|
225
|
+
users: usersDefaultTranslations.en,
|
|
226
|
+
},
|
|
227
|
+
components: {
|
|
228
|
+
rolePermissionMatrix:
|
|
229
|
+
rolePermissionMatrixClientDefaultTranslations.en,
|
|
230
|
+
},
|
|
231
|
+
},
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
const mergedRBACTranslations = getMergedTranslations({
|
|
235
|
+
defaultTranslations: defaultRBACTranslations,
|
|
236
|
+
translations: pluginOptions.translations,
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
const finalTranslations = getMergedTranslations({
|
|
240
|
+
defaultTranslations: existingTranslations as Record<string, any>,
|
|
241
|
+
translations: mergedRBACTranslations,
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
config.i18n = {
|
|
245
|
+
...config.i18n,
|
|
246
|
+
translations: finalTranslations,
|
|
247
|
+
};
|
|
248
|
+
return config;
|
|
249
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const NAMESPACE = "plugin-rbac" as const
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import * as general from "./general.js";
|
|
2
|
+
import * as permission from "./permission.js";
|
|
3
|
+
import * as permissionAction from "./permission-action.js";
|
|
4
|
+
import * as permissionFeature from "./permission-feature.js";
|
|
5
|
+
import * as role from "./role.js";
|
|
6
|
+
import * as user from "./user.js";
|
|
7
|
+
|
|
8
|
+
export const CONSTANTS = {
|
|
9
|
+
GENERAL: general,
|
|
10
|
+
PERMISSION: permission,
|
|
11
|
+
PERMISSION_FEATURE: permissionFeature,
|
|
12
|
+
PERMISSION_ACTION: permissionAction,
|
|
13
|
+
ROLE: role,
|
|
14
|
+
USER: user,
|
|
15
|
+
} as const;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const PARENT_PATH_SEPARATOR = "," as const;
|