Your favorite shift management application!
fix: enforce viewStats/stampPreset capabilities client-side (#205)
panteL127136985d1b
diff --git a/app/page.tsx b/app/page.tsxindex bef6d39..575bd26 100644--- a/app/page.tsx+++ b/app/page.tsx@@ -169,6 +169,18 @@ function HomeContent() {const viewSettings = useViewSettings();const dialogStates = useDialogStates();+ // Symmetric to CalendarWorkspace's mobile stats-sheet guard: close the+ // desktop stats dialog immediately if the capability disappears mid-session,+ // rather than leaving it open against a now-forbidden endpoint.+ const canViewStats = can("viewStats");+ const [prevCanViewStats, setPrevCanViewStats] = useState(canViewStats);+ if (canViewStats !== prevCanViewStats) {+ setPrevCanViewStats(canViewStats);+ if (!canViewStats && dialogStates.showMonthStatsDialog) {+ dialogStates.setShowMonthStatsDialog(false);+ }+ }+const noteActions = useNoteActions({createNote: createNoteHook,updateNote: updateNoteHook,@@ -290,12 +302,16 @@ function HomeContent() {calendars.find((c) => c.id === selectedCalendar));+ // Mirrors the server's OR check in app/api/shifts/route.ts — stamping only+ // needs one of the two capabilities, not specifically createShift.+ const canStampPreset = can("stampPreset") || can("createShift");+// The armed presets are page state that outlives the stamp bar: it survives a// calendar switch, going offline and the personal toggle, none of which render// the dock. Derived here so a day click can never stamp without it on screen.const presetIdSet = new Set(presets.map((p) => p.id));const armedPresetIds =- can("createShift") && isOnline && calendarView.showStampBar+ canStampPreset && isOnline && calendarView.showStampBar? selectedPresetIds.filter((id) => presetIdSet.has(id)): [];@@ -491,6 +507,8 @@ function HomeContent() {canEditShift={canEditShift}canDeleteShift={canDeleteShift}showStampBar={calendarView.showStampBar}+ canStampPreset={canStampPreset}+ canViewStats={canViewStats}selectedPresetIds={armedPresetIds}onSelectPreset={handlePresetSelection}onManagePresets={() => dialogStates.setShowPresetManageDialog(true)}diff --git a/components/calendar-workspace.tsx b/components/calendar-workspace.tsxindex 49d50e7..74b10a0 100644--- a/components/calendar-workspace.tsx+++ b/components/calendar-workspace.tsx@@ -42,7 +42,7 @@ interface CalendarWorkspaceProps {showShiftNotes: boolean;highlightedWeekdays: number[];highlightColor: string;- /** createShift — gates stamping and the "add a new shift" affordances */+ /** createShift — gates the manual "add a new shift" affordance only */canCreateShift: boolean;/** manageOwnNotesEvents — creating a note/event only ever needs "own" */canAddNote: boolean;@@ -51,6 +51,10 @@ interface CalendarWorkspaceProps {/** Per-shift own/any precision (deleteOwnShift/deleteAnyShift) for the delete affordance */canDeleteShift: (shift: ShiftWithCalendar) => boolean;showStampBar: boolean;+ /** stampPreset OR createShift — mirrors the server's OR check for stamping a preset */+ canStampPreset: boolean;+ /** viewStats — hides the stats trigger entirely when absent, instead of failing on click */+ canViewStats: boolean;selectedPresetIds: string[];onSelectPreset: (id: string | undefined, multiSelect?: boolean) => void;onManagePresets: () => void;@@ -82,6 +86,8 @@ export function CalendarWorkspace({canEditShift,canDeleteShift,showStampBar,+ canStampPreset,+ canViewStats,selectedPresetIds,onSelectPreset,onManagePresets,@@ -98,22 +104,32 @@ export function CalendarWorkspace({const [period, setPeriod] = useState<StatsPeriod>("month");const [statsOpen, setStatsOpen] = useState(false);+ // MobileStatsSheet is skip-mounted below when !canViewStats, so a lingering+ // `true` here would pop it open unbidden if the capability comes back later.+ // Adjusted during render (not an effect) per React's "adjusting state when a+ // prop changes" pattern, to avoid an extra commit.+ const [prevCanViewStats, setPrevCanViewStats] = useState(canViewStats);+ if (canViewStats !== prevCanViewStats) {+ setPrevCanViewStats(canViewStats);+ if (!canViewStats) setStatsOpen(false);+ }+const dayData = useDayData({ selectedDay, shifts, notes });const monthSummary = usePeriodSummary({- calendarId,+ calendarId: canViewStats ? calendarId : undefined,anchorDate: currentDate,period: "month",shifts,});// Anchored on the week, not the day, so tapping around inside a week reuses one cache entryconst sheetSummary = usePeriodSummary({- calendarId: statsOpen ? calendarId : undefined,+ calendarId: statsOpen && canViewStats ? calendarId : undefined,anchorDate: period === "week" ? startOfWeek(selectedDay, { weekStartsOn: 1 }) : currentDate,period,shifts,});- const stampingEnabled = canCreateShift && isOnline && showStampBar;+ const stampingEnabled = canStampPreset && isOnline && showStampBar;const stampPresetIds = useMemo(() => orderStampPresets(presets).map((p) => p.id),[presets]@@ -194,7 +210,7 @@ export function CalendarWorkspace({/>)}</main>- <DayInspector model={model} actions={actions} />+ <DayInspector model={model} actions={actions} canViewStats={canViewStats} /></div></div>);@@ -238,11 +254,14 @@ export function CalendarWorkspace({)}<MobileDayFootersummary={monthSummary}+ currentDate={currentDate}+ canViewStats={canViewStats}onOpenStats={() => {// The footer shows the month, so the sheet opens on itsetPeriod("month");setStatsOpen(true);}}+ onOpenMonthShifts={actions.onOpenMonthShifts}onAddShift={model.canAddShift ? actions.onAddShift : undefined}/></div>@@ -252,15 +271,17 @@ export function CalendarWorkspace({open={sheetOpen}onOpenChange={onSheetOpenChange}/>- <MobileStatsSheet- model={model}- actions={actions}- period={period}- onPeriodChange={setPeriod}- summary={sheetSummary}- open={statsOpen}- onOpenChange={setStatsOpen}- />+ {canViewStats && (+ <MobileStatsSheet+ model={model}+ actions={actions}+ period={period}+ onPeriodChange={setPeriod}+ summary={sheetSummary}+ open={statsOpen}+ onOpenChange={setStatsOpen}+ />+ )}</div>);}diff --git a/components/compare-workspace.tsx b/components/compare-workspace.tsxindex c5b660e..1824de0 100644--- a/components/compare-workspace.tsx+++ b/components/compare-workspace.tsx@@ -115,7 +115,8 @@ export function CompareWorkspace(props: CompareWorkspaceProps) {// presets the shortcuts arm — without this a key press stamps into a calendar// the caller can't create shifts in and the write is rejected server-side.const { can: canStampActiveColumn } = useCalendarPermission(activeColumnId);- const canEditActiveColumn = canStampActiveColumn("createShift");+ const canEditActiveColumn =+ canStampActiveColumn("stampPreset") || canStampActiveColumn("createShift");const { isOnline } = useConnectionStatus({ toasts: false });useStampShortcuts({@@ -323,7 +324,7 @@ function CompareColumn({const t = useTranslations();const locale = useLocale();const { can } = useCalendarPermission(calendar.id);- const canStamp = can("createShift");+ const canStamp = can("stampPreset") || can("createShift");// The gear only needs to be worth opening — PresetManageSheet gates its own actions internally.const canOpenPresetManage = can("createPreset") || can("manageOwnPresets");const canAddNote = can("manageOwnNotesEvents");diff --git a/components/day-inspector.tsx b/components/day-inspector.tsxindex 412444d..4912a6a 100644--- a/components/day-inspector.tsx+++ b/components/day-inspector.tsx@@ -63,9 +63,11 @@ export function useDayLabels(day: Date) {export function DayInspector({model,actions,+ canViewStats,}: {model: DayViewModel;actions: DayActions;+ canViewStats: boolean;}) {const t = useTranslations();const locale = useLocale();@@ -156,9 +158,11 @@ export function DayInspector({</div><div className="border-t border-line px-[18px] py-3.5">- <div className="mb-2.5 flex items-center justify-between">+ <div className={cn("flex items-center justify-between", canViewStats && "mb-2.5")}><span className="eyebrow">- {t("calendarView.monthTotal", { month: monthName })}+ {canViewStats+ ? t("calendarView.monthTotal", { month: monthName })+ : t("calendarView.allShiftsIn", { month: monthName })}</span><div className="flex items-center gap-0.5"><button@@ -170,18 +174,20 @@ export function DayInspector({><List className="size-[15px]" /></button>- <button- type="button"- onClick={actions.onOpenStats}- title={t("calendarView.openStats")}- aria-label={t("calendarView.openStats")}- className="flex size-7 items-center justify-center rounded-md text-fg-tertiary transition-colors hover:bg-surface-sunken"- >- <ArrowUpRight className="size-[15px]" />- </button>+ {canViewStats && (+ <button+ type="button"+ onClick={actions.onOpenStats}+ title={t("calendarView.openStats")}+ aria-label={t("calendarView.openStats")}+ className="flex size-7 items-center justify-center rounded-md text-fg-tertiary transition-colors hover:bg-surface-sunken"+ >+ <ArrowUpRight className="size-[15px]" />+ </button>+ )}</div></div>- <PeriodSummaryView summary={summary} />+ {canViewStats && <PeriodSummaryView summary={summary} />}</div></aside>);diff --git a/components/mobile-day-sheet.tsx b/components/mobile-day-sheet.tsxindex a5c09c6..3e04eb6 100644--- a/components/mobile-day-sheet.tsx+++ b/components/mobile-day-sheet.tsx@@ -26,52 +26,79 @@ const LOW_MAX = 0.6;// vaul's own snap transition, so the inner height moves with the sheetconst SNAP_EASE = "duration-500 ease-[cubic-bezier(0.32,0.72,0,1)]";-/** Bottom bar under the phone grid: the visible month in figures and a button to add a shift. */+/** Bottom bar under the phone grid: the visible month in figures — or, without viewStats, a link to the month's shifts — and a button to add a shift. */export function MobileDayFooter({summary,+ currentDate,onOpenStats,+ onOpenMonthShifts,onAddShift,+ canViewStats,}: {summary: PeriodSummary;+ /** Only needed for the "all shifts in <month>" fallback label when stats are hidden */+ currentDate: Date;onOpenStats: () => void;+ onOpenMonthShifts: () => void;/** Hidden when the calendar can't be edited */onAddShift?: () => void;+ canViewStats: boolean;}) {const t = useTranslations();const locale = useLocale();const { stats, freeDays } = summary;- const figures = [- { label: t("calendarView.kpiShifts"), value: stats?.totalShifts ?? "–" },- {- label: t("calendarView.kpiHours"),- value: stats- ? new Intl.NumberFormat(locale, { maximumFractionDigits: 1 }).format(stats.totalMinutes / 60)- : "–",- },- { label: t("calendarView.kpiFreeDays"), value: freeDays ?? "–" },- ];+ const figures = canViewStats+ ? [+ { label: t("calendarView.kpiShifts"), value: stats?.totalShifts ?? "–" },+ {+ label: t("calendarView.kpiHours"),+ value: stats+ ? new Intl.NumberFormat(locale, { maximumFractionDigits: 1 }).format(+ stats.totalMinutes / 60+ )+ : "–",+ },+ { label: t("calendarView.kpiFreeDays"), value: freeDays ?? "–" },+ ]+ : [];return (<div className="flex items-center gap-2.5 border-t border-line bg-surface-panel px-3.5 pb-[max(9px,env(safe-area-inset-bottom))] pt-[9px]">- <button- type="button"- onClick={onOpenStats}- className="flex min-w-0 flex-1 items-center gap-2 py-0.5 text-left"- >- <span className="grid min-w-0 flex-1 grid-cols-3 gap-2">- {figures.map((figure) => (- <span key={figure.label} className="min-w-0">- <span className="block truncate font-mono text-[15px] font-medium leading-5 text-fg-strong">- {figure.value}- </span>- <span className="block truncate text-[10.5px] leading-[14px] text-fg-tertiary">- {figure.label}+ {canViewStats ? (+ <button+ type="button"+ onClick={onOpenStats}+ className="flex min-w-0 flex-1 items-center gap-2 py-0.5 text-left"+ >+ <span className="grid min-w-0 flex-1 grid-cols-3 gap-2">+ {figures.map((figure) => (+ <span key={figure.label} className="min-w-0">+ <span className="block truncate font-mono text-[15px] font-medium leading-5 text-fg-strong">+ {figure.value}+ </span>+ <span className="block truncate text-[10.5px] leading-[14px] text-fg-tertiary">+ {figure.label}+ </span></span>- </span>- ))}- </span>- <ChevronUp className="size-4 shrink-0 text-fg-tertiary" />- </button>+ ))}+ </span>+ <ChevronUp className="size-4 shrink-0 text-fg-tertiary" />+ </button>+ ) : (+ <button+ type="button"+ onClick={onOpenMonthShifts}+ className="flex min-w-0 flex-1 items-center gap-2.5 py-0.5 text-left"+ >+ <List className="size-4 shrink-0 text-fg-tertiary" />+ <span className="min-w-0 flex-1 truncate text-[13px] font-medium text-fg-secondary">+ {t("calendarView.allShiftsIn", {+ month: format(currentDate, "LLLL", { locale: getDateLocale(locale) }),+ })}+ </span>+ <ChevronRight className="size-4 shrink-0 text-fg-tertiary" />+ </button>+ )}{onAddShift && (<buttontype="button"diff --git a/docs/PERMISSIONS.md b/docs/PERMISSIONS.mdindex be2eefb..5ee2c7f 100644--- a/docs/PERMISSIONS.md+++ b/docs/PERMISSIONS.md@@ -89,7 +89,7 @@ The bundle picker shown when assigning guest access or a link only offers bundle### Read access is enforced everywhere-Viewing shifts, notes & events, statistics, and the member list is checked on every request that reads that data, not just used to decide what the interface shows. A bundle without "view statistics", for example, gets the statistics endpoint blocked outright — removing a button from the UI isn't what keeps the underlying data private.+Viewing shifts, notes & events, statistics, and the member list is checked on every request that reads that data, not just used to decide what the interface shows. A bundle without "view statistics", for example, gets the statistics endpoint blocked outright — removing a button from the UI isn't what keeps the underlying data private. As a UX improvement, the client also hides the corresponding controls and summaries (the statistics panel and its button, replaced by a plain "all shifts in month" link when absent, and the preset-stamp bar) when these capabilities are missing, though the server-side enforcement above remains the actual security boundary.### Who can assign or edit bundles