@m5kdev/commons 0.23.0 → 0.25.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/src/modules/auth/auth.roles.cjs +85 -0
- package/dist/src/modules/auth/auth.roles.cjs.map +1 -0
- package/dist/src/modules/auth/auth.roles.d.cts +39 -0
- package/dist/src/modules/auth/auth.roles.d.mts +39 -0
- package/dist/src/modules/auth/auth.roles.mjs +77 -0
- package/dist/src/modules/auth/auth.roles.mjs.map +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let zod = require("zod");
|
|
3
|
+
const DEFAULT_AUTH_ROLES = {
|
|
4
|
+
user: {
|
|
5
|
+
roles: ["user", "admin"],
|
|
6
|
+
managerRoles: ["admin"],
|
|
7
|
+
assignableRoles: ["user", "admin"],
|
|
8
|
+
defaultRole: "user"
|
|
9
|
+
},
|
|
10
|
+
organization: {
|
|
11
|
+
roles: [
|
|
12
|
+
"member",
|
|
13
|
+
"admin",
|
|
14
|
+
"owner"
|
|
15
|
+
],
|
|
16
|
+
managerRoles: ["admin", "owner"],
|
|
17
|
+
assignableRoles: [
|
|
18
|
+
"member",
|
|
19
|
+
"admin",
|
|
20
|
+
"owner"
|
|
21
|
+
],
|
|
22
|
+
defaultRole: "member"
|
|
23
|
+
},
|
|
24
|
+
team: {
|
|
25
|
+
roles: ["owner"],
|
|
26
|
+
managerRoles: ["owner"],
|
|
27
|
+
assignableRoles: ["owner"],
|
|
28
|
+
defaultRole: "owner"
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
function pickSubset(values, allowed, fallback) {
|
|
32
|
+
const allowedSet = new Set(allowed);
|
|
33
|
+
const picked = values.filter((value) => allowedSet.has(value));
|
|
34
|
+
if (picked.length > 0) return picked;
|
|
35
|
+
const fallbackPicked = fallback.filter((value) => allowedSet.has(value));
|
|
36
|
+
if (fallbackPicked.length > 0) return fallbackPicked;
|
|
37
|
+
return allowed.length > 0 ? [allowed[0]] : [];
|
|
38
|
+
}
|
|
39
|
+
function normalizeRoleScopeConfig(scope, defaults) {
|
|
40
|
+
const roles = scope.roles.length > 0 ? scope.roles : defaults.roles;
|
|
41
|
+
const roleSet = new Set(roles);
|
|
42
|
+
const defaultRole = scope.defaultRole && roleSet.has(scope.defaultRole) ? scope.defaultRole : defaults.defaultRole && roleSet.has(defaults.defaultRole) ? defaults.defaultRole : roles[0];
|
|
43
|
+
return {
|
|
44
|
+
roles,
|
|
45
|
+
managerRoles: pickSubset(scope.managerRoles ?? defaults.managerRoles, roles, defaults.managerRoles),
|
|
46
|
+
assignableRoles: pickSubset(scope.assignableRoles ?? roles, roles, roles),
|
|
47
|
+
defaultRole
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function normalizeAuthRolesConfig(config) {
|
|
51
|
+
if (!config) return DEFAULT_AUTH_ROLES;
|
|
52
|
+
return {
|
|
53
|
+
user: normalizeRoleScopeConfig(config.user, DEFAULT_AUTH_ROLES.user),
|
|
54
|
+
organization: normalizeRoleScopeConfig(config.organization, DEFAULT_AUTH_ROLES.organization),
|
|
55
|
+
team: normalizeRoleScopeConfig(config.team, DEFAULT_AUTH_ROLES.team)
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
function defineAuthRoles(config) {
|
|
59
|
+
return normalizeAuthRolesConfig(config);
|
|
60
|
+
}
|
|
61
|
+
function getRoleKeys(config, scope) {
|
|
62
|
+
return config[scope].roles;
|
|
63
|
+
}
|
|
64
|
+
function isAllowedRole(config, scope, role) {
|
|
65
|
+
return getRoleKeys(config, scope).includes(role);
|
|
66
|
+
}
|
|
67
|
+
function createRoleValueSchema(scope) {
|
|
68
|
+
const allowed = [...scope.roles];
|
|
69
|
+
if (allowed.length === 0) return zod.z.string();
|
|
70
|
+
return zod.z.enum(allowed);
|
|
71
|
+
}
|
|
72
|
+
function getAppRoleTranslationKey(scope, role) {
|
|
73
|
+
return `${scope}.role.${role}`;
|
|
74
|
+
}
|
|
75
|
+
//#endregion
|
|
76
|
+
exports.DEFAULT_AUTH_ROLES = DEFAULT_AUTH_ROLES;
|
|
77
|
+
exports.createRoleValueSchema = createRoleValueSchema;
|
|
78
|
+
exports.defineAuthRoles = defineAuthRoles;
|
|
79
|
+
exports.getAppRoleTranslationKey = getAppRoleTranslationKey;
|
|
80
|
+
exports.getRoleKeys = getRoleKeys;
|
|
81
|
+
exports.isAllowedRole = isAllowedRole;
|
|
82
|
+
exports.normalizeAuthRolesConfig = normalizeAuthRolesConfig;
|
|
83
|
+
exports.normalizeRoleScopeConfig = normalizeRoleScopeConfig;
|
|
84
|
+
|
|
85
|
+
//# sourceMappingURL=auth.roles.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.roles.cjs","names":["z"],"sources":["../../../../src/modules/auth/auth.roles.ts"],"sourcesContent":["import { z } from \"zod\";\r\n\r\nexport type AuthRoleScope = \"user\" | \"organization\" | \"team\";\r\n\r\nexport interface AuthRoleScopeConfig {\r\n roles: readonly string[];\r\n managerRoles?: readonly string[];\r\n assignableRoles?: readonly string[];\r\n defaultRole?: string;\r\n}\r\n\r\nexport interface AuthRolesConfig {\r\n user: AuthRoleScopeConfig;\r\n organization: AuthRoleScopeConfig;\r\n team: AuthRoleScopeConfig;\r\n}\r\n\r\nexport interface NormalizedAuthRoleScopeConfig {\r\n roles: readonly string[];\r\n managerRoles: readonly string[];\r\n assignableRoles: readonly string[];\r\n defaultRole: string;\r\n}\r\n\r\nexport interface NormalizedAuthRolesConfig {\r\n user: NormalizedAuthRoleScopeConfig;\r\n organization: NormalizedAuthRoleScopeConfig;\r\n team: NormalizedAuthRoleScopeConfig;\r\n}\r\n\r\nconst DEFAULT_USER_SCOPE: NormalizedAuthRoleScopeConfig = {\r\n roles: [\"user\", \"admin\"],\r\n managerRoles: [\"admin\"],\r\n assignableRoles: [\"user\", \"admin\"],\r\n defaultRole: \"user\",\r\n};\r\n\r\nconst DEFAULT_ORGANIZATION_SCOPE: NormalizedAuthRoleScopeConfig = {\r\n roles: [\"member\", \"admin\", \"owner\"],\r\n managerRoles: [\"admin\", \"owner\"],\r\n assignableRoles: [\"member\", \"admin\", \"owner\"],\r\n defaultRole: \"member\",\r\n};\r\n\r\nconst DEFAULT_TEAM_SCOPE: NormalizedAuthRoleScopeConfig = {\r\n roles: [\"owner\"],\r\n managerRoles: [\"owner\"],\r\n assignableRoles: [\"owner\"],\r\n defaultRole: \"owner\",\r\n};\r\n\r\nexport const DEFAULT_AUTH_ROLES: NormalizedAuthRolesConfig = {\r\n user: DEFAULT_USER_SCOPE,\r\n organization: DEFAULT_ORGANIZATION_SCOPE,\r\n team: DEFAULT_TEAM_SCOPE,\r\n};\r\n\r\nfunction pickSubset(\r\n values: readonly string[],\r\n allowed: readonly string[],\r\n fallback: readonly string[]\r\n): readonly string[] {\r\n const allowedSet = new Set(allowed);\r\n const picked = values.filter((value) => allowedSet.has(value));\r\n if (picked.length > 0) {\r\n return picked;\r\n }\r\n\r\n const fallbackPicked = fallback.filter((value) => allowedSet.has(value));\r\n if (fallbackPicked.length > 0) {\r\n return fallbackPicked;\r\n }\r\n\r\n return allowed.length > 0 ? [allowed[0]] : [];\r\n}\r\n\r\nexport function normalizeRoleScopeConfig(\r\n scope: AuthRoleScopeConfig,\r\n defaults: NormalizedAuthRoleScopeConfig\r\n): NormalizedAuthRoleScopeConfig {\r\n const roles = scope.roles.length > 0 ? scope.roles : defaults.roles;\r\n const roleSet = new Set(roles);\r\n\r\n const defaultRole =\r\n scope.defaultRole && roleSet.has(scope.defaultRole)\r\n ? scope.defaultRole\r\n : defaults.defaultRole && roleSet.has(defaults.defaultRole)\r\n ? defaults.defaultRole\r\n : roles[0];\r\n\r\n const managerRoles = pickSubset(\r\n scope.managerRoles ?? defaults.managerRoles,\r\n roles,\r\n defaults.managerRoles\r\n );\r\n\r\n const assignableRoles = pickSubset(\r\n scope.assignableRoles ?? roles,\r\n roles,\r\n roles\r\n );\r\n\r\n return {\r\n roles,\r\n managerRoles,\r\n assignableRoles,\r\n defaultRole,\r\n };\r\n}\r\n\r\nexport function normalizeAuthRolesConfig(\r\n config?: AuthRolesConfig | null\r\n): NormalizedAuthRolesConfig {\r\n if (!config) {\r\n return DEFAULT_AUTH_ROLES;\r\n }\r\n\r\n return {\r\n user: normalizeRoleScopeConfig(config.user, DEFAULT_AUTH_ROLES.user),\r\n organization: normalizeRoleScopeConfig(config.organization, DEFAULT_AUTH_ROLES.organization),\r\n team: normalizeRoleScopeConfig(config.team, DEFAULT_AUTH_ROLES.team),\r\n };\r\n}\r\n\r\nexport function defineAuthRoles(config: AuthRolesConfig): NormalizedAuthRolesConfig {\r\n return normalizeAuthRolesConfig(config);\r\n}\r\n\r\nexport function getRoleKeys(\r\n config: NormalizedAuthRolesConfig,\r\n scope: AuthRoleScope\r\n): readonly string[] {\r\n return config[scope].roles;\r\n}\r\n\r\nexport function isAllowedRole(\r\n config: NormalizedAuthRolesConfig,\r\n scope: AuthRoleScope,\r\n role: string\r\n): boolean {\r\n return getRoleKeys(config, scope).includes(role);\r\n}\r\n\r\nexport function createRoleValueSchema(scope: NormalizedAuthRoleScopeConfig) {\r\n const allowed = [...scope.roles];\r\n if (allowed.length === 0) {\r\n return z.string();\r\n }\r\n return z.enum(allowed as [string, ...string[]]);\r\n}\r\n\r\nexport function getAppRoleTranslationKey(scope: AuthRoleScope, role: string): string {\r\n return `${scope}.role.${role}`;\r\n}\r\n"],"mappings":";;AAmDA,MAAa,qBAAgD;CAC3D,MAtBwD;EACxD,OAAO,CAAC,QAAQ,QAAQ;EACxB,cAAc,CAAC,QAAQ;EACvB,iBAAiB,CAAC,QAAQ,QAAQ;EAClC,aAAa;EACd;CAkBC,cAhBgE;EAChE,OAAO;GAAC;GAAU;GAAS;GAAQ;EACnC,cAAc,CAAC,SAAS,QAAQ;EAChC,iBAAiB;GAAC;GAAU;GAAS;GAAQ;EAC7C,aAAa;EACd;CAYC,MAVwD;EACxD,OAAO,CAAC,QAAQ;EAChB,cAAc,CAAC,QAAQ;EACvB,iBAAiB,CAAC,QAAQ;EAC1B,aAAa;EACd;CAMA;AAED,SAAS,WACP,QACA,SACA,UACmB;CACnB,MAAM,aAAa,IAAI,IAAI,QAAQ;CACnC,MAAM,SAAS,OAAO,QAAQ,UAAU,WAAW,IAAI,MAAM,CAAC;AAC9D,KAAI,OAAO,SAAS,EAClB,QAAO;CAGT,MAAM,iBAAiB,SAAS,QAAQ,UAAU,WAAW,IAAI,MAAM,CAAC;AACxE,KAAI,eAAe,SAAS,EAC1B,QAAO;AAGT,QAAO,QAAQ,SAAS,IAAI,CAAC,QAAQ,GAAG,GAAG,EAAE;;AAG/C,SAAgB,yBACd,OACA,UAC+B;CAC/B,MAAM,QAAQ,MAAM,MAAM,SAAS,IAAI,MAAM,QAAQ,SAAS;CAC9D,MAAM,UAAU,IAAI,IAAI,MAAM;CAE9B,MAAM,cACJ,MAAM,eAAe,QAAQ,IAAI,MAAM,YAAY,GAC/C,MAAM,cACN,SAAS,eAAe,QAAQ,IAAI,SAAS,YAAY,GACvD,SAAS,cACT,MAAM;AAcd,QAAO;EACL;EACA,cAdmB,WACnB,MAAM,gBAAgB,SAAS,cAC/B,OACA,SAAS,aACV;EAWC,iBATsB,WACtB,MAAM,mBAAmB,OACzB,OACA,MACD;EAMC;EACD;;AAGH,SAAgB,yBACd,QAC2B;AAC3B,KAAI,CAAC,OACH,QAAO;AAGT,QAAO;EACL,MAAM,yBAAyB,OAAO,MAAM,mBAAmB,KAAK;EACpE,cAAc,yBAAyB,OAAO,cAAc,mBAAmB,aAAa;EAC5F,MAAM,yBAAyB,OAAO,MAAM,mBAAmB,KAAK;EACrE;;AAGH,SAAgB,gBAAgB,QAAoD;AAClF,QAAO,yBAAyB,OAAO;;AAGzC,SAAgB,YACd,QACA,OACmB;AACnB,QAAO,OAAO,OAAO;;AAGvB,SAAgB,cACd,QACA,OACA,MACS;AACT,QAAO,YAAY,QAAQ,MAAM,CAAC,SAAS,KAAK;;AAGlD,SAAgB,sBAAsB,OAAsC;CAC1E,MAAM,UAAU,CAAC,GAAG,MAAM,MAAM;AAChC,KAAI,QAAQ,WAAW,EACrB,QAAOA,IAAAA,EAAE,QAAQ;AAEnB,QAAOA,IAAAA,EAAE,KAAK,QAAiC;;AAGjD,SAAgB,yBAAyB,OAAsB,MAAsB;AACnF,QAAO,GAAG,MAAM,QAAQ"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
//#region src/modules/auth/auth.roles.d.ts
|
|
4
|
+
type AuthRoleScope = "user" | "organization" | "team";
|
|
5
|
+
interface AuthRoleScopeConfig {
|
|
6
|
+
roles: readonly string[];
|
|
7
|
+
managerRoles?: readonly string[];
|
|
8
|
+
assignableRoles?: readonly string[];
|
|
9
|
+
defaultRole?: string;
|
|
10
|
+
}
|
|
11
|
+
interface AuthRolesConfig {
|
|
12
|
+
user: AuthRoleScopeConfig;
|
|
13
|
+
organization: AuthRoleScopeConfig;
|
|
14
|
+
team: AuthRoleScopeConfig;
|
|
15
|
+
}
|
|
16
|
+
interface NormalizedAuthRoleScopeConfig {
|
|
17
|
+
roles: readonly string[];
|
|
18
|
+
managerRoles: readonly string[];
|
|
19
|
+
assignableRoles: readonly string[];
|
|
20
|
+
defaultRole: string;
|
|
21
|
+
}
|
|
22
|
+
interface NormalizedAuthRolesConfig {
|
|
23
|
+
user: NormalizedAuthRoleScopeConfig;
|
|
24
|
+
organization: NormalizedAuthRoleScopeConfig;
|
|
25
|
+
team: NormalizedAuthRoleScopeConfig;
|
|
26
|
+
}
|
|
27
|
+
declare const DEFAULT_AUTH_ROLES: NormalizedAuthRolesConfig;
|
|
28
|
+
declare function normalizeRoleScopeConfig(scope: AuthRoleScopeConfig, defaults: NormalizedAuthRoleScopeConfig): NormalizedAuthRoleScopeConfig;
|
|
29
|
+
declare function normalizeAuthRolesConfig(config?: AuthRolesConfig | null): NormalizedAuthRolesConfig;
|
|
30
|
+
declare function defineAuthRoles(config: AuthRolesConfig): NormalizedAuthRolesConfig;
|
|
31
|
+
declare function getRoleKeys(config: NormalizedAuthRolesConfig, scope: AuthRoleScope): readonly string[];
|
|
32
|
+
declare function isAllowedRole(config: NormalizedAuthRolesConfig, scope: AuthRoleScope, role: string): boolean;
|
|
33
|
+
declare function createRoleValueSchema(scope: NormalizedAuthRoleScopeConfig): z.ZodString | z.ZodEnum<{
|
|
34
|
+
[x: string]: string;
|
|
35
|
+
}>;
|
|
36
|
+
declare function getAppRoleTranslationKey(scope: AuthRoleScope, role: string): string;
|
|
37
|
+
//#endregion
|
|
38
|
+
export { AuthRoleScope, AuthRoleScopeConfig, AuthRolesConfig, DEFAULT_AUTH_ROLES, NormalizedAuthRoleScopeConfig, NormalizedAuthRolesConfig, createRoleValueSchema, defineAuthRoles, getAppRoleTranslationKey, getRoleKeys, isAllowedRole, normalizeAuthRolesConfig, normalizeRoleScopeConfig };
|
|
39
|
+
//# sourceMappingURL=auth.roles.d.cts.map
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
//#region src/modules/auth/auth.roles.d.ts
|
|
4
|
+
type AuthRoleScope = "user" | "organization" | "team";
|
|
5
|
+
interface AuthRoleScopeConfig {
|
|
6
|
+
roles: readonly string[];
|
|
7
|
+
managerRoles?: readonly string[];
|
|
8
|
+
assignableRoles?: readonly string[];
|
|
9
|
+
defaultRole?: string;
|
|
10
|
+
}
|
|
11
|
+
interface AuthRolesConfig {
|
|
12
|
+
user: AuthRoleScopeConfig;
|
|
13
|
+
organization: AuthRoleScopeConfig;
|
|
14
|
+
team: AuthRoleScopeConfig;
|
|
15
|
+
}
|
|
16
|
+
interface NormalizedAuthRoleScopeConfig {
|
|
17
|
+
roles: readonly string[];
|
|
18
|
+
managerRoles: readonly string[];
|
|
19
|
+
assignableRoles: readonly string[];
|
|
20
|
+
defaultRole: string;
|
|
21
|
+
}
|
|
22
|
+
interface NormalizedAuthRolesConfig {
|
|
23
|
+
user: NormalizedAuthRoleScopeConfig;
|
|
24
|
+
organization: NormalizedAuthRoleScopeConfig;
|
|
25
|
+
team: NormalizedAuthRoleScopeConfig;
|
|
26
|
+
}
|
|
27
|
+
declare const DEFAULT_AUTH_ROLES: NormalizedAuthRolesConfig;
|
|
28
|
+
declare function normalizeRoleScopeConfig(scope: AuthRoleScopeConfig, defaults: NormalizedAuthRoleScopeConfig): NormalizedAuthRoleScopeConfig;
|
|
29
|
+
declare function normalizeAuthRolesConfig(config?: AuthRolesConfig | null): NormalizedAuthRolesConfig;
|
|
30
|
+
declare function defineAuthRoles(config: AuthRolesConfig): NormalizedAuthRolesConfig;
|
|
31
|
+
declare function getRoleKeys(config: NormalizedAuthRolesConfig, scope: AuthRoleScope): readonly string[];
|
|
32
|
+
declare function isAllowedRole(config: NormalizedAuthRolesConfig, scope: AuthRoleScope, role: string): boolean;
|
|
33
|
+
declare function createRoleValueSchema(scope: NormalizedAuthRoleScopeConfig): z.ZodString | z.ZodEnum<{
|
|
34
|
+
[x: string]: string;
|
|
35
|
+
}>;
|
|
36
|
+
declare function getAppRoleTranslationKey(scope: AuthRoleScope, role: string): string;
|
|
37
|
+
//#endregion
|
|
38
|
+
export { AuthRoleScope, AuthRoleScopeConfig, AuthRolesConfig, DEFAULT_AUTH_ROLES, NormalizedAuthRoleScopeConfig, NormalizedAuthRolesConfig, createRoleValueSchema, defineAuthRoles, getAppRoleTranslationKey, getRoleKeys, isAllowedRole, normalizeAuthRolesConfig, normalizeRoleScopeConfig };
|
|
39
|
+
//# sourceMappingURL=auth.roles.d.mts.map
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
const DEFAULT_AUTH_ROLES = {
|
|
3
|
+
user: {
|
|
4
|
+
roles: ["user", "admin"],
|
|
5
|
+
managerRoles: ["admin"],
|
|
6
|
+
assignableRoles: ["user", "admin"],
|
|
7
|
+
defaultRole: "user"
|
|
8
|
+
},
|
|
9
|
+
organization: {
|
|
10
|
+
roles: [
|
|
11
|
+
"member",
|
|
12
|
+
"admin",
|
|
13
|
+
"owner"
|
|
14
|
+
],
|
|
15
|
+
managerRoles: ["admin", "owner"],
|
|
16
|
+
assignableRoles: [
|
|
17
|
+
"member",
|
|
18
|
+
"admin",
|
|
19
|
+
"owner"
|
|
20
|
+
],
|
|
21
|
+
defaultRole: "member"
|
|
22
|
+
},
|
|
23
|
+
team: {
|
|
24
|
+
roles: ["owner"],
|
|
25
|
+
managerRoles: ["owner"],
|
|
26
|
+
assignableRoles: ["owner"],
|
|
27
|
+
defaultRole: "owner"
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
function pickSubset(values, allowed, fallback) {
|
|
31
|
+
const allowedSet = new Set(allowed);
|
|
32
|
+
const picked = values.filter((value) => allowedSet.has(value));
|
|
33
|
+
if (picked.length > 0) return picked;
|
|
34
|
+
const fallbackPicked = fallback.filter((value) => allowedSet.has(value));
|
|
35
|
+
if (fallbackPicked.length > 0) return fallbackPicked;
|
|
36
|
+
return allowed.length > 0 ? [allowed[0]] : [];
|
|
37
|
+
}
|
|
38
|
+
function normalizeRoleScopeConfig(scope, defaults) {
|
|
39
|
+
const roles = scope.roles.length > 0 ? scope.roles : defaults.roles;
|
|
40
|
+
const roleSet = new Set(roles);
|
|
41
|
+
const defaultRole = scope.defaultRole && roleSet.has(scope.defaultRole) ? scope.defaultRole : defaults.defaultRole && roleSet.has(defaults.defaultRole) ? defaults.defaultRole : roles[0];
|
|
42
|
+
return {
|
|
43
|
+
roles,
|
|
44
|
+
managerRoles: pickSubset(scope.managerRoles ?? defaults.managerRoles, roles, defaults.managerRoles),
|
|
45
|
+
assignableRoles: pickSubset(scope.assignableRoles ?? roles, roles, roles),
|
|
46
|
+
defaultRole
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
function normalizeAuthRolesConfig(config) {
|
|
50
|
+
if (!config) return DEFAULT_AUTH_ROLES;
|
|
51
|
+
return {
|
|
52
|
+
user: normalizeRoleScopeConfig(config.user, DEFAULT_AUTH_ROLES.user),
|
|
53
|
+
organization: normalizeRoleScopeConfig(config.organization, DEFAULT_AUTH_ROLES.organization),
|
|
54
|
+
team: normalizeRoleScopeConfig(config.team, DEFAULT_AUTH_ROLES.team)
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
function defineAuthRoles(config) {
|
|
58
|
+
return normalizeAuthRolesConfig(config);
|
|
59
|
+
}
|
|
60
|
+
function getRoleKeys(config, scope) {
|
|
61
|
+
return config[scope].roles;
|
|
62
|
+
}
|
|
63
|
+
function isAllowedRole(config, scope, role) {
|
|
64
|
+
return getRoleKeys(config, scope).includes(role);
|
|
65
|
+
}
|
|
66
|
+
function createRoleValueSchema(scope) {
|
|
67
|
+
const allowed = [...scope.roles];
|
|
68
|
+
if (allowed.length === 0) return z.string();
|
|
69
|
+
return z.enum(allowed);
|
|
70
|
+
}
|
|
71
|
+
function getAppRoleTranslationKey(scope, role) {
|
|
72
|
+
return `${scope}.role.${role}`;
|
|
73
|
+
}
|
|
74
|
+
//#endregion
|
|
75
|
+
export { DEFAULT_AUTH_ROLES, createRoleValueSchema, defineAuthRoles, getAppRoleTranslationKey, getRoleKeys, isAllowedRole, normalizeAuthRolesConfig, normalizeRoleScopeConfig };
|
|
76
|
+
|
|
77
|
+
//# sourceMappingURL=auth.roles.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.roles.mjs","names":[],"sources":["../../../../src/modules/auth/auth.roles.ts"],"sourcesContent":["import { z } from \"zod\";\r\n\r\nexport type AuthRoleScope = \"user\" | \"organization\" | \"team\";\r\n\r\nexport interface AuthRoleScopeConfig {\r\n roles: readonly string[];\r\n managerRoles?: readonly string[];\r\n assignableRoles?: readonly string[];\r\n defaultRole?: string;\r\n}\r\n\r\nexport interface AuthRolesConfig {\r\n user: AuthRoleScopeConfig;\r\n organization: AuthRoleScopeConfig;\r\n team: AuthRoleScopeConfig;\r\n}\r\n\r\nexport interface NormalizedAuthRoleScopeConfig {\r\n roles: readonly string[];\r\n managerRoles: readonly string[];\r\n assignableRoles: readonly string[];\r\n defaultRole: string;\r\n}\r\n\r\nexport interface NormalizedAuthRolesConfig {\r\n user: NormalizedAuthRoleScopeConfig;\r\n organization: NormalizedAuthRoleScopeConfig;\r\n team: NormalizedAuthRoleScopeConfig;\r\n}\r\n\r\nconst DEFAULT_USER_SCOPE: NormalizedAuthRoleScopeConfig = {\r\n roles: [\"user\", \"admin\"],\r\n managerRoles: [\"admin\"],\r\n assignableRoles: [\"user\", \"admin\"],\r\n defaultRole: \"user\",\r\n};\r\n\r\nconst DEFAULT_ORGANIZATION_SCOPE: NormalizedAuthRoleScopeConfig = {\r\n roles: [\"member\", \"admin\", \"owner\"],\r\n managerRoles: [\"admin\", \"owner\"],\r\n assignableRoles: [\"member\", \"admin\", \"owner\"],\r\n defaultRole: \"member\",\r\n};\r\n\r\nconst DEFAULT_TEAM_SCOPE: NormalizedAuthRoleScopeConfig = {\r\n roles: [\"owner\"],\r\n managerRoles: [\"owner\"],\r\n assignableRoles: [\"owner\"],\r\n defaultRole: \"owner\",\r\n};\r\n\r\nexport const DEFAULT_AUTH_ROLES: NormalizedAuthRolesConfig = {\r\n user: DEFAULT_USER_SCOPE,\r\n organization: DEFAULT_ORGANIZATION_SCOPE,\r\n team: DEFAULT_TEAM_SCOPE,\r\n};\r\n\r\nfunction pickSubset(\r\n values: readonly string[],\r\n allowed: readonly string[],\r\n fallback: readonly string[]\r\n): readonly string[] {\r\n const allowedSet = new Set(allowed);\r\n const picked = values.filter((value) => allowedSet.has(value));\r\n if (picked.length > 0) {\r\n return picked;\r\n }\r\n\r\n const fallbackPicked = fallback.filter((value) => allowedSet.has(value));\r\n if (fallbackPicked.length > 0) {\r\n return fallbackPicked;\r\n }\r\n\r\n return allowed.length > 0 ? [allowed[0]] : [];\r\n}\r\n\r\nexport function normalizeRoleScopeConfig(\r\n scope: AuthRoleScopeConfig,\r\n defaults: NormalizedAuthRoleScopeConfig\r\n): NormalizedAuthRoleScopeConfig {\r\n const roles = scope.roles.length > 0 ? scope.roles : defaults.roles;\r\n const roleSet = new Set(roles);\r\n\r\n const defaultRole =\r\n scope.defaultRole && roleSet.has(scope.defaultRole)\r\n ? scope.defaultRole\r\n : defaults.defaultRole && roleSet.has(defaults.defaultRole)\r\n ? defaults.defaultRole\r\n : roles[0];\r\n\r\n const managerRoles = pickSubset(\r\n scope.managerRoles ?? defaults.managerRoles,\r\n roles,\r\n defaults.managerRoles\r\n );\r\n\r\n const assignableRoles = pickSubset(\r\n scope.assignableRoles ?? roles,\r\n roles,\r\n roles\r\n );\r\n\r\n return {\r\n roles,\r\n managerRoles,\r\n assignableRoles,\r\n defaultRole,\r\n };\r\n}\r\n\r\nexport function normalizeAuthRolesConfig(\r\n config?: AuthRolesConfig | null\r\n): NormalizedAuthRolesConfig {\r\n if (!config) {\r\n return DEFAULT_AUTH_ROLES;\r\n }\r\n\r\n return {\r\n user: normalizeRoleScopeConfig(config.user, DEFAULT_AUTH_ROLES.user),\r\n organization: normalizeRoleScopeConfig(config.organization, DEFAULT_AUTH_ROLES.organization),\r\n team: normalizeRoleScopeConfig(config.team, DEFAULT_AUTH_ROLES.team),\r\n };\r\n}\r\n\r\nexport function defineAuthRoles(config: AuthRolesConfig): NormalizedAuthRolesConfig {\r\n return normalizeAuthRolesConfig(config);\r\n}\r\n\r\nexport function getRoleKeys(\r\n config: NormalizedAuthRolesConfig,\r\n scope: AuthRoleScope\r\n): readonly string[] {\r\n return config[scope].roles;\r\n}\r\n\r\nexport function isAllowedRole(\r\n config: NormalizedAuthRolesConfig,\r\n scope: AuthRoleScope,\r\n role: string\r\n): boolean {\r\n return getRoleKeys(config, scope).includes(role);\r\n}\r\n\r\nexport function createRoleValueSchema(scope: NormalizedAuthRoleScopeConfig) {\r\n const allowed = [...scope.roles];\r\n if (allowed.length === 0) {\r\n return z.string();\r\n }\r\n return z.enum(allowed as [string, ...string[]]);\r\n}\r\n\r\nexport function getAppRoleTranslationKey(scope: AuthRoleScope, role: string): string {\r\n return `${scope}.role.${role}`;\r\n}\r\n"],"mappings":";AAmDA,MAAa,qBAAgD;CAC3D,MAtBwD;EACxD,OAAO,CAAC,QAAQ,QAAQ;EACxB,cAAc,CAAC,QAAQ;EACvB,iBAAiB,CAAC,QAAQ,QAAQ;EAClC,aAAa;EACd;CAkBC,cAhBgE;EAChE,OAAO;GAAC;GAAU;GAAS;GAAQ;EACnC,cAAc,CAAC,SAAS,QAAQ;EAChC,iBAAiB;GAAC;GAAU;GAAS;GAAQ;EAC7C,aAAa;EACd;CAYC,MAVwD;EACxD,OAAO,CAAC,QAAQ;EAChB,cAAc,CAAC,QAAQ;EACvB,iBAAiB,CAAC,QAAQ;EAC1B,aAAa;EACd;CAMA;AAED,SAAS,WACP,QACA,SACA,UACmB;CACnB,MAAM,aAAa,IAAI,IAAI,QAAQ;CACnC,MAAM,SAAS,OAAO,QAAQ,UAAU,WAAW,IAAI,MAAM,CAAC;AAC9D,KAAI,OAAO,SAAS,EAClB,QAAO;CAGT,MAAM,iBAAiB,SAAS,QAAQ,UAAU,WAAW,IAAI,MAAM,CAAC;AACxE,KAAI,eAAe,SAAS,EAC1B,QAAO;AAGT,QAAO,QAAQ,SAAS,IAAI,CAAC,QAAQ,GAAG,GAAG,EAAE;;AAG/C,SAAgB,yBACd,OACA,UAC+B;CAC/B,MAAM,QAAQ,MAAM,MAAM,SAAS,IAAI,MAAM,QAAQ,SAAS;CAC9D,MAAM,UAAU,IAAI,IAAI,MAAM;CAE9B,MAAM,cACJ,MAAM,eAAe,QAAQ,IAAI,MAAM,YAAY,GAC/C,MAAM,cACN,SAAS,eAAe,QAAQ,IAAI,SAAS,YAAY,GACvD,SAAS,cACT,MAAM;AAcd,QAAO;EACL;EACA,cAdmB,WACnB,MAAM,gBAAgB,SAAS,cAC/B,OACA,SAAS,aACV;EAWC,iBATsB,WACtB,MAAM,mBAAmB,OACzB,OACA,MACD;EAMC;EACD;;AAGH,SAAgB,yBACd,QAC2B;AAC3B,KAAI,CAAC,OACH,QAAO;AAGT,QAAO;EACL,MAAM,yBAAyB,OAAO,MAAM,mBAAmB,KAAK;EACpE,cAAc,yBAAyB,OAAO,cAAc,mBAAmB,aAAa;EAC5F,MAAM,yBAAyB,OAAO,MAAM,mBAAmB,KAAK;EACrE;;AAGH,SAAgB,gBAAgB,QAAoD;AAClF,QAAO,yBAAyB,OAAO;;AAGzC,SAAgB,YACd,QACA,OACmB;AACnB,QAAO,OAAO,OAAO;;AAGvB,SAAgB,cACd,QACA,OACA,MACS;AACT,QAAO,YAAY,QAAQ,MAAM,CAAC,SAAS,KAAK;;AAGlD,SAAgB,sBAAsB,OAAsC;CAC1E,MAAM,UAAU,CAAC,GAAG,MAAM,MAAM;AAChC,KAAI,QAAQ,WAAW,EACrB,QAAO,EAAE,QAAQ;AAEnB,QAAO,EAAE,KAAK,QAAiC;;AAGjD,SAAgB,yBAAyB,OAAsB,MAAsB;AACnF,QAAO,GAAG,MAAM,QAAQ"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@m5kdev/commons",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.25.1",
|
|
4
4
|
"license": "GPL-3.0-only",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"devDependencies": {
|
|
19
19
|
"tsdown": "0.21.7",
|
|
20
20
|
"typescript": "6.0.3",
|
|
21
|
-
"@m5kdev/config": "0.
|
|
21
|
+
"@m5kdev/config": "0.25.1"
|
|
22
22
|
},
|
|
23
23
|
"exports": {
|
|
24
24
|
"./utils/*": {
|