mirror of
https://github.com/rommapp/romm.git
synced 2026-12-06 15:54:45 +01:00
feat(frontend-v2): permission system (useCan, permissionsStore, role-map)
Centralised permission checks for v2 — closes the constitution's
"never inline-check user.role in features" rule. Three pieces:
- src/stores/permissions.ts: Pinia store holding the user's
effective grants. Today populated by hydrateFromRole() against a
role-map; tomorrow populated by setGrants() from /permissions/me.
Lives in the canonical store directory but is v2-only consumed —
v1 keeps its inline checks until removal.
- src/v2/composables/useCan: ActionKey vocabulary, scope vocabulary
({global,platform,collection,rom}), role-map mirroring backend
guards, and useCan(action, scope?) returning ComputedRef<boolean>.
installPermissionsHydration() watches authStore.user and keeps
the store in sync; AppLayout calls it on mount.
- Migrations: UserMenu.vue's isAdmin computed and SettingsNav's
Server Stats visibility gate now go through useCan("app.admin").
No more `user.role === "admin"` inline checks in v2.
Backend debt remains: ActionKey enum in OpenAPI + /permissions/me
endpoint + permissions:changed socket event. Until those land, the
role-map.ts file must be reviewed against backend role guards by
hand. Drift means the UI will offer actions the server rejects, or
hide actions it would have permitted.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
7 files changed
+263
-2
No files matched your search
@@ -0,0 +1,65 @@
|
||||
// permissions — Pinia store holding the current user's effective grants.
|
||||
//
|
||||
// Today the grants are derived from `user.role` via a hardcoded role-map
|
||||
// in src/v2/composables/useCan/role-map.ts. When the backend ships
|
||||
// `/permissions/me` returning normalised grants, that response replaces
|
||||
// the role-derived hydration here and the role-map disappears.
|
||||
//
|
||||
// v2 features consume this store via `useCan(action, scope?)`. v1 does
|
||||
// not import from here — its existing inline role checks keep working.
|
||||
import { defineStore } from "pinia";
|
||||
import type { Role } from "@/__generated__";
|
||||
import { type ActionKey, type Grant } from "@/v2/composables/useCan/actions";
|
||||
import { actionsForRole } from "@/v2/composables/useCan/role-map";
|
||||
|
||||
interface State {
|
||||
grants: Grant[];
|
||||
hydratedFromRole: Role | null;
|
||||
}
|
||||
|
||||
export default defineStore("permissions", {
|
||||
state: (): State => ({
|
||||
grants: [],
|
||||
hydratedFromRole: null,
|
||||
}),
|
||||
|
||||
actions: {
|
||||
/** Replace the stored grants with the role's blanket global grants.
|
||||
* Temporary; replaced by setGrants once /permissions/me lands. */
|
||||
hydrateFromRole(role: Role | null) {
|
||||
if (!role) {
|
||||
this.grants = [];
|
||||
this.hydratedFromRole = null;
|
||||
return;
|
||||
}
|
||||
const actions = actionsForRole(role);
|
||||
this.grants = actions.map((action) => ({
|
||||
action,
|
||||
scope: { kind: "global" },
|
||||
}));
|
||||
this.hydratedFromRole = role;
|
||||
},
|
||||
|
||||
/** Replace the stored grants with a normalised list from the backend. */
|
||||
setGrants(grants: Grant[]) {
|
||||
this.grants = grants;
|
||||
this.hydratedFromRole = null;
|
||||
},
|
||||
|
||||
/** Add a single grant — useful for tests and per-resource updates. */
|
||||
addGrant(grant: Grant) {
|
||||
this.grants.push(grant);
|
||||
},
|
||||
|
||||
reset() {
|
||||
this.grants = [];
|
||||
this.hydratedFromRole = null;
|
||||
},
|
||||
},
|
||||
|
||||
getters: {
|
||||
actions(state): Set<ActionKey> {
|
||||
return new Set(state.grants.map((g) => g.action));
|
||||
},
|
||||
},
|
||||
});
|
||||
@@ -23,6 +23,7 @@ import { refetchCSRFToken } from "@/services/api";
|
||||
import identityApi from "@/services/api/identity";
|
||||
import storeAuth from "@/stores/auth";
|
||||
import type { Events } from "@/types/emitter";
|
||||
import { useCan } from "@/v2/composables/useCan";
|
||||
|
||||
defineOptions({ inheritAttrs: false });
|
||||
|
||||
@@ -38,7 +39,7 @@ const userInitials = computed(() => {
|
||||
return name.slice(0, 2).toUpperCase();
|
||||
});
|
||||
|
||||
const isAdmin = computed(() => user.value?.role === "admin");
|
||||
const isAdmin = useCan("app.admin");
|
||||
|
||||
function showAbout() {
|
||||
open.value = false;
|
||||
|
||||
@@ -10,12 +10,14 @@ import { computed } from "vue";
|
||||
import { useI18n } from "vue-i18n";
|
||||
import { ROUTES } from "@/plugins/router";
|
||||
import storeAuth from "@/stores/auth";
|
||||
import { useCan } from "@/v2/composables/useCan";
|
||||
|
||||
defineOptions({ inheritAttrs: false });
|
||||
|
||||
const { t } = useI18n();
|
||||
const auth = storeAuth();
|
||||
const { user, scopes } = storeToRefs(auth);
|
||||
const canSeeServerStats = useCan("app.admin");
|
||||
|
||||
type Entry = {
|
||||
icon: string;
|
||||
@@ -65,7 +67,7 @@ const entries = computed<Entry[]>(() => [
|
||||
icon: "mdi-chart-bar",
|
||||
label: t("common.server-stats"),
|
||||
to: { name: ROUTES.SERVER_STATS },
|
||||
visible: user.value?.role === "admin",
|
||||
visible: canSeeServerStats.value,
|
||||
},
|
||||
]);
|
||||
</script>
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
// ActionKey — canonical vocabulary of permission-gated actions in v2.
|
||||
//
|
||||
// This list is authored by hand today. It mirrors the backend's permission
|
||||
// guards by convention. When the backend exposes its action catalog in
|
||||
// OpenAPI (debt — see CLAUDE.md), this file is replaced by a generated
|
||||
// type and the manual review checklist disappears.
|
||||
//
|
||||
// Naming convention: `domain.action`. Domains are nouns (rom, platform,
|
||||
// collection, user, library); actions are verbs (view, create, edit,
|
||||
// delete, upload, scan, …).
|
||||
|
||||
export const ACTIONS = [
|
||||
// ROMs
|
||||
"rom.view",
|
||||
"rom.play",
|
||||
"rom.download",
|
||||
"rom.upload",
|
||||
"rom.edit",
|
||||
"rom.delete",
|
||||
"rom.match",
|
||||
"rom.refresh",
|
||||
"rom.favorite",
|
||||
// Platforms
|
||||
"platform.view",
|
||||
"platform.create",
|
||||
"platform.edit",
|
||||
"platform.delete",
|
||||
// Collections
|
||||
"collection.view",
|
||||
"collection.create",
|
||||
"collection.edit",
|
||||
"collection.delete",
|
||||
// Library
|
||||
"library.scan",
|
||||
// Users
|
||||
"user.view",
|
||||
"user.create",
|
||||
"user.edit",
|
||||
"user.delete",
|
||||
// App-wide — generic "is admin" hatch for surfaces that don't map to a
|
||||
// specific resource action (server stats, About, debug panels). Prefer
|
||||
// a more specific action when one fits.
|
||||
"app.admin",
|
||||
] as const;
|
||||
|
||||
export type ActionKey = (typeof ACTIONS)[number];
|
||||
|
||||
export type PermissionScope =
|
||||
| { kind: "global" }
|
||||
| { kind: "platform"; id: number }
|
||||
| { kind: "collection"; id: number }
|
||||
| { kind: "rom"; id: number };
|
||||
|
||||
export type Grant = {
|
||||
action: ActionKey;
|
||||
scope: PermissionScope;
|
||||
};
|
||||
@@ -0,0 +1,67 @@
|
||||
// useCan — reactive permission check against the v2 permissions store.
|
||||
//
|
||||
// useCan('rom.upload') — global "anywhere" check.
|
||||
// useCan('rom.upload', { kind: 'platform', id: 5 })
|
||||
// — scoped to a single resource.
|
||||
//
|
||||
// Returns ComputedRef<boolean>. False when no user is authenticated. Reads
|
||||
// from `permissionsStore.grants`, which today is derived from the user's
|
||||
// role via a hardcoded role-map. Once the backend ships /permissions/me,
|
||||
// the store hydrates directly from the response and this composable is
|
||||
// unchanged.
|
||||
//
|
||||
// Frontend is a UX hint, never the authority — the backend remains the
|
||||
// source of truth and rejects unauthorised actions regardless of what the
|
||||
// UI showed.
|
||||
import { storeToRefs } from "pinia";
|
||||
import { computed, type ComputedRef, watch } from "vue";
|
||||
import storeAuth from "@/stores/auth";
|
||||
import storePermissions from "@/stores/permissions";
|
||||
import type { ActionKey, Grant, PermissionScope } from "./actions";
|
||||
|
||||
export type { ActionKey, Grant, PermissionScope };
|
||||
|
||||
/** Mount-time helper: keep permissionsStore in sync with the current user's
|
||||
* role until the backend ships /permissions/me. Call once high in the
|
||||
* v2 tree (AppLayout) — safe to call multiple times, the watch is idempotent. */
|
||||
export function installPermissionsHydration() {
|
||||
const auth = storeAuth();
|
||||
const permissions = storePermissions();
|
||||
const { user } = storeToRefs(auth);
|
||||
watch(
|
||||
user,
|
||||
(u) => {
|
||||
permissions.hydrateFromRole(u?.role ?? null);
|
||||
},
|
||||
{ immediate: true },
|
||||
);
|
||||
}
|
||||
|
||||
function scopeMatches(
|
||||
grantScope: PermissionScope,
|
||||
asked: PermissionScope,
|
||||
): boolean {
|
||||
if (grantScope.kind === "global") return true;
|
||||
if (grantScope.kind !== asked.kind) return false;
|
||||
// Both are non-global same kind — the IDs must match.
|
||||
return (grantScope as { id: number }).id === (asked as { id: number }).id;
|
||||
}
|
||||
|
||||
export function useCan(
|
||||
action: ActionKey,
|
||||
scope?: PermissionScope,
|
||||
): ComputedRef<boolean> {
|
||||
const permissions = storePermissions();
|
||||
return computed(() => {
|
||||
const asked: PermissionScope = scope ?? { kind: "global" };
|
||||
for (const grant of permissions.grants) {
|
||||
if (grant.action !== action) continue;
|
||||
if (asked.kind === "global") {
|
||||
// "Can do this anywhere?" — any scope of grant satisfies.
|
||||
return true;
|
||||
}
|
||||
if (scopeMatches(grant.scope, asked)) return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
// role-map — role → set of action keys that the role can perform globally.
|
||||
//
|
||||
// This is a TEMPORARY hardcoded mirror of the backend's role-based guards.
|
||||
// When the backend ships /permissions/me with normalised grants per user,
|
||||
// this file is deleted and the store hydrates directly from the response.
|
||||
//
|
||||
// Review this file against backend guards when changing it. If a backend
|
||||
// guard changes shape (role allowed/forbidden), update here too. Drift
|
||||
// between this map and backend means the UI will offer actions the
|
||||
// server rejects, or hide actions the server would have permitted.
|
||||
import type { Role } from "@/__generated__";
|
||||
import type { ActionKey } from "./actions";
|
||||
|
||||
const ALL_ROLES: Role[] = ["viewer", "editor", "admin"];
|
||||
const EDITOR_UP: Role[] = ["editor", "admin"];
|
||||
const ADMIN_ONLY: Role[] = ["admin"];
|
||||
|
||||
const ACTION_ROLES: Record<ActionKey, Role[]> = {
|
||||
// ROMs — read access for any role; write access from editor up; delete admin only.
|
||||
"rom.view": ALL_ROLES,
|
||||
"rom.play": ALL_ROLES,
|
||||
"rom.download": ALL_ROLES,
|
||||
"rom.favorite": ALL_ROLES,
|
||||
"rom.upload": EDITOR_UP,
|
||||
"rom.edit": EDITOR_UP,
|
||||
"rom.match": EDITOR_UP,
|
||||
"rom.refresh": EDITOR_UP,
|
||||
"rom.delete": ADMIN_ONLY,
|
||||
|
||||
// Platforms
|
||||
"platform.view": ALL_ROLES,
|
||||
"platform.create": ADMIN_ONLY,
|
||||
"platform.edit": EDITOR_UP,
|
||||
"platform.delete": ADMIN_ONLY,
|
||||
|
||||
// Collections — any role can see and curate their own; per-resource
|
||||
// ownership checks happen on the backend. The blanket "create" here is
|
||||
// the global "is allowed to create some collection" question.
|
||||
"collection.view": ALL_ROLES,
|
||||
"collection.create": ALL_ROLES,
|
||||
"collection.edit": ALL_ROLES,
|
||||
"collection.delete": ALL_ROLES,
|
||||
|
||||
// Library
|
||||
"library.scan": EDITOR_UP,
|
||||
|
||||
// Users — admin only for now.
|
||||
"user.view": ADMIN_ONLY,
|
||||
"user.create": ADMIN_ONLY,
|
||||
"user.edit": ADMIN_ONLY,
|
||||
"user.delete": ADMIN_ONLY,
|
||||
|
||||
// Generic "admin can do this" hatch.
|
||||
"app.admin": ADMIN_ONLY,
|
||||
};
|
||||
|
||||
export function actionsForRole(role: Role): ActionKey[] {
|
||||
const out: ActionKey[] = [];
|
||||
for (const [action, roles] of Object.entries(ACTION_ROLES) as [
|
||||
ActionKey,
|
||||
Role[],
|
||||
][]) {
|
||||
if (roles.includes(role)) out.push(action);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
@@ -15,11 +15,14 @@ import AppNav from "@/v2/components/AppShell/AppNav.vue";
|
||||
import BackgroundArt from "@/v2/components/AppShell/BackgroundArt.vue";
|
||||
import GlobalDialogs from "@/v2/components/AppShell/GlobalDialogs.vue";
|
||||
import { BACKGROUND_ART_KEY } from "@/v2/composables/useBackgroundArt";
|
||||
import { installPermissionsHydration } from "@/v2/composables/useCan";
|
||||
import { useGamepad } from "@/v2/composables/useGamepad";
|
||||
import { useGlobalHotkeys } from "@/v2/composables/useGlobalHotkeys";
|
||||
import { useInputModality } from "@/v2/composables/useInputModality";
|
||||
import { installBackMorph } from "@/v2/composables/useViewTransition";
|
||||
|
||||
installPermissionsHydration();
|
||||
|
||||
// Shared reactive background art — views paint covers via the injected setter.
|
||||
const layerA = ref<string | null>(null);
|
||||
const layerB = ref<string | null>(null);
|
||||
|
||||
Reference in new issue
Block a user