@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
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import type { SelectField } from "payload";
|
|
2
|
+
|
|
3
|
+
/** Builds `{ [locale]: string }` from `getValue`, skipping empty values. */
|
|
4
|
+
export const toLocaleRecord = (
|
|
5
|
+
locales: string[],
|
|
6
|
+
getValue: (locale: string) => string | undefined,
|
|
7
|
+
): Record<string, string> =>
|
|
8
|
+
locales.reduce<Record<string, string>>((acc, locale) => {
|
|
9
|
+
const value = getValue(locale);
|
|
10
|
+
if (value) {
|
|
11
|
+
acc[locale] = value;
|
|
12
|
+
}
|
|
13
|
+
return acc;
|
|
14
|
+
}, {});
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Select placeholder as per-locale strings (not `LabelFunction`) so it survives
|
|
18
|
+
* Next.js server → client serialization; Payload resolves it at render time.
|
|
19
|
+
*/
|
|
20
|
+
export const toSelectPlaceholder = (
|
|
21
|
+
locales: string[],
|
|
22
|
+
getValue: (locale: string) => string | undefined,
|
|
23
|
+
): NonNullable<SelectField["admin"]>["placeholder"] =>
|
|
24
|
+
toLocaleRecord(locales, getValue) as unknown as NonNullable<
|
|
25
|
+
SelectField["admin"]
|
|
26
|
+
>["placeholder"];
|
|
27
|
+
|
|
28
|
+
type TranslationObject = {
|
|
29
|
+
[key: string]: TranslationValue;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
type TranslationValue = TranslationObject | string | undefined;
|
|
33
|
+
|
|
34
|
+
const isPlainObject = (value: TranslationValue): value is TranslationObject =>
|
|
35
|
+
typeof value === "object" && value !== null && !Array.isArray(value);
|
|
36
|
+
|
|
37
|
+
/** Deep-merge translation trees; leaf overrides replace defaults. */
|
|
38
|
+
const mergeTranslationNodes = (
|
|
39
|
+
defaultNode: TranslationValue,
|
|
40
|
+
overrideNode: TranslationValue,
|
|
41
|
+
): TranslationValue => {
|
|
42
|
+
if (isPlainObject(defaultNode) && isPlainObject(overrideNode)) {
|
|
43
|
+
const merged: TranslationObject = { ...defaultNode };
|
|
44
|
+
|
|
45
|
+
for (const key of Object.keys(overrideNode)) {
|
|
46
|
+
merged[key] = mergeTranslationNodes(defaultNode[key], overrideNode[key]);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return merged;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return overrideNode ?? defaultNode;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/** Plugin defaults merged with host `translations` overrides. */
|
|
56
|
+
export const getMergedTranslations = <
|
|
57
|
+
T extends Record<string, TranslationValue>,
|
|
58
|
+
>({
|
|
59
|
+
defaultTranslations,
|
|
60
|
+
translations,
|
|
61
|
+
}: {
|
|
62
|
+
defaultTranslations: T;
|
|
63
|
+
translations: Partial<T>;
|
|
64
|
+
}): T => mergeTranslationNodes(defaultTranslations, translations) as T;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Picks a nested branch from each locale, e.g. path `"collections.roles"`.
|
|
68
|
+
* Use `locales: "all"` or `["en", "vi"]`.
|
|
69
|
+
*/
|
|
70
|
+
export const getAllTranslationsOfSpecificObject = <T = unknown>({
|
|
71
|
+
translations,
|
|
72
|
+
path,
|
|
73
|
+
locales = "all",
|
|
74
|
+
}: {
|
|
75
|
+
translations: Record<string, Record<string, unknown>>;
|
|
76
|
+
path: string;
|
|
77
|
+
locales?: "all" | string[];
|
|
78
|
+
}): Record<string, T> => {
|
|
79
|
+
const segments = path.split(".");
|
|
80
|
+
const localeKeys = locales === "all" ? Object.keys(translations) : locales;
|
|
81
|
+
|
|
82
|
+
return localeKeys.reduce<Record<string, T>>((acc, locale) => {
|
|
83
|
+
const localeData = translations[locale];
|
|
84
|
+
if (!localeData) {
|
|
85
|
+
return acc;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
let current: unknown = localeData;
|
|
89
|
+
for (const segment of segments) {
|
|
90
|
+
if (
|
|
91
|
+
typeof current !== "object" ||
|
|
92
|
+
current === null ||
|
|
93
|
+
!(segment in current)
|
|
94
|
+
) {
|
|
95
|
+
current = undefined;
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
current = (current as Record<string, unknown>)[segment];
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (current !== undefined) {
|
|
102
|
+
acc[locale] = current as T;
|
|
103
|
+
}
|
|
104
|
+
return acc;
|
|
105
|
+
}, {});
|
|
106
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
$rbac-prefix: "rbac";
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// Collections types
|
|
2
|
+
import type * as PermissionActionTypes from "./collections/permission-actions/types.js";
|
|
3
|
+
import type * as PermissionFeatureTypes from "./collections/permission-features/types.js";
|
|
4
|
+
import type * as PermissionTypes from "./collections/permissions/types.js";
|
|
5
|
+
import type * as RoleTypes from "./collections/roles/types.js";
|
|
6
|
+
import type * as RolePermissionTypes from "./collections/roles-permissions/types.js";
|
|
7
|
+
import type * as UsersTypes from "./collections/users/types.js";
|
|
8
|
+
|
|
9
|
+
// Components types
|
|
10
|
+
import type * as RolePermissionMatrixClientTypes from "./components/role-permission-matrix-client/types.js";
|
|
11
|
+
|
|
12
|
+
export * from "./collections/permission-actions/types.js";
|
|
13
|
+
export * from "./collections/permission-features/types.js";
|
|
14
|
+
export * from "./collections/permissions/types.js";
|
|
15
|
+
export * from "./collections/roles/types.js";
|
|
16
|
+
export * from "./collections/roles-permissions/types.js";
|
|
17
|
+
export * from "./collections/users/types.js";
|
|
18
|
+
export * from "./components/role-permission-matrix-client/types.js";
|
|
19
|
+
|
|
20
|
+
export type RBACTranslations = {
|
|
21
|
+
[locale: string]: {
|
|
22
|
+
// Collections types
|
|
23
|
+
collections?: {
|
|
24
|
+
permissionActions?: PermissionActionTypes.PermissionActionsCollectionTranslations[string];
|
|
25
|
+
permissionFeatures?: PermissionFeatureTypes.PermissionFeaturesCollectionTranslations[string];
|
|
26
|
+
permissions?: PermissionTypes.PermissionsCollectionTranslations[string];
|
|
27
|
+
roles?: RoleTypes.RolesCollectionTranslations[string];
|
|
28
|
+
rolesPermissions?: RolePermissionTypes.RolesPermissionsCollectionTranslations[string];
|
|
29
|
+
users?: UsersTypes.UsersModificationTranslations[string];
|
|
30
|
+
};
|
|
31
|
+
// Components types
|
|
32
|
+
components?: {
|
|
33
|
+
rolePermissionMatrix?: RolePermissionMatrixClientTypes.RolePermissionMatrixClientTranslations[string];
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export type PayloadAuthRbacPluginConfig = {
|
|
39
|
+
/**
|
|
40
|
+
* Collection slugs to augment (may include plugin-only collections absent from generated `CollectionSlug`).
|
|
41
|
+
*/
|
|
42
|
+
collections?: Partial<
|
|
43
|
+
Record<
|
|
44
|
+
string,
|
|
45
|
+
| Omit<
|
|
46
|
+
PermissionActionTypes.PermissionActionsCollectionParams,
|
|
47
|
+
"translations"
|
|
48
|
+
>
|
|
49
|
+
| Omit<
|
|
50
|
+
PermissionFeatureTypes.PermissionFeaturesCollectionParams,
|
|
51
|
+
"translations"
|
|
52
|
+
>
|
|
53
|
+
| Omit<PermissionTypes.PermissionsCollectionParams, "translations">
|
|
54
|
+
| Omit<RoleTypes.RolesCollectionParams, "translations">
|
|
55
|
+
| Omit<
|
|
56
|
+
RolePermissionTypes.RolesPermissionsCollectionParams,
|
|
57
|
+
"translations"
|
|
58
|
+
>
|
|
59
|
+
>
|
|
60
|
+
>;
|
|
61
|
+
disabled?: boolean;
|
|
62
|
+
translations?: RBACTranslations;
|
|
63
|
+
autoModifyUsersCollection?: boolean;
|
|
64
|
+
};
|