@zenstackhq/plugin-policy 3.10.2 → 3.10.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1335,11 +1335,14 @@ var PolicyHandler = class extends OperationNodeTransformer {
1335
1335
  async enforcePreCreatePolicy(node, mutationModel, isManyToManyJoinTable, proceed) {
1336
1336
  const fields = node.columns?.map((c) => c.column.name) ?? [];
1337
1337
  const valueRows = node.values ? this.unwrapCreateValueRows(node.values, mutationModel, fields, isManyToManyJoinTable) : [[]];
1338
- if (!isManyToManyJoinTable) {
1339
- if (isTrueNode(this.buildPolicyFilter(mutationModel, void 0, "create"))) return;
1338
+ if (isManyToManyJoinTable) {
1339
+ for (const values of valueRows) await this.enforcePreCreatePolicyForManyToManyJoinTable(mutationModel, fields, values.map((v) => v.node), proceed);
1340
+ return;
1340
1341
  }
1341
- for (const values of valueRows) if (isManyToManyJoinTable) await this.enforcePreCreatePolicyForManyToManyJoinTable(mutationModel, fields, values.map((v) => v.node), proceed);
1342
- else await this.enforcePreCreatePolicyForOne(mutationModel, fields, values.map((v) => v.node), proceed);
1342
+ const filter = this.buildPolicyFilter(mutationModel, void 0, "create");
1343
+ if (isTrueNode(filter)) return;
1344
+ if (isFalseNode(filter) && valueRows.length > 0) throw createRejectedByPolicyError(mutationModel, RejectedByPolicyReason.NO_ACCESS);
1345
+ await this.enforcePreCreatePolicyForRows(mutationModel, fields, valueRows.map((values) => values.map((v) => v.node)), filter, proceed);
1343
1346
  }
1344
1347
  async enforcePreCreatePolicyForManyToManyJoinTable(tableName, fields, values, proceed) {
1345
1348
  const m2m = this.resolveManyToManyJoinTable(tableName);
@@ -1364,21 +1367,12 @@ var PolicyHandler = class extends OperationNodeTransformer {
1364
1367
  if (!result.rows[0]?.$conditionA) throw createRejectedByPolicyError(m2m.firstModel, RejectedByPolicyReason.NO_ACCESS, `many-to-many relation participant model "${m2m.firstModel}" not updatable`);
1365
1368
  if (!result.rows[0]?.$conditionB) throw createRejectedByPolicyError(m2m.secondModel, RejectedByPolicyReason.NO_ACCESS, `many-to-many relation participant model "${m2m.secondModel}" not updatable`);
1366
1369
  }
1367
- async enforcePreCreatePolicyForOne(model, fields, values, proceed) {
1370
+ async enforcePreCreatePolicyForRows(model, fields, rows, filter, proceed) {
1368
1371
  const allFields = QueryUtils.getModelFields(this.client.$schema, model, { inherited: true });
1369
- const allValues = [];
1370
- for (const def of allFields) {
1371
- const index = fields.indexOf(def.name);
1372
- if (index >= 0) allValues.push(new ExpressionWrapper(values[index]));
1373
- else allValues.push(this.eb.lit(null));
1374
- }
1375
- const valuesTable = this.dialect.buildValuesTableSelect(allFields, [allValues]);
1376
- const filter = this.buildPolicyFilter(model, void 0, "create");
1377
- const preCreateInner = this.eb.selectFrom(valuesTable.as(model)).select(this.eb.lit(1).as("_")).where(() => new ExpressionWrapper(filter));
1378
- if (!(await proceed({
1379
- kind: "SelectQueryNode",
1380
- selections: [SelectionNode.create(AliasNode.create(this.eb.exists(preCreateInner).toOperationNode(), IdentifierNode.create("$condition")))]
1381
- })).rows[0]?.$condition) throw createRejectedByPolicyError(model, RejectedByPolicyReason.NO_ACCESS);
1372
+ const fieldIndices = allFields.map((def) => fields.indexOf(def.name));
1373
+ const allRows = rows.map((values) => fieldIndices.map((index) => index >= 0 ? new ExpressionWrapper(values[index]) : this.eb.lit(null)));
1374
+ const valuesTable = this.dialect.buildValuesTableSelect(allFields, allRows);
1375
+ if (!(await proceed(this.eb.selectFrom(valuesTable.as(model)).select(this.eb(this.eb.fn("COUNT", [this.eb.lit(1)]), "=", this.eb.lit(rows.length)).as("$condition")).where(() => new ExpressionWrapper(filter)).toOperationNode())).rows[0]?.$condition) throw createRejectedByPolicyError(model, RejectedByPolicyReason.NO_ACCESS);
1382
1376
  }
1383
1377
  unwrapCreateValueRows(node, model, fields, isManyToManyJoinTable) {
1384
1378
  if (ValuesNode.is(node)) return node.values.map((v) => this.unwrapCreateValueRow(v.values, model, fields, isManyToManyJoinTable, ValueListNode.is(v)));
@@ -1394,7 +1388,7 @@ var PolicyHandler = class extends OperationNodeTransformer {
1394
1388
  const itemNode = item;
1395
1389
  if (DefaultInsertValueNode.is(itemNode)) {
1396
1390
  result.push({
1397
- node: ValueNode.create(null),
1391
+ node: ValueNode.createImmediate(null),
1398
1392
  raw: null
1399
1393
  });
1400
1394
  continue;
@@ -1411,13 +1405,7 @@ var PolicyHandler = class extends OperationNodeTransformer {
1411
1405
  const fieldDef = QueryUtils.requireField(this.client.$schema, model, fields[i]);
1412
1406
  value = this.dialect.transformInput(item, fieldDef.type, !!fieldDef.array);
1413
1407
  }
1414
- if (Array.isArray(value)) {
1415
- const fieldDef = QueryUtils.requireField(this.client.$schema, model, fields[i]);
1416
- result.push({
1417
- node: this.dialect.buildArrayValue(value, fieldDef.type).toOperationNode(),
1418
- raw: value
1419
- });
1420
- } else result.push({
1408
+ result.push({
1421
1409
  node: ValueNode.create(value),
1422
1410
  raw: value
1423
1411
  });