refactor(permissions): move the management access rules into the shared definitions
home.getStats re-implemented the same rule the pages and navigation use, which is exactly the drift that let a stat card link to a page that returned not-found. getAppManagementAccess and getIntegrationManagementAccess now hold the rule, and the nextjs helper is only a session/lookup adapter around them.
This commit is contained in:
5 files changed
+115
-66
No files matched your search
@@ -1,73 +1,30 @@
|
||||
import type { Session } from "@homarr/auth";
|
||||
|
||||
import { api } from "@homarr/api/server";
|
||||
import { getAppManagementAccess, getIntegrationManagementAccess } from "@homarr/definitions";
|
||||
|
||||
/**
|
||||
* Access rules for the management sections, shared by the navigation and the pages themselves so
|
||||
* the two can never drift apart. The rule is always the same: a section is reachable when the user
|
||||
* can do at least one thing in it, and the section only ever lists what the user is allowed to see.
|
||||
* Session-aware wrappers around the shared management access rules in `@homarr/definitions`, so the
|
||||
* navigation and the pages resolve access exactly the same way the API does.
|
||||
*/
|
||||
|
||||
export interface AppsSectionAccess {
|
||||
/** May see and edit every app */
|
||||
canManageAll: boolean;
|
||||
/** May add new apps */
|
||||
canCreate: boolean;
|
||||
/** May delete apps */
|
||||
canDelete: boolean;
|
||||
/** May open /manage/apps at all */
|
||||
canAccess: boolean;
|
||||
}
|
||||
|
||||
export const getAppsSectionAccess = (session: Session | null): AppsSectionAccess => {
|
||||
const permissions = session?.user.permissions ?? [];
|
||||
const canManageAll = permissions.includes("app-modify-all");
|
||||
const canCreate = permissions.includes("app-create");
|
||||
|
||||
return {
|
||||
canManageAll,
|
||||
canCreate,
|
||||
canDelete: permissions.includes("app-full-all"),
|
||||
canAccess: canManageAll || canCreate,
|
||||
};
|
||||
};
|
||||
|
||||
export interface IntegrationsSectionAccess {
|
||||
/** May manage every integration */
|
||||
canManageAll: boolean;
|
||||
/** May add new integrations */
|
||||
canCreate: boolean;
|
||||
/** May manage at least one specific integration through a delegated grant */
|
||||
hasDelegatedAccess: boolean;
|
||||
/** May open /manage/integrations at all */
|
||||
canAccess: boolean;
|
||||
}
|
||||
export const getAppsSectionAccess = (session: Session | null) =>
|
||||
getAppManagementAccess(session?.user.permissions ?? []);
|
||||
|
||||
/**
|
||||
* Pure variant for callers that already fetched the integrations the user can see, so the page and
|
||||
* the navigation never disagree and the list is only queried once per request.
|
||||
* Pure variant for callers that already fetched the integrations the user can see, so the list is
|
||||
* only queried once per request.
|
||||
*/
|
||||
export const getIntegrationsSectionAccess = (
|
||||
session: Session | null,
|
||||
visibleIntegrations: { permissions: { hasFullAccess: boolean } }[],
|
||||
): IntegrationsSectionAccess => {
|
||||
const permissions = session?.user.permissions ?? [];
|
||||
const canManageAll = permissions.includes("integration-full-all");
|
||||
const canCreate = permissions.includes("integration-create");
|
||||
const hasDelegatedAccess =
|
||||
!canManageAll && visibleIntegrations.some((integration) => integration.permissions.hasFullAccess);
|
||||
) =>
|
||||
getIntegrationManagementAccess(
|
||||
session?.user.permissions ?? [],
|
||||
visibleIntegrations.some((integration) => integration.permissions.hasFullAccess),
|
||||
);
|
||||
|
||||
return {
|
||||
canManageAll,
|
||||
canCreate,
|
||||
hasDelegatedAccess,
|
||||
canAccess: canManageAll || canCreate || hasDelegatedAccess,
|
||||
};
|
||||
};
|
||||
|
||||
export const getIntegrationsSectionAccessAsync = async (
|
||||
session: Session | null,
|
||||
): Promise<IntegrationsSectionAccess> => {
|
||||
export const getIntegrationsSectionAccessAsync = async (session: Session | null) => {
|
||||
const permissions = session?.user.permissions ?? [];
|
||||
|
||||
// Only pay for the lookup when the answer is not already known from the global permissions.
|
||||
|
||||
@@ -42,8 +42,9 @@ interface AppsPageProps {
|
||||
export default async function AppsPage(props: AppsPageProps) {
|
||||
const session = await auth();
|
||||
if (!session) redirect("/auth/login");
|
||||
const { canManageAll, canCreate, canDelete, canAccess } = getAppsSectionAccess(session);
|
||||
const { canManageAll, canCreate, canAccess } = getAppsSectionAccess(session);
|
||||
if (!canAccess) notFound();
|
||||
const canDelete = session.user.permissions.includes("app-full-all");
|
||||
|
||||
const searchParams = searchParamsSchema.parse(await props.searchParams);
|
||||
// Without app-modify-all the user may add apps but not browse the ones they cannot manage,
|
||||
|
||||
Reference in new issue
Block a user