Skip to content

A modern and easy to use dashboard. 80+ integrations. 20K+ icons built in. Authentication out of the box. No YAML, drag and drop configuration.

1.7 KB
Raw Blame
import type { Session } from "@homarr/auth";
import { cache } from "react";

import { api } from "@homarr/api/server";
import { getAppManagementAccess, getIntegrationManagementAccess } from "@homarr/definitions";

/**
 * 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 const getAppsSectionAccess = (session: Session | null) =>
  getAppManagementAccess(session?.user.permissions ?? []);

// Share the authorized list within this render request, keyed by the current user.
export const getManageIntegrationsAsync = cache((_userId: string) => api.integration.all());

/**
 * 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 } }[],
) =>
  getIntegrationManagementAccess(
    session?.user.permissions ?? [],
    visibleIntegrations.some((integration) => integration.permissions.hasFullAccess),
  );

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.
  const needsDelegatedLookup =
    session !== null && !permissions.includes("integration-full-all") && !permissions.includes("integration-create");

  if (needsDelegatedLookup) {
    return getIntegrationsSectionAccess(session, await getManageIntegrationsAsync(session.user.id));
  }

  return getIntegrationsSectionAccess(session, []);
};