@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.cjs
CHANGED
|
@@ -192,6 +192,13 @@ function createRejectedByPolicyError(model, reason, message) {
|
|
|
192
192
|
function createUnsupportedError(message) {
|
|
193
193
|
return new _zenstackhq_orm.ORMError(_zenstackhq_orm.ORMErrorReason.NOT_SUPPORTED, message);
|
|
194
194
|
}
|
|
195
|
+
/**
|
|
196
|
+
* Throws a config error if the given field is a parameterized computed field. Such fields need
|
|
197
|
+
* query-time args, which policy expressions have no way to provide.
|
|
198
|
+
*/
|
|
199
|
+
function rejectParameterizedComputedField(fieldDef, model) {
|
|
200
|
+
if (fieldDef.computed && fieldDef.params) throw new _zenstackhq_orm.ORMError(_zenstackhq_orm.ORMErrorReason.CONFIG_ERROR, `Parameterized computed field "${fieldDef.name}" of model "${model}" cannot be used in a policy expression`);
|
|
201
|
+
}
|
|
195
202
|
//#endregion
|
|
196
203
|
//#region \0@oxc-project+runtime@0.124.0/helpers/decorateMetadata.js
|
|
197
204
|
function __decorateMetadata(k, v) {
|
|
@@ -313,7 +320,7 @@ var ExpressionTransformer = class {
|
|
|
313
320
|
const leftFieldDef = this.getFieldDefFromFieldRef(normalizedLeft, context);
|
|
314
321
|
const rightFieldDef = this.getFieldDefFromFieldRef(normalizedRight, context);
|
|
315
322
|
const sqlOp = op === "==" ? "=" : op;
|
|
316
|
-
const asText = this.isBeforeEnumField(normalizedLeft, context) || this.isBeforeEnumField(normalizedRight, context);
|
|
323
|
+
const asText = this.isBeforeEnumField(normalizedLeft, context) || this.isBeforeEnumField(normalizedRight, context) || this.isCreateInputEnumComparison(normalizedLeft, normalizedRight, context);
|
|
317
324
|
const operand = (node) => asText ? this.dialect.castText(new kysely.ExpressionWrapper(node)) : new kysely.ExpressionWrapper(node);
|
|
318
325
|
return this.dialect.buildComparison(operand(left), leftFieldDef, sqlOp, operand(right), rightFieldDef).toOperationNode();
|
|
319
326
|
}
|
|
@@ -674,8 +681,7 @@ var ExpressionTransformer = class {
|
|
|
674
681
|
} else {
|
|
675
682
|
(0, _zenstackhq_common_helpers.invariant)(i === members.length - 1, "plain field access must be the last segment");
|
|
676
683
|
(0, _zenstackhq_common_helpers.invariant)(!currNode, "plain field access must be the last segment");
|
|
677
|
-
|
|
678
|
-
else currNode = kysely.ReferenceNode.create(kysely.ColumnNode.create(member), kysely.TableNode.create(fromAlias));
|
|
684
|
+
currNode = this.buildFieldRef(fromModel, member, fromAlias);
|
|
679
685
|
}
|
|
680
686
|
}
|
|
681
687
|
return {
|
|
@@ -735,10 +741,18 @@ var ExpressionTransformer = class {
|
|
|
735
741
|
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();
|
|
736
742
|
}
|
|
737
743
|
createColumnRef(column, context) {
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
744
|
+
return this.buildFieldRef(context.modelOrType, column, context.alias ?? context.modelOrType);
|
|
745
|
+
}
|
|
746
|
+
/**
|
|
747
|
+
* Builds a reference to a scalar field of `model` whose table is aliased as `modelAlias`.
|
|
748
|
+
* Handles regular columns, `@computed` fields (inlined via the dialect), and fields inherited
|
|
749
|
+
* from a delegate base model (looked up from the base table with a correlated subquery).
|
|
750
|
+
*/
|
|
751
|
+
buildFieldRef(model, field, modelAlias) {
|
|
752
|
+
const fieldDef = _zenstackhq_orm.QueryUtils.requireField(this.schema, model, field);
|
|
753
|
+
rejectParameterizedComputedField(fieldDef, model);
|
|
754
|
+
if (!fieldDef.originModel || fieldDef.originModel === model) return fieldDef.computed ? this.dialect.fieldRef(model, field, modelAlias).toOperationNode() : kysely.ReferenceNode.create(kysely.ColumnNode.create(field), kysely.TableNode.create(modelAlias));
|
|
755
|
+
return this.buildDelegateBaseFieldSelect(model, modelAlias, field, fieldDef.originModel);
|
|
742
756
|
}
|
|
743
757
|
getEvaluationBindingScope(scope) {
|
|
744
758
|
if (!scope) return;
|
|
@@ -751,7 +765,7 @@ var ExpressionTransformer = class {
|
|
|
751
765
|
return {
|
|
752
766
|
kind: "SelectQueryNode",
|
|
753
767
|
from: kysely.FromNode.create([kysely.TableNode.create(baseModel)]),
|
|
754
|
-
selections: [kysely.SelectionNode.create(
|
|
768
|
+
selections: [kysely.SelectionNode.create(this.buildFieldRef(baseModel, field, baseModel))],
|
|
755
769
|
where: kysely.WhereNode.create(conjunction(this.dialect, idFields.map((idField) => kysely.BinaryOperationNode.create(kysely.ReferenceNode.create(kysely.ColumnNode.create(idField), kysely.TableNode.create(baseModel)), kysely.OperatorNode.create("="), kysely.ReferenceNode.create(kysely.ColumnNode.create(idField), kysely.TableNode.create(modelAlias))))))
|
|
756
770
|
};
|
|
757
771
|
}
|
|
@@ -783,6 +797,50 @@ var ExpressionTransformer = class {
|
|
|
783
797
|
const fieldDef = _zenstackhq_orm.QueryUtils.getField(this.schema, context.thisType, expr.members[0]);
|
|
784
798
|
return !!fieldDef && _zenstackhq_orm.QueryUtils.isEnum(this.schema, fieldDef.type);
|
|
785
799
|
}
|
|
800
|
+
/**
|
|
801
|
+
* Whether a comparison in a create rule needs both operands compared as text.
|
|
802
|
+
*
|
|
803
|
+
* Create rules are evaluated before the insert, against a `VALUES` table built from the input
|
|
804
|
+
* (see `enforcePreCreatePolicyForOne`). That table carries enum fields as `text`, while stored
|
|
805
|
+
* columns have the native enum type, and PostgreSQL has no `text = "Enum"` operator (42883).
|
|
806
|
+
*
|
|
807
|
+
* Returns true only when one operand is an enum field of the entity being created and the
|
|
808
|
+
* other is a stored field of the same enum, e.g. for `model Grant { permission Permission }`:
|
|
809
|
+
* - `permission == anchor.defaultPermission` (bare top-level field vs. related row)
|
|
810
|
+
* - `anchor.approvals?[permission == this.permission]` (stored row vs. `this`)
|
|
811
|
+
*
|
|
812
|
+
* Not for literals (`permission == EDIT`): the name mapper still translates `@map`-ed literals.
|
|
813
|
+
* Not for enums with `@map`-ed values: the input holds names (`ACTIVE`) but stored columns
|
|
814
|
+
* hold mapped values (`active`), so a text comparison would be wrong (#2867).
|
|
815
|
+
*/
|
|
816
|
+
isCreateInputEnumComparison(left, right, context) {
|
|
817
|
+
if (context.operation !== "create") return false;
|
|
818
|
+
const leftInput = this.getCreateInputEnumField(left, context);
|
|
819
|
+
const rightInput = this.getCreateInputEnumField(right, context);
|
|
820
|
+
if (!leftInput === !rightInput) return false;
|
|
821
|
+
const inputDef = leftInput ?? rightInput;
|
|
822
|
+
return this.getFieldDefFromFieldRef(leftInput ? right : left, context)?.type === inputDef.type && !this.hasMappedEnumValues(inputDef.type);
|
|
823
|
+
}
|
|
824
|
+
/**
|
|
825
|
+
* If the expression reads an enum field of the entity being created (i.e. a column of the input
|
|
826
|
+
* `VALUES` table), returns that field's definition; otherwise `undefined`.
|
|
827
|
+
*
|
|
828
|
+
* - `this.field` always refers to the entity the rule belongs to. A longer path such as
|
|
829
|
+
* `this.anchor.defaultPermission` goes through a relation and reads a stored row.
|
|
830
|
+
* - a bare `field` refers to the entity being created only at the top level of the rule. Inside
|
|
831
|
+
* a collection predicate (`relation?[field == ...]`) it refers to the related stored row.
|
|
832
|
+
*/
|
|
833
|
+
getCreateInputEnumField(expr, context) {
|
|
834
|
+
let field;
|
|
835
|
+
if (_zenstackhq_orm_schema.ExpressionUtils.isMember(expr) && _zenstackhq_orm_schema.ExpressionUtils.isThis(expr.receiver) && expr.members.length === 1) field = expr.members[0];
|
|
836
|
+
else if (_zenstackhq_orm_schema.ExpressionUtils.isField(expr) && context.modelOrType === context.thisType && context.alias === context.thisAlias) field = expr.field;
|
|
837
|
+
const fieldDef = field ? _zenstackhq_orm.QueryUtils.getField(this.schema, context.thisType, field) : void 0;
|
|
838
|
+
return fieldDef && _zenstackhq_orm.QueryUtils.isEnum(this.schema, fieldDef.type) ? fieldDef : void 0;
|
|
839
|
+
}
|
|
840
|
+
hasMappedEnumValues(enumType) {
|
|
841
|
+
const enumDef = _zenstackhq_orm.QueryUtils.getEnum(this.schema, enumType);
|
|
842
|
+
return Object.values(enumDef?.fields ?? {}).some((f) => f.attributes?.some((attr) => attr.name === "@map"));
|
|
843
|
+
}
|
|
786
844
|
getFieldDefFromFieldRef(expr, context) {
|
|
787
845
|
return this.resolveFieldRef(expr, context)?.fieldDef;
|
|
788
846
|
}
|
|
@@ -1159,6 +1217,10 @@ var PolicyHandler = class extends kysely.OperationNodeTransformer {
|
|
|
1159
1217
|
const fieldDef = _zenstackhq_orm.QueryUtils.getField(this.client.$schema, model, f);
|
|
1160
1218
|
const owningTable = fieldDef?.originModel ?? model;
|
|
1161
1219
|
if (fieldDef?.originModel) baseModelsToJoin.add(fieldDef.originModel);
|
|
1220
|
+
if (fieldDef?.computed) {
|
|
1221
|
+
rejectParameterizedComputedField(fieldDef, model);
|
|
1222
|
+
return kysely.SelectionNode.create(kysely.AliasNode.create(this.dialect.fieldRef(owningTable, f, owningTable).toOperationNode(), kysely.IdentifierNode.create(f)));
|
|
1223
|
+
}
|
|
1162
1224
|
return kysely.SelectionNode.create(kysely.ReferenceNode.create(kysely.ColumnNode.create(f), kysely.TableNode.create(owningTable)));
|
|
1163
1225
|
});
|
|
1164
1226
|
const idFields = _zenstackhq_orm.QueryUtils.requireIdFields(this.client.$schema, model);
|