@zenstackhq/plugin-policy 3.10.0 → 3.10.2
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 +70 -8
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +70 -8
- 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) {
|
|
@@ -312,7 +319,7 @@ var ExpressionTransformer = class {
|
|
|
312
319
|
const leftFieldDef = this.getFieldDefFromFieldRef(normalizedLeft, context);
|
|
313
320
|
const rightFieldDef = this.getFieldDefFromFieldRef(normalizedRight, context);
|
|
314
321
|
const sqlOp = op === "==" ? "=" : op;
|
|
315
|
-
const asText = this.isBeforeEnumField(normalizedLeft, context) || this.isBeforeEnumField(normalizedRight, context);
|
|
322
|
+
const asText = this.isBeforeEnumField(normalizedLeft, context) || this.isBeforeEnumField(normalizedRight, context) || this.isCreateInputEnumComparison(normalizedLeft, normalizedRight, context);
|
|
316
323
|
const operand = (node) => asText ? this.dialect.castText(new ExpressionWrapper(node)) : new ExpressionWrapper(node);
|
|
317
324
|
return this.dialect.buildComparison(operand(left), leftFieldDef, sqlOp, operand(right), rightFieldDef).toOperationNode();
|
|
318
325
|
}
|
|
@@ -673,8 +680,7 @@ var ExpressionTransformer = class {
|
|
|
673
680
|
} else {
|
|
674
681
|
invariant(i === members.length - 1, "plain field access must be the last segment");
|
|
675
682
|
invariant(!currNode, "plain field access must be the last segment");
|
|
676
|
-
|
|
677
|
-
else currNode = ReferenceNode.create(ColumnNode.create(member), TableNode.create(fromAlias));
|
|
683
|
+
currNode = this.buildFieldRef(fromModel, member, fromAlias);
|
|
678
684
|
}
|
|
679
685
|
}
|
|
680
686
|
return {
|
|
@@ -734,10 +740,18 @@ var ExpressionTransformer = class {
|
|
|
734
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();
|
|
735
741
|
}
|
|
736
742
|
createColumnRef(column, context) {
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
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);
|
|
741
755
|
}
|
|
742
756
|
getEvaluationBindingScope(scope) {
|
|
743
757
|
if (!scope) return;
|
|
@@ -750,7 +764,7 @@ var ExpressionTransformer = class {
|
|
|
750
764
|
return {
|
|
751
765
|
kind: "SelectQueryNode",
|
|
752
766
|
from: FromNode.create([TableNode.create(baseModel)]),
|
|
753
|
-
selections: [SelectionNode.create(
|
|
767
|
+
selections: [SelectionNode.create(this.buildFieldRef(baseModel, field, baseModel))],
|
|
754
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))))))
|
|
755
769
|
};
|
|
756
770
|
}
|
|
@@ -782,6 +796,50 @@ var ExpressionTransformer = class {
|
|
|
782
796
|
const fieldDef = QueryUtils.getField(this.schema, context.thisType, expr.members[0]);
|
|
783
797
|
return !!fieldDef && QueryUtils.isEnum(this.schema, fieldDef.type);
|
|
784
798
|
}
|
|
799
|
+
/**
|
|
800
|
+
* Whether a comparison in a create rule needs both operands compared as text.
|
|
801
|
+
*
|
|
802
|
+
* Create rules are evaluated before the insert, against a `VALUES` table built from the input
|
|
803
|
+
* (see `enforcePreCreatePolicyForOne`). That table carries enum fields as `text`, while stored
|
|
804
|
+
* columns have the native enum type, and PostgreSQL has no `text = "Enum"` operator (42883).
|
|
805
|
+
*
|
|
806
|
+
* Returns true only when one operand is an enum field of the entity being created and the
|
|
807
|
+
* other is a stored field of the same enum, e.g. for `model Grant { permission Permission }`:
|
|
808
|
+
* - `permission == anchor.defaultPermission` (bare top-level field vs. related row)
|
|
809
|
+
* - `anchor.approvals?[permission == this.permission]` (stored row vs. `this`)
|
|
810
|
+
*
|
|
811
|
+
* Not for literals (`permission == EDIT`): the name mapper still translates `@map`-ed literals.
|
|
812
|
+
* Not for enums with `@map`-ed values: the input holds names (`ACTIVE`) but stored columns
|
|
813
|
+
* hold mapped values (`active`), so a text comparison would be wrong (#2867).
|
|
814
|
+
*/
|
|
815
|
+
isCreateInputEnumComparison(left, right, context) {
|
|
816
|
+
if (context.operation !== "create") return false;
|
|
817
|
+
const leftInput = this.getCreateInputEnumField(left, context);
|
|
818
|
+
const rightInput = this.getCreateInputEnumField(right, context);
|
|
819
|
+
if (!leftInput === !rightInput) return false;
|
|
820
|
+
const inputDef = leftInput ?? rightInput;
|
|
821
|
+
return this.getFieldDefFromFieldRef(leftInput ? right : left, context)?.type === inputDef.type && !this.hasMappedEnumValues(inputDef.type);
|
|
822
|
+
}
|
|
823
|
+
/**
|
|
824
|
+
* If the expression reads an enum field of the entity being created (i.e. a column of the input
|
|
825
|
+
* `VALUES` table), returns that field's definition; otherwise `undefined`.
|
|
826
|
+
*
|
|
827
|
+
* - `this.field` always refers to the entity the rule belongs to. A longer path such as
|
|
828
|
+
* `this.anchor.defaultPermission` goes through a relation and reads a stored row.
|
|
829
|
+
* - a bare `field` refers to the entity being created only at the top level of the rule. Inside
|
|
830
|
+
* a collection predicate (`relation?[field == ...]`) it refers to the related stored row.
|
|
831
|
+
*/
|
|
832
|
+
getCreateInputEnumField(expr, context) {
|
|
833
|
+
let field;
|
|
834
|
+
if (ExpressionUtils.isMember(expr) && ExpressionUtils.isThis(expr.receiver) && expr.members.length === 1) field = expr.members[0];
|
|
835
|
+
else if (ExpressionUtils.isField(expr) && context.modelOrType === context.thisType && context.alias === context.thisAlias) field = expr.field;
|
|
836
|
+
const fieldDef = field ? QueryUtils.getField(this.schema, context.thisType, field) : void 0;
|
|
837
|
+
return fieldDef && QueryUtils.isEnum(this.schema, fieldDef.type) ? fieldDef : void 0;
|
|
838
|
+
}
|
|
839
|
+
hasMappedEnumValues(enumType) {
|
|
840
|
+
const enumDef = QueryUtils.getEnum(this.schema, enumType);
|
|
841
|
+
return Object.values(enumDef?.fields ?? {}).some((f) => f.attributes?.some((attr) => attr.name === "@map"));
|
|
842
|
+
}
|
|
785
843
|
getFieldDefFromFieldRef(expr, context) {
|
|
786
844
|
return this.resolveFieldRef(expr, context)?.fieldDef;
|
|
787
845
|
}
|
|
@@ -1158,6 +1216,10 @@ var PolicyHandler = class extends OperationNodeTransformer {
|
|
|
1158
1216
|
const fieldDef = QueryUtils.getField(this.client.$schema, model, f);
|
|
1159
1217
|
const owningTable = fieldDef?.originModel ?? model;
|
|
1160
1218
|
if (fieldDef?.originModel) baseModelsToJoin.add(fieldDef.originModel);
|
|
1219
|
+
if (fieldDef?.computed) {
|
|
1220
|
+
rejectParameterizedComputedField(fieldDef, model);
|
|
1221
|
+
return SelectionNode.create(AliasNode.create(this.dialect.fieldRef(owningTable, f, owningTable).toOperationNode(), IdentifierNode.create(f)));
|
|
1222
|
+
}
|
|
1161
1223
|
return SelectionNode.create(ReferenceNode.create(ColumnNode.create(f), TableNode.create(owningTable)));
|
|
1162
1224
|
});
|
|
1163
1225
|
const idFields = QueryUtils.requireIdFields(this.client.$schema, model);
|