This commit is contained in:
@@ -1,14 +1,11 @@
|
||||
export const adminAppPermissionsStorageKey = 'studio-admin-app-permissions';
|
||||
|
||||
export type CurrentAdminLike = {
|
||||
id?: string;
|
||||
email?: string;
|
||||
name?: string;
|
||||
role?: string;
|
||||
appPermissions?: string[];
|
||||
};
|
||||
|
||||
type PermissionMap = Record<string, string[]>;
|
||||
|
||||
export function readCurrentAdmin(): CurrentAdminLike | null {
|
||||
try {
|
||||
const raw = window.localStorage.getItem('studio-admin');
|
||||
@@ -20,44 +17,20 @@ export function readCurrentAdmin(): CurrentAdminLike | null {
|
||||
}
|
||||
}
|
||||
|
||||
export function getAdminPermissionKey(admin: CurrentAdminLike) {
|
||||
return admin.id || admin.email || '';
|
||||
}
|
||||
|
||||
export function isOwnerAdmin(admin?: CurrentAdminLike | null) {
|
||||
return admin?.role === 'OWNER';
|
||||
}
|
||||
|
||||
export function getAdminAppPermissionMap(): PermissionMap {
|
||||
try {
|
||||
const raw = window.localStorage.getItem(adminAppPermissionsStorageKey);
|
||||
if (!raw) return {};
|
||||
const parsed = JSON.parse(raw) as PermissionMap;
|
||||
return parsed && typeof parsed === 'object' ? parsed : {};
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
export function getAdminAppPermissions(admin: CurrentAdminLike) {
|
||||
const key = getAdminPermissionKey(admin);
|
||||
const map = getAdminAppPermissionMap();
|
||||
if (key && map[key]) return map[key];
|
||||
if (admin.id && map[admin.id]) return map[admin.id];
|
||||
if (admin.email && map[admin.email]) return map[admin.email];
|
||||
return [];
|
||||
return Array.isArray(admin.appPermissions) ? admin.appPermissions : [];
|
||||
}
|
||||
|
||||
export function setAdminAppPermissions(admin: CurrentAdminLike, appIds: string[]) {
|
||||
const key = getAdminPermissionKey(admin);
|
||||
if (!key) return;
|
||||
const current = readCurrentAdmin();
|
||||
if (!current || !admin.id || current.id !== admin.id) return;
|
||||
|
||||
const nextMap = getAdminAppPermissionMap();
|
||||
const nextAppIds = [...new Set(appIds.filter(Boolean))];
|
||||
nextMap[key] = nextAppIds;
|
||||
if (admin.id) nextMap[admin.id] = nextAppIds;
|
||||
if (admin.email) nextMap[admin.email] = nextAppIds;
|
||||
window.localStorage.setItem(adminAppPermissionsStorageKey, JSON.stringify(nextMap));
|
||||
window.localStorage.setItem('studio-admin', JSON.stringify({ ...current, appPermissions: nextAppIds }));
|
||||
window.dispatchEvent(new CustomEvent('studio-admin-app-permissions-change'));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user