fix(ui): проверять права каждого источника дашборда

This commit is contained in:
Denozordec committed 2026-10-03 19:38:30 +07:00
1 parent 1999e8160e
commit 587dd94f82
7 files changed
+59 -5

No files matched your search

+19 -3
View File
@@ -31,12 +31,21 @@ import type { FiltersRulesetRow, RouteOptimizerSpeedProbe } from "@/lib/route-op
import { updateDashboardSource, type DashboardSource, type DashboardSources } from "@/lib/dashboard-source-state"
import {can,ensureAuthConfig} from "@/lib/auth"
import {dashboardRequestPermission} from "@/lib/dashboard-permissions"
async function dashboardRequest<T>(path:string,work:()=>Promise<T>,method="GET"):Promise<T>{
await ensureAuthConfig();const permission=dashboardRequestPermission(method,path)
if(!can(permission))throw Error("Нет доступа к источнику: "+permission)
const result=await work();if(!can(permission))throw Error("Доступ к источнику отозван: "+permission)
return result
}
const MOCK_DASH_STARS_LS = "mm:dashboard-probe-ids"
const UPTIME_PROBES_CHANGED = "mm:uptime-probes-changed"
function makeApiFetch(backendUrl: string) {
return async function apiFetch<T>(path: string, init?: RequestInit): Promise<T> {
return requestJson<T>(backendUrl, path, init)
return dashboardRequest(path,()=>requestJson<T>(backendUrl,path,init),init?.method??"GET")
}
}
@@ -273,6 +282,13 @@ export function useDashboardLive() {
setLoading(true)
setInternetPathLoading(true)
try {
await ensureAuthConfig();if(!current())return
if(!can("mm:servers:read"))setLiveServers(null)
if(!can("mm:uptime:read"))setLiveProbes(null)
if(!can("mm:bgp:read"))setBgp(null)
if(!can("mm:traffic:read"))setTrafficSeries(null)
if(!can("mm:network:read"))setInternetPath(null)
setOverlayItems(previous=>previous?.filter(item=>can(item.kind==="gre"?"mm:filters:read":item.kind==="wg"?"mm:network:read":"mm:dashboard:read"))??null)
const [overviewRes, serversRes, fr, br, ipRes, greRes, wgRes, trafficRes, vxRes] = await Promise.allSettled([
apiFetch<{ probes: PingProbe[] }>("/api/uptime/overview?range=1h"),
apiFetch<BackendServerRow[]>("/api/servers"),
@@ -280,7 +296,7 @@ export function useDashboardLive() {
apiFetch<Array<{ state?: string; prefixesRx?: number }>>("/api/bgp/sessions"),
apiFetch<{ snapshot: InternetPathSnapshotPayload | null }>("/api/internet-path/latest"),
apiFetch<{ tunnels?: ApiGreTunnelRow[] }>("/api/filters/gre-tunnels"),
listWireGuard(backendUrl),
dashboardRequest("/api/wireguard",()=>listWireGuard(backendUrl)),
apiFetch<{ servers?: TrafficServerRow[] }>("/api/traffic/servers?range=1h"),
apiFetch<{ tunnels?: Array<{ id: string; serverId: string; name: string; status: OverlayItem["status"] }> }>("/api/vxlan"),
])
@@ -458,7 +474,7 @@ export function useDashboardLive() {
const current = () => request === eventsRequest.current
setEventsLoading(true)
try {
const rows = await listEvents(backendUrl, { limit: 6 })
const rows = await dashboardRequest("/api/events",()=>listEvents(backendUrl, { limit: 6 }))
if (!current()) return
setRecentEvents(rows)
recordSource("events", null)