@zenstackhq/plugin-policy 3.5.2 → 3.5.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
@@ -78,6 +78,10 @@ var ExpressionEvaluator = class {
78
78
  evaluateCall(expr2, context) {
79
79
  if (expr2.function === "auth") {
80
80
  return context.auth;
81
+ } else if (expr2.function === "currentModel") {
82
+ return context.thisType;
83
+ } else if (expr2.function === "currentOperation") {
84
+ return context.operation;
81
85
  } else {
82
86
  throw new Error(`Unsupported call expression function: ${expr2.function}`);
83
87
  }
@@ -421,7 +425,10 @@ var ExpressionTransformer = class {
421
425
  } else if (this.isNullNode(left)) {
422
426
  return this.transformNullCheck(right, expr2.op);
423
427
  } else {
424
- return import_kysely2.BinaryOperationNode.create(left, this.transformOperator(op), right);
428
+ const leftFieldDef = this.getFieldDefFromFieldRef(normalizedLeft, context);
429
+ const rightFieldDef = this.getFieldDefFromFieldRef(normalizedRight, context);
430
+ const sqlOp = op === "==" ? "=" : op;
431
+ return this.dialect.buildComparison(new import_kysely2.ExpressionWrapper(left), leftFieldDef, sqlOp, new import_kysely2.ExpressionWrapper(right), rightFieldDef).toOperationNode();
425
432
  }
426
433
  }
427
434
  transformNullCheck(expr2, operator) {
@@ -447,17 +454,17 @@ var ExpressionTransformer = class {
447
454
  };
448
455
  }
449
456
  let normalizedLeft = expr2.left;
450
- if (this.isRelationField(expr2.left, context.modelOrType)) {
457
+ if (this.isRelationField(expr2.left, context)) {
451
458
  (0, import_common_helpers2.invariant)(import_schema3.ExpressionUtils.isNull(expr2.right), "only null comparison is supported for relation field");
452
- const leftRelDef = this.getFieldDefFromFieldRef(expr2.left, context.modelOrType);
459
+ const leftRelDef = this.getFieldDefFromFieldRef(expr2.left, context);
453
460
  (0, import_common_helpers2.invariant)(leftRelDef, "failed to get relation field definition");
454
461
  const idFields = import_orm3.QueryUtils.requireIdFields(this.schema, leftRelDef.type);
455
462
  normalizedLeft = this.makeOrAppendMember(normalizedLeft, idFields[0]);
456
463
  }
457
464
  let normalizedRight = expr2.right;
458
- if (this.isRelationField(expr2.right, context.modelOrType)) {
465
+ if (this.isRelationField(expr2.right, context)) {
459
466
  (0, import_common_helpers2.invariant)(import_schema3.ExpressionUtils.isNull(expr2.left), "only null comparison is supported for relation field");
460
- const rightRelDef = this.getFieldDefFromFieldRef(expr2.right, context.modelOrType);
467
+ const rightRelDef = this.getFieldDefFromFieldRef(expr2.right, context);
461
468
  (0, import_common_helpers2.invariant)(rightRelDef, "failed to get relation field definition");
462
469
  const idFields = import_orm3.QueryUtils.requireIdFields(this.schema, rightRelDef.type);
463
470
  normalizedRight = this.makeOrAppendMember(normalizedRight, idFields[0]);
@@ -475,7 +482,9 @@ var ExpressionTransformer = class {
475
482
  const receiver = evaluator.evaluate(expr2.left, {
476
483
  thisValue: context.contextValue,
477
484
  auth: this.auth,
478
- bindingScope: this.getEvaluationBindingScope(context.bindingScope)
485
+ bindingScope: this.getEvaluationBindingScope(context.bindingScope),
486
+ operation: context.operation,
487
+ thisType: context.thisType
479
488
  });
480
489
  const baseType = this.isAuthMember(expr2.left) ? this.authType : context.modelOrType;
481
490
  const memberType = this.getMemberType(baseType, expr2.left);
@@ -486,7 +495,7 @@ var ExpressionTransformer = class {
486
495
  }
487
496
  (0, import_common_helpers2.invariant)(import_schema3.ExpressionUtils.isField(expr2.left) || import_schema3.ExpressionUtils.isMember(expr2.left), "left operand must be field or member access");
488
497
  let newContextModel;
489
- const fieldDef = this.getFieldDefFromFieldRef(expr2.left, context.modelOrType);
498
+ const fieldDef = this.getFieldDefFromFieldRef(expr2.left, context);
490
499
  if (fieldDef) {
491
500
  (0, import_common_helpers2.invariant)(fieldDef.relation, `field is not a relation: ${JSON.stringify(expr2.left)}`);
492
501
  newContextModel = fieldDef.type;
@@ -543,7 +552,9 @@ var ExpressionTransformer = class {
543
552
  const value = new ExpressionEvaluator().evaluate(expr2, {
544
553
  auth: this.auth,
545
554
  thisValue: context.contextValue,
546
- bindingScope: this.getEvaluationBindingScope(context.bindingScope)
555
+ bindingScope: this.getEvaluationBindingScope(context.bindingScope),
556
+ operation: context.operation,
557
+ thisType: context.thisType
547
558
  });
548
559
  return this.transformValue(value, "Boolean");
549
560
  } else {
@@ -646,10 +657,6 @@ var ExpressionTransformer = class {
646
657
  (0, import_common_helpers2.invariant)(expr2.op === "!", 'only "!" operator is supported');
647
658
  return logicalNot(this.dialect, this.transform(expr2.operand, context));
648
659
  }
649
- transformOperator(op) {
650
- const mappedOp = (0, import_ts_pattern2.match)(op).with("==", () => "=").otherwise(() => op);
651
- return import_kysely2.OperatorNode.create(mappedOp);
652
- }
653
660
  _call(expr2, context) {
654
661
  const result = this.transformCall(expr2, context);
655
662
  return result.toOperationNode();
@@ -920,15 +927,26 @@ var ExpressionTransformer = class {
920
927
  return conditions.reduce((acc, condition) => import_schema3.ExpressionUtils.binary(acc, "&&", condition));
921
928
  }
922
929
  }
923
- isRelationField(expr2, model) {
924
- const fieldDef = this.getFieldDefFromFieldRef(expr2, model);
930
+ isRelationField(expr2, context) {
931
+ const fieldDef = this.getFieldDefFromFieldRef(expr2, context);
925
932
  return !!fieldDef?.relation;
926
933
  }
927
- getFieldDefFromFieldRef(expr2, model) {
934
+ getFieldDefFromFieldRef(expr2, context) {
935
+ const model = import_schema3.ExpressionUtils.isMember(expr2) && import_schema3.ExpressionUtils.isThis(expr2.receiver) ? context.thisType : context.modelOrType;
928
936
  if (import_schema3.ExpressionUtils.isField(expr2)) {
929
937
  return import_orm3.QueryUtils.getField(this.schema, model, expr2.field);
930
938
  } else if (import_schema3.ExpressionUtils.isMember(expr2) && expr2.members.length === 1 && import_schema3.ExpressionUtils.isThis(expr2.receiver)) {
931
939
  return import_orm3.QueryUtils.getField(this.schema, model, expr2.members[0]);
940
+ } else if (import_schema3.ExpressionUtils.isMember(expr2) && import_schema3.ExpressionUtils.isField(expr2.receiver)) {
941
+ const receiverDef = import_orm3.QueryUtils.getField(this.schema, model, expr2.receiver.field);
942
+ if (!receiverDef?.relation) return void 0;
943
+ let currModel = receiverDef.type;
944
+ for (let i = 0; i < expr2.members.length - 1; i++) {
945
+ const hopDef = import_orm3.QueryUtils.getField(this.schema, currModel, expr2.members[i]);
946
+ if (!hopDef?.relation) return void 0;
947
+ currModel = hopDef.type;
948
+ }
949
+ return import_orm3.QueryUtils.getField(this.schema, currModel, expr2.members[expr2.members.length - 1]);
932
950
  } else {
933
951
  return void 0;
934
952
  }