重构Salon后台单管理员与经营驾驶舱
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
import { beforeEach, describe, expect, it } from 'vitest'
|
||||
import {
|
||||
canAccessAdminRoute,
|
||||
clearAdminBootstrapSession,
|
||||
ensureAdminAuthBootstrap,
|
||||
getAdminBootstrapSnapshot,
|
||||
resolveAdminLandingPath
|
||||
} from './admin-auth-bootstrap'
|
||||
|
||||
describe('admin-auth-bootstrap', () => {
|
||||
beforeEach(() => {
|
||||
localStorage.clear()
|
||||
sessionStorage.clear()
|
||||
})
|
||||
|
||||
it('lets a platform administrator enter any admin route without permission data', async () => {
|
||||
localStorage.setItem('adminAccessToken', 'admin-token')
|
||||
localStorage.setItem('adminUserInfo', JSON.stringify({ id: 1, type: 4 }))
|
||||
|
||||
expect(getAdminBootstrapSnapshot().permissions).toEqual([])
|
||||
expect(await ensureAdminAuthBootstrap()).toBe('ready')
|
||||
expect(canAccessAdminRoute('/admin/inventory')).toBe(true)
|
||||
expect(canAccessAdminRoute('/admin-elsewhere')).toBe(false)
|
||||
expect(resolveAdminLandingPath()).toBe('/admin')
|
||||
})
|
||||
|
||||
it('clears a non-admin session instead of allowing backend access', async () => {
|
||||
localStorage.setItem('adminAccessToken', 'shop-token')
|
||||
localStorage.setItem('adminUserInfo', JSON.stringify({ id: 2, type: 2 }))
|
||||
|
||||
expect(await ensureAdminAuthBootstrap()).toBe('invalid')
|
||||
expect(canAccessAdminRoute('/admin')).toBe(false)
|
||||
expect(localStorage.getItem('adminAccessToken')).toBeNull()
|
||||
})
|
||||
|
||||
it('clears the current administrator session on logout', () => {
|
||||
localStorage.setItem('adminAccessToken', 'admin-token')
|
||||
localStorage.setItem('adminUserInfo', JSON.stringify({ id: 1, type: 4 }))
|
||||
|
||||
clearAdminBootstrapSession()
|
||||
|
||||
expect(getAdminBootstrapSnapshot().token).toBeNull()
|
||||
expect(getAdminBootstrapSnapshot().userInfo).toBeNull()
|
||||
})
|
||||
})
|
||||
@@ -2,165 +2,48 @@ import { computed, ref } from 'vue'
|
||||
import {
|
||||
clearAdminSessionStorage,
|
||||
getAdminAccessToken,
|
||||
isBootstrapAdminUser,
|
||||
loadCachedAdminUserInfo,
|
||||
} from '@/utils/admin-session'
|
||||
import { ADMIN_DASHBOARD_PATH, adminPermissionRouteMap } from '@/config/admin-navigation'
|
||||
import { isAllowedForShoplessPlatformAdmin } from '@/utils/platform-admin-permissions'
|
||||
import {
|
||||
clearStoredPermissions,
|
||||
fetchUserPermissionsStrict,
|
||||
getStoredUserPermissions,
|
||||
} from '@/utils/permission'
|
||||
|
||||
|
||||
/**
|
||||
* Purpose: AuthBootstrapState supports admin-auth-bootstrap.ts by holding or executing the auth bootstrap state page or API role.
|
||||
* Usage: import, call, read, or update this type from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
export type AuthBootstrapState = 'pending' | 'ready' | 'invalid'
|
||||
|
||||
/**
|
||||
* Purpose: adminBootstrapState supports admin-auth-bootstrap.ts by holding or executing the admin bootstrap state page or API role.
|
||||
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
const adminBootstrapState = ref<AuthBootstrapState>('pending')
|
||||
let bootstrapPromise: Promise<AuthBootstrapState> | null = null
|
||||
|
||||
|
||||
/**
|
||||
* Purpose: useAdminBootstrapState supports admin-auth-bootstrap.ts by holding or executing the use admin bootstrap state page or API role.
|
||||
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
export const useAdminBootstrapState = () => computed(() => adminBootstrapState.value)
|
||||
|
||||
/**
|
||||
* Purpose: invalidateAdminBootstrap supports admin-auth-bootstrap.ts by holding or executing the invalidate admin bootstrap page or API role.
|
||||
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
export const invalidateAdminBootstrap = () => {
|
||||
adminBootstrapState.value = 'pending'
|
||||
bootstrapPromise = null
|
||||
}
|
||||
|
||||
/**
|
||||
* Purpose: clearAdminBootstrapSession supports admin-auth-bootstrap.ts by holding or executing the clear admin bootstrap session page or API role.
|
||||
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
export const clearAdminBootstrapSession = () => {
|
||||
clearAdminSessionStorage()
|
||||
clearStoredPermissions()
|
||||
adminBootstrapState.value = 'invalid'
|
||||
bootstrapPromise = null
|
||||
}
|
||||
|
||||
/**
|
||||
* Purpose: getAdminBootstrapSnapshot supports admin-auth-bootstrap.ts by holding or executing the get admin bootstrap snapshot page or API role.
|
||||
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
export const getAdminBootstrapSnapshot = () => {
|
||||
const userInfo = loadCachedAdminUserInfo()
|
||||
return {
|
||||
token: getAdminAccessToken(),
|
||||
userInfo,
|
||||
isBootstrapAdmin: isBootstrapAdminUser(userInfo),
|
||||
isPlatformAdmin: Number(userInfo?.type) === 4,
|
||||
permissions: getStoredUserPermissions() || [],
|
||||
isBootstrapAdmin: Number(userInfo?.type) === 4,
|
||||
permissions: [] as string[],
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Purpose: hasAdminPermission supports admin-auth-bootstrap.ts by holding or executing the has admin permission page or API role.
|
||||
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
export const hasAdminPermission = (permissionCode?: string) => {
|
||||
if (!permissionCode) {
|
||||
return true
|
||||
}
|
||||
export const resolveAdminLandingPath = () => '/admin'
|
||||
|
||||
export const canAccessAdminRoute = (path: string) => {
|
||||
const snapshot = getAdminBootstrapSnapshot()
|
||||
if (snapshot.isBootstrapAdmin || (snapshot.isPlatformAdmin && isAllowedForShoplessPlatformAdmin(permissionCode))) {
|
||||
return isAllowedForShoplessPlatformAdmin(permissionCode)
|
||||
}
|
||||
|
||||
return snapshot.permissions.includes(permissionCode)
|
||||
return Boolean(snapshot.token && snapshot.isPlatformAdmin && (path === '/admin' || path.startsWith('/admin/')))
|
||||
}
|
||||
|
||||
/**
|
||||
* Purpose: resolveAdminLandingPath supports admin-auth-bootstrap.ts by holding or executing the resolve admin landing path page or API role.
|
||||
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
export const resolveAdminLandingPath = () => {
|
||||
export const ensureAdminAuthBootstrap = async (): Promise<AuthBootstrapState> => {
|
||||
const snapshot = getAdminBootstrapSnapshot()
|
||||
if (snapshot.userInfo?.shopAuthorizationExpired && snapshot.userInfo?.type !== 4) {
|
||||
return '/admin'
|
||||
}
|
||||
if (snapshot.isBootstrapAdmin) {
|
||||
return ADMIN_DASHBOARD_PATH
|
||||
}
|
||||
|
||||
const matchedRoute = adminPermissionRouteMap.find(item => snapshot.permissions.includes(item.code))
|
||||
return matchedRoute?.path || '/admin'
|
||||
}
|
||||
|
||||
/**
|
||||
* Purpose: canAccessAdminRoute supports admin-auth-bootstrap.ts by holding or executing the can access admin route page or API role.
|
||||
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
export const canAccessAdminRoute = (path: string, permissionCode?: string) => {
|
||||
const snapshot = getAdminBootstrapSnapshot()
|
||||
if (snapshot.userInfo?.shopAuthorizationExpired && snapshot.userInfo?.type !== 4) {
|
||||
return path === '/admin' || path === '/admin/profile'
|
||||
}
|
||||
if (snapshot.isBootstrapAdmin) {
|
||||
return hasAdminPermission(permissionCode)
|
||||
}
|
||||
|
||||
return hasAdminPermission(permissionCode)
|
||||
}
|
||||
|
||||
/**
|
||||
* Purpose: ensureAdminAuthBootstrap supports admin-auth-bootstrap.ts by holding or executing the ensure admin auth bootstrap page or API role.
|
||||
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
export const ensureAdminAuthBootstrap = async (force = false): Promise<AuthBootstrapState> => {
|
||||
const snapshot = getAdminBootstrapSnapshot()
|
||||
if (!snapshot.token || !snapshot.userInfo) {
|
||||
if (!snapshot.token || !snapshot.isPlatformAdmin) {
|
||||
clearAdminBootstrapSession()
|
||||
return 'invalid'
|
||||
}
|
||||
|
||||
if (snapshot.isBootstrapAdmin) {
|
||||
clearStoredPermissions()
|
||||
adminBootstrapState.value = 'ready'
|
||||
bootstrapPromise = null
|
||||
return 'ready'
|
||||
}
|
||||
|
||||
const cachedPermissions = getStoredUserPermissions()
|
||||
if (!force && cachedPermissions) {
|
||||
adminBootstrapState.value = 'ready'
|
||||
return 'ready'
|
||||
}
|
||||
|
||||
if (!force && bootstrapPromise) {
|
||||
return bootstrapPromise
|
||||
}
|
||||
|
||||
adminBootstrapState.value = 'pending'
|
||||
bootstrapPromise = (async () => {
|
||||
try {
|
||||
await fetchUserPermissionsStrict()
|
||||
adminBootstrapState.value = 'ready'
|
||||
return 'ready' as AuthBootstrapState
|
||||
} catch (error) {
|
||||
console.error('Failed to bootstrap admin permissions:', error)
|
||||
clearAdminBootstrapSession()
|
||||
return 'invalid' as AuthBootstrapState
|
||||
} finally {
|
||||
bootstrapPromise = null
|
||||
}
|
||||
})()
|
||||
|
||||
return bootstrapPromise
|
||||
adminBootstrapState.value = 'ready'
|
||||
return 'ready'
|
||||
}
|
||||
|
||||
@@ -1,21 +1,13 @@
|
||||
import type { IdType } from '@/utils/id'
|
||||
|
||||
/**
|
||||
* Purpose: AdminCachedUserInfo supports admin-session.ts by holding or executing the admin cached user info page or API role.
|
||||
* Usage: import, call, read, or update this interface from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
export interface AdminCachedUserInfo {
|
||||
id?: IdType
|
||||
id?: string | number
|
||||
type?: number
|
||||
account?: string
|
||||
nickname?: string
|
||||
avatar?: string
|
||||
shopId?: IdType
|
||||
shopName?: string
|
||||
shopAuthorizationExpireTime?: string
|
||||
shopAuthorizationExpired?: boolean
|
||||
shopIds?: IdType[]
|
||||
shopNames?: string[]
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -51,19 +43,6 @@ export const loadCachedAdminUserInfo = (): AdminCachedUserInfo | null => {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Purpose: isBootstrapAdminUser supports admin-session.ts by holding or executing the is bootstrap admin user page or API role.
|
||||
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
export const isBootstrapAdminUser = (userInfo?: AdminCachedUserInfo | null) =>
|
||||
userInfo?.type === 4 && !userInfo?.shopId
|
||||
|
||||
/**
|
||||
* Purpose: isPlatformOverviewAdminUser supports admin-session.ts by holding or executing the is platform overview admin user page or API role.
|
||||
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
export const isPlatformOverviewAdminUser = isBootstrapAdminUser
|
||||
|
||||
/**
|
||||
* Purpose: clearAdminSessionStorage supports admin-session.ts by holding or executing the clear admin session storage page or API role.
|
||||
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { permissionApi } from '@/api/permission'
|
||||
import { fetchUserPermissionsStrict, setStoredUserPermissions, usePermission } from './permission'
|
||||
|
||||
describe('admin permission compatibility', () => {
|
||||
beforeEach(() => {
|
||||
localStorage.clear()
|
||||
sessionStorage.clear()
|
||||
})
|
||||
|
||||
it('allows platform administrators without loading or storing permission records', async () => {
|
||||
localStorage.setItem('adminUserInfo', JSON.stringify({ id: 1, type: 4 }))
|
||||
const loadPermissions = vi.spyOn(permissionApi, 'getUserPermissions')
|
||||
|
||||
expect(usePermission().hasPermission('any:unconfigured:permission')).toBe(true)
|
||||
expect(await fetchUserPermissionsStrict()).toEqual([])
|
||||
expect(loadPermissions).not.toHaveBeenCalled()
|
||||
expect(localStorage.getItem('userPermissions')).toBe('[]')
|
||||
})
|
||||
|
||||
it('keeps permission checks for non-administrator users', () => {
|
||||
localStorage.setItem('adminUserInfo', JSON.stringify({ id: 2, type: 2 }))
|
||||
setStoredUserPermissions(['shop:view'])
|
||||
|
||||
const permissions = usePermission()
|
||||
expect(permissions.hasPermission('shop:view')).toBe(true)
|
||||
expect(permissions.hasPermission('shop:delete')).toBe(false)
|
||||
})
|
||||
})
|
||||
@@ -1,7 +1,6 @@
|
||||
import { permissionApi } from '@/api/permission'
|
||||
import { computed, ref } from 'vue'
|
||||
import { isBootstrapAdminUser, loadCachedAdminUserInfo } from '@/utils/admin-session'
|
||||
import { isAllowedForShoplessPlatformAdmin } from '@/utils/platform-admin-permissions'
|
||||
import { loadCachedAdminUserInfo } from '@/utils/admin-session'
|
||||
|
||||
/**
|
||||
* Purpose: USER_PERMISSIONS_STORAGE_KEY supports permission.ts by holding or executing the user permissions storage key page or API role.
|
||||
@@ -76,6 +75,9 @@ export const clearStoredPermissions = () => {
|
||||
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
export const fetchUserPermissionsStrict = async () => {
|
||||
if (isPlatformAdminUser()) {
|
||||
return setStoredUserPermissions([])
|
||||
}
|
||||
loadingPermissions.value = true
|
||||
try {
|
||||
const permissions = await permissionApi.getUserPermissions()
|
||||
@@ -95,32 +97,17 @@ export const fetchUserPermissionsStrict = async () => {
|
||||
*/
|
||||
export const usePermission = () => {
|
||||
const hasPermission = (permissionCode: string) => {
|
||||
if (isBootstrapAdminUser(loadCachedAdminUserInfo())) {
|
||||
return isAllowedForShoplessPlatformAdmin(permissionCode)
|
||||
}
|
||||
if (isPlatformAdminUser() && isAllowedForShoplessPlatformAdmin(permissionCode)) {
|
||||
return isAllowedForShoplessPlatformAdmin(permissionCode)
|
||||
}
|
||||
if (isPlatformAdminUser()) return true
|
||||
return userPermissions.value.includes(permissionCode)
|
||||
}
|
||||
|
||||
const hasAnyPermission = (permissionCodes: string[]) => {
|
||||
if (isBootstrapAdminUser(loadCachedAdminUserInfo())) {
|
||||
return permissionCodes.some(isAllowedForShoplessPlatformAdmin)
|
||||
}
|
||||
if (isPlatformAdminUser() && permissionCodes.some(isAllowedForShoplessPlatformAdmin)) {
|
||||
return permissionCodes.some(isAllowedForShoplessPlatformAdmin)
|
||||
}
|
||||
if (isPlatformAdminUser()) return true
|
||||
return permissionCodes.some(code => userPermissions.value.includes(code))
|
||||
}
|
||||
|
||||
const hasAllPermissions = (permissionCodes: string[]) => {
|
||||
if (isBootstrapAdminUser(loadCachedAdminUserInfo())) {
|
||||
return permissionCodes.every(isAllowedForShoplessPlatformAdmin)
|
||||
}
|
||||
if (isPlatformAdminUser() && permissionCodes.every(isAllowedForShoplessPlatformAdmin)) {
|
||||
return permissionCodes.every(isAllowedForShoplessPlatformAdmin)
|
||||
}
|
||||
if (isPlatformAdminUser()) return true
|
||||
return permissionCodes.every(code => userPermissions.value.includes(code))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user