This commit is contained in:
2026-08-19 16:45:51 +08:00
parent d1de19dc82
commit 65cd9ecefa
79 changed files with 3397 additions and 38 deletions
+30 -2
View File
@@ -1,10 +1,13 @@
import { createPinia, setActivePinia } from 'pinia'
import { beforeEach, describe, expect, it } from 'vitest'
import { useAccessPreviewStore } from '@/core/access/accessPreview'
import { isLocalFullAccessHost, useAccessPreviewStore } from '@/core/access/accessPreview'
import { useAuthStore } from '@/core/auth/auth'
describe('server access store', () => {
beforeEach(() => setActivePinia(createPinia()))
beforeEach(() => {
window.localStorage.clear()
setActivePinia(createPinia())
})
it('uses only the authenticated permission snapshot', () => {
const auth = useAuthStore()
@@ -24,4 +27,29 @@ describe('server access store', () => {
expect(access.filteredNavigation).toHaveLength(1)
expect(access.firstAccessiblePath).toBe('/dashboard')
})
it('allows every frontend permission only while the localhost control is enabled', () => {
const access = useAccessPreviewStore()
expect(access.localFullAccessAvailable).toBe(true)
expect(access.can('platform:anything:create')).toBe(false)
access.setLocalFullAccessEnabled(true)
expect(access.can('platform:anything:create')).toBe(true)
expect(access.permissionCodes.has('platform:anything:edit')).toBe(true)
expect(access.filteredNavigation.length).toBeGreaterThan(1)
expect(window.localStorage.getItem('baiyue-admin-local-full-access')).toBe('true')
access.setLocalFullAccessEnabled(false)
expect(access.can('platform:anything:create')).toBe(false)
expect(window.localStorage.getItem('baiyue-admin-local-full-access')).toBeNull()
})
it('recognizes only local preview hosts', () => {
expect(isLocalFullAccessHost('localhost')).toBe(true)
expect(isLocalFullAccessHost('127.0.0.1')).toBe(true)
expect(isLocalFullAccessHost('::1')).toBe(true)
expect(isLocalFullAccessHost('admin.example.com')).toBe(false)
})
})
+44 -2
View File
@@ -7,11 +7,38 @@ import {
} from '@/core/navigation/adminNavigation'
import { useAuthStore } from '@/core/auth/auth'
const LOCAL_FULL_ACCESS_STORAGE_KEY = 'baiyue-admin-local-full-access'
class LocalFullAccessPermissionSet extends Set<string> {
override has(_permissionCode: string): boolean {
return true
}
}
export function isLocalFullAccessHost(hostname: string): boolean {
return hostname === 'localhost' || hostname === '127.0.0.1' || hostname === '::1'
}
export const useAccessPreviewStore = defineStore('access-preview', () => {
const auth = useAuthStore()
const initialized = ref(false)
const permissionCodes = computed<ReadonlySet<string>>(() => auth.permissionCodes)
const permissionSignature = computed(() => [...permissionCodes.value].sort().join('|'))
const localFullAccessAvailable = import.meta.env.DEV
&& typeof window !== 'undefined'
&& isLocalFullAccessHost(window.location.hostname)
const localFullAccessEnabled = ref(
localFullAccessAvailable
&& window.localStorage.getItem(LOCAL_FULL_ACCESS_STORAGE_KEY) === 'true',
)
const permissionCodes = computed<ReadonlySet<string>>(() => (
localFullAccessEnabled.value
? new LocalFullAccessPermissionSet(auth.permissionCodes)
: auth.permissionCodes
))
const permissionSignature = computed(() => (
localFullAccessEnabled.value
? '__LOCAL_FULL_ACCESS__'
: [...permissionCodes.value].sort().join('|')
))
const filteredNavigation = computed(() => filterAdminNavigation(adminNavigation, permissionCodes.value))
const firstAccessiblePath = computed(() => getFirstNavigationLeaf(filteredNavigation.value)?.path ?? null)
@@ -23,18 +50,33 @@ export const useAccessPreviewStore = defineStore('access-preview', () => {
return permissionCodes.value.has(permissionCode)
}
function setLocalFullAccessEnabled(enabled: boolean) {
if (!localFullAccessAvailable) return
localFullAccessEnabled.value = enabled
if (enabled) window.localStorage.setItem(LOCAL_FULL_ACCESS_STORAGE_KEY, 'true')
else window.localStorage.removeItem(LOCAL_FULL_ACCESS_STORAGE_KEY)
}
function toggleLocalFullAccess() {
setLocalFullAccessEnabled(!localFullAccessEnabled.value)
}
function reset() {
initialized.value = false
}
return {
initialized,
localFullAccessAvailable,
localFullAccessEnabled,
permissionCodes,
permissionSignature,
filteredNavigation,
firstAccessiblePath,
initializeAfterLogin,
can,
setLocalFullAccessEnabled,
toggleLocalFullAccess,
reset,
}
})