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