forked from wangziqi/gongxue-base
fix: guard admin h5 entry pages
This commit is contained in:
@@ -19,6 +19,10 @@ function loginUrl(redirectPath: string) {
|
||||
return `/pages/student/login/index?redirect=${encodeURIComponent(safeRedirectPath(redirectPath))}`;
|
||||
}
|
||||
|
||||
function forbiddenUrl(reason: string, redirectPath: string) {
|
||||
return `/pages/student/login/index?reason=${encodeURIComponent(reason)}&redirect=${encodeURIComponent(safeRedirectPath(redirectPath))}`;
|
||||
}
|
||||
|
||||
function currentPagePath() {
|
||||
const instance = Taro.getCurrentInstance();
|
||||
const path = instance.router?.path || '';
|
||||
@@ -69,7 +73,21 @@ export async function requirePlatformAdmin(redirectPath: string) {
|
||||
const user = payload.user || payload.item;
|
||||
const roles = user?.roles || [];
|
||||
if (user?.primaryRole === 'platform_admin' || roles.includes('platform_admin')) return payload;
|
||||
throw new Error('当前账号不是平台管理员。');
|
||||
Taro.redirectTo({ url: forbiddenUrl('当前账号不是平台管理员', redirectPath) });
|
||||
return null;
|
||||
}
|
||||
|
||||
export async function requireTenantAdmin(redirectPath: string) {
|
||||
const payload = await requireSignedIn(redirectPath);
|
||||
if (!payload) return null;
|
||||
const user = payload.user || payload.item;
|
||||
const roles = user?.roles || [];
|
||||
const allowedRoles = new Set(['tenant_owner', 'tenant_admin', 'tenant_operator', 'teacher', 'sales', 'agent']);
|
||||
if (user?.primaryRole === 'platform_admin') return payload;
|
||||
if (user?.primaryRole && allowedRoles.has(user.primaryRole)) return payload;
|
||||
if (roles.some(role => allowedRoles.has(role) || role === 'platform_admin')) return payload;
|
||||
Taro.redirectTo({ url: forbiddenUrl('当前账号没有租户后台权限', redirectPath) });
|
||||
return null;
|
||||
}
|
||||
|
||||
export async function guardCurrentRoute() {
|
||||
@@ -82,6 +100,10 @@ export async function guardCurrentRoute() {
|
||||
await requirePlatformAdmin(path);
|
||||
return;
|
||||
}
|
||||
if (appEnv.portal === 'tenant-admin') {
|
||||
await requireTenantAdmin(path);
|
||||
return;
|
||||
}
|
||||
await requireSignedIn(path);
|
||||
} finally {
|
||||
pendingGuardPath = '';
|
||||
|
||||
Reference in New Issue
Block a user