@zenstackhq/plugin-policy 3.5.2 → 3.5.3
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 +33 -15
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +33 -15
- package/dist/index.js.map +1 -1
- package/package.json +19 -9
package/dist/index.js
CHANGED
|
@@ -54,6 +54,10 @@ var ExpressionEvaluator = class {
|
|
|
54
54
|
evaluateCall(expr2, context) {
|
|
55
55
|
if (expr2.function === "auth") {
|
|
56
56
|
return context.auth;
|
|
57
|
+
} else if (expr2.function === "currentModel") {
|
|
58
|
+
return context.thisType;
|
|
59
|
+
} else if (expr2.function === "currentOperation") {
|
|
60
|
+
return context.operation;
|
|
57
61
|
} else {
|
|
58
62
|
throw new Error(`Unsupported call expression function: ${expr2.function}`);
|
|
59
63
|
}
|
|
@@ -397,7 +401,10 @@ var ExpressionTransformer = class {
|
|
|
397
401
|
} else if (this.isNullNode(left)) {
|
|
398
402
|
return this.transformNullCheck(right, expr2.op);
|
|
399
403
|
} else {
|
|
400
|
-
|
|
404
|
+
const leftFieldDef = this.getFieldDefFromFieldRef(normalizedLeft, context);
|
|
405
|
+
const rightFieldDef = this.getFieldDefFromFieldRef(normalizedRight, context);
|
|
406
|
+
const sqlOp = op === "==" ? "=" : op;
|
|
407
|
+
return this.dialect.buildComparison(new ExpressionWrapper(left), leftFieldDef, sqlOp, new ExpressionWrapper(right), rightFieldDef).toOperationNode();
|
|
401
408
|
}
|
|
402
409
|
}
|
|
403
410
|
transformNullCheck(expr2, operator) {
|
|
@@ -423,17 +430,17 @@ var ExpressionTransformer = class {
|
|
|
423
430
|
};
|
|
424
431
|
}
|
|
425
432
|
let normalizedLeft = expr2.left;
|
|
426
|
-
if (this.isRelationField(expr2.left, context
|
|
433
|
+
if (this.isRelationField(expr2.left, context)) {
|
|
427
434
|
invariant2(ExpressionUtils3.isNull(expr2.right), "only null comparison is supported for relation field");
|
|
428
|
-
const leftRelDef = this.getFieldDefFromFieldRef(expr2.left, context
|
|
435
|
+
const leftRelDef = this.getFieldDefFromFieldRef(expr2.left, context);
|
|
429
436
|
invariant2(leftRelDef, "failed to get relation field definition");
|
|
430
437
|
const idFields = QueryUtils.requireIdFields(this.schema, leftRelDef.type);
|
|
431
438
|
normalizedLeft = this.makeOrAppendMember(normalizedLeft, idFields[0]);
|
|
432
439
|
}
|
|
433
440
|
let normalizedRight = expr2.right;
|
|
434
|
-
if (this.isRelationField(expr2.right, context
|
|
441
|
+
if (this.isRelationField(expr2.right, context)) {
|
|
435
442
|
invariant2(ExpressionUtils3.isNull(expr2.left), "only null comparison is supported for relation field");
|
|
436
|
-
const rightRelDef = this.getFieldDefFromFieldRef(expr2.right, context
|
|
443
|
+
const rightRelDef = this.getFieldDefFromFieldRef(expr2.right, context);
|
|
437
444
|
invariant2(rightRelDef, "failed to get relation field definition");
|
|
438
445
|
const idFields = QueryUtils.requireIdFields(this.schema, rightRelDef.type);
|
|
439
446
|
normalizedRight = this.makeOrAppendMember(normalizedRight, idFields[0]);
|
|
@@ -451,7 +458,9 @@ var ExpressionTransformer = class {
|
|
|
451
458
|
const receiver = evaluator.evaluate(expr2.left, {
|
|
452
459
|
thisValue: context.contextValue,
|
|
453
460
|
auth: this.auth,
|
|
454
|
-
bindingScope: this.getEvaluationBindingScope(context.bindingScope)
|
|
461
|
+
bindingScope: this.getEvaluationBindingScope(context.bindingScope),
|
|
462
|
+
operation: context.operation,
|
|
463
|
+
thisType: context.thisType
|
|
455
464
|
});
|
|
456
465
|
const baseType = this.isAuthMember(expr2.left) ? this.authType : context.modelOrType;
|
|
457
466
|
const memberType = this.getMemberType(baseType, expr2.left);
|
|
@@ -462,7 +471,7 @@ var ExpressionTransformer = class {
|
|
|
462
471
|
}
|
|
463
472
|
invariant2(ExpressionUtils3.isField(expr2.left) || ExpressionUtils3.isMember(expr2.left), "left operand must be field or member access");
|
|
464
473
|
let newContextModel;
|
|
465
|
-
const fieldDef = this.getFieldDefFromFieldRef(expr2.left, context
|
|
474
|
+
const fieldDef = this.getFieldDefFromFieldRef(expr2.left, context);
|
|
466
475
|
if (fieldDef) {
|
|
467
476
|
invariant2(fieldDef.relation, `field is not a relation: ${JSON.stringify(expr2.left)}`);
|
|
468
477
|
newContextModel = fieldDef.type;
|
|
@@ -519,7 +528,9 @@ var ExpressionTransformer = class {
|
|
|
519
528
|
const value = new ExpressionEvaluator().evaluate(expr2, {
|
|
520
529
|
auth: this.auth,
|
|
521
530
|
thisValue: context.contextValue,
|
|
522
|
-
bindingScope: this.getEvaluationBindingScope(context.bindingScope)
|
|
531
|
+
bindingScope: this.getEvaluationBindingScope(context.bindingScope),
|
|
532
|
+
operation: context.operation,
|
|
533
|
+
thisType: context.thisType
|
|
523
534
|
});
|
|
524
535
|
return this.transformValue(value, "Boolean");
|
|
525
536
|
} else {
|
|
@@ -622,10 +633,6 @@ var ExpressionTransformer = class {
|
|
|
622
633
|
invariant2(expr2.op === "!", 'only "!" operator is supported');
|
|
623
634
|
return logicalNot(this.dialect, this.transform(expr2.operand, context));
|
|
624
635
|
}
|
|
625
|
-
transformOperator(op) {
|
|
626
|
-
const mappedOp = match2(op).with("==", () => "=").otherwise(() => op);
|
|
627
|
-
return OperatorNode2.create(mappedOp);
|
|
628
|
-
}
|
|
629
636
|
_call(expr2, context) {
|
|
630
637
|
const result = this.transformCall(expr2, context);
|
|
631
638
|
return result.toOperationNode();
|
|
@@ -896,15 +903,26 @@ var ExpressionTransformer = class {
|
|
|
896
903
|
return conditions.reduce((acc, condition) => ExpressionUtils3.binary(acc, "&&", condition));
|
|
897
904
|
}
|
|
898
905
|
}
|
|
899
|
-
isRelationField(expr2,
|
|
900
|
-
const fieldDef = this.getFieldDefFromFieldRef(expr2,
|
|
906
|
+
isRelationField(expr2, context) {
|
|
907
|
+
const fieldDef = this.getFieldDefFromFieldRef(expr2, context);
|
|
901
908
|
return !!fieldDef?.relation;
|
|
902
909
|
}
|
|
903
|
-
getFieldDefFromFieldRef(expr2,
|
|
910
|
+
getFieldDefFromFieldRef(expr2, context) {
|
|
911
|
+
const model = ExpressionUtils3.isMember(expr2) && ExpressionUtils3.isThis(expr2.receiver) ? context.thisType : context.modelOrType;
|
|
904
912
|
if (ExpressionUtils3.isField(expr2)) {
|
|
905
913
|
return QueryUtils.getField(this.schema, model, expr2.field);
|
|
906
914
|
} else if (ExpressionUtils3.isMember(expr2) && expr2.members.length === 1 && ExpressionUtils3.isThis(expr2.receiver)) {
|
|
907
915
|
return QueryUtils.getField(this.schema, model, expr2.members[0]);
|
|
916
|
+
} else if (ExpressionUtils3.isMember(expr2) && ExpressionUtils3.isField(expr2.receiver)) {
|
|
917
|
+
const receiverDef = QueryUtils.getField(this.schema, model, expr2.receiver.field);
|
|
918
|
+
if (!receiverDef?.relation) return void 0;
|
|
919
|
+
let currModel = receiverDef.type;
|
|
920
|
+
for (let i = 0; i < expr2.members.length - 1; i++) {
|
|
921
|
+
const hopDef = QueryUtils.getField(this.schema, currModel, expr2.members[i]);
|
|
922
|
+
if (!hopDef?.relation) return void 0;
|
|
923
|
+
currModel = hopDef.type;
|
|
924
|
+
}
|
|
925
|
+
return QueryUtils.getField(this.schema, currModel, expr2.members[expr2.members.length - 1]);
|
|
908
926
|
} else {
|
|
909
927
|
return void 0;
|
|
910
928
|
}
|