Compare commits

..
Author SHA1 Message Date
ajnart 82e70ae402 fix: restrict apps management access 2026-07-21 10:22:26 +02:00
12 changed files with 111 additions and 23 deletions

No files matched your search

@@ -16,7 +16,10 @@ export default async function AppEditPage(props: AppEditPageProps) {
const params = await props.params; const params = await props.params;
const session = await auth(); const session = await auth();
if (!session?.user.permissions.includes("app-modify-all")) { if (
!session?.user.permissions.includes("board-modify-all") ||
!session?.user.permissions.includes("app-modify-all")
) {
notFound(); notFound();
} }
const app = await api.app.byId({ id: params.id }); const app = await api.app.byId({ id: params.id });
@@ -10,7 +10,7 @@ import { DynamicBreadcrumb } from "~/components/navigation/dynamic-breadcrumb";
export default async function AppNewPage() { export default async function AppNewPage() {
const session = await auth(); const session = await auth();
if (!session?.user.permissions.includes("app-create")) { if (!session?.user.permissions.includes("board-modify-all") || !session?.user.permissions.includes("app-create")) {
notFound(); notFound();
} }
@@ -1,5 +1,5 @@
import { Fragment } from "react"; import { Fragment } from "react";
import { redirect } from "next/navigation"; import { notFound, redirect } from "next/navigation";
import { ActionIcon, ActionIconGroup, Anchor, Avatar, Card, Group, Stack, Text } from "@mantine/core"; import { ActionIcon, ActionIconGroup, Anchor, Avatar, Card, Group, Stack, Text } from "@mantine/core";
import { IconBox, IconPencil } from "@tabler/icons-react"; import { IconBox, IconPencil } from "@tabler/icons-react";
import { z } from "zod/v4"; import { z } from "zod/v4";
@@ -33,6 +33,9 @@ export default async function AppsPage(props: AppsPageProps) {
if (!session) { if (!session) {
redirect("/auth/login"); redirect("/auth/login");
} }
if (!session.user.permissions.includes("board-modify-all")) {
notFound();
}
const searchParams = searchParamsSchema.parse(await props.searchParams); const searchParams = searchParamsSchema.parse(await props.searchParams);
@@ -61,7 +61,7 @@ export default async function ManageLayout({ children }: PropsWithChildren) {
icon: IconBox, icon: IconBox,
href: "/manage/apps", href: "/manage/apps",
label: t("items.apps"), label: t("items.apps"),
hidden: !session, hidden: !session?.user.permissions.includes("board-modify-all"),
iconProps: { iconProps: {
strokeWidth: 2.5, strokeWidth: 2.5,
}, },
+2 -1
View File
@@ -18,7 +18,8 @@ import { AppAccessControl } from "./app/app-access-control";
const defaultIcon = "https://cdn.jsdelivr.net/gh/homarr-labs/dashboard-icons@master/svg/homarr.svg"; const defaultIcon = "https://cdn.jsdelivr.net/gh/homarr-labs/dashboard-icons@master/svg/homarr.svg";
export const appRouter = createTRPCRouter({ export const appRouter = createTRPCRouter({
getPaginated: protectedProcedure getPaginated: permissionRequiredProcedure
.requiresPermission("board-modify-all")
.input(paginatedSchema) .input(paginatedSchema)
.output(z.object({ items: z.array(selectAppSchema), totalCount: z.number() })) .output(z.object({ items: z.array(selectAppSchema), totalCount: z.number() }))
.meta({ .meta({
+1 -1
View File
@@ -101,7 +101,7 @@ export const homeRouter = createTRPCRouter({
}); });
} }
if (ctx.session?.user) { if (ctx.session?.user.permissions.includes("board-modify-all")) {
statistics.push({ statistics.push({
titleKey: "app", titleKey: "app",
subtitleKey: "resources", subtitleKey: "resources",
+27
View File
@@ -76,6 +76,33 @@ describe("all should return all apps", () => {
}); });
}); });
describe("getPaginated should require board modification access", () => {
test("should throw FORBIDDEN without board-modify-all", async () => {
const caller = appRouter.createCaller({
db: createDb(),
deviceType: undefined,
session: createDefaultSession(),
});
await expect(caller.getPaginated({ page: 1, pageSize: 10 })).rejects.toThrow("Permission denied");
});
test("should return apps with board-modify-all", async () => {
const db = createDb();
const caller = appRouter.createCaller({
db,
deviceType: undefined,
session: createDefaultSession(["board-modify-all"]),
});
await db.insert(apps).values({ id: "1", name: "Homarr", iconUrl: "https://homarr.dev/icon.svg" });
await expect(caller.getPaginated({ page: 1, pageSize: 10 })).resolves.toMatchObject({
totalCount: 1,
items: [{ id: "1" }],
});
});
});
describe("byId should return an app by id", () => { describe("byId should return an app by id", () => {
test("should return an app by id when canUserSeeAppAsync returns true", async () => { test("should return an app by id when canUserSeeAppAsync returns true", async () => {
// Arrange // Arrange
@@ -17,6 +17,10 @@ export const appIntegrationBoardMode = {
return groups; return groups;
} }
return groups.concat([appsSearchGroup, integrationsSearchGroup]); return groups.concat(
session.user.permissions.includes("board-modify-all")
? [appsSearchGroup, integrationsSearchGroup]
: [integrationsSearchGroup],
);
}, },
} satisfies SearchMode; } satisfies SearchMode;
@@ -87,7 +87,8 @@ export const globalCommandGroup = createGroup<Command>({
icon: IconBox, icon: IconBox,
name: tOption("newApp.label"), name: tOption("newApp.label"),
useInteraction: interaction.link(() => ({ href: "/manage/apps/new" })), useInteraction: interaction.link(() => ({ href: "/manage/apps/new" })),
hidden: !session?.user.permissions.includes("app-create"), hidden:
!session?.user.permissions.includes("board-modify-all") || !session?.user.permissions.includes("app-create"),
}, },
{ {
commandKey: "newIntegration", commandKey: "newIntegration",
@@ -76,7 +76,7 @@ export const pagesSearchGroup = createGroup<{
icon: IconBox, icon: IconBox,
path: "/manage/apps", path: "/manage/apps",
name: t("manageApp.label"), name: t("manageApp.label"),
hidden: !session, hidden: !session?.user.permissions.includes("board-modify-all"),
}, },
{ {
icon: IconPlug, icon: IconPlug,
+56 -13
View File
@@ -1,4 +1,6 @@
import { Box, Container, Flex, HoverCard, Text, useMantineTheme } from "@mantine/core"; import { useCallback, useState } from "react";
import { Box, Container, Flex, Popover, Text, useMantineTheme } from "@mantine/core";
import { useElementSize } from "@mantine/hooks";
import { useRequiredBoard } from "@homarr/boards/context"; import { useRequiredBoard } from "@homarr/boards/context";
import type { CalendarEvent } from "@homarr/integrations/types"; import type { CalendarEvent } from "@homarr/integrations/types";
@@ -11,9 +13,14 @@ interface CalendarDayProps {
disabled: boolean; disabled: boolean;
rootWidth: number; rootWidth: number;
rootHeight: number; rootHeight: number;
onOpen: (close: () => void) => void;
} }
export const CalendarDay = ({ date, events, disabled, rootHeight, rootWidth }: CalendarDayProps) => { export const CalendarDay = ({ date, events, disabled, rootHeight, rootWidth, onOpen }: CalendarDayProps) => {
const [opened, setOpened] = useState(false);
const [pinned, setPinned] = useState(false);
const { primaryColor } = useMantineTheme();
const { ref, height } = useElementSize();
const board = useRequiredBoard(); const board = useRequiredBoard();
const mantineTheme = useMantineTheme(); const mantineTheme = useMantineTheme();
const actualItemRadius = mantineTheme.radius[board.itemRadius]; const actualItemRadius = mantineTheme.radius[board.itemRadius];
@@ -22,19 +29,39 @@ export const CalendarDay = ({ date, events, disabled, rootHeight, rootWidth }: C
const shouldScaleDown = minAxisSize < 350; const shouldScaleDown = minAxisSize < 350;
const isSmall = rootHeight < 256; const isSmall = rootHeight < 256;
const isTooSmallForIndicators = height < 30;
const handleMouseEnter = useCallback(() => {
if (disabled) return;
onOpen(() => {
setOpened(false);
setPinned(false);
});
setOpened(true);
}, [disabled, onOpen]);
const handleMouseLeave = useCallback(() => {
if (!pinned) setOpened(false);
}, [pinned]);
return ( return (
<HoverCard <Popover
position="bottom" position="bottom"
withArrow withArrow
withinPortal withinPortal
radius="lg" radius="lg"
shadow="sm" shadow="sm"
transitionProps={{ transition: "pop" }} transitionProps={{
openDelay={350} transition: "pop",
closeDelay={400} }}
onChange={(value) => {
setOpened(value);
if (!value) setPinned(false);
}}
opened={opened}
disabled={disabled} disabled={disabled}
> >
<HoverCard.Target> <Popover.Target>
<Container <Container
h="100%" h="100%"
w="100%" w="100%"
@@ -42,23 +69,38 @@ export const CalendarDay = ({ date, events, disabled, rootHeight, rootWidth }: C
pt={isSmall ? 0 : 10} pt={isSmall ? 0 : 10}
pb={isSmall ? 0 : 10} pb={isSmall ? 0 : 10}
m={0} m={0}
pos="relative" ref={ref}
bd={`2px solid ${opened && !disabled ? primaryColor : "transparent"}`}
style={{ style={{
alignContent: "center", alignContent: "center",
borderRadius: actualItemRadius, borderRadius: actualItemRadius,
cursor: disabled ? "default" : "pointer", cursor: disabled ? "default" : "pointer",
}} }}
onClick={() => {
if (!disabled) setPinned((prev) => !prev);
}}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
> >
<Text ta={"center"} size={shouldScaleDown ? "xs" : "md"} lh={1}> <Text ta={"center"} size={shouldScaleDown ? "xs" : "md"} lh={1}>
{date.getDate()} {date.getDate()}
</Text> </Text>
<NotificationIndicator events={events} isSmall={isSmall} /> {!isTooSmallForIndicators && <NotificationIndicator events={events} isSmall={isSmall} />}
</Container> </Container>
</HoverCard.Target> </Popover.Target>
<HoverCard.Dropdown maw="calc(100vw - 24px)" w={512} pe={4} pb={0} style={{ overflow: "hidden" }}> {/* Popover has some offset on the left side, padding is removed because of scrollarea paddings */}
<Popover.Dropdown
maw="calc(100vw - 24px)"
w={512}
pe={4}
pb={0}
style={{ overflow: "hidden" }}
onMouseEnter={() => setOpened(true)}
onMouseLeave={handleMouseLeave}
>
<CalendarEventList events={events} /> <CalendarEventList events={events} />
</HoverCard.Dropdown> </Popover.Dropdown>
</HoverCard> </Popover>
); );
}; };
@@ -69,6 +111,7 @@ interface NotificationIndicatorProps {
const NotificationIndicator = ({ events, isSmall }: NotificationIndicatorProps) => { const NotificationIndicator = ({ events, isSmall }: NotificationIndicatorProps) => {
const notificationEvents = [...new Set(events.map((event) => event.indicatorColor))].filter(String); const notificationEvents = [...new Set(events.map((event) => event.indicatorColor))].filter(String);
/* position bottom is lower when small to not be on top of number*/
return ( return (
<Flex <Flex
w="75%" w="75%"
+7 -1
View File
@@ -1,6 +1,6 @@
"use client"; "use client";
import { useMemo, useState } from "react"; import { useCallback, useMemo, useRef, useState } from "react";
import { useParams } from "next/navigation"; import { useParams } from "next/navigation";
import { useMantineTheme } from "@mantine/core"; import { useMantineTheme } from "@mantine/core";
import { Calendar } from "@mantine/dates"; import { Calendar } from "@mantine/dates";
@@ -65,6 +65,11 @@ const CalendarBase = ({ isEditMode, events, month, setMonth, options }: Calendar
const isSmall = width < 256; const isSmall = width < 256;
const normalizedEvents = useMemo(() => splitEvents(events), [events]); const normalizedEvents = useMemo(() => splitEvents(events), [events]);
const activeCloseRef = useRef<(() => void) | null>(null);
const onDayOpen = useCallback((close: () => void) => {
activeCloseRef.current?.();
activeCloseRef.current = close;
}, []);
return ( return (
<Calendar <Calendar
@@ -141,6 +146,7 @@ const CalendarBase = ({ isEditMode, events, month, setMonth, options }: Calendar
disabled={isEditMode || eventsForDate.length === 0} disabled={isEditMode || eventsForDate.length === 0}
rootWidth={width} rootWidth={width}
rootHeight={height} rootHeight={height}
onOpen={onDayOpen}
/> />
); );
}} }}