Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

48.6 KB
Raw Blame
import { Knex } from "knex";
import { z } from "zod";

import { TDbClient } from "@app/db";
import {
  AccessScope,
  AccessScopeData,
  MembershipRolesSchema,
  MembershipsSchema,
  RESOURCE_SCOPE,
  TableName,
  TMemberships,
  TRoles
} from "@app/db/schemas";
import { generateCacheKeyFromData } from "@app/lib/crypto/cache";
import { DatabaseError } from "@app/lib/errors";
import { selectAllTableCols, sqlNestRelationships } from "@app/lib/knex";
import { ActorType } from "@app/services/auth/auth-type";

interface TPermissionDataReturn extends TMemberships {
  orgAuthEnforced?: boolean | null;
  orgGoogleSsoAuthEnforced?: boolean | null;
  shouldUseNewPrivilegeSystem?: boolean | null;
  rootOrgId?: string | null;
  bypassOrgAuthEnabled?: boolean | null;
  roles: {
    id: string;
    createdAt: Date;
    updatedAt: Date;
    isTemporary: boolean;
    role: string;
    temporaryRange?: string | null | undefined;
    permissions?: unknown;
    customRoleId?: string | null | undefined;
    temporaryMode?: string | null | undefined;
    temporaryAccessStartTime?: Date | null | undefined;
    temporaryAccessEndTime?: Date | null | undefined;
    customRoleSlug?: string | null | undefined;
    customRoleName?: string | null | undefined;
  }[];
  additionalPrivileges: {
    id: string;
    permissions: unknown;
    temporaryRange: string | null | undefined;
    temporaryMode: string | null | undefined;
    temporaryAccessEndTime: Date | null | undefined;
    temporaryAccessStartTime: Date | null | undefined;
    isTemporary: boolean;
  }[];
  metadata: {
    id: string;
    key: string;
    value: string;
  }[];
}

export interface TPermissionDALFactory {
  getProjectUserPermissions: (
    projectId: string,
    orgId: string
  ) => Promise<
    {
      roles: {
        id: string;
        role: string;
        customRoleSlug: string;
        customRoleId?: string | null;
        customRoleName?: string | null;
        permissions: unknown;
        temporaryRange: string | null | undefined;
        temporaryMode: string | null | undefined;
        temporaryAccessStartTime: Date | null | undefined;
        temporaryAccessEndTime: Date | null | undefined;
        isTemporary: boolean;
      }[];
      additionalPrivileges: {
        id: string;
        name: string;
        permissions: unknown;
        temporaryRange: string | null | undefined;
        temporaryMode: string | null | undefined;
        temporaryAccessStartTime: Date | null | undefined;
        temporaryAccessEndTime: Date | null | undefined;
        isTemporary: boolean;
      }[];
      id: string;
      userId: string;
      username: string;
      metadata: {
        id: string;
        key: string;
        value: string;
      }[];
    }[]
  >;
  getProjectIdentityPermissions: (
    projectId: string,
    orgId: string
  ) => Promise<
    {
      roles: {
        id: string;
        createdAt: Date;
        updatedAt: Date;
        isTemporary: boolean;
        role: string;
        temporaryRange?: string | null | undefined;
        permissions?: unknown;
        customRoleId?: string | null | undefined;
        temporaryMode?: string | null | undefined;
        temporaryAccessStartTime?: Date | null | undefined;
        temporaryAccessEndTime?: Date | null | undefined;
        customRoleSlug?: string | null | undefined;
        customRoleName?: string | null | undefined;
      }[];
      additionalPrivileges: {
        id: string;
        name: string;
        permissions: unknown;
        temporaryRange: string | null | undefined;
        temporaryMode: string | null | undefined;
        temporaryAccessEndTime: Date | null | undefined;
        temporaryAccessStartTime: Date | null | undefined;
        isTemporary: boolean;
      }[];
      id: string;
      identityId: string;
      username: string;
      projectId: string;
      createdAt: Date;
      updatedAt: Date;
      orgId: string;
      metadata: {
        id: string;
        key: string;
        value: string;
      }[];
    }[]
  >;
  getProjectGroupPermissions: (
    projectId: string,
    filterGroupId?: string
  ) => Promise<
    {
      roles: {
        id: string;
        role: string;
        customRoleSlug: string;
        customRoleId?: string | null;
        customRoleName?: string | null;
        permissions: unknown;
        temporaryRange: string | null | undefined;
        temporaryMode: string | null | undefined;
        temporaryAccessStartTime: Date | null | undefined;
        temporaryAccessEndTime: Date | null | undefined;
        isTemporary: boolean;
      }[];
      groupId: string;
      username: string;
      id: string;
      groupRoles: {
        id: string;
        role: string;
        customRoleSlug: string;
        customRoleId?: string | null;
        customRoleName?: string | null;
        permissions: unknown;
        temporaryRange: string | null | undefined;
        temporaryMode: string | null | undefined;
        temporaryAccessStartTime: Date | null | undefined;
        temporaryAccessEndTime: Date | null | undefined;
        isTemporary: boolean;
      }[];
    }[]
  >;
  getPermission: (dto: {
    scopeData: AccessScopeData;
    actorId: string;
    actorType: ActorType.IDENTITY | ActorType.USER;
    tx?: Knex;
  }) => Promise<TPermissionDataReturn[]>;
  getResourceMembership: (dto: {
    projectId: string;
    resourceType: string;
    resourceId: string;
    actorId: string;
    actorType: ActorType.IDENTITY | ActorType.USER;
    tx?: Knex;
  }) => Promise<TResourceMembershipReturn[]>;
  getPermissionFingerprint: (dto: {
    projectId: string;
    orgId: string;
    actorId: string;
    actorType: ActorType.IDENTITY | ActorType.USER;
  }) => Promise<string>;
}

interface TResourceMembershipReturn extends TMemberships {
  roles: {
    id: string;
    role: string;
    permissions?: unknown;
    customRoleId?: string | null;
    customRoleSlug?: string | null;
    isTemporary: boolean;
    temporaryMode?: string | null;
    temporaryRange?: string | null;
    temporaryAccessStartTime?: Date | null;
    temporaryAccessEndTime?: Date | null;
    createdAt: Date;
    updatedAt: Date;
  }[];
}

export const permissionDALFactory = (db: TDbClient): TPermissionDALFactory => {
  const getPermission: TPermissionDALFactory["getPermission"] = async ({ scopeData, tx, actorId, actorType }) => {
    try {
      const conn = tx || db;
      const userGroupSubquery = conn(TableName.Groups)
        .join(TableName.UserGroupMembership, `${TableName.UserGroupMembership}.groupId`, `${TableName.Groups}.id`)
        .where(`${TableName.UserGroupMembership}.userId`, actorId)
        .select(db.ref("id").withSchema(TableName.Groups));

      const identityGroupSubquery = conn(TableName.Groups)
        .join(
          TableName.IdentityGroupMembership,
          `${TableName.IdentityGroupMembership}.groupId`,
          `${TableName.Groups}.id`
        )
        .where(`${TableName.IdentityGroupMembership}.identityId`, actorId)
        .select(db.ref("id").withSchema(TableName.Groups));

      const docs = await (tx || db)
        .replicaNode()(TableName.Membership)
        .join(TableName.MembershipRole, `${TableName.Membership}.id`, `${TableName.MembershipRole}.membershipId`)
        .join(TableName.Organization, `${TableName.Membership}.scopeOrgId`, `${TableName.Organization}.id`)
        .leftJoin(TableName.Role, `${TableName.MembershipRole}.customRoleId`, `${TableName.Role}.id`)
        .leftJoin(TableName.AdditionalPrivilege, (qb) => {
          // Match the privilege against the request's actor literal, not against
          // Membership.actor*Id. Group-derived memberships have actorUserId/actorIdentityId
          // NULL, so the column-to-column predicate dropped privileges for any user
          // whose only project access is via a group.
          if (actorType === ActorType.IDENTITY) {
            qb.on(`${TableName.AdditionalPrivilege}.actorIdentityId`, db.raw("?", [actorId]));
          } else {
            qb.on(`${TableName.AdditionalPrivilege}.actorUserId`, db.raw("?", [actorId]));
          }

          if (scopeData.scope === AccessScope.Organization) {
            qb.andOn(`${TableName.Membership}.scopeOrgId`, `${TableName.AdditionalPrivilege}.orgId`);
          } else if (scopeData.scope === AccessScope.Project) {
            qb.andOn(`${TableName.Membership}.scopeProjectId`, `${TableName.AdditionalPrivilege}.projectId`);
          }

          // Folder-scoped privileges are resolved separately and must never reach the project
          // ability: they would apply project-wide, and they carry a role slug with a NULL
          // permissions blob that the unpackRules path cannot read.
          qb.andOnNull(`${TableName.AdditionalPrivilege}.folderId`);
        })
        .leftJoin(TableName.IdentityMetadata, (queryBuilder) => {
          if (actorType === ActorType.USER) {
            void queryBuilder
              .on(`${TableName.IdentityMetadata}.userId`, db.raw("?", [actorId]))
              .andOn(`${TableName.Membership}.scopeOrgId`, `${TableName.IdentityMetadata}.orgId`);
          } else if (actorType === ActorType.IDENTITY) {
            void queryBuilder.on(`${TableName.IdentityMetadata}.identityId`, db.raw("?", [actorId]));
          }
        })
        .where(`${TableName.Membership}.scopeOrgId`, scopeData.orgId)
        .where((qb) => {
          if (actorType === ActorType.USER) {
            void qb
              .where(`${TableName.Membership}.actorUserId`, actorId)
              .orWhereIn(`${TableName.Membership}.actorGroupId`, userGroupSubquery);
          } else if (actorType === ActorType.IDENTITY) {
            void qb
              .where(`${TableName.Membership}.actorIdentityId`, actorId)
              .orWhereIn(`${TableName.Membership}.actorGroupId`, identityGroupSubquery);
          }
        })
        .where((qb) => {
          if (scopeData.scope === AccessScope.Organization) {
            void qb.where(`${TableName.Membership}.scope`, AccessScope.Organization);
          } else if (scopeData.scope === AccessScope.Project) {
            void qb
              .where(`${TableName.Membership}.scope`, AccessScope.Project)
              .where(`${TableName.Membership}.scopeProjectId`, scopeData.projectId);
          }
        })
        .select(selectAllTableCols(TableName.Membership))
        .select(
          db.ref("slug").withSchema(TableName.Role).as("roleSlug"),
          db.ref("name").withSchema(TableName.Role).as("roleName"),
          db.ref("permissions").withSchema(TableName.Role).as("customRolePermission"),
          db.ref("id").withSchema(TableName.MembershipRole).as("membershipRoleId"),
          db.ref("role").withSchema(TableName.MembershipRole).as("membershipRole"),
          db.ref("temporaryMode").withSchema(TableName.MembershipRole).as("membershipRoleTemporaryMode"),
          db.ref("isTemporary").withSchema(TableName.MembershipRole).as("membershipRoleIsTemporary"),
          db.ref("temporaryRange").withSchema(TableName.MembershipRole).as("membershipRoleTemporaryRange"),
          db
            .ref("temporaryAccessStartTime")
            .withSchema(TableName.MembershipRole)
            .as("membershipRoleTemporaryAccessStartTime"),
          db
            .ref("temporaryAccessEndTime")
            .withSchema(TableName.MembershipRole)
            .as("membershipRoleTemporaryAccessEndTime"),
          db.ref("createdAt").withSchema(TableName.MembershipRole).as("membershipRoleCreatedAt"),
          db.ref("updatedAt").withSchema(TableName.MembershipRole).as("membershipRoleUpdatedAt"),
          db.ref("id").withSchema(TableName.AdditionalPrivilege).as("additionalPrivilegeId"),
          db.ref("name").withSchema(TableName.AdditionalPrivilege).as("additionalPrivilegeName"),
          db.ref("permissions").withSchema(TableName.AdditionalPrivilege).as("additionalPrivilegePermissions"),
          db.ref("id").withSchema(TableName.AdditionalPrivilege).as("additionalPrivilegeId"),
          db.ref("temporaryMode").withSchema(TableName.AdditionalPrivilege).as("additionalPrivilegeTemporaryMode"),
          db.ref("isTemporary").withSchema(TableName.AdditionalPrivilege).as("additionalPrivilegeIsTemporary"),
          db.ref("temporaryRange").withSchema(TableName.AdditionalPrivilege).as("additionalPrivilegeTemporaryRange"),
          db
            .ref("temporaryAccessStartTime")
            .withSchema(TableName.AdditionalPrivilege)
            .as("additionalPrivilegeTemporaryAccessStartTime"),
          db
            .ref("temporaryAccessEndTime")
            .withSchema(TableName.AdditionalPrivilege)
            .as("additionalPrivilegeTemporaryAccessEndTime"),
          db.ref("createdAt").withSchema(TableName.AdditionalPrivilege).as("additionalPrivilegeCreatedAt"),
          db.ref("updatedAt").withSchema(TableName.AdditionalPrivilege).as("additionalPrivilegeUpdatedAt"),
          db.ref("id").withSchema(TableName.IdentityMetadata).as("metadataId"),
          db.ref("key").withSchema(TableName.IdentityMetadata).as("metadataKey"),
          db.ref("value").withSchema(TableName.IdentityMetadata).as("metadataValue"),
          db.ref("shouldUseNewPrivilegeSystem").withSchema(TableName.Organization),
          db.ref("authEnforced").withSchema(TableName.Organization).as("orgAuthEnforced"),
          db.ref("googleSsoAuthEnforced").withSchema(TableName.Organization).as("orgGoogleSsoAuthEnforced"),
          db.ref("bypassOrgAuthEnabled").withSchema(TableName.Organization).as("bypassOrgAuthEnabled"),
          db.ref("rootOrgId").withSchema(TableName.Organization).as("rootOrgId")
        );

      const data = sqlNestRelationships({
        data: docs,
        key: "id",
        parentMapper: (el) =>
          MembershipsSchema.extend({
            orgAuthEnforced: z.boolean().optional().nullable(),
            shouldUseNewPrivilegeSystem: z.boolean().optional().nullable(),
            rootOrgId: z.string().optional().nullable(),
            orgGoogleSsoAuthEnforced: z.boolean(),
            bypassOrgAuthEnabled: z.boolean()
          }).parse(el),
        childrenMapper: [
          {
            key: "additionalPrivilegeId",
            label: "additionalPrivileges" as const,
            mapper: ({
              additionalPrivilegeId,
              additionalPrivilegePermissions,
              additionalPrivilegeIsTemporary,
              additionalPrivilegeTemporaryMode,
              additionalPrivilegeTemporaryRange,
              additionalPrivilegeTemporaryAccessEndTime,
              additionalPrivilegeTemporaryAccessStartTime,
              additionalPrivilegeCreatedAt,
              additionalPrivilegeUpdatedAt
            }) => ({
              id: additionalPrivilegeId,
              permissions: additionalPrivilegePermissions,
              temporaryRange: additionalPrivilegeTemporaryRange,
              temporaryMode: additionalPrivilegeTemporaryMode,
              temporaryAccessStartTime: additionalPrivilegeTemporaryAccessStartTime,
              temporaryAccessEndTime: additionalPrivilegeTemporaryAccessEndTime,
              isTemporary: additionalPrivilegeIsTemporary,
              createdAt: additionalPrivilegeCreatedAt,
              updatedAt: additionalPrivilegeUpdatedAt
            })
          },
          {
            key: "membershipRoleId",
            label: "roles" as const,
            mapper: ({
              roleSlug,
              roleName,
              customRolePermission,
              membershipRoleId,
              membershipRole,
              membershipRoleIsTemporary,
              membershipRoleTemporaryMode,
              membershipRoleTemporaryRange,
              membershipRoleTemporaryAccessEndTime,
              membershipRoleTemporaryAccessStartTime,
              membershipRoleCreatedAt,
              membershipRoleUpdatedAt
            }) => ({
              id: membershipRoleId,
              role: membershipRole,
              permissions: customRolePermission,
              customRoleSlug: roleSlug,
              customRoleName: roleName,
              temporaryRange: membershipRoleTemporaryRange,
              temporaryMode: membershipRoleTemporaryMode,
              temporaryAccessStartTime: membershipRoleTemporaryAccessStartTime,
              temporaryAccessEndTime: membershipRoleTemporaryAccessEndTime,
              isTemporary: membershipRoleIsTemporary,
              createdAt: membershipRoleCreatedAt,
              updatedAt: membershipRoleUpdatedAt
            })
          },
          {
            key: "metadataId",
            label: "metadata" as const,
            mapper: ({ metadataKey, metadataValue, metadataId }) => ({
              id: metadataId,
              key: metadataKey,
              value: metadataValue
            })
          }
        ]
      });

      return data;
    } catch (error) {
      throw new DatabaseError({ error, name: "Get Permission" });
    }
  };

  const getResourceMembership: TPermissionDALFactory["getResourceMembership"] = async ({
    projectId,
    resourceType,
    resourceId,
    actorId,
    actorType,
    tx
  }) => {
    try {
      const conn = tx || db.replicaNode();

      const userGroupSubquery = conn(TableName.UserGroupMembership).where("userId", actorId).select("groupId");
      const identityGroupSubquery = conn(TableName.IdentityGroupMembership)
        .where("identityId", actorId)
        .select("groupId");

      const docs = await conn(TableName.Membership)
        .join(TableName.MembershipRole, `${TableName.Membership}.id`, `${TableName.MembershipRole}.membershipId`)
        .leftJoin(TableName.Role, `${TableName.MembershipRole}.customRoleId`, `${TableName.Role}.id`)
        .where(`${TableName.Membership}.scope`, RESOURCE_SCOPE)
        .where(`${TableName.Membership}.scopeProjectId`, projectId)
        .where(`${TableName.Membership}.scopeResourceType`, resourceType)
        .where(`${TableName.Membership}.scopeResourceId`, resourceId)
        .where(`${TableName.Membership}.isActive`, true)
        .where((qb) => {
          if (actorType === ActorType.USER) {
            void qb
              .where(`${TableName.Membership}.actorUserId`, actorId)
              .orWhereIn(`${TableName.Membership}.actorGroupId`, userGroupSubquery);
          } else {
            void qb
              .where(`${TableName.Membership}.actorIdentityId`, actorId)
              .orWhereIn(`${TableName.Membership}.actorGroupId`, identityGroupSubquery);
          }
        })
        .select(selectAllTableCols(TableName.Membership))
        .select(
          db.ref("id").withSchema(TableName.MembershipRole).as("membershipRoleId"),
          db.ref("role").withSchema(TableName.MembershipRole).as("membershipRole"),
          db.ref("customRoleId").withSchema(TableName.MembershipRole).as("membershipCustomRoleId"),
          db.ref("isTemporary").withSchema(TableName.MembershipRole).as("membershipRoleIsTemporary"),
          db.ref("temporaryMode").withSchema(TableName.MembershipRole).as("membershipRoleTemporaryMode"),
          db.ref("temporaryRange").withSchema(TableName.MembershipRole).as("membershipRoleTemporaryRange"),
          db
            .ref("temporaryAccessStartTime")
            .withSchema(TableName.MembershipRole)
            .as("membershipRoleTemporaryAccessStartTime"),
          db
            .ref("temporaryAccessEndTime")
            .withSchema(TableName.MembershipRole)
            .as("membershipRoleTemporaryAccessEndTime"),
          db.ref("createdAt").withSchema(TableName.MembershipRole).as("membershipRoleCreatedAt"),
          db.ref("updatedAt").withSchema(TableName.MembershipRole).as("membershipRoleUpdatedAt"),
          db.ref("slug").withSchema(TableName.Role).as("customRoleSlug"),
          db.ref("permissions").withSchema(TableName.Role).as("customRolePermissions")
        );

      const data = sqlNestRelationships({
        data: docs,
        key: "id",
        parentMapper: (el) => MembershipsSchema.parse(el),
        childrenMapper: [
          {
            key: "membershipRoleId",
            label: "roles" as const,
            mapper: ({
              membershipRoleId,
              membershipRole,
              membershipCustomRoleId,
              membershipRoleIsTemporary,
              membershipRoleTemporaryMode,
              membershipRoleTemporaryRange,
              membershipRoleTemporaryAccessStartTime,
              membershipRoleTemporaryAccessEndTime,
              membershipRoleCreatedAt,
              membershipRoleUpdatedAt,
              customRoleSlug,
              customRolePermissions
            }) => ({
              id: membershipRoleId,
              role: membershipRole,
              customRoleId: membershipCustomRoleId,
              customRoleSlug: customRoleSlug as string | null | undefined,
              permissions: customRolePermissions,
              isTemporary: Boolean(membershipRoleIsTemporary),
              temporaryMode: membershipRoleTemporaryMode,
              temporaryRange: membershipRoleTemporaryRange,
              temporaryAccessStartTime: membershipRoleTemporaryAccessStartTime,
              temporaryAccessEndTime: membershipRoleTemporaryAccessEndTime,
              createdAt: membershipRoleCreatedAt,
              updatedAt: membershipRoleUpdatedAt
            })
          }
        ]
      });

      return data as TResourceMembershipReturn[];
    } catch (error) {
      throw new DatabaseError({ error, name: "Get Resource Membership" });
    }
  };

  const getProjectGroupPermissions: TPermissionDALFactory["getProjectGroupPermissions"] = async (
    projectId: string,
    filterGroupId?: string
  ) => {
    try {
      const docs = await db
        .replicaNode()(TableName.Membership)
        .whereNotNull(`${TableName.Membership}.actorGroupId`)
        .where(`${TableName.Membership}.scope`, AccessScope.Project)
        .join(TableName.Groups, `${TableName.Groups}.id`, `${TableName.Membership}.actorGroupId`)
        .join(TableName.MembershipRole, `${TableName.MembershipRole}.membershipId`, `${TableName.Membership}.id`)
        .leftJoin<TRoles>(
          { groupCustomRoles: TableName.Role },
          `${TableName.MembershipRole}.customRoleId`,
          `groupCustomRoles.id`
        )
        .where(`${TableName.Membership}.scopeProjectId`, "=", projectId)
        .where((bd) => {
          if (filterGroupId) {
            void bd.where(`${TableName.Membership}.actorGroupId`, "=", filterGroupId);
          }
        })
        .select(
          db.ref("id").withSchema(TableName.Membership).as("membershipId"),
          db.ref("id").withSchema(TableName.Groups).as("groupId"),
          db.ref("name").withSchema(TableName.Groups).as("groupName"),
          db.ref("slug").withSchema("groupCustomRoles").as("groupProjectMembershipRoleCustomRoleSlug"),
          db.ref("name").withSchema("groupCustomRoles").as("groupProjectMembershipRoleCustomRoleName"),
          db.ref("permissions").withSchema("groupCustomRoles").as("groupProjectMembershipRolePermission"),
          db.ref("id").withSchema(TableName.MembershipRole).as("groupProjectMembershipRoleId"),
          db.ref("role").withSchema(TableName.MembershipRole).as("groupProjectMembershipRole"),
          db.ref("customRoleId").withSchema(TableName.MembershipRole).as("groupProjectMembershipRoleCustomRoleId"),
          db.ref("isTemporary").withSchema(TableName.MembershipRole).as("groupProjectMembershipRoleIsTemporary"),
          db.ref("temporaryMode").withSchema(TableName.MembershipRole).as("groupProjectMembershipRoleTemporaryMode"),
          db.ref("temporaryRange").withSchema(TableName.MembershipRole).as("groupProjectMembershipRoleTemporaryRange"),
          db
            .ref("temporaryAccessStartTime")
            .withSchema(TableName.MembershipRole)
            .as("groupProjectMembershipRoleTemporaryAccessStartTime"),
          db
            .ref("temporaryAccessEndTime")
            .withSchema(TableName.MembershipRole)
            .as("groupProjectMembershipRoleTemporaryAccessEndTime")
        );

      const groupPermissions = sqlNestRelationships({
        data: docs,
        key: "groupId",
        parentMapper: ({ groupId, groupName, membershipId }) => ({
          groupId,
          username: groupName,
          id: membershipId
        }),
        childrenMapper: [
          {
            key: "groupProjectMembershipRoleId",
            label: "groupRoles" as const,
            mapper: ({
              groupProjectMembershipRoleId,
              groupProjectMembershipRole,
              groupProjectMembershipRolePermission,
              groupProjectMembershipRoleCustomRoleSlug,
              groupProjectMembershipRoleCustomRoleId,
              groupProjectMembershipRoleCustomRoleName,
              groupProjectMembershipRoleIsTemporary,
              groupProjectMembershipRoleTemporaryMode,
              groupProjectMembershipRoleTemporaryAccessEndTime,
              groupProjectMembershipRoleTemporaryAccessStartTime,
              groupProjectMembershipRoleTemporaryRange
            }) => ({
              id: groupProjectMembershipRoleId,
              role: groupProjectMembershipRole,
              customRoleSlug: groupProjectMembershipRoleCustomRoleSlug,
              customRoleId: groupProjectMembershipRoleCustomRoleId as string | null,
              customRoleName: groupProjectMembershipRoleCustomRoleName as string | null,
              permissions: groupProjectMembershipRolePermission,
              temporaryRange: groupProjectMembershipRoleTemporaryRange,
              temporaryMode: groupProjectMembershipRoleTemporaryMode,
              temporaryAccessStartTime: groupProjectMembershipRoleTemporaryAccessStartTime,
              temporaryAccessEndTime: groupProjectMembershipRoleTemporaryAccessEndTime,
              isTemporary: groupProjectMembershipRoleIsTemporary
            })
          }
        ]
      });

      return groupPermissions
        .map((groupPermission) => {
          if (!groupPermission) return undefined;

          const activeGroupRoles =
            groupPermission?.groupRoles?.filter(
              ({ isTemporary, temporaryAccessEndTime }) =>
                !isTemporary || (isTemporary && temporaryAccessEndTime && new Date() < temporaryAccessEndTime)
            ) ?? [];

          return {
            ...groupPermission,
            roles: activeGroupRoles
          };
        })
        .filter((item): item is NonNullable<typeof item> => Boolean(item));
    } catch (error) {
      throw new DatabaseError({ error, name: "GetProjectGroupPermissions" });
    }
  };

  const getProjectUserPermissions: TPermissionDALFactory["getProjectUserPermissions"] = async (
    projectId: string,
    orgId: string
  ) => {
    const userGroupSubquery = db(TableName.Groups)
      .leftJoin(TableName.UserGroupMembership, `${TableName.UserGroupMembership}.groupId`, `${TableName.Groups}.id`)
      .join(TableName.Membership, `${TableName.Groups}.id`, `${TableName.Membership}.actorGroupId`)
      .where(`${TableName.Membership}.scopeOrgId`, orgId)
      .select(db.ref("id").withSchema(TableName.Groups));

    try {
      const docs = await db
        .replicaNode()(TableName.Users)
        .where("isGhost", "=", false)
        .join(TableName.Membership, `${TableName.Users}.id`, `${TableName.Membership}.actorUserId`)
        .join(TableName.MembershipRole, `${TableName.Membership}.id`, `${TableName.MembershipRole}.membershipId`)
        .leftJoin(TableName.Role, `${TableName.MembershipRole}.customRoleId`, `${TableName.Role}.id`)
        .leftJoin(TableName.AdditionalPrivilege, (qb) => {
          qb.on(`${TableName.Membership}.actorUserId`, `${TableName.AdditionalPrivilege}.actorUserId`)
            .andOn(`${TableName.Membership}.scopeProjectId`, `${TableName.AdditionalPrivilege}.projectId`)
            // Folder-scoped privileges are resolved separately, so they are not part of a
            // project-wide listing.
            .andOnNull(`${TableName.AdditionalPrivilege}.folderId`);
        })
        .leftJoin(TableName.IdentityMetadata, (queryBuilder) => {
          void queryBuilder
            .on(`${TableName.Users}.id`, `${TableName.IdentityMetadata}.userId`)
            .andOn(`${TableName.Membership}.scopeOrgId`, `${TableName.IdentityMetadata}.orgId`);
        })
        .where(`${TableName.Membership}.scopeOrgId`, orgId)
        .where((qb) => {
          void qb
            .whereNotNull(`${TableName.Membership}.actorUserId`)
            .orWhereIn(`${TableName.Membership}.actorGroupId`, userGroupSubquery);
        })
        .where((qb) => {
          void qb
            .where(`${TableName.Membership}.scope`, AccessScope.Project)
            .where(`${TableName.Membership}.scopeProjectId`, projectId);
        })
        .select(
          db.ref("id").withSchema(TableName.Users).as("userId"),
          db.ref("id").withSchema(TableName.Membership).as("membershipId"),
          db.ref("username").withSchema(TableName.Users).as("username"),
          db.ref("slug").withSchema(TableName.Role).as("roleSlug"),
          db.ref("name").withSchema(TableName.Role).as("roleName"),
          db.ref("customRoleId").withSchema(TableName.MembershipRole).as("membershipRoleCustomRoleId"),
          db.ref("permissions").withSchema(TableName.Role).as("customRolePermission"),
          db.ref("id").withSchema(TableName.MembershipRole).as("membershipRoleId"),
          db.ref("role").withSchema(TableName.MembershipRole).as("membershipRole"),
          db.ref("temporaryMode").withSchema(TableName.MembershipRole).as("membershipRoleTemporaryMode"),
          db.ref("isTemporary").withSchema(TableName.MembershipRole).as("membershipRoleIsTemporary"),
          db.ref("temporaryRange").withSchema(TableName.MembershipRole).as("membershipRoleTemporaryRange"),
          db
            .ref("temporaryAccessStartTime")
            .withSchema(TableName.MembershipRole)
            .as("membershipRoleTemporaryAccessStartTime"),
          db
            .ref("temporaryAccessEndTime")
            .withSchema(TableName.MembershipRole)
            .as("membershipRoleTemporaryAccessEndTime"),
          db.ref("createdAt").withSchema(TableName.MembershipRole).as("membershipRoleCreatedAt"),
          db.ref("updatedAt").withSchema(TableName.MembershipRole).as("membershipRoleUpdatedAt"),
          db.ref("id").withSchema(TableName.AdditionalPrivilege).as("additionalPrivilegeId"),
          db.ref("name").withSchema(TableName.AdditionalPrivilege).as("additionalPrivilegeName"),
          db.ref("permissions").withSchema(TableName.AdditionalPrivilege).as("additionalPrivilegePermissions"),
          db.ref("id").withSchema(TableName.AdditionalPrivilege).as("additionalPrivilegeId"),
          db.ref("temporaryMode").withSchema(TableName.AdditionalPrivilege).as("additionalPrivilegeTemporaryMode"),
          db.ref("isTemporary").withSchema(TableName.AdditionalPrivilege).as("additionalPrivilegeIsTemporary"),
          db.ref("temporaryRange").withSchema(TableName.AdditionalPrivilege).as("additionalPrivilegeTemporaryRange"),
          db
            .ref("temporaryAccessStartTime")
            .withSchema(TableName.AdditionalPrivilege)
            .as("additionalPrivilegeTemporaryAccessStartTime"),
          db
            .ref("temporaryAccessEndTime")
            .withSchema(TableName.AdditionalPrivilege)
            .as("additionalPrivilegeTemporaryAccessEndTime"),
          db.ref("createdAt").withSchema(TableName.AdditionalPrivilege).as("additionalPrivilegeCreatedAt"),
          db.ref("updatedAt").withSchema(TableName.AdditionalPrivilege).as("additionalPrivilegeUpdatedAt"),
          // general
          db.ref("id").withSchema(TableName.IdentityMetadata).as("metadataId"),
          db.ref("key").withSchema(TableName.IdentityMetadata).as("metadataKey"),
          db.ref("value").withSchema(TableName.IdentityMetadata).as("metadataValue")
        );

      const userPermissions = sqlNestRelationships({
        data: docs,
        key: "userId",
        parentMapper: ({ username, userId, membershipId }) => ({
          userId,
          projectId,
          username,
          id: membershipId
        }),
        childrenMapper: [
          {
            key: "additionalPrivilegeId",
            label: "additionalPrivileges" as const,
            mapper: ({
              additionalPrivilegeId,
              additionalPrivilegeName,
              additionalPrivilegePermissions,
              additionalPrivilegeIsTemporary,
              additionalPrivilegeTemporaryMode,
              additionalPrivilegeTemporaryRange,
              additionalPrivilegeTemporaryAccessEndTime,
              additionalPrivilegeTemporaryAccessStartTime,
              additionalPrivilegeCreatedAt,
              additionalPrivilegeUpdatedAt
            }) => ({
              id: additionalPrivilegeId,
              name: additionalPrivilegeName,
              permissions: additionalPrivilegePermissions,
              temporaryRange: additionalPrivilegeTemporaryRange,
              temporaryMode: additionalPrivilegeTemporaryMode,
              temporaryAccessStartTime: additionalPrivilegeTemporaryAccessStartTime,
              temporaryAccessEndTime: additionalPrivilegeTemporaryAccessEndTime,
              isTemporary: additionalPrivilegeIsTemporary,
              createdAt: additionalPrivilegeCreatedAt,
              updatedAt: additionalPrivilegeUpdatedAt
            })
          },
          {
            key: "membershipRoleId",
            label: "roles" as const,
            mapper: ({
              roleSlug,
              roleName,
              membershipRoleCustomRoleId,
              customRolePermission,
              membershipRoleId,
              membershipRole,
              membershipRoleIsTemporary,
              membershipRoleTemporaryMode,
              membershipRoleTemporaryRange,
              membershipRoleTemporaryAccessEndTime,
              membershipRoleTemporaryAccessStartTime,
              membershipRoleCreatedAt,
              membershipRoleUpdatedAt
            }) => ({
              id: membershipRoleId,
              role: membershipRole,
              permissions: customRolePermission,
              customRoleSlug: roleSlug,
              customRoleId: membershipRoleCustomRoleId as string | null,
              customRoleName: roleName as string | null,
              temporaryRange: membershipRoleTemporaryRange,
              temporaryMode: membershipRoleTemporaryMode,
              temporaryAccessStartTime: membershipRoleTemporaryAccessStartTime,
              temporaryAccessEndTime: membershipRoleTemporaryAccessEndTime,
              isTemporary: membershipRoleIsTemporary,
              createdAt: membershipRoleCreatedAt,
              updatedAt: membershipRoleUpdatedAt
            })
          },
          {
            key: "metadataId",
            label: "metadata" as const,
            mapper: ({ metadataKey, metadataValue, metadataId }) => ({
              id: metadataId,
              key: metadataKey,
              value: metadataValue
            })
          }
        ]
      });

      return userPermissions
        .map((userPermission) => {
          if (!userPermission) return undefined;
          if (!userPermission?.roles?.[0]) return undefined;

          // when introducting cron mode change it here
          const activeRoles =
            userPermission?.roles?.filter(
              ({ isTemporary, temporaryAccessEndTime }) =>
                !isTemporary || (isTemporary && temporaryAccessEndTime && new Date() < temporaryAccessEndTime)
            ) ?? [];

          const activeAdditionalPrivileges =
            userPermission?.additionalPrivileges?.filter(
              ({ isTemporary, temporaryAccessEndTime }) =>
                !isTemporary || (isTemporary && temporaryAccessEndTime && new Date() < temporaryAccessEndTime)
            ) ?? [];

          return {
            ...userPermission,
            roles: activeRoles,
            additionalPrivileges: activeAdditionalPrivileges
          };
        })
        .filter((item): item is NonNullable<typeof item> => Boolean(item));
    } catch (error) {
      throw new DatabaseError({ error, name: "GetProjectUserPermissions" });
    }
  };

  const getProjectIdentityPermissions: TPermissionDALFactory["getProjectIdentityPermissions"] = async (
    projectId: string,
    orgId: string
  ) => {
    try {
      const identityGroupSubquery = db(TableName.Groups)
        .leftJoin(
          TableName.IdentityGroupMembership,
          `${TableName.IdentityGroupMembership}.groupId`,
          `${TableName.Groups}.id`
        )
        .join(TableName.Membership, `${TableName.Groups}.id`, `${TableName.Membership}.actorGroupId`)
        .where(`${TableName.Membership}.scopeOrgId`, orgId)
        .select(db.ref("id").withSchema(TableName.Groups));

      const docs = await db
        .replicaNode()(TableName.Membership)
        .join(TableName.MembershipRole, `${TableName.Membership}.id`, `${TableName.MembershipRole}.membershipId`)
        .leftJoin(TableName.Role, `${TableName.MembershipRole}.customRoleId`, `${TableName.Role}.id`)
        .leftJoin(TableName.AdditionalPrivilege, (qb) => {
          qb.on(`${TableName.Membership}.actorIdentityId`, `${TableName.AdditionalPrivilege}.actorIdentityId`)
            .andOn(`${TableName.Membership}.scopeProjectId`, `${TableName.AdditionalPrivilege}.projectId`)
            // Folder-scoped privileges are resolved separately, so they are not part of a
            // project-wide listing.
            .andOnNull(`${TableName.AdditionalPrivilege}.folderId`);
        })
        .join(TableName.Identity, `${TableName.Identity}.id`, `${TableName.Membership}.actorIdentityId`)
        .leftJoin(TableName.IdentityMetadata, (queryBuilder) => {
          void queryBuilder.on(`${TableName.Membership}.actorIdentityId`, `${TableName.IdentityMetadata}.identityId`);
        })
        .where(`${TableName.Membership}.scopeOrgId`, orgId)
        .where((qb) => {
          void qb
            .whereNotNull(`${TableName.Membership}.actorIdentityId`)
            .orWhereIn(`${TableName.Membership}.actorGroupId`, identityGroupSubquery);
        })
        .where(`${TableName.Membership}.scope`, AccessScope.Project)
        .where(`${TableName.Membership}.scopeProjectId`, projectId)
        .select(selectAllTableCols(TableName.MembershipRole))
        .select(
          db.ref("id").withSchema(TableName.Membership).as("membershipId"),
          db.ref("id").withSchema(TableName.Identity).as("identityId"),
          db.ref("name").withSchema(TableName.Identity).as("identityName"),
          db.ref("createdAt").withSchema(TableName.Membership).as("membershipCreatedAt"),
          db.ref("updatedAt").withSchema(TableName.Membership).as("membershipUpdatedAt"),
          db.ref("slug").withSchema(TableName.Role).as("customRoleSlug"),
          db.ref("name").withSchema(TableName.Role).as("customRoleName"),
          db.ref("permissions").withSchema(TableName.Role),
          db.ref("id").withSchema(TableName.AdditionalPrivilege).as("identityApId"),
          db.ref("name").withSchema(TableName.AdditionalPrivilege).as("identityApName"),
          db.ref("permissions").withSchema(TableName.AdditionalPrivilege).as("identityApPermissions"),
          db.ref("temporaryMode").withSchema(TableName.AdditionalPrivilege).as("identityApTemporaryMode"),
          db.ref("isTemporary").withSchema(TableName.AdditionalPrivilege).as("identityApIsTemporary"),
          db.ref("temporaryRange").withSchema(TableName.AdditionalPrivilege).as("identityApTemporaryRange"),
          db
            .ref("temporaryAccessStartTime")
            .withSchema(TableName.AdditionalPrivilege)
            .as("identityApTemporaryAccessStartTime"),
          db
            .ref("temporaryAccessEndTime")
            .withSchema(TableName.AdditionalPrivilege)
            .as("identityApTemporaryAccessEndTime"),
          db.ref("id").withSchema(TableName.IdentityMetadata).as("metadataId"),
          db.ref("key").withSchema(TableName.IdentityMetadata).as("metadataKey"),
          db.ref("value").withSchema(TableName.IdentityMetadata).as("metadataValue")
        );

      const permissions = sqlNestRelationships({
        data: docs,
        key: "identityId",
        parentMapper: ({ membershipId, membershipCreatedAt, membershipUpdatedAt, identityName, identityId }) => ({
          id: membershipId,
          identityId,
          username: identityName,
          projectId,
          createdAt: membershipCreatedAt,
          updatedAt: membershipUpdatedAt,
          orgId,
          // just a prefilled value
          orgAuthEnforced: false
        }),
        childrenMapper: [
          {
            key: "id",
            label: "roles" as const,
            mapper: (data) =>
              MembershipRolesSchema.extend({
                permissions: z.unknown(),
                customRoleSlug: z.string().optional().nullable(),
                customRoleName: z.string().optional().nullable()
              }).parse(data)
          },
          {
            key: "identityApId",
            label: "additionalPrivileges" as const,
            mapper: ({
              identityApId,
              identityApName,
              identityApPermissions,
              identityApIsTemporary,
              identityApTemporaryMode,
              identityApTemporaryRange,
              identityApTemporaryAccessEndTime,
              identityApTemporaryAccessStartTime
            }) => ({
              id: identityApId,
              name: identityApName,
              permissions: identityApPermissions,
              temporaryRange: identityApTemporaryRange,
              temporaryMode: identityApTemporaryMode,
              temporaryAccessEndTime: identityApTemporaryAccessEndTime,
              temporaryAccessStartTime: identityApTemporaryAccessStartTime,
              isTemporary: identityApIsTemporary
            })
          },
          {
            key: "metadataId",
            label: "metadata" as const,
            mapper: ({ metadataKey, metadataValue, metadataId }) => ({
              id: metadataId,
              key: metadataKey,
              value: metadataValue
            })
          }
        ]
      });

      return permissions
        .map((permission) => {
          if (!permission) {
            return undefined;
          }

          // when introducting cron mode change it here
          const activeRoles = permission?.roles.filter(
            ({ isTemporary, temporaryAccessEndTime }) =>
              !isTemporary || (isTemporary && temporaryAccessEndTime && new Date() < temporaryAccessEndTime)
          );
          const activeAdditionalPrivileges = permission?.additionalPrivileges?.filter(
            ({ isTemporary, temporaryAccessEndTime }) =>
              !isTemporary || (isTemporary && temporaryAccessEndTime && new Date() < temporaryAccessEndTime)
          );

          return { ...permission, roles: activeRoles, additionalPrivileges: activeAdditionalPrivileges };
        })
        .filter((item): item is NonNullable<typeof item> => Boolean(item));
    } catch (error) {
      throw new DatabaseError({ error, name: "GetProjectIdentityPermissions" });
    }
  };

  // Lightweight permission fingerprint for ETag validation.
  // Same tables/joins/WHERE as getPermission but selects only IDs + timestamps,
  // then hashes in JS. The CASE expressions make the hash flip at temporary access expiry.
  // Includes both project-scoped AND org-scoped memberships so that org-level
  // permission changes (e.g. SSO bypass grant/revoke) also invalidate the cache.
  const getPermissionFingerprint: TPermissionDALFactory["getPermissionFingerprint"] = async ({
    projectId,
    orgId,
    actorId,
    actorType
  }) => {
    try {
      const groupTable =
        actorType === ActorType.USER ? TableName.UserGroupMembership : TableName.IdentityGroupMembership;
      const groupActorField = actorType === ActorType.USER ? "userId" : "identityId";

      const groupSubquery = db
        .replicaNode()(TableName.Groups)
        .join(groupTable, `${groupTable}.groupId`, `${TableName.Groups}.id`)
        .where(`${groupTable}.${groupActorField}`, actorId)
        .select(db.ref("id").withSchema(TableName.Groups));

      const rows = await db
        .replicaNode()(TableName.Membership)
        .join(TableName.MembershipRole, `${TableName.Membership}.id`, `${TableName.MembershipRole}.membershipId`)
        .leftJoin(TableName.Role, `${TableName.MembershipRole}.customRoleId`, `${TableName.Role}.id`)
        .leftJoin(TableName.AdditionalPrivilege, (qb) => {
          // Match by literal actor id, not Membership.actor*Id — see getPermission for the
          // group-derived membership rationale. Read and fingerprint must stay aligned, or
          // the cache will think nothing changed and serve stale abilities.
          const privActorCol =
            actorType === ActorType.IDENTITY
              ? `${TableName.AdditionalPrivilege}.actorIdentityId`
              : `${TableName.AdditionalPrivilege}.actorUserId`;
          qb.on(privActorCol, db.raw("?", [actorId]))
            .andOn(`${TableName.Membership}.scopeProjectId`, `${TableName.AdditionalPrivilege}.projectId`)
            // Folder-scoped privileges are excluded from getPermission, so excluding them here too
            // is what keeps the fingerprint aligned with the data it validates.
            .andOnNull(`${TableName.AdditionalPrivilege}.folderId`);
        })
        .leftJoin(TableName.IdentityMetadata, (qb) => {
          if (actorType === ActorType.IDENTITY) {
            qb.on(`${TableName.IdentityMetadata}.identityId`, `${TableName.Membership}.actorIdentityId`);
          } else {
            void qb
              .on(`${TableName.IdentityMetadata}.userId`, db.raw("?", [actorId]))
              .andOn(`${TableName.Membership}.scopeOrgId`, `${TableName.IdentityMetadata}.orgId`);
          }
        })
        .leftJoin(TableName.Project, `${TableName.Membership}.scopeProjectId`, `${TableName.Project}.id`)
        .where(`${TableName.Membership}.scopeOrgId`, orgId)
        .where((scopeQb) => {
          void scopeQb
            .where((inner) => {
              void inner
                .where(`${TableName.Membership}.scope`, AccessScope.Project)
                .where(`${TableName.Membership}.scopeProjectId`, projectId);
            })
            .orWhere((inner) => {
              void inner
                .where(`${TableName.Membership}.scope`, RESOURCE_SCOPE)
                .where(`${TableName.Membership}.scopeProjectId`, projectId);
            })
            .orWhere(`${TableName.Membership}.scope`, AccessScope.Organization);
        })
        .where((qb) => {
          const directCol =
            actorType === ActorType.USER
              ? `${TableName.Membership}.actorUserId`
              : `${TableName.Membership}.actorIdentityId`;
          void qb.where(directCol, actorId).orWhereIn(`${TableName.Membership}.actorGroupId`, groupSubquery);
        })
        .select(
          db.ref("id").withSchema(TableName.Membership).as("mId"),
          // Track isActive/status rather than updatedAt: every login bumps Membership.updatedAt
          // (lastLoginTime/lastLoginAuthMethod), which would needlessly bust the fingerprint/ETag on
          // every auth. isActive/status are the membership columns that actually gate permissions.
          db.ref("isActive").withSchema(TableName.Membership).as("mActive"),
          db.ref("status").withSchema(TableName.Membership).as("mStatus"),
          // project soft-delete state — flips the fingerprint when the project is soft-deleted
          db.ref("deleteAfter").withSchema(TableName.Project).as("pDel"),
          db.ref("id").withSchema(TableName.MembershipRole).as("rId"),
          db.ref("updatedAt").withSchema(TableName.MembershipRole).as("rUp"),
          db.ref("updatedAt").withSchema(TableName.Role).as("crUp"),
          db.ref("id").withSchema(TableName.AdditionalPrivilege).as("pId"),
          db.ref("updatedAt").withSchema(TableName.AdditionalPrivilege).as("pUp"),
          db.ref("id").withSchema(TableName.IdentityMetadata).as("imId"),
          db.ref("updatedAt").withSchema(TableName.IdentityMetadata).as("imUp"),
          db.raw(
            `CASE WHEN "${TableName.MembershipRole}"."isTemporary" AND NOW() >= "${TableName.MembershipRole}"."temporaryAccessEndTime" THEN true ELSE false END AS "rExp"`
          ),
          db.raw(
            `CASE WHEN "${TableName.AdditionalPrivilege}"."isTemporary" AND NOW() >= "${TableName.AdditionalPrivilege}"."temporaryAccessEndTime" THEN true ELSE false END AS "pExp"`
          )
        )
        // deterministic row order — Postgres doesn't guarantee ordering without it, and an unstable
        // order would flip the hashed fingerprint between calls, silently breaking ETag/cache hits.
        .orderBy([
          { column: `${TableName.Membership}.id` },
          { column: `${TableName.MembershipRole}.id` },
          { column: `${TableName.AdditionalPrivilege}.id` },
          { column: `${TableName.IdentityMetadata}.id` }
        ]);

      return generateCacheKeyFromData(rows);
    } catch (error) {
      throw new DatabaseError({ error, name: "GetPermissionFingerprint" });
    }
  };

  return {
    getProjectUserPermissions,
    getProjectIdentityPermissions,
    getProjectGroupPermissions,
    getPermission,
    getResourceMembership,
    getPermissionFingerprint
  };
};