@zenstackhq/plugin-policy 3.9.8 → 3.10.0
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 +75 -28
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +75 -28
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -7
package/dist/index.mjs
CHANGED
|
@@ -287,25 +287,34 @@ var ExpressionTransformer = class {
|
|
|
287
287
|
if (op === "?" || op === "!" || op === "^") return this.transformCollectionPredicate(expr, context);
|
|
288
288
|
const { normalizedLeft, normalizedRight } = this.normalizeBinaryOperationOperands(expr, context);
|
|
289
289
|
const left = this.transform(normalizedLeft, context);
|
|
290
|
-
const right = this.transform(normalizedRight, context);
|
|
291
290
|
if (op === "in") if (this.isNullNode(left)) return this.transformValue(false, "Boolean");
|
|
292
|
-
else if (ValueListNode.is(right)) return BinaryOperationNode.create(left, OperatorNode.create("in"), right);
|
|
293
291
|
else {
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
292
|
+
if (this.isLiteralArray(normalizedRight)) {
|
|
293
|
+
if (normalizedRight.items.length === 0) return this.transformValue(false, "Boolean");
|
|
294
|
+
return BinaryOperationNode.create(left, OperatorNode.create("in"), ValueListNode.create(normalizedRight.items.map((item) => this.transform(item, context))));
|
|
295
|
+
}
|
|
296
|
+
const right = this.transform(normalizedRight, context);
|
|
297
|
+
if (ValueListNode.is(right)) return BinaryOperationNode.create(left, OperatorNode.create("in"), right);
|
|
298
|
+
else {
|
|
299
|
+
const leftFieldDef = this.getFieldDefFromFieldRef(normalizedLeft, context);
|
|
300
|
+
const comparand = leftFieldDef && QueryUtils.isEnum(this.schema, leftFieldDef.type) ? this.dialect.castText(new ExpressionWrapper(left)).toOperationNode() : left;
|
|
301
|
+
if (SelectQueryNode.is(right)) {
|
|
302
|
+
const rightFieldDef = this.getFieldDefFromFieldRef(normalizedRight, context);
|
|
303
|
+
return this.dialect.buildArrayContains(new ExpressionWrapper(right), new ExpressionWrapper(comparand), rightFieldDef?.type).toOperationNode();
|
|
304
|
+
}
|
|
305
|
+
return BinaryOperationNode.create(comparand, OperatorNode.create("="), FunctionNode.create("any", [right]));
|
|
299
306
|
}
|
|
300
|
-
return BinaryOperationNode.create(comparand, OperatorNode.create("="), FunctionNode.create("any", [right]));
|
|
301
307
|
}
|
|
308
|
+
const right = this.transform(normalizedRight, context);
|
|
302
309
|
if (this.isNullNode(right)) return this.transformNullCheck(left, expr.op);
|
|
303
310
|
else if (this.isNullNode(left)) return this.transformNullCheck(right, expr.op);
|
|
304
311
|
else {
|
|
305
312
|
const leftFieldDef = this.getFieldDefFromFieldRef(normalizedLeft, context);
|
|
306
313
|
const rightFieldDef = this.getFieldDefFromFieldRef(normalizedRight, context);
|
|
307
314
|
const sqlOp = op === "==" ? "=" : op;
|
|
308
|
-
|
|
315
|
+
const asText = this.isBeforeEnumField(normalizedLeft, context) || this.isBeforeEnumField(normalizedRight, context);
|
|
316
|
+
const operand = (node) => asText ? this.dialect.castText(new ExpressionWrapper(node)) : new ExpressionWrapper(node);
|
|
317
|
+
return this.dialect.buildComparison(operand(left), leftFieldDef, sqlOp, operand(right), rightFieldDef).toOperationNode();
|
|
309
318
|
}
|
|
310
319
|
}
|
|
311
320
|
transformNullCheck(expr, operator) {
|
|
@@ -314,6 +323,9 @@ var ExpressionTransformer = class {
|
|
|
314
323
|
else return operator === "==" ? BinaryOperationNode.create(expr, OperatorNode.create("is"), ValueNode.createImmediate(null)) : BinaryOperationNode.create(expr, OperatorNode.create("is not"), ValueNode.createImmediate(null));
|
|
315
324
|
else return ValueNode.createImmediate(null);
|
|
316
325
|
}
|
|
326
|
+
isLiteralArray(expr) {
|
|
327
|
+
return expr.kind === "array" && expr.items.every((item) => item.kind === "literal");
|
|
328
|
+
}
|
|
317
329
|
normalizeBinaryOperationOperands(expr, context) {
|
|
318
330
|
let normalizedLeft = expr.left;
|
|
319
331
|
if (this.isRelationField(expr.left, context)) {
|
|
@@ -765,6 +777,11 @@ var ExpressionTransformer = class {
|
|
|
765
777
|
isRelationField(expr, context) {
|
|
766
778
|
return !!this.getFieldDefFromFieldRef(expr, context)?.relation;
|
|
767
779
|
}
|
|
780
|
+
isBeforeEnumField(expr, context) {
|
|
781
|
+
if (!ExpressionUtils.isMember(expr) || !isBeforeInvocation(expr.receiver)) return false;
|
|
782
|
+
const fieldDef = QueryUtils.getField(this.schema, context.thisType, expr.members[0]);
|
|
783
|
+
return !!fieldDef && QueryUtils.isEnum(this.schema, fieldDef.type);
|
|
784
|
+
}
|
|
768
785
|
getFieldDefFromFieldRef(expr, context) {
|
|
769
786
|
return this.resolveFieldRef(expr, context)?.fieldDef;
|
|
770
787
|
}
|
|
@@ -910,40 +927,59 @@ var PolicyHandler = class extends OperationNodeTransformer {
|
|
|
910
927
|
const sides = [{
|
|
911
928
|
column: "A",
|
|
912
929
|
model: m2m.firstModel,
|
|
930
|
+
field: m2m.firstField,
|
|
913
931
|
idField: m2m.firstIdField
|
|
914
932
|
}, {
|
|
915
933
|
column: "B",
|
|
916
934
|
model: m2m.secondModel,
|
|
935
|
+
field: m2m.secondField,
|
|
917
936
|
idField: m2m.secondIdField
|
|
918
937
|
}].map((side) => ({
|
|
919
938
|
...side,
|
|
920
|
-
|
|
921
|
-
})).filter((side) => side.
|
|
939
|
+
values: this.extractConstrainedValues(node.where?.where, side.column)
|
|
940
|
+
})).filter((side) => side.values !== void 0);
|
|
922
941
|
if (sides.length === 0) return;
|
|
923
942
|
const result = await proceed({
|
|
924
943
|
kind: "SelectQueryNode",
|
|
925
|
-
selections: sides.map((side, index) => SelectionNode.create(AliasNode.create(this.eb.selectFrom(side.model).where(this.eb(this.eb.ref(`${side.model}.${side.idField}`), "
|
|
944
|
+
selections: sides.map((side, index) => SelectionNode.create(AliasNode.create(this.eb.selectFrom(side.model).where(this.eb(this.eb.ref(`${side.model}.${side.idField}`), "in", side.values)).where(() => new ExpressionWrapper(this.buildM2mSidePolicyFilter(side.model, side.field))).select((eb) => eb.fn("COUNT", [eb.lit(1)]).as("_")).toOperationNode(), IdentifierNode.create(`$condition${index}`))))
|
|
926
945
|
});
|
|
927
|
-
for (const [index, side] of sides.entries())
|
|
946
|
+
for (const [index, side] of sides.entries()) {
|
|
947
|
+
const distinctValues = new Set(side.values).size;
|
|
948
|
+
if (Number(result.rows[0]?.[`$condition${index}`] ?? 0) < distinctValues) throw createRejectedByPolicyError(side.model, RejectedByPolicyReason.NO_ACCESS, `many-to-many relation participant model "${side.model}" not updatable`);
|
|
949
|
+
}
|
|
928
950
|
}
|
|
929
951
|
/**
|
|
930
|
-
* Finds the literal
|
|
931
|
-
* where clause, or `undefined` if there's no such constraint.
|
|
952
|
+
* Finds the literal values that `column` is constrained to by a top-level conjunction of the
|
|
953
|
+
* given where clause, or `undefined` if there's no such constraint. Supports both `=` and `IN`
|
|
954
|
+
* operators.
|
|
932
955
|
*
|
|
933
|
-
* E.g., given the where clause `("A" = 1 AND "B" IN (2, 3))`, extracting column "A" returns
|
|
934
|
-
*
|
|
956
|
+
* E.g., given the where clause `("A" = 1 AND "B" IN (2, 3))`, extracting column "A" returns
|
|
957
|
+
* `[1]`, and extracting column "B" returns `[2, 3]`.
|
|
935
958
|
*/
|
|
936
|
-
|
|
959
|
+
extractConstrainedValues(node, column) {
|
|
937
960
|
if (!node) return;
|
|
938
|
-
if (ParensNode.is(node)) return this.
|
|
939
|
-
if (AndNode.is(node)) return this.
|
|
961
|
+
if (ParensNode.is(node)) return this.extractConstrainedValues(node.node, column);
|
|
962
|
+
if (AndNode.is(node)) return this.extractConstrainedValues(node.left, column) ?? this.extractConstrainedValues(node.right, column);
|
|
940
963
|
if (!BinaryOperationNode.is(node)) return;
|
|
941
|
-
if (!OperatorNode.is(node.operator)
|
|
964
|
+
if (!OperatorNode.is(node.operator)) return;
|
|
942
965
|
const { leftOperand, rightOperand } = node;
|
|
943
966
|
if (!ReferenceNode.is(leftOperand) || !ColumnNode.is(leftOperand.column)) return;
|
|
944
967
|
if (leftOperand.column.column.name !== column) return;
|
|
945
|
-
if (
|
|
946
|
-
|
|
968
|
+
if (node.operator.operator === "=") {
|
|
969
|
+
if (!ValueNode.is(rightOperand) || rightOperand.value === null || rightOperand.value === void 0) return;
|
|
970
|
+
return [rightOperand.value];
|
|
971
|
+
}
|
|
972
|
+
if (node.operator.operator === "in") {
|
|
973
|
+
if (PrimitiveValueListNode.is(rightOperand)) {
|
|
974
|
+
const values = rightOperand.values.filter((v) => v !== null && v !== void 0);
|
|
975
|
+
return values.length > 0 ? values : void 0;
|
|
976
|
+
}
|
|
977
|
+
if (ValueListNode.is(rightOperand)) {
|
|
978
|
+
const values = rightOperand.values.filter((v) => ValueNode.is(v) && v.value !== null && v.value !== void 0);
|
|
979
|
+
return values.length > 0 ? values.map((v) => v.value) : void 0;
|
|
980
|
+
}
|
|
981
|
+
return;
|
|
982
|
+
}
|
|
947
983
|
}
|
|
948
984
|
async postUpdateCheck(model, beforeUpdateInfo, updateResult, proceed) {
|
|
949
985
|
let postUpdateRows;
|
|
@@ -1212,6 +1248,17 @@ var PolicyHandler = class extends OperationNodeTransformer {
|
|
|
1212
1248
|
}
|
|
1213
1249
|
return combinedPolicy;
|
|
1214
1250
|
}
|
|
1251
|
+
/**
|
|
1252
|
+
* Builds the update policy filter for one side of an implicit many-to-many relation.
|
|
1253
|
+
*
|
|
1254
|
+
* If the relation field declares field-level `update` policies, they take precedence over
|
|
1255
|
+
* the model-level policy for this side; otherwise the model-level `update` policy applies
|
|
1256
|
+
* (preserving the pre-existing behavior).
|
|
1257
|
+
*/
|
|
1258
|
+
buildM2mSidePolicyFilter(model, field) {
|
|
1259
|
+
if (this.getFieldPolicies(model, field, "update").length > 0) return this.buildFieldPolicyFilter(model, field, "update");
|
|
1260
|
+
return this.buildPolicyFilter(model, void 0, "update");
|
|
1261
|
+
}
|
|
1215
1262
|
onlyReturningId(node) {
|
|
1216
1263
|
if (!node.returning) return true;
|
|
1217
1264
|
const { mutationModel } = this.getMutationModel(node);
|
|
@@ -1244,9 +1291,9 @@ var PolicyHandler = class extends OperationNodeTransformer {
|
|
|
1244
1291
|
invariant(aValue !== null && aValue !== void 0, "A value cannot be null or undefined");
|
|
1245
1292
|
invariant(bValue !== null && bValue !== void 0, "B value cannot be null or undefined");
|
|
1246
1293
|
const eb = expressionBuilder();
|
|
1247
|
-
const filterA = this.
|
|
1294
|
+
const filterA = this.buildM2mSidePolicyFilter(m2m.firstModel, m2m.firstField);
|
|
1248
1295
|
const queryA = eb.selectFrom(m2m.firstModel).where(eb(eb.ref(`${m2m.firstModel}.${m2m.firstIdField}`), "=", aValue)).select(() => new ExpressionWrapper(filterA).as("_"));
|
|
1249
|
-
const filterB = this.
|
|
1296
|
+
const filterB = this.buildM2mSidePolicyFilter(m2m.secondModel, m2m.secondField);
|
|
1250
1297
|
const queryB = eb.selectFrom(m2m.secondModel).where(eb(eb.ref(`${m2m.secondModel}.${m2m.secondIdField}`), "=", bValue)).select(() => new ExpressionWrapper(filterB).as("_"));
|
|
1251
1298
|
const result = await proceed({
|
|
1252
1299
|
kind: "SelectQueryNode",
|
|
@@ -1483,10 +1530,10 @@ var PolicyHandler = class extends OperationNodeTransformer {
|
|
|
1483
1530
|
getModelPolicyFilterForManyToManyJoinTable(tableName, alias, operation) {
|
|
1484
1531
|
const m2m = this.resolveManyToManyJoinTable(tableName);
|
|
1485
1532
|
if (!m2m) return;
|
|
1486
|
-
const
|
|
1533
|
+
const isRead = operation === "read";
|
|
1487
1534
|
const joinTable = alias ?? tableName;
|
|
1488
|
-
const aQuery = this.eb.selectFrom(m2m.firstModel).whereRef(`${m2m.firstModel}.${m2m.firstIdField}`, "=", `${joinTable}.A`).select(() => new ExpressionWrapper(this.buildPolicyFilter(m2m.firstModel, void 0,
|
|
1489
|
-
const bQuery = this.eb.selectFrom(m2m.secondModel).whereRef(`${m2m.secondModel}.${m2m.secondIdField}`, "=", `${joinTable}.B`).select(() => new ExpressionWrapper(this.buildPolicyFilter(m2m.secondModel, void 0,
|
|
1535
|
+
const aQuery = this.eb.selectFrom(m2m.firstModel).whereRef(`${m2m.firstModel}.${m2m.firstIdField}`, "=", `${joinTable}.A`).select(() => new ExpressionWrapper(isRead ? this.buildPolicyFilter(m2m.firstModel, void 0, "read") : this.buildM2mSidePolicyFilter(m2m.firstModel, m2m.firstField)).as("$conditionA"));
|
|
1536
|
+
const bQuery = this.eb.selectFrom(m2m.secondModel).whereRef(`${m2m.secondModel}.${m2m.secondIdField}`, "=", `${joinTable}.B`).select(() => new ExpressionWrapper(isRead ? this.buildPolicyFilter(m2m.secondModel, void 0, "read") : this.buildM2mSidePolicyFilter(m2m.secondModel, m2m.secondField)).as("$conditionB"));
|
|
1490
1537
|
return this.eb.and([aQuery, bQuery]).toOperationNode();
|
|
1491
1538
|
}
|
|
1492
1539
|
tryRejectNonexistentModel(model) {
|