@zenstackhq/plugin-policy 3.9.8 → 3.10.1
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 +100 -35
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +100 -35
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -7
package/dist/index.mjs
CHANGED
|
@@ -191,6 +191,13 @@ function createRejectedByPolicyError(model, reason, message) {
|
|
|
191
191
|
function createUnsupportedError(message) {
|
|
192
192
|
return new ORMError(ORMErrorReason.NOT_SUPPORTED, message);
|
|
193
193
|
}
|
|
194
|
+
/**
|
|
195
|
+
* Throws a config error if the given field is a parameterized computed field. Such fields need
|
|
196
|
+
* query-time args, which policy expressions have no way to provide.
|
|
197
|
+
*/
|
|
198
|
+
function rejectParameterizedComputedField(fieldDef, model) {
|
|
199
|
+
if (fieldDef.computed && fieldDef.params) throw new ORMError(ORMErrorReason.CONFIG_ERROR, `Parameterized computed field "${fieldDef.name}" of model "${model}" cannot be used in a policy expression`);
|
|
200
|
+
}
|
|
194
201
|
//#endregion
|
|
195
202
|
//#region \0@oxc-project+runtime@0.124.0/helpers/decorateMetadata.js
|
|
196
203
|
function __decorateMetadata(k, v) {
|
|
@@ -287,25 +294,34 @@ var ExpressionTransformer = class {
|
|
|
287
294
|
if (op === "?" || op === "!" || op === "^") return this.transformCollectionPredicate(expr, context);
|
|
288
295
|
const { normalizedLeft, normalizedRight } = this.normalizeBinaryOperationOperands(expr, context);
|
|
289
296
|
const left = this.transform(normalizedLeft, context);
|
|
290
|
-
const right = this.transform(normalizedRight, context);
|
|
291
297
|
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
298
|
else {
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
+
if (this.isLiteralArray(normalizedRight)) {
|
|
300
|
+
if (normalizedRight.items.length === 0) return this.transformValue(false, "Boolean");
|
|
301
|
+
return BinaryOperationNode.create(left, OperatorNode.create("in"), ValueListNode.create(normalizedRight.items.map((item) => this.transform(item, context))));
|
|
302
|
+
}
|
|
303
|
+
const right = this.transform(normalizedRight, context);
|
|
304
|
+
if (ValueListNode.is(right)) return BinaryOperationNode.create(left, OperatorNode.create("in"), right);
|
|
305
|
+
else {
|
|
306
|
+
const leftFieldDef = this.getFieldDefFromFieldRef(normalizedLeft, context);
|
|
307
|
+
const comparand = leftFieldDef && QueryUtils.isEnum(this.schema, leftFieldDef.type) ? this.dialect.castText(new ExpressionWrapper(left)).toOperationNode() : left;
|
|
308
|
+
if (SelectQueryNode.is(right)) {
|
|
309
|
+
const rightFieldDef = this.getFieldDefFromFieldRef(normalizedRight, context);
|
|
310
|
+
return this.dialect.buildArrayContains(new ExpressionWrapper(right), new ExpressionWrapper(comparand), rightFieldDef?.type).toOperationNode();
|
|
311
|
+
}
|
|
312
|
+
return BinaryOperationNode.create(comparand, OperatorNode.create("="), FunctionNode.create("any", [right]));
|
|
299
313
|
}
|
|
300
|
-
return BinaryOperationNode.create(comparand, OperatorNode.create("="), FunctionNode.create("any", [right]));
|
|
301
314
|
}
|
|
315
|
+
const right = this.transform(normalizedRight, context);
|
|
302
316
|
if (this.isNullNode(right)) return this.transformNullCheck(left, expr.op);
|
|
303
317
|
else if (this.isNullNode(left)) return this.transformNullCheck(right, expr.op);
|
|
304
318
|
else {
|
|
305
319
|
const leftFieldDef = this.getFieldDefFromFieldRef(normalizedLeft, context);
|
|
306
320
|
const rightFieldDef = this.getFieldDefFromFieldRef(normalizedRight, context);
|
|
307
321
|
const sqlOp = op === "==" ? "=" : op;
|
|
308
|
-
|
|
322
|
+
const asText = this.isBeforeEnumField(normalizedLeft, context) || this.isBeforeEnumField(normalizedRight, context);
|
|
323
|
+
const operand = (node) => asText ? this.dialect.castText(new ExpressionWrapper(node)) : new ExpressionWrapper(node);
|
|
324
|
+
return this.dialect.buildComparison(operand(left), leftFieldDef, sqlOp, operand(right), rightFieldDef).toOperationNode();
|
|
309
325
|
}
|
|
310
326
|
}
|
|
311
327
|
transformNullCheck(expr, operator) {
|
|
@@ -314,6 +330,9 @@ var ExpressionTransformer = class {
|
|
|
314
330
|
else return operator === "==" ? BinaryOperationNode.create(expr, OperatorNode.create("is"), ValueNode.createImmediate(null)) : BinaryOperationNode.create(expr, OperatorNode.create("is not"), ValueNode.createImmediate(null));
|
|
315
331
|
else return ValueNode.createImmediate(null);
|
|
316
332
|
}
|
|
333
|
+
isLiteralArray(expr) {
|
|
334
|
+
return expr.kind === "array" && expr.items.every((item) => item.kind === "literal");
|
|
335
|
+
}
|
|
317
336
|
normalizeBinaryOperationOperands(expr, context) {
|
|
318
337
|
let normalizedLeft = expr.left;
|
|
319
338
|
if (this.isRelationField(expr.left, context)) {
|
|
@@ -661,8 +680,7 @@ var ExpressionTransformer = class {
|
|
|
661
680
|
} else {
|
|
662
681
|
invariant(i === members.length - 1, "plain field access must be the last segment");
|
|
663
682
|
invariant(!currNode, "plain field access must be the last segment");
|
|
664
|
-
|
|
665
|
-
else currNode = ReferenceNode.create(ColumnNode.create(member), TableNode.create(fromAlias));
|
|
683
|
+
currNode = this.buildFieldRef(fromModel, member, fromAlias);
|
|
666
684
|
}
|
|
667
685
|
}
|
|
668
686
|
return {
|
|
@@ -722,10 +740,18 @@ var ExpressionTransformer = class {
|
|
|
722
740
|
return eb.selectFrom(`${m2m.otherModel} as ${relationAlias}`).innerJoin(`${m2m.joinTable} as ${joinTableAlias}`, (join) => join.onRef(`${relationAlias}.${m2m.otherPKName}`, "=", `${joinTableAlias}.${m2m.otherFkName}`).onRef(`${joinTableAlias}.${m2m.parentFkName}`, "=", `${context.alias ?? context.modelOrType}.${m2m.parentPKName}`)).toOperationNode();
|
|
723
741
|
}
|
|
724
742
|
createColumnRef(column, context) {
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
743
|
+
return this.buildFieldRef(context.modelOrType, column, context.alias ?? context.modelOrType);
|
|
744
|
+
}
|
|
745
|
+
/**
|
|
746
|
+
* Builds a reference to a scalar field of `model` whose table is aliased as `modelAlias`.
|
|
747
|
+
* Handles regular columns, `@computed` fields (inlined via the dialect), and fields inherited
|
|
748
|
+
* from a delegate base model (looked up from the base table with a correlated subquery).
|
|
749
|
+
*/
|
|
750
|
+
buildFieldRef(model, field, modelAlias) {
|
|
751
|
+
const fieldDef = QueryUtils.requireField(this.schema, model, field);
|
|
752
|
+
rejectParameterizedComputedField(fieldDef, model);
|
|
753
|
+
if (!fieldDef.originModel || fieldDef.originModel === model) return fieldDef.computed ? this.dialect.fieldRef(model, field, modelAlias).toOperationNode() : ReferenceNode.create(ColumnNode.create(field), TableNode.create(modelAlias));
|
|
754
|
+
return this.buildDelegateBaseFieldSelect(model, modelAlias, field, fieldDef.originModel);
|
|
729
755
|
}
|
|
730
756
|
getEvaluationBindingScope(scope) {
|
|
731
757
|
if (!scope) return;
|
|
@@ -738,7 +764,7 @@ var ExpressionTransformer = class {
|
|
|
738
764
|
return {
|
|
739
765
|
kind: "SelectQueryNode",
|
|
740
766
|
from: FromNode.create([TableNode.create(baseModel)]),
|
|
741
|
-
selections: [SelectionNode.create(
|
|
767
|
+
selections: [SelectionNode.create(this.buildFieldRef(baseModel, field, baseModel))],
|
|
742
768
|
where: WhereNode.create(conjunction(this.dialect, idFields.map((idField) => BinaryOperationNode.create(ReferenceNode.create(ColumnNode.create(idField), TableNode.create(baseModel)), OperatorNode.create("="), ReferenceNode.create(ColumnNode.create(idField), TableNode.create(modelAlias))))))
|
|
743
769
|
};
|
|
744
770
|
}
|
|
@@ -765,6 +791,11 @@ var ExpressionTransformer = class {
|
|
|
765
791
|
isRelationField(expr, context) {
|
|
766
792
|
return !!this.getFieldDefFromFieldRef(expr, context)?.relation;
|
|
767
793
|
}
|
|
794
|
+
isBeforeEnumField(expr, context) {
|
|
795
|
+
if (!ExpressionUtils.isMember(expr) || !isBeforeInvocation(expr.receiver)) return false;
|
|
796
|
+
const fieldDef = QueryUtils.getField(this.schema, context.thisType, expr.members[0]);
|
|
797
|
+
return !!fieldDef && QueryUtils.isEnum(this.schema, fieldDef.type);
|
|
798
|
+
}
|
|
768
799
|
getFieldDefFromFieldRef(expr, context) {
|
|
769
800
|
return this.resolveFieldRef(expr, context)?.fieldDef;
|
|
770
801
|
}
|
|
@@ -910,40 +941,59 @@ var PolicyHandler = class extends OperationNodeTransformer {
|
|
|
910
941
|
const sides = [{
|
|
911
942
|
column: "A",
|
|
912
943
|
model: m2m.firstModel,
|
|
944
|
+
field: m2m.firstField,
|
|
913
945
|
idField: m2m.firstIdField
|
|
914
946
|
}, {
|
|
915
947
|
column: "B",
|
|
916
948
|
model: m2m.secondModel,
|
|
949
|
+
field: m2m.secondField,
|
|
917
950
|
idField: m2m.secondIdField
|
|
918
951
|
}].map((side) => ({
|
|
919
952
|
...side,
|
|
920
|
-
|
|
921
|
-
})).filter((side) => side.
|
|
953
|
+
values: this.extractConstrainedValues(node.where?.where, side.column)
|
|
954
|
+
})).filter((side) => side.values !== void 0);
|
|
922
955
|
if (sides.length === 0) return;
|
|
923
956
|
const result = await proceed({
|
|
924
957
|
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}`), "
|
|
958
|
+
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
959
|
});
|
|
927
|
-
for (const [index, side] of sides.entries())
|
|
960
|
+
for (const [index, side] of sides.entries()) {
|
|
961
|
+
const distinctValues = new Set(side.values).size;
|
|
962
|
+
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`);
|
|
963
|
+
}
|
|
928
964
|
}
|
|
929
965
|
/**
|
|
930
|
-
* Finds the literal
|
|
931
|
-
* where clause, or `undefined` if there's no such constraint.
|
|
966
|
+
* Finds the literal values that `column` is constrained to by a top-level conjunction of the
|
|
967
|
+
* given where clause, or `undefined` if there's no such constraint. Supports both `=` and `IN`
|
|
968
|
+
* operators.
|
|
932
969
|
*
|
|
933
|
-
* E.g., given the where clause `("A" = 1 AND "B" IN (2, 3))`, extracting column "A" returns
|
|
934
|
-
*
|
|
970
|
+
* E.g., given the where clause `("A" = 1 AND "B" IN (2, 3))`, extracting column "A" returns
|
|
971
|
+
* `[1]`, and extracting column "B" returns `[2, 3]`.
|
|
935
972
|
*/
|
|
936
|
-
|
|
973
|
+
extractConstrainedValues(node, column) {
|
|
937
974
|
if (!node) return;
|
|
938
|
-
if (ParensNode.is(node)) return this.
|
|
939
|
-
if (AndNode.is(node)) return this.
|
|
975
|
+
if (ParensNode.is(node)) return this.extractConstrainedValues(node.node, column);
|
|
976
|
+
if (AndNode.is(node)) return this.extractConstrainedValues(node.left, column) ?? this.extractConstrainedValues(node.right, column);
|
|
940
977
|
if (!BinaryOperationNode.is(node)) return;
|
|
941
|
-
if (!OperatorNode.is(node.operator)
|
|
978
|
+
if (!OperatorNode.is(node.operator)) return;
|
|
942
979
|
const { leftOperand, rightOperand } = node;
|
|
943
980
|
if (!ReferenceNode.is(leftOperand) || !ColumnNode.is(leftOperand.column)) return;
|
|
944
981
|
if (leftOperand.column.column.name !== column) return;
|
|
945
|
-
if (
|
|
946
|
-
|
|
982
|
+
if (node.operator.operator === "=") {
|
|
983
|
+
if (!ValueNode.is(rightOperand) || rightOperand.value === null || rightOperand.value === void 0) return;
|
|
984
|
+
return [rightOperand.value];
|
|
985
|
+
}
|
|
986
|
+
if (node.operator.operator === "in") {
|
|
987
|
+
if (PrimitiveValueListNode.is(rightOperand)) {
|
|
988
|
+
const values = rightOperand.values.filter((v) => v !== null && v !== void 0);
|
|
989
|
+
return values.length > 0 ? values : void 0;
|
|
990
|
+
}
|
|
991
|
+
if (ValueListNode.is(rightOperand)) {
|
|
992
|
+
const values = rightOperand.values.filter((v) => ValueNode.is(v) && v.value !== null && v.value !== void 0);
|
|
993
|
+
return values.length > 0 ? values.map((v) => v.value) : void 0;
|
|
994
|
+
}
|
|
995
|
+
return;
|
|
996
|
+
}
|
|
947
997
|
}
|
|
948
998
|
async postUpdateCheck(model, beforeUpdateInfo, updateResult, proceed) {
|
|
949
999
|
let postUpdateRows;
|
|
@@ -1122,6 +1172,10 @@ var PolicyHandler = class extends OperationNodeTransformer {
|
|
|
1122
1172
|
const fieldDef = QueryUtils.getField(this.client.$schema, model, f);
|
|
1123
1173
|
const owningTable = fieldDef?.originModel ?? model;
|
|
1124
1174
|
if (fieldDef?.originModel) baseModelsToJoin.add(fieldDef.originModel);
|
|
1175
|
+
if (fieldDef?.computed) {
|
|
1176
|
+
rejectParameterizedComputedField(fieldDef, model);
|
|
1177
|
+
return SelectionNode.create(AliasNode.create(this.dialect.fieldRef(owningTable, f, owningTable).toOperationNode(), IdentifierNode.create(f)));
|
|
1178
|
+
}
|
|
1125
1179
|
return SelectionNode.create(ReferenceNode.create(ColumnNode.create(f), TableNode.create(owningTable)));
|
|
1126
1180
|
});
|
|
1127
1181
|
const idFields = QueryUtils.requireIdFields(this.client.$schema, model);
|
|
@@ -1212,6 +1266,17 @@ var PolicyHandler = class extends OperationNodeTransformer {
|
|
|
1212
1266
|
}
|
|
1213
1267
|
return combinedPolicy;
|
|
1214
1268
|
}
|
|
1269
|
+
/**
|
|
1270
|
+
* Builds the update policy filter for one side of an implicit many-to-many relation.
|
|
1271
|
+
*
|
|
1272
|
+
* If the relation field declares field-level `update` policies, they take precedence over
|
|
1273
|
+
* the model-level policy for this side; otherwise the model-level `update` policy applies
|
|
1274
|
+
* (preserving the pre-existing behavior).
|
|
1275
|
+
*/
|
|
1276
|
+
buildM2mSidePolicyFilter(model, field) {
|
|
1277
|
+
if (this.getFieldPolicies(model, field, "update").length > 0) return this.buildFieldPolicyFilter(model, field, "update");
|
|
1278
|
+
return this.buildPolicyFilter(model, void 0, "update");
|
|
1279
|
+
}
|
|
1215
1280
|
onlyReturningId(node) {
|
|
1216
1281
|
if (!node.returning) return true;
|
|
1217
1282
|
const { mutationModel } = this.getMutationModel(node);
|
|
@@ -1244,9 +1309,9 @@ var PolicyHandler = class extends OperationNodeTransformer {
|
|
|
1244
1309
|
invariant(aValue !== null && aValue !== void 0, "A value cannot be null or undefined");
|
|
1245
1310
|
invariant(bValue !== null && bValue !== void 0, "B value cannot be null or undefined");
|
|
1246
1311
|
const eb = expressionBuilder();
|
|
1247
|
-
const filterA = this.
|
|
1312
|
+
const filterA = this.buildM2mSidePolicyFilter(m2m.firstModel, m2m.firstField);
|
|
1248
1313
|
const queryA = eb.selectFrom(m2m.firstModel).where(eb(eb.ref(`${m2m.firstModel}.${m2m.firstIdField}`), "=", aValue)).select(() => new ExpressionWrapper(filterA).as("_"));
|
|
1249
|
-
const filterB = this.
|
|
1314
|
+
const filterB = this.buildM2mSidePolicyFilter(m2m.secondModel, m2m.secondField);
|
|
1250
1315
|
const queryB = eb.selectFrom(m2m.secondModel).where(eb(eb.ref(`${m2m.secondModel}.${m2m.secondIdField}`), "=", bValue)).select(() => new ExpressionWrapper(filterB).as("_"));
|
|
1251
1316
|
const result = await proceed({
|
|
1252
1317
|
kind: "SelectQueryNode",
|
|
@@ -1483,10 +1548,10 @@ var PolicyHandler = class extends OperationNodeTransformer {
|
|
|
1483
1548
|
getModelPolicyFilterForManyToManyJoinTable(tableName, alias, operation) {
|
|
1484
1549
|
const m2m = this.resolveManyToManyJoinTable(tableName);
|
|
1485
1550
|
if (!m2m) return;
|
|
1486
|
-
const
|
|
1551
|
+
const isRead = operation === "read";
|
|
1487
1552
|
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,
|
|
1553
|
+
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"));
|
|
1554
|
+
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
1555
|
return this.eb.and([aQuery, bQuery]).toOperationNode();
|
|
1491
1556
|
}
|
|
1492
1557
|
tryRejectNonexistentModel(model) {
|