@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.cjs CHANGED
@@ -1336,11 +1336,14 @@ var PolicyHandler = class extends kysely.OperationNodeTransformer {
1336
1336
  async enforcePreCreatePolicy(node, mutationModel, isManyToManyJoinTable, proceed) {
1337
1337
  const fields = node.columns?.map((c) => c.column.name) ?? [];
1338
1338
  const valueRows = node.values ? this.unwrapCreateValueRows(node.values, mutationModel, fields, isManyToManyJoinTable) : [[]];
1339
- if (!isManyToManyJoinTable) {
1340
- if (isTrueNode(this.buildPolicyFilter(mutationModel, void 0, "create"))) return;
1339
+ if (isManyToManyJoinTable) {
1340
+ for (const values of valueRows) await this.enforcePreCreatePolicyForManyToManyJoinTable(mutationModel, fields, values.map((v) => v.node), proceed);
1341
+ return;
1341
1342
  }
1342
- for (const values of valueRows) if (isManyToManyJoinTable) await this.enforcePreCreatePolicyForManyToManyJoinTable(mutationModel, fields, values.map((v) => v.node), proceed);
1343
- else await this.enforcePreCreatePolicyForOne(mutationModel, fields, values.map((v) => v.node), proceed);
1343
+ const filter = this.buildPolicyFilter(mutationModel, void 0, "create");
1344
+ if (isTrueNode(filter)) return;
1345
+ if (isFalseNode(filter) && valueRows.length > 0) throw createRejectedByPolicyError(mutationModel, _zenstackhq_orm.RejectedByPolicyReason.NO_ACCESS);
1346
+ await this.enforcePreCreatePolicyForRows(mutationModel, fields, valueRows.map((values) => values.map((v) => v.node)), filter, proceed);
1344
1347
  }
1345
1348
  async enforcePreCreatePolicyForManyToManyJoinTable(tableName, fields, values, proceed) {
1346
1349
  const m2m = this.resolveManyToManyJoinTable(tableName);
@@ -1365,21 +1368,12 @@ var PolicyHandler = class extends kysely.OperationNodeTransformer {
1365
1368
  if (!result.rows[0]?.$conditionA) throw createRejectedByPolicyError(m2m.firstModel, _zenstackhq_orm.RejectedByPolicyReason.NO_ACCESS, `many-to-many relation participant model "${m2m.firstModel}" not updatable`);
1366
1369
  if (!result.rows[0]?.$conditionB) throw createRejectedByPolicyError(m2m.secondModel, _zenstackhq_orm.RejectedByPolicyReason.NO_ACCESS, `many-to-many relation participant model "${m2m.secondModel}" not updatable`);
1367
1370
  }
1368
- async enforcePreCreatePolicyForOne(model, fields, values, proceed) {
1371
+ async enforcePreCreatePolicyForRows(model, fields, rows, filter, proceed) {
1369
1372
  const allFields = _zenstackhq_orm.QueryUtils.getModelFields(this.client.$schema, model, { inherited: true });
1370
- const allValues = [];
1371
- for (const def of allFields) {
1372
- const index = fields.indexOf(def.name);
1373
- if (index >= 0) allValues.push(new kysely.ExpressionWrapper(values[index]));
1374
- else allValues.push(this.eb.lit(null));
1375
- }
1376
- const valuesTable = this.dialect.buildValuesTableSelect(allFields, [allValues]);
1377
- const filter = this.buildPolicyFilter(model, void 0, "create");
1378
- const preCreateInner = this.eb.selectFrom(valuesTable.as(model)).select(this.eb.lit(1).as("_")).where(() => new kysely.ExpressionWrapper(filter));
1379
- if (!(await proceed({
1380
- kind: "SelectQueryNode",
1381
- selections: [kysely.SelectionNode.create(kysely.AliasNode.create(this.eb.exists(preCreateInner).toOperationNode(), kysely.IdentifierNode.create("$condition")))]
1382
- })).rows[0]?.$condition) throw createRejectedByPolicyError(model, _zenstackhq_orm.RejectedByPolicyReason.NO_ACCESS);
1373
+ const fieldIndices = allFields.map((def) => fields.indexOf(def.name));
1374
+ const allRows = rows.map((values) => fieldIndices.map((index) => index >= 0 ? new kysely.ExpressionWrapper(values[index]) : this.eb.lit(null)));
1375
+ const valuesTable = this.dialect.buildValuesTableSelect(allFields, allRows);
1376
+ 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 kysely.ExpressionWrapper(filter)).toOperationNode())).rows[0]?.$condition) throw createRejectedByPolicyError(model, _zenstackhq_orm.RejectedByPolicyReason.NO_ACCESS);
1383
1377
  }
1384
1378
  unwrapCreateValueRows(node, model, fields, isManyToManyJoinTable) {
1385
1379
  if (kysely.ValuesNode.is(node)) return node.values.map((v) => this.unwrapCreateValueRow(v.values, model, fields, isManyToManyJoinTable, kysely.ValueListNode.is(v)));
@@ -1395,7 +1389,7 @@ var PolicyHandler = class extends kysely.OperationNodeTransformer {
1395
1389
  const itemNode = item;
1396
1390
  if (kysely.DefaultInsertValueNode.is(itemNode)) {
1397
1391
  result.push({
1398
- node: kysely.ValueNode.create(null),
1392
+ node: kysely.ValueNode.createImmediate(null),
1399
1393
  raw: null
1400
1394
  });
1401
1395
  continue;
@@ -1412,13 +1406,7 @@ var PolicyHandler = class extends kysely.OperationNodeTransformer {
1412
1406
  const fieldDef = _zenstackhq_orm.QueryUtils.requireField(this.client.$schema, model, fields[i]);
1413
1407
  value = this.dialect.transformInput(item, fieldDef.type, !!fieldDef.array);
1414
1408
  }
1415
- if (Array.isArray(value)) {
1416
- const fieldDef = _zenstackhq_orm.QueryUtils.requireField(this.client.$schema, model, fields[i]);
1417
- result.push({
1418
- node: this.dialect.buildArrayValue(value, fieldDef.type).toOperationNode(),
1419
- raw: value
1420
- });
1421
- } else result.push({
1409
+ result.push({
1422
1410
  node: kysely.ValueNode.create(value),
1423
1411
  raw: value
1424
1412
  });