Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

870 B
Raw Blame
import type { InstancePermission } from "@kaneo/permissions";
import type { Context, Next } from "hono";
import { ApiError } from "../../errors/api-error";
import { getUserAccess } from "./get-instance-access";

/** Admin routes run after `requireUserSession`, so the caller is a user. */
export function requireInstancePermission(
  ...permissions: InstancePermission[]
) {
  return async function requirePermission(c: Context, next: Next) {
    const access = await getUserAccess(c.get("user").id);
    const missingPermissions = permissions.filter(
      (permission) => !access.permissions.includes(permission),
    );
    if (missingPermissions.length > 0) {
      throw new ApiError(403, {
        code: "INSTANCE_PERMISSION_REQUIRED",
        message: "You don't have permission to do this.",
        missingPermissions,
      });
    }
    return next();
  };
}