@zenstackhq/plugin-policy 3.9.0-beta.1 → 3.9.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 +48 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +48 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -7
package/dist/index.cjs
CHANGED
|
@@ -772,6 +772,7 @@ var PolicyHandler = class extends kysely.OperationNodeTransformer {
|
|
|
772
772
|
this.tryRejectNonexistentModel(mutationModel);
|
|
773
773
|
if (kysely.InsertQueryNode.is(node)) await this.preCreateCheck(mutationModel, node, proceed);
|
|
774
774
|
if (kysely.UpdateQueryNode.is(node)) await this.preUpdateCheck(mutationModel, node, proceed);
|
|
775
|
+
if (kysely.DeleteQueryNode.is(node)) await this.preDeleteCheck(mutationModel, node, proceed);
|
|
775
776
|
const needsPostUpdateCheck = kysely.UpdateQueryNode.is(node) && this.hasPostUpdatePolicies(mutationModel);
|
|
776
777
|
let beforeUpdateInfo;
|
|
777
778
|
if (needsPostUpdateCheck) beforeUpdateInfo = await this.loadBeforeUpdateEntities(mutationModel, node.where, proceed, !this.dialect.supportsReturning);
|
|
@@ -806,6 +807,52 @@ var PolicyHandler = class extends kysely.OperationNodeTransformer {
|
|
|
806
807
|
selections: [kysely.SelectionNode.create(kysely.AliasNode.create(this.eb.exists(violatingRowsQuery).toOperationNode(), kysely.IdentifierNode.create("$condition")))]
|
|
807
808
|
})).rows[0].$condition) throw createRejectedByPolicyError(mutationModel, _zenstackhq_orm.RejectedByPolicyReason.NO_ACCESS, "some rows cannot be updated due to field policies");
|
|
808
809
|
}
|
|
810
|
+
/**
|
|
811
|
+
* Deleting rows from an implicit many-to-many join table requires both participant sides to be
|
|
812
|
+
* updatable. Unlike inserts (which are explicitly rejected by {@link enforcePreCreatePolicyForManyToManyJoinTable}),
|
|
813
|
+
* a delete would otherwise just be silently filtered down to zero rows and report success. See issue #2752.
|
|
814
|
+
*/
|
|
815
|
+
async preDeleteCheck(mutationModel, node, proceed) {
|
|
816
|
+
const m2m = this.resolveManyToManyJoinTable(mutationModel);
|
|
817
|
+
if (!m2m) return;
|
|
818
|
+
const sides = [{
|
|
819
|
+
column: "A",
|
|
820
|
+
model: m2m.firstModel,
|
|
821
|
+
idField: m2m.firstIdField
|
|
822
|
+
}, {
|
|
823
|
+
column: "B",
|
|
824
|
+
model: m2m.secondModel,
|
|
825
|
+
idField: m2m.secondIdField
|
|
826
|
+
}].map((side) => ({
|
|
827
|
+
...side,
|
|
828
|
+
value: this.extractEqualityValue(node.where?.where, side.column)
|
|
829
|
+
})).filter((side) => side.value !== void 0);
|
|
830
|
+
if (sides.length === 0) return;
|
|
831
|
+
const result = await proceed({
|
|
832
|
+
kind: "SelectQueryNode",
|
|
833
|
+
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}`), "=", side.value)).select(() => new kysely.ExpressionWrapper(this.buildPolicyFilter(side.model, void 0, "update")).as("_")).toOperationNode(), kysely.IdentifierNode.create(`$condition${index}`))))
|
|
834
|
+
});
|
|
835
|
+
for (const [index, side] of sides.entries()) if (!result.rows[0]?.[`$condition${index}`]) throw createRejectedByPolicyError(side.model, _zenstackhq_orm.RejectedByPolicyReason.NO_ACCESS, `many-to-many relation participant model "${side.model}" not updatable`);
|
|
836
|
+
}
|
|
837
|
+
/**
|
|
838
|
+
* Finds the literal value that `column` is constrained to by a top-level conjunction of the given
|
|
839
|
+
* where clause, or `undefined` if there's no such constraint.
|
|
840
|
+
*
|
|
841
|
+
* E.g., given the where clause `("A" = 1 AND "B" IN (2, 3))`, extracting column "A" returns `1`,
|
|
842
|
+
* while extracting column "B" returns `undefined` since it's not an equality constraint.
|
|
843
|
+
*/
|
|
844
|
+
extractEqualityValue(node, column) {
|
|
845
|
+
if (!node) return;
|
|
846
|
+
if (kysely.ParensNode.is(node)) return this.extractEqualityValue(node.node, column);
|
|
847
|
+
if (kysely.AndNode.is(node)) return this.extractEqualityValue(node.left, column) ?? this.extractEqualityValue(node.right, column);
|
|
848
|
+
if (!kysely.BinaryOperationNode.is(node)) return;
|
|
849
|
+
if (!kysely.OperatorNode.is(node.operator) || node.operator.operator !== "=") return;
|
|
850
|
+
const { leftOperand, rightOperand } = node;
|
|
851
|
+
if (!kysely.ReferenceNode.is(leftOperand) || !kysely.ColumnNode.is(leftOperand.column)) return;
|
|
852
|
+
if (leftOperand.column.column.name !== column) return;
|
|
853
|
+
if (!kysely.ValueNode.is(rightOperand) || rightOperand.value === null || rightOperand.value === void 0) return;
|
|
854
|
+
return rightOperand.value;
|
|
855
|
+
}
|
|
809
856
|
async postUpdateCheck(model, beforeUpdateInfo, updateResult, proceed) {
|
|
810
857
|
let postUpdateRows;
|
|
811
858
|
if (this.dialect.supportsReturning) postUpdateRows = updateResult.rows;
|
|
@@ -1110,7 +1157,7 @@ var PolicyHandler = class extends kysely.OperationNodeTransformer {
|
|
|
1110
1157
|
kind: "SelectQueryNode",
|
|
1111
1158
|
selections: [kysely.SelectionNode.create(kysely.AliasNode.create(queryA.toOperationNode(), kysely.IdentifierNode.create("$conditionA"))), kysely.SelectionNode.create(kysely.AliasNode.create(queryB.toOperationNode(), kysely.IdentifierNode.create("$conditionB")))]
|
|
1112
1159
|
});
|
|
1113
|
-
if (!result.rows[0]?.$conditionA) throw createRejectedByPolicyError(m2m.firstModel, _zenstackhq_orm.RejectedByPolicyReason.
|
|
1160
|
+
if (!result.rows[0]?.$conditionA) throw createRejectedByPolicyError(m2m.firstModel, _zenstackhq_orm.RejectedByPolicyReason.NO_ACCESS, `many-to-many relation participant model "${m2m.firstModel}" not updatable`);
|
|
1114
1161
|
if (!result.rows[0]?.$conditionB) throw createRejectedByPolicyError(m2m.secondModel, _zenstackhq_orm.RejectedByPolicyReason.NO_ACCESS, `many-to-many relation participant model "${m2m.secondModel}" not updatable`);
|
|
1115
1162
|
}
|
|
1116
1163
|
async enforcePreCreatePolicyForOne(model, fields, values, proceed) {
|