forked from baron/baron-sso
31 lines
692 B
TypeScript
31 lines
692 B
TypeScript
export function normalizeRole(rawRole: unknown): string {
|
|
if (typeof rawRole !== "string") return "";
|
|
const role = rawRole.trim().toLowerCase();
|
|
|
|
switch (role) {
|
|
case "super_admin":
|
|
case "superadmin":
|
|
case "super-admin":
|
|
return "super_admin";
|
|
default:
|
|
return "user";
|
|
}
|
|
}
|
|
|
|
export function resolveProfileRole(
|
|
profile: Record<string, unknown> | undefined,
|
|
) {
|
|
if (!profile) return "";
|
|
const candidates = [
|
|
profile.role,
|
|
profile.grade,
|
|
profile["custom:role"],
|
|
profile["custom:grade"],
|
|
];
|
|
for (const candidate of candidates) {
|
|
const normalized = normalizeRole(candidate);
|
|
if (normalized) return normalized;
|
|
}
|
|
return "";
|
|
}
|