Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
1 line
4.3 KiB
Plaintext
1 line
4.3 KiB
Plaintext
{"version":3,"sources":["../../src/query-presets/preventLockout.ts"],"sourcesContent":["import type { Validate } from '../fields/config/types.js'\n\nimport { APIError } from '../errors/APIError.js'\nimport { createLocalReq } from '../utilities/createLocalReq.js'\nimport { initTransaction } from '../utilities/initTransaction.js'\nimport { killTransaction } from '../utilities/killTransaction.js'\nimport { queryPresetsCollectionSlug } from './config.js'\n\n/**\n * Prevents \"accidental lockouts\" where a user makes an update that removes their own access to the preset.\n * This is effectively an access control function proxied through a `validate` function.\n * How it works:\n * 1. Creates a temporary record with the incoming data\n * 2. Attempts to read and update that record with the incoming user\n * 3. If either of those fail, throws an error to the user\n * 4. Once finished, prevents the temp record from persisting to the database\n */\nexport const preventLockout: Validate = async (\n value,\n { data, overrideAccess, req: incomingReq },\n) => {\n // Use context to ensure an infinite loop doesn't occur\n if (!incomingReq.context._preventLockout && !overrideAccess) {\n const req = await createLocalReq(\n {\n context: {\n _preventLockout: true,\n },\n req: {\n user: incomingReq.user,\n },\n },\n incomingReq.payload,\n )\n\n // Might be `null` if no transactions are enabled\n const transaction = await initTransaction(req)\n\n // create a temp record to validate the constraints, using the req\n const tempPreset = await req.payload.create({\n collection: queryPresetsCollectionSlug,\n data: {\n ...data,\n isTemp: true,\n },\n req,\n })\n\n let canUpdate = false\n let canRead = false\n\n try {\n await req.payload.findByID({\n id: tempPreset.id,\n collection: queryPresetsCollectionSlug,\n overrideAccess: false,\n req,\n user: req.user,\n })\n\n canRead = true\n\n await req.payload.update({\n id: tempPreset.id,\n collection: queryPresetsCollectionSlug,\n data: tempPreset,\n overrideAccess: false,\n req,\n user: req.user,\n })\n\n canUpdate = true\n } catch (_err) {\n if (!canRead || !canUpdate) {\n throw new APIError('This action will lock you out of this preset.', 403, {}, true)\n }\n } finally {\n if (transaction) {\n await killTransaction(req)\n } else {\n // delete the temp record\n await req.payload.delete({\n id: tempPreset.id,\n collection: queryPresetsCollectionSlug,\n req,\n })\n }\n }\n }\n\n return true as unknown as true\n}\n"],"names":["APIError","createLocalReq","initTransaction","killTransaction","queryPresetsCollectionSlug","preventLockout","value","data","overrideAccess","req","incomingReq","context","_preventLockout","user","payload","transaction","tempPreset","create","collection","isTemp","canUpdate","canRead","findByID","id","update","_err","delete"],"mappings":"AAEA,SAASA,QAAQ,QAAQ,wBAAuB;AAChD,SAASC,cAAc,QAAQ,iCAAgC;AAC/D,SAASC,eAAe,QAAQ,kCAAiC;AACjE,SAASC,eAAe,QAAQ,kCAAiC;AACjE,SAASC,0BAA0B,QAAQ,cAAa;AAExD;;;;;;;;CAQC,GACD,OAAO,MAAMC,iBAA2B,OACtCC,OACA,EAAEC,IAAI,EAAEC,cAAc,EAAEC,KAAKC,WAAW,EAAE;IAE1C,uDAAuD;IACvD,IAAI,CAACA,YAAYC,OAAO,CAACC,eAAe,IAAI,CAACJ,gBAAgB;QAC3D,MAAMC,MAAM,MAAMR,eAChB;YACEU,SAAS;gBACPC,iBAAiB;YACnB;YACAH,KAAK;gBACHI,MAAMH,YAAYG,IAAI;YACxB;QACF,GACAH,YAAYI,OAAO;QAGrB,iDAAiD;QACjD,MAAMC,cAAc,MAAMb,gBAAgBO;QAE1C,kEAAkE;QAClE,MAAMO,aAAa,MAAMP,IAAIK,OAAO,CAACG,MAAM,CAAC;YAC1CC,YAAYd;YACZG,MAAM;gBACJ,GAAGA,IAAI;gBACPY,QAAQ;YACV;YACAV;QACF;QAEA,IAAIW,YAAY;QAChB,IAAIC,UAAU;QAEd,IAAI;YACF,MAAMZ,IAAIK,OAAO,CAACQ,QAAQ,CAAC;gBACzB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