UNPKG

@lucidcms/core

Version:

The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.

1 lines 2.55 kB
{"version":3,"file":"check-document-access.mjs","names":[],"sources":["../../../../src/services/documents/checks/check-document-access.ts"],"sourcesContent":["import { getTableNames } from \"../../../libs/collection/schema/runtime/runtime-schema-selectors.js\";\nimport { copy } from \"../../../libs/i18n/index.js\";\nimport { DocumentsRepository } from \"../../../libs/repositories/index.js\";\nimport type { ServiceFn } from \"../../../utils/services/types.js\";\n\n/**\n * Confirms documents are visible in the current tenant scope before writes.\n * Global documents remain valid for tenant requests, matching read behaviour.\n */\nconst checkDocumentAccess: ServiceFn<\n\t[\n\t\t{\n\t\t\tcollectionKey: string;\n\t\t\tid?: number;\n\t\t\tids?: number[];\n\t\t},\n\t],\n\tundefined\n> = async (context, data) => {\n\tconst ids = Array.from(\n\t\tnew Set(data.ids ?? (data.id !== undefined ? [data.id] : [])),\n\t);\n\n\tif (ids.length === 0) {\n\t\treturn {\n\t\t\terror: undefined,\n\t\t\tdata: undefined,\n\t\t};\n\t}\n\n\tconst tableNamesRes = await getTableNames(context, data.collectionKey);\n\tif (tableNamesRes.error) return tableNamesRes;\n\n\tconst Documents = new DocumentsRepository(\n\t\tcontext.db.client,\n\t\tcontext.config.db,\n\t);\n\n\tconst documentsRes = await Documents.selectMultipleValidationIds(\n\t\t{\n\t\t\tids,\n\t\t\ttenantKey: context.request.tenantKey,\n\t\t\tvalidation: {\n\t\t\t\tenabled: true,\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\ttableName: tableNamesRes.data.document,\n\t\t},\n\t);\n\tif (documentsRes.error) return documentsRes;\n\n\tif (documentsRes.data.length !== ids.length) {\n\t\treturn {\n\t\t\terror: {\n\t\t\t\ttype: \"basic\",\n\t\t\t\tmessage: copy(\"server:core.documents.not.found.message\"),\n\t\t\t\tstatus: 404,\n\t\t\t},\n\t\t\tdata: undefined,\n\t\t};\n\t}\n\n\treturn {\n\t\terror: undefined,\n\t\tdata: undefined,\n\t};\n};\n\nexport default checkDocumentAccess;\n"],"mappings":"gNASA,MAAM,EASF,MAAO,EAAS,IAAS,CAC5B,IAAM,EAAM,MAAM,KACjB,IAAI,IAAI,EAAK,MAAQ,EAAK,KAAO,IAAA,GAAwB,CAAC,EAAb,CAAC,EAAK,EAAE,EAAO,CAC7D,EAEA,GAAI,EAAI,SAAW,EAClB,MAAO,CACN,MAAO,IAAA,GACP,KAAM,IAAA,EACP,EAGD,IAAM,EAAgB,MAAM,EAAc,EAAS,EAAK,aAAa,EACrE,GAAI,EAAc,MAAO,OAAO,EAOhC,IAAM,EAAe,MAAM,IALL,EACrB,EAAQ,GAAG,OACX,EAAQ,OAAO,EAGmB,CAAC,CAAC,4BACpC,CACC,MACA,UAAW,EAAQ,QAAQ,UAC3B,WAAY,CACX,QAAS,EACV,CACD,EACA,CACC,UAAW,EAAc,KAAK,QAC/B,CACD,EAcA,OAbI,EAAa,MAAc,EAE3B,EAAa,KAAK,SAAW,EAAI,OAW9B,CACN,MAAO,IAAA,GACP,KAAM,IAAA,EACP,EAbQ,CACN,MAAO,CACN,KAAM,QACN,QAAS,EAAK,yCAAyC,EACvD,OAAQ,GACT,EACA,KAAM,IAAA,EACP,CAOF"}