@contember/engine-content-api 2.2.0-alpha.5 → 2.2.0-alpha.7
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/development/src/acl/PredicatesInjector.cjs +1 -68
- package/dist/development/src/acl/PredicatesInjector.cjs.map +1 -1
- package/dist/development/src/acl/PredicatesInjector.js +1 -68
- package/dist/development/src/acl/PredicatesInjector.js.map +1 -1
- package/dist/production/src/acl/PredicatesInjector.cjs +1 -68
- package/dist/production/src/acl/PredicatesInjector.cjs.map +1 -1
- package/dist/production/src/acl/PredicatesInjector.js +1 -68
- package/dist/production/src/acl/PredicatesInjector.js.map +1 -1
- package/dist/tests/tsconfig.tsbuildinfo +1 -1
- package/dist/types/acl/PredicatesInjector.d.ts +0 -6
- package/dist/types/acl/PredicatesInjector.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +9 -9
- package/src/acl/PredicatesInjector.ts +1 -104
- package/tests/cases/unit/predicatesInjector.test.ts +15 -67
|
@@ -30,14 +30,7 @@ class PredicatesInjector {
|
|
|
30
30
|
if (shouldSimplify) {
|
|
31
31
|
predicatesWhere = { [entity.primary]: { always: true } };
|
|
32
32
|
} else {
|
|
33
|
-
|
|
34
|
-
predicatesWhere = this.injectPredicatesToPredicate(
|
|
35
|
-
rawPredicate,
|
|
36
|
-
entity,
|
|
37
|
-
isBackReferenceContext ?? false,
|
|
38
|
-
ancestorPath ?? [],
|
|
39
|
-
isQueryRoot
|
|
40
|
-
);
|
|
33
|
+
predicatesWhere = this.predicateFactory.create(entity, schema.Acl.Operation.read, fieldNames, relationContext, isQueryRoot);
|
|
41
34
|
}
|
|
42
35
|
const and = [where, predicatesWhere].filter((it) => Object.keys(it).length > 0);
|
|
43
36
|
if (and.length === 0) {
|
|
@@ -48,66 +41,6 @@ class PredicatesInjector {
|
|
|
48
41
|
}
|
|
49
42
|
return { and };
|
|
50
43
|
}
|
|
51
|
-
/**
|
|
52
|
-
* Processes a predicate and injects nested entity predicates for any relation traversals.
|
|
53
|
-
* This ensures that when a predicate like { department: { company: { name: 'Acme' } } }
|
|
54
|
-
* is used, the predicates of Department and Company are also applied.
|
|
55
|
-
*/
|
|
56
|
-
injectPredicatesToPredicate(where, entity, isBackReferenceContext, ancestorPath, isQueryRoot) {
|
|
57
|
-
const resultWhere = {};
|
|
58
|
-
if (where.and) {
|
|
59
|
-
resultWhere.and = where.and.filter((it) => !!it).map((it) => this.injectPredicatesToPredicate(it, entity, isBackReferenceContext, ancestorPath, isQueryRoot));
|
|
60
|
-
}
|
|
61
|
-
if (where.or) {
|
|
62
|
-
resultWhere.or = where.or.filter((it) => !!it).map((it) => this.injectPredicatesToPredicate(it, entity, isBackReferenceContext, ancestorPath, isQueryRoot));
|
|
63
|
-
}
|
|
64
|
-
if (where.not) {
|
|
65
|
-
resultWhere.not = this.injectPredicatesToPredicate(where.not, entity, isBackReferenceContext, ancestorPath, isQueryRoot);
|
|
66
|
-
}
|
|
67
|
-
const fields = Object.keys(where).filter((it) => !["and", "or", "not"].includes(it));
|
|
68
|
-
for (const field of fields) {
|
|
69
|
-
resultWhere[field] = schemaUtils.acceptFieldVisitor(this.schema, entity, field, {
|
|
70
|
-
visitColumn: () => where[field],
|
|
71
|
-
visitRelation: (context) => {
|
|
72
|
-
const relationWhere = where[field];
|
|
73
|
-
if (relationWhere === null) {
|
|
74
|
-
return null;
|
|
75
|
-
}
|
|
76
|
-
const isBackReference = this.findBackReferencedAncestor(
|
|
77
|
-
ancestorPath,
|
|
78
|
-
context.relation.name,
|
|
79
|
-
context.entity.name
|
|
80
|
-
) !== void 0;
|
|
81
|
-
const nestedIsBackReferenceContext = isBackReference || isBackReferenceContext;
|
|
82
|
-
const nestedAncestorPath = [...ancestorPath, context];
|
|
83
|
-
const processedNestedWhere = this.injectPredicatesToPredicate(
|
|
84
|
-
relationWhere,
|
|
85
|
-
context.targetEntity,
|
|
86
|
-
nestedIsBackReferenceContext,
|
|
87
|
-
nestedAncestorPath,
|
|
88
|
-
isQueryRoot
|
|
89
|
-
);
|
|
90
|
-
const shouldSimplifyNested = nestedIsBackReferenceContext && this.findBackReferencedAncestor(nestedAncestorPath, context.relation.name, context.entity.name) !== void 0;
|
|
91
|
-
const targetPredicate = shouldSimplifyNested ? { [context.targetEntity.primary]: { always: true } } : this.predicateFactory.create(context.targetEntity, schema.Acl.Operation.read, void 0, context, isQueryRoot);
|
|
92
|
-
const primaryKey = context.targetEntity.primary;
|
|
93
|
-
const nestedIsAlwaysTrue = Object.keys(processedNestedWhere).length === 1 && processedNestedWhere[primaryKey] !== void 0 && processedNestedWhere[primaryKey]?.always === true;
|
|
94
|
-
const targetIsAlwaysTrue = Object.keys(targetPredicate).length === 1 && targetPredicate[primaryKey] !== void 0 && targetPredicate[primaryKey]?.always === true;
|
|
95
|
-
if (nestedIsAlwaysTrue && targetIsAlwaysTrue) {
|
|
96
|
-
return processedNestedWhere;
|
|
97
|
-
}
|
|
98
|
-
const parts = [processedNestedWhere, targetPredicate].filter((it) => Object.keys(it).length > 0);
|
|
99
|
-
if (parts.length === 0) {
|
|
100
|
-
return {};
|
|
101
|
-
}
|
|
102
|
-
if (parts.length === 1) {
|
|
103
|
-
return parts[0];
|
|
104
|
-
}
|
|
105
|
-
return { and: parts };
|
|
106
|
-
}
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
return resultWhere;
|
|
110
|
-
}
|
|
111
44
|
injectToWhere(where, entity, isRoot, relationContext, isBackReferenceContext, ancestorPath, isQueryRoot) {
|
|
112
45
|
const resultWhere = {};
|
|
113
46
|
if (where.and) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PredicatesInjector.cjs","sources":["../../../../../packages/engine-content-api/src/acl/PredicatesInjector.ts"],"sourcesContent":["import { Acl, Input, Model, Writable } from '@contember/schema'\nimport { acceptFieldVisitor } from '@contember/schema-utils'\nimport { PredicateFactory } from './PredicateFactory.js'\n\nexport class PredicatesInjector {\n\tconstructor(private readonly schema: Model.Schema, private readonly predicateFactory: PredicateFactory) {}\n\n\tpublic inject(\n\t\tentity: Model.Entity,\n\t\twhere: Input.OptionalWhere,\n\t\trelationContext?: Model.AnyRelationContext,\n\t\tancestorPath?: readonly Model.AnyRelationContext[],\n\t): Input.OptionalWhere {\n\t\tconst isQueryRoot = !relationContext && (!ancestorPath || ancestorPath.length === 0)\n\t\tconst restrictedWhere = this.injectToWhere(where, entity, true, relationContext, false, ancestorPath ?? [], isQueryRoot)\n\t\treturn this.createWhere(entity, undefined, restrictedWhere, relationContext, false, ancestorPath ?? [], isQueryRoot)\n\t}\n\n\t/**\n\t * Finds an ancestor in the path that matches the given relation as a back-reference.\n\t * A match occurs when:\n\t * 1. The relation we're traversing has the same name as the inverse (targetRelation) of a relation in the ancestor path\n\t * 2. AND the entity where our relation is defined matches the targetEntity in the path\n\t * (to prevent false positives when different entities have relations with the same name)\n\t */\n\tprivate findBackReferencedAncestor(\n\t\tancestorPath: readonly Model.AnyRelationContext[],\n\t\trelationName: string,\n\t\trelationSourceEntityName: string,\n\t): Model.AnyRelationContext | undefined {\n\t\treturn ancestorPath.find(ctx =>\n\t\t\tctx.targetRelation?.name === relationName\n\t\t\t&& ctx.targetEntity.name === relationSourceEntityName\n\t\t)\n\t}\n\n\tprivate createWhere(\n\t\tentity: Model.Entity,\n\t\tfieldNames: string[] | undefined,\n\t\twhere: Input.OptionalWhere,\n\t\trelationContext?: Model.AnyRelationContext,\n\t\tisBackReferenceContext?: boolean,\n\t\tancestorPath?: readonly Model.AnyRelationContext[],\n\t\tisQueryRoot?: boolean,\n\t): Input.OptionalWhere {\n\t\t// Simplify predicates when:\n\t\t// 1. We're in a back-reference context (inside a filter that traverses back)\n\t\t// 2. AND the relation we traversed to get here corresponds to a relation in our query path\n\t\t// This ensures we only simplify when the traversed relation actually corresponds\n\t\t// to a relation in our query path (not just any relation to the same entity type)\n\t\tconst shouldSimplify = isBackReferenceContext === true\n\t\t\t&& ancestorPath !== undefined\n\t\t\t&& relationContext !== undefined\n\t\t\t&& this.findBackReferencedAncestor(ancestorPath, relationContext.relation.name, relationContext.entity.name) !== undefined\n\n\t\tlet predicatesWhere: Input.OptionalWhere\n\t\tif (shouldSimplify) {\n\t\t\tpredicatesWhere = { [entity.primary]: { always: true } }\n\t\t} else {\n\t\t\tconst rawPredicate = this.predicateFactory.create(entity, Acl.Operation.read, fieldNames, relationContext, isQueryRoot)\n\t\t\t// Process the predicate to inject nested entity predicates\n\t\t\tpredicatesWhere = this.injectPredicatesToPredicate(\n\t\t\t\trawPredicate,\n\t\t\t\tentity,\n\t\t\t\tisBackReferenceContext ?? false,\n\t\t\t\tancestorPath ?? [],\n\t\t\t\tisQueryRoot,\n\t\t\t)\n\t\t}\n\n\t\tconst and = [where, predicatesWhere].filter(it => Object.keys(it).length > 0)\n\t\tif (and.length === 0) {\n\t\t\treturn {}\n\t\t}\n\t\tif (and.length === 1) {\n\t\t\treturn and[0]\n\t\t}\n\t\treturn { and: and }\n\t}\n\n\t/**\n\t * Processes a predicate and injects nested entity predicates for any relation traversals.\n\t * This ensures that when a predicate like { department: { company: { name: 'Acme' } } }\n\t * is used, the predicates of Department and Company are also applied.\n\t */\n\tprivate injectPredicatesToPredicate(\n\t\twhere: Input.OptionalWhere,\n\t\tentity: Model.Entity,\n\t\tisBackReferenceContext: boolean,\n\t\tancestorPath: readonly Model.AnyRelationContext[],\n\t\tisQueryRoot?: boolean,\n\t): Input.OptionalWhere {\n\t\tconst resultWhere: Writable<Input.OptionalWhere> = {}\n\n\t\tif (where.and) {\n\t\t\tresultWhere.and = where.and\n\t\t\t\t.filter((it): it is Input.Where => !!it)\n\t\t\t\t.map(it => this.injectPredicatesToPredicate(it, entity, isBackReferenceContext, ancestorPath, isQueryRoot))\n\t\t}\n\t\tif (where.or) {\n\t\t\tresultWhere.or = where.or\n\t\t\t\t.filter((it): it is Input.Where => !!it)\n\t\t\t\t.map(it => this.injectPredicatesToPredicate(it, entity, isBackReferenceContext, ancestorPath, isQueryRoot))\n\t\t}\n\t\tif (where.not) {\n\t\t\tresultWhere.not = this.injectPredicatesToPredicate(where.not, entity, isBackReferenceContext, ancestorPath, isQueryRoot)\n\t\t}\n\n\t\tconst fields = Object.keys(where).filter(it => !['and', 'or', 'not'].includes(it))\n\n\t\tfor (const field of fields) {\n\t\t\tresultWhere[field] = acceptFieldVisitor(this.schema, entity, field, {\n\t\t\t\tvisitColumn: () => where[field],\n\t\t\t\tvisitRelation: context => {\n\t\t\t\t\tconst relationWhere = where[field] as Input.OptionalWhere | null\n\t\t\t\t\tif (relationWhere === null) {\n\t\t\t\t\t\treturn null\n\t\t\t\t\t}\n\n\t\t\t\t\t// Check if this relation is a back-reference to somewhere in our ancestor path\n\t\t\t\t\tconst isBackReference = this.findBackReferencedAncestor(\n\t\t\t\t\t\tancestorPath,\n\t\t\t\t\t\tcontext.relation.name,\n\t\t\t\t\t\tcontext.entity.name,\n\t\t\t\t\t) !== undefined\n\t\t\t\t\tconst nestedIsBackReferenceContext = isBackReference || isBackReferenceContext\n\t\t\t\t\tconst nestedAncestorPath: readonly Model.AnyRelationContext[] = [...ancestorPath, context]\n\n\t\t\t\t\t// Recursively process the nested where (always do this to handle deeper nesting)\n\t\t\t\t\tconst processedNestedWhere = this.injectPredicatesToPredicate(\n\t\t\t\t\t\trelationWhere,\n\t\t\t\t\t\tcontext.targetEntity,\n\t\t\t\t\t\tnestedIsBackReferenceContext,\n\t\t\t\t\t\tnestedAncestorPath,\n\t\t\t\t\t\tisQueryRoot,\n\t\t\t\t\t)\n\n\t\t\t\t\t// Check if we should simplify the target entity's predicate\n\t\t\t\t\tconst shouldSimplifyNested = nestedIsBackReferenceContext\n\t\t\t\t\t\t&& this.findBackReferencedAncestor(nestedAncestorPath, context.relation.name, context.entity.name) !== undefined\n\n\t\t\t\t\t// Get target entity's predicate (simplified if back-reference)\n\t\t\t\t\tconst targetPredicate = shouldSimplifyNested\n\t\t\t\t\t\t? { [context.targetEntity.primary]: { always: true } }\n\t\t\t\t\t\t: this.predicateFactory.create(context.targetEntity, Acl.Operation.read, undefined, context, isQueryRoot)\n\n\t\t\t\t\t// Optimization: avoid duplicate { id: always } when both are simplified\n\t\t\t\t\tconst primaryKey = context.targetEntity.primary\n\t\t\t\t\tconst nestedIsAlwaysTrue = Object.keys(processedNestedWhere).length === 1\n\t\t\t\t\t\t&& (processedNestedWhere as Record<string, unknown>)[primaryKey] !== undefined\n\t\t\t\t\t\t&& (processedNestedWhere as Record<string, Record<string, unknown>>)[primaryKey]?.always === true\n\t\t\t\t\tconst targetIsAlwaysTrue = Object.keys(targetPredicate).length === 1\n\t\t\t\t\t\t&& (targetPredicate as Record<string, unknown>)[primaryKey] !== undefined\n\t\t\t\t\t\t&& (targetPredicate as Record<string, Record<string, unknown>>)[primaryKey]?.always === true\n\n\t\t\t\t\tif (nestedIsAlwaysTrue && targetIsAlwaysTrue) {\n\t\t\t\t\t\treturn processedNestedWhere\n\t\t\t\t\t}\n\n\t\t\t\t\t// Combine the processed nested where with target entity's predicate\n\t\t\t\t\tconst parts = [processedNestedWhere, targetPredicate].filter(it => Object.keys(it).length > 0)\n\t\t\t\t\tif (parts.length === 0) {\n\t\t\t\t\t\treturn {}\n\t\t\t\t\t}\n\t\t\t\t\tif (parts.length === 1) {\n\t\t\t\t\t\treturn parts[0]\n\t\t\t\t\t}\n\t\t\t\t\treturn { and: parts }\n\t\t\t\t},\n\t\t\t})\n\t\t}\n\n\t\treturn resultWhere\n\t}\n\n\tprivate injectToWhere(\n\t\twhere: Input.OptionalWhere,\n\t\tentity: Model.Entity,\n\t\tisRoot: boolean,\n\t\trelationContext: Model.AnyRelationContext | undefined,\n\t\tisBackReferenceContext: boolean,\n\t\tancestorPath: readonly Model.AnyRelationContext[],\n\t\tisQueryRoot?: boolean,\n\t): Input.OptionalWhere {\n\t\tconst resultWhere: Writable<Input.OptionalWhere> = {}\n\t\tif (where.and) {\n\t\t\tresultWhere.and = where.and.filter((it): it is Input.Where => !!it).map(it =>\n\t\t\t\tthis.injectToWhere(it, entity, isRoot, relationContext, isBackReferenceContext, ancestorPath, isQueryRoot)\n\t\t\t)\n\t\t}\n\t\tif (where.or) {\n\t\t\tresultWhere.or = where.or.filter((it): it is Input.Where => !!it).map(it =>\n\t\t\t\tthis.injectToWhere(it, entity, isRoot, relationContext, isBackReferenceContext, ancestorPath, isQueryRoot)\n\t\t\t)\n\t\t}\n\t\tif (where.not) {\n\t\t\tresultWhere.not = this.injectToWhere(where.not, entity, isRoot, relationContext, isBackReferenceContext, ancestorPath, isQueryRoot)\n\t\t}\n\n\t\tconst fields = Object.keys(where).filter(it => !['and', 'or', 'not'].includes(it))\n\n\t\tif (fields.length === 0) {\n\t\t\treturn resultWhere\n\t\t}\n\t\tfor (let field of fields) {\n\t\t\tresultWhere[field] = acceptFieldVisitor(this.schema, entity, field, {\n\t\t\t\tvisitColumn: () => where[field],\n\t\t\t\tvisitRelation: context => {\n\t\t\t\t\tconst relationWhere = where[field] as Input.OptionalWhere | null\n\t\t\t\t\tif (relationWhere === null) {\n\t\t\t\t\t\treturn null\n\t\t\t\t\t}\n\t\t\t\t\t// Check if this relation is a back-reference to somewhere in our ancestor path\n\t\t\t\t\tconst isBackReference = this.findBackReferencedAncestor(ancestorPath, context.relation.name, context.entity.name) !== undefined\n\t\t\t\t\t// Once we enter a back-reference context, stay in it for nested relations\n\t\t\t\t\tconst nestedIsBackReferenceContext = isBackReference || isBackReferenceContext\n\t\t\t\t\t// Build extended ancestor path for nested traversal\n\t\t\t\t\tconst nestedAncestorPath: Model.AnyRelationContext[] = [...ancestorPath, context]\n\t\t\t\t\treturn this.injectToWhere(relationWhere, context.targetEntity, false, context, nestedIsBackReferenceContext, nestedAncestorPath, isQueryRoot)\n\t\t\t\t},\n\t\t\t})\n\t\t}\n\t\tconst fieldsForPredicate = !isRoot\n\t\t\t? fields\n\t\t\t: fields.filter(it => this.predicateFactory.shouldApplyCellLevelPredicate(entity, Acl.Operation.read, it, isQueryRoot))\n\n\t\treturn this.createWhere(entity, fieldsForPredicate, resultWhere, relationContext, isBackReferenceContext, ancestorPath, isQueryRoot)\n\t}\n}\n"],"names":["schema","Acl","acceptFieldVisitor"],"mappings":";;;;AAIO,MAAM,mBAAmB;AAAA,EAC/B,YAA6BA,SAAuC,kBAAoC;AAA3E,SAAA,SAAAA;AAAuC,SAAA,mBAAA;AAAA,EAAqC;AAAA,EAElG,OACN,QACA,OACA,iBACA,cACsB;AACtB,UAAM,cAAc,CAAC,oBAAoB,CAAC,gBAAgB,aAAa,WAAW;AAClF,UAAM,kBAAkB,KAAK,cAAc,OAAO,QAAQ,MAAM,iBAAiB,OAAO,gBAAgB,CAAA,GAAI,WAAW;AACvH,WAAO,KAAK,YAAY,QAAQ,QAAW,iBAAiB,iBAAiB,OAAO,gBAAgB,CAAA,GAAI,WAAW;AAAA,EACpH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASQ,2BACP,cACA,cACA,0BACuC;AACvC,WAAO,aAAa;AAAA,MAAK,SACxB,IAAI,gBAAgB,SAAS,gBAC1B,IAAI,aAAa,SAAS;AAAA,IAAA;AAAA,EAE/B;AAAA,EAEQ,YACP,QACA,YACA,OACA,iBACA,wBACA,cACA,aACsB;AAMtB,UAAM,iBAAiB,2BAA2B,QAC9C,iBAAiB,UACjB,oBAAoB,UACpB,KAAK,2BAA2B,cAAc,gBAAgB,SAAS,MAAM,gBAAgB,OAAO,IAAI,MAAM;AAElH,QAAI;AACJ,QAAI,gBAAgB;AACnB,wBAAkB,EAAE,CAAC,OAAO,OAAO,GAAG,EAAE,QAAQ,OAAK;AAAA,IACtD,OAAO;AACN,YAAM,eAAe,KAAK,iBAAiB,OAAO,QAAQC,WAAI,UAAU,MAAM,YAAY,iBAAiB,WAAW;AAEtH,wBAAkB,KAAK;AAAA,QACtB;AAAA,QACA;AAAA,QACA,0BAA0B;AAAA,QAC1B,gBAAgB,CAAA;AAAA,QAChB;AAAA,MAAA;AAAA,IAEF;AAEA,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qB,CAAC,SACzB,SACA,OAAO,OAAO,CAAA,OAAM,KAAK,iBAAiB,8BAA8B,QAAQD,OAAAA,IAAI,UAAU,MAAM,IAAI,WAAW,CAAC;AAEvH,WAAO,KAAK,YAAY,QAAQ,oBAAoB,aAAa,iBAAiB,wBAAwB,cAAc,WAAW;AAAA,EACpI;AACD;;"}
|
|
1
|
+
{"version":3,"file":"PredicatesInjector.cjs","sources":["../../../../../packages/engine-content-api/src/acl/PredicatesInjector.ts"],"sourcesContent":["import { Acl, Input, Model, Writable } from '@contember/schema'\nimport { acceptFieldVisitor } from '@contember/schema-utils'\nimport { PredicateFactory } from './PredicateFactory.js'\n\nexport class PredicatesInjector {\n\tconstructor(private readonly schema: Model.Schema, private readonly predicateFactory: PredicateFactory) {}\n\n\tpublic inject(\n\t\tentity: Model.Entity,\n\t\twhere: Input.OptionalWhere,\n\t\trelationContext?: Model.AnyRelationContext,\n\t\tancestorPath?: readonly Model.AnyRelationContext[],\n\t): Input.OptionalWhere {\n\t\tconst isQueryRoot = !relationContext && (!ancestorPath || ancestorPath.length === 0)\n\t\tconst restrictedWhere = this.injectToWhere(where, entity, true, relationContext, false, ancestorPath ?? [], isQueryRoot)\n\t\treturn this.createWhere(entity, undefined, restrictedWhere, relationContext, false, ancestorPath ?? [], isQueryRoot)\n\t}\n\n\t/**\n\t * Finds an ancestor in the path that matches the given relation as a back-reference.\n\t * A match occurs when:\n\t * 1. The relation we're traversing has the same name as the inverse (targetRelation) of a relation in the ancestor path\n\t * 2. AND the entity where our relation is defined matches the targetEntity in the path\n\t * (to prevent false positives when different entities have relations with the same name)\n\t */\n\tprivate findBackReferencedAncestor(\n\t\tancestorPath: readonly Model.AnyRelationContext[],\n\t\trelationName: string,\n\t\trelationSourceEntityName: string,\n\t): Model.AnyRelationContext | undefined {\n\t\treturn ancestorPath.find(ctx =>\n\t\t\tctx.targetRelation?.name === relationName\n\t\t\t&& ctx.targetEntity.name === relationSourceEntityName\n\t\t)\n\t}\n\n\tprivate createWhere(\n\t\tentity: Model.Entity,\n\t\tfieldNames: string[] | undefined,\n\t\twhere: Input.OptionalWhere,\n\t\trelationContext?: Model.AnyRelationContext,\n\t\tisBackReferenceContext?: boolean,\n\t\tancestorPath?: readonly Model.AnyRelationContext[],\n\t\tisQueryRoot?: boolean,\n\t): Input.OptionalWhere {\n\t\t// Simplify predicates when:\n\t\t// 1. We're in a back-reference context (inside a filter that traverses back)\n\t\t// 2. AND the relation we traversed to get here corresponds to a relation in our query path\n\t\t// This ensures we only simplify when the traversed relation actually corresponds\n\t\t// to a relation in our query path (not just any relation to the same entity type)\n\t\tconst shouldSimplify = isBackReferenceContext === true\n\t\t\t&& ancestorPath !== undefined\n\t\t\t&& relationContext !== undefined\n\t\t\t&& this.findBackReferencedAncestor(ancestorPath, relationContext.relation.name, relationContext.entity.name) !== undefined\n\n\t\tlet predicatesWhere: Input.OptionalWhere\n\t\tif (shouldSimplify) {\n\t\t\tpredicatesWhere = { [entity.primary]: { always: true } }\n\t\t} else {\n\t\t\tpredicatesWhere = this.predicateFactory.create(entity, Acl.Operation.read, fieldNames, relationContext, isQueryRoot)\n\t\t}\n\n\t\tconst and = [where, predicatesWhere].filter(it => Object.keys(it).length > 0)\n\t\tif (and.length === 0) {\n\t\t\treturn {}\n\t\t}\n\t\tif (and.length === 1) {\n\t\t\treturn and[0]\n\t\t}\n\t\treturn { and: and }\n\t}\n\n\tprivate injectToWhere(\n\t\twhere: Input.OptionalWhere,\n\t\tentity: Model.Entity,\n\t\tisRoot: boolean,\n\t\trelationContext: Model.AnyRelationContext | undefined,\n\t\tisBackReferenceContext: boolean,\n\t\tancestorPath: readonly Model.AnyRelationContext[],\n\t\tisQueryRoot?: boolean,\n\t): Input.OptionalWhere {\n\t\tconst resultWhere: Writable<Input.OptionalWhere> = {}\n\t\tif (where.and) {\n\t\t\tresultWhere.and = where.and.filter((it): it is Input.Where => !!it).map(it =>\n\t\t\t\tthis.injectToWhere(it, entity, isRoot, relationContext, isBackReferenceContext, ancestorPath, isQueryRoot)\n\t\t\t)\n\t\t}\n\t\tif (where.or) {\n\t\t\tresultWhere.or = where.or.filter((it): it is Input.Where => !!it).map(it =>\n\t\t\t\tthis.injectToWhere(it, entity, isRoot, relationContext, isBackReferenceContext, ancestorPath, isQueryRoot)\n\t\t\t)\n\t\t}\n\t\tif (where.not) {\n\t\t\tresultWhere.not = this.injectToWhere(where.not, entity, isRoot, relationContext, isBackReferenceContext, ancestorPath, isQueryRoot)\n\t\t}\n\n\t\tconst fields = Object.keys(where).filter(it => !['and', 'or', 'not'].includes(it))\n\n\t\tif (fields.length === 0) {\n\t\t\treturn resultWhere\n\t\t}\n\t\tfor (let field of fields) {\n\t\t\tresultWhere[field] = acceptFieldVisitor(this.schema, entity, field, {\n\t\t\t\tvisitColumn: () => where[field],\n\t\t\t\tvisitRelation: context => {\n\t\t\t\t\tconst relationWhere = where[field] as Input.OptionalWhere | null\n\t\t\t\t\tif (relationWhere === null) {\n\t\t\t\t\t\treturn null\n\t\t\t\t\t}\n\t\t\t\t\t// Check if this relation is a back-reference to somewhere in our ancestor path\n\t\t\t\t\tconst isBackReference = this.findBackReferencedAncestor(ancestorPath, context.relation.name, context.entity.name) !== undefined\n\t\t\t\t\t// Once we enter a back-reference context, stay in it for nested relations\n\t\t\t\t\tconst nestedIsBackReferenceContext = isBackReference || isBackReferenceContext\n\t\t\t\t\t// Build extended ancestor path for nested traversal\n\t\t\t\t\tconst nestedAncestorPath: Model.AnyRelationContext[] = [...ancestorPath, context]\n\t\t\t\t\treturn this.injectToWhere(relationWhere, context.targetEntity, false, context, nestedIsBackReferenceContext, nestedAncestorPath, isQueryRoot)\n\t\t\t\t},\n\t\t\t})\n\t\t}\n\t\tconst fieldsForPredicate = !isRoot\n\t\t\t? fields\n\t\t\t: fields.filter(it => this.predicateFactory.shouldApplyCellLevelPredicate(entity, Acl.Operation.read, it, isQueryRoot))\n\n\t\treturn this.createWhere(entity, fieldsForPredicate, resultWhere, relationContext, isBackReferenceContext, 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,qBAAiD,CAAC,GAAG,cAAc,OAAO;AAChF,iBAAO,KAAK,cAAc,eAAe,QAAQ,cAAc,OAAO,SAAS,8BAA8B,oBAAoB,WAAW;AAAA,QAC7I;AAAA,MAAA,CACA;AAAA,IACF;AACA,UAAM,qBAAqB,CAAC,SACzB,SACA,OAAO,OAAO,CAAA,OAAM,KAAK,iBAAiB,8BAA8B,QAAQD,OAAAA,IAAI,UAAU,MAAM,IAAI,WAAW,CAAC;AAEvH,WAAO,KAAK,YAAY,QAAQ,oBAAoB,aAAa,iBAAiB,wBAAwB,cAAc,WAAW;AAAA,EACpI;AACD;;"}
|
|
@@ -28,14 +28,7 @@ class PredicatesInjector {
|
|
|
28
28
|
if (shouldSimplify) {
|
|
29
29
|
predicatesWhere = { [entity.primary]: { always: true } };
|
|
30
30
|
} else {
|
|
31
|
-
|
|
32
|
-
predicatesWhere = this.injectPredicatesToPredicate(
|
|
33
|
-
rawPredicate,
|
|
34
|
-
entity,
|
|
35
|
-
isBackReferenceContext ?? false,
|
|
36
|
-
ancestorPath ?? [],
|
|
37
|
-
isQueryRoot
|
|
38
|
-
);
|
|
31
|
+
predicatesWhere = this.predicateFactory.create(entity, Acl.Operation.read, fieldNames, relationContext, isQueryRoot);
|
|
39
32
|
}
|
|
40
33
|
const and = [where, predicatesWhere].filter((it) => Object.keys(it).length > 0);
|
|
41
34
|
if (and.length === 0) {
|
|
@@ -46,66 +39,6 @@ class PredicatesInjector {
|
|
|
46
39
|
}
|
|
47
40
|
return { and };
|
|
48
41
|
}
|
|
49
|
-
/**
|
|
50
|
-
* Processes a predicate and injects nested entity predicates for any relation traversals.
|
|
51
|
-
* This ensures that when a predicate like { department: { company: { name: 'Acme' } } }
|
|
52
|
-
* is used, the predicates of Department and Company are also applied.
|
|
53
|
-
*/
|
|
54
|
-
injectPredicatesToPredicate(where, entity, isBackReferenceContext, ancestorPath, isQueryRoot) {
|
|
55
|
-
const resultWhere = {};
|
|
56
|
-
if (where.and) {
|
|
57
|
-
resultWhere.and = where.and.filter((it) => !!it).map((it) => this.injectPredicatesToPredicate(it, entity, isBackReferenceContext, ancestorPath, isQueryRoot));
|
|
58
|
-
}
|
|
59
|
-
if (where.or) {
|
|
60
|
-
resultWhere.or = where.or.filter((it) => !!it).map((it) => this.injectPredicatesToPredicate(it, entity, isBackReferenceContext, ancestorPath, isQueryRoot));
|
|
61
|
-
}
|
|
62
|
-
if (where.not) {
|
|
63
|
-
resultWhere.not = this.injectPredicatesToPredicate(where.not, entity, isBackReferenceContext, ancestorPath, isQueryRoot);
|
|
64
|
-
}
|
|
65
|
-
const fields = Object.keys(where).filter((it) => !["and", "or", "not"].includes(it));
|
|
66
|
-
for (const field of fields) {
|
|
67
|
-
resultWhere[field] = acceptFieldVisitor(this.schema, entity, field, {
|
|
68
|
-
visitColumn: () => where[field],
|
|
69
|
-
visitRelation: (context) => {
|
|
70
|
-
const relationWhere = where[field];
|
|
71
|
-
if (relationWhere === null) {
|
|
72
|
-
return null;
|
|
73
|
-
}
|
|
74
|
-
const isBackReference = this.findBackReferencedAncestor(
|
|
75
|
-
ancestorPath,
|
|
76
|
-
context.relation.name,
|
|
77
|
-
context.entity.name
|
|
78
|
-
) !== void 0;
|
|
79
|
-
const nestedIsBackReferenceContext = isBackReference || isBackReferenceContext;
|
|
80
|
-
const nestedAncestorPath = [...ancestorPath, context];
|
|
81
|
-
const processedNestedWhere = this.injectPredicatesToPredicate(
|
|
82
|
-
relationWhere,
|
|
83
|
-
context.targetEntity,
|
|
84
|
-
nestedIsBackReferenceContext,
|
|
85
|
-
nestedAncestorPath,
|
|
86
|
-
isQueryRoot
|
|
87
|
-
);
|
|
88
|
-
const shouldSimplifyNested = nestedIsBackReferenceContext && this.findBackReferencedAncestor(nestedAncestorPath, context.relation.name, context.entity.name) !== void 0;
|
|
89
|
-
const targetPredicate = shouldSimplifyNested ? { [context.targetEntity.primary]: { always: true } } : this.predicateFactory.create(context.targetEntity, Acl.Operation.read, void 0, context, isQueryRoot);
|
|
90
|
-
const primaryKey = context.targetEntity.primary;
|
|
91
|
-
const nestedIsAlwaysTrue = Object.keys(processedNestedWhere).length === 1 && processedNestedWhere[primaryKey] !== void 0 && processedNestedWhere[primaryKey]?.always === true;
|
|
92
|
-
const targetIsAlwaysTrue = Object.keys(targetPredicate).length === 1 && targetPredicate[primaryKey] !== void 0 && targetPredicate[primaryKey]?.always === true;
|
|
93
|
-
if (nestedIsAlwaysTrue && targetIsAlwaysTrue) {
|
|
94
|
-
return processedNestedWhere;
|
|
95
|
-
}
|
|
96
|
-
const parts = [processedNestedWhere, targetPredicate].filter((it) => Object.keys(it).length > 0);
|
|
97
|
-
if (parts.length === 0) {
|
|
98
|
-
return {};
|
|
99
|
-
}
|
|
100
|
-
if (parts.length === 1) {
|
|
101
|
-
return parts[0];
|
|
102
|
-
}
|
|
103
|
-
return { and: parts };
|
|
104
|
-
}
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
return resultWhere;
|
|
108
|
-
}
|
|
109
42
|
injectToWhere(where, entity, isRoot, relationContext, isBackReferenceContext, ancestorPath, isQueryRoot) {
|
|
110
43
|
const resultWhere = {};
|
|
111
44
|
if (where.and) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PredicatesInjector.js","sources":["../../../../../packages/engine-content-api/src/acl/PredicatesInjector.ts"],"sourcesContent":["import { Acl, Input, Model, Writable } from '@contember/schema'\nimport { acceptFieldVisitor } from '@contember/schema-utils'\nimport { PredicateFactory } from './PredicateFactory.js'\n\nexport class PredicatesInjector {\n\tconstructor(private readonly schema: Model.Schema, private readonly predicateFactory: PredicateFactory) {}\n\n\tpublic inject(\n\t\tentity: Model.Entity,\n\t\twhere: Input.OptionalWhere,\n\t\trelationContext?: Model.AnyRelationContext,\n\t\tancestorPath?: readonly Model.AnyRelationContext[],\n\t): Input.OptionalWhere {\n\t\tconst isQueryRoot = !relationContext && (!ancestorPath || ancestorPath.length === 0)\n\t\tconst restrictedWhere = this.injectToWhere(where, entity, true, relationContext, false, ancestorPath ?? [], isQueryRoot)\n\t\treturn this.createWhere(entity, undefined, restrictedWhere, relationContext, false, ancestorPath ?? [], isQueryRoot)\n\t}\n\n\t/**\n\t * Finds an ancestor in the path that matches the given relation as a back-reference.\n\t * A match occurs when:\n\t * 1. The relation we're traversing has the same name as the inverse (targetRelation) of a relation in the ancestor path\n\t * 2. AND the entity where our relation is defined matches the targetEntity in the path\n\t * (to prevent false positives when different entities have relations with the same name)\n\t */\n\tprivate findBackReferencedAncestor(\n\t\tancestorPath: readonly Model.AnyRelationContext[],\n\t\trelationName: string,\n\t\trelationSourceEntityName: string,\n\t): Model.AnyRelationContext | undefined {\n\t\treturn ancestorPath.find(ctx =>\n\t\t\tctx.targetRelation?.name === relationName\n\t\t\t&& ctx.targetEntity.name === relationSourceEntityName\n\t\t)\n\t}\n\n\tprivate createWhere(\n\t\tentity: Model.Entity,\n\t\tfieldNames: string[] | undefined,\n\t\twhere: Input.OptionalWhere,\n\t\trelationContext?: Model.AnyRelationContext,\n\t\tisBackReferenceContext?: boolean,\n\t\tancestorPath?: readonly Model.AnyRelationContext[],\n\t\tisQueryRoot?: boolean,\n\t): Input.OptionalWhere {\n\t\t// Simplify predicates when:\n\t\t// 1. We're in a back-reference context (inside a filter that traverses back)\n\t\t// 2. AND the relation we traversed to get here corresponds to a relation in our query path\n\t\t// This ensures we only simplify when the traversed relation actually corresponds\n\t\t// to a relation in our query path (not just any relation to the same entity type)\n\t\tconst shouldSimplify = isBackReferenceContext === true\n\t\t\t&& ancestorPath !== undefined\n\t\t\t&& relationContext !== undefined\n\t\t\t&& this.findBackReferencedAncestor(ancestorPath, relationContext.relation.name, relationContext.entity.name) !== undefined\n\n\t\tlet predicatesWhere: Input.OptionalWhere\n\t\tif (shouldSimplify) {\n\t\t\tpredicatesWhere = { [entity.primary]: { always: true } }\n\t\t} else {\n\t\t\tconst rawPredicate = this.predicateFactory.create(entity, Acl.Operation.read, fieldNames, relationContext, isQueryRoot)\n\t\t\t// Process the predicate to inject nested entity predicates\n\t\t\tpredicatesWhere = this.injectPredicatesToPredicate(\n\t\t\t\trawPredicate,\n\t\t\t\tentity,\n\t\t\t\tisBackReferenceContext ?? false,\n\t\t\t\tancestorPath ?? [],\n\t\t\t\tisQueryRoot,\n\t\t\t)\n\t\t}\n\n\t\tconst and = [where, predicatesWhere].filter(it => Object.keys(it).length > 0)\n\t\tif (and.length === 0) {\n\t\t\treturn {}\n\t\t}\n\t\tif (and.length === 1) {\n\t\t\treturn and[0]\n\t\t}\n\t\treturn { and: and }\n\t}\n\n\t/**\n\t * Processes a predicate and injects nested entity predicates for any relation traversals.\n\t * This ensures that when a predicate like { department: { company: { name: 'Acme' } } }\n\t * is used, the predicates of Department and Company are also applied.\n\t */\n\tprivate injectPredicatesToPredicate(\n\t\twhere: Input.OptionalWhere,\n\t\tentity: Model.Entity,\n\t\tisBackReferenceContext: boolean,\n\t\tancestorPath: readonly Model.AnyRelationContext[],\n\t\tisQueryRoot?: boolean,\n\t): Input.OptionalWhere {\n\t\tconst resultWhere: Writable<Input.OptionalWhere> = {}\n\n\t\tif (where.and) {\n\t\t\tresultWhere.and = where.and\n\t\t\t\t.filter((it): it is Input.Where => !!it)\n\t\t\t\t.map(it => this.injectPredicatesToPredicate(it, entity, isBackReferenceContext, ancestorPath, isQueryRoot))\n\t\t}\n\t\tif (where.or) {\n\t\t\tresultWhere.or = where.or\n\t\t\t\t.filter((it): it is Input.Where => !!it)\n\t\t\t\t.map(it => this.injectPredicatesToPredicate(it, entity, isBackReferenceContext, ancestorPath, isQueryRoot))\n\t\t}\n\t\tif (where.not) {\n\t\t\tresultWhere.not = this.injectPredicatesToPredicate(where.not, entity, isBackReferenceContext, ancestorPath, isQueryRoot)\n\t\t}\n\n\t\tconst fields = Object.keys(where).filter(it => !['and', 'or', 'not'].includes(it))\n\n\t\tfor (const field of fields) {\n\t\t\tresultWhere[field] = acceptFieldVisitor(this.schema, entity, field, {\n\t\t\t\tvisitColumn: () => where[field],\n\t\t\t\tvisitRelation: context => {\n\t\t\t\t\tconst relationWhere = where[field] as Input.OptionalWhere | null\n\t\t\t\t\tif (relationWhere === null) {\n\t\t\t\t\t\treturn null\n\t\t\t\t\t}\n\n\t\t\t\t\t// Check if this relation is a back-reference to somewhere in our ancestor path\n\t\t\t\t\tconst isBackReference = this.findBackReferencedAncestor(\n\t\t\t\t\t\tancestorPath,\n\t\t\t\t\t\tcontext.relation.name,\n\t\t\t\t\t\tcontext.entity.name,\n\t\t\t\t\t) !== undefined\n\t\t\t\t\tconst nestedIsBackReferenceContext = isBackReference || isBackReferenceContext\n\t\t\t\t\tconst nestedAncestorPath: readonly Model.AnyRelationContext[] = [...ancestorPath, context]\n\n\t\t\t\t\t// Recursively process the nested where (always do this to handle deeper nesting)\n\t\t\t\t\tconst processedNestedWhere = this.injectPredicatesToPredicate(\n\t\t\t\t\t\trelationWhere,\n\t\t\t\t\t\tcontext.targetEntity,\n\t\t\t\t\t\tnestedIsBackReferenceContext,\n\t\t\t\t\t\tnestedAncestorPath,\n\t\t\t\t\t\tisQueryRoot,\n\t\t\t\t\t)\n\n\t\t\t\t\t// Check if we should simplify the target entity's predicate\n\t\t\t\t\tconst shouldSimplifyNested = nestedIsBackReferenceContext\n\t\t\t\t\t\t&& this.findBackReferencedAncestor(nestedAncestorPath, context.relation.name, context.entity.name) !== undefined\n\n\t\t\t\t\t// Get target entity's predicate (simplified if back-reference)\n\t\t\t\t\tconst targetPredicate = shouldSimplifyNested\n\t\t\t\t\t\t? { [context.targetEntity.primary]: { always: true } }\n\t\t\t\t\t\t: this.predicateFactory.create(context.targetEntity, Acl.Operation.read, undefined, context, isQueryRoot)\n\n\t\t\t\t\t// Optimization: avoid duplicate { id: always } when both are simplified\n\t\t\t\t\tconst primaryKey = context.targetEntity.primary\n\t\t\t\t\tconst nestedIsAlwaysTrue = Object.keys(processedNestedWhere).length === 1\n\t\t\t\t\t\t&& (processedNestedWhere as Record<string, unknown>)[primaryKey] !== undefined\n\t\t\t\t\t\t&& (processedNestedWhere as Record<string, Record<string, unknown>>)[primaryKey]?.always === true\n\t\t\t\t\tconst targetIsAlwaysTrue = Object.keys(targetPredicate).length === 1\n\t\t\t\t\t\t&& (targetPredicate as Record<string, unknown>)[primaryKey] !== undefined\n\t\t\t\t\t\t&& (targetPredicate as Record<string, Record<string, unknown>>)[primaryKey]?.always === true\n\n\t\t\t\t\tif (nestedIsAlwaysTrue && targetIsAlwaysTrue) {\n\t\t\t\t\t\treturn processedNestedWhere\n\t\t\t\t\t}\n\n\t\t\t\t\t// Combine the processed nested where with target entity's predicate\n\t\t\t\t\tconst parts = [processedNestedWhere, targetPredicate].filter(it => Object.keys(it).length > 0)\n\t\t\t\t\tif (parts.length === 0) {\n\t\t\t\t\t\treturn {}\n\t\t\t\t\t}\n\t\t\t\t\tif (parts.length === 1) {\n\t\t\t\t\t\treturn parts[0]\n\t\t\t\t\t}\n\t\t\t\t\treturn { and: parts }\n\t\t\t\t},\n\t\t\t})\n\t\t}\n\n\t\treturn resultWhere\n\t}\n\n\tprivate injectToWhere(\n\t\twhere: Input.OptionalWhere,\n\t\tentity: Model.Entity,\n\t\tisRoot: boolean,\n\t\trelationContext: Model.AnyRelationContext | undefined,\n\t\tisBackReferenceContext: boolean,\n\t\tancestorPath: readonly Model.AnyRelationContext[],\n\t\tisQueryRoot?: boolean,\n\t): Input.OptionalWhere {\n\t\tconst resultWhere: Writable<Input.OptionalWhere> = {}\n\t\tif (where.and) {\n\t\t\tresultWhere.and = where.and.filter((it): it is Input.Where => !!it).map(it =>\n\t\t\t\tthis.injectToWhere(it, entity, isRoot, relationContext, isBackReferenceContext, ancestorPath, isQueryRoot)\n\t\t\t)\n\t\t}\n\t\tif (where.or) {\n\t\t\tresultWhere.or = where.or.filter((it): it is Input.Where => !!it).map(it =>\n\t\t\t\tthis.injectToWhere(it, entity, isRoot, relationContext, isBackReferenceContext, ancestorPath, isQueryRoot)\n\t\t\t)\n\t\t}\n\t\tif (where.not) {\n\t\t\tresultWhere.not = this.injectToWhere(where.not, entity, isRoot, relationContext, isBackReferenceContext, ancestorPath, isQueryRoot)\n\t\t}\n\n\t\tconst fields = Object.keys(where).filter(it => !['and', 'or', 'not'].includes(it))\n\n\t\tif (fields.length === 0) {\n\t\t\treturn resultWhere\n\t\t}\n\t\tfor (let field of fields) {\n\t\t\tresultWhere[field] = acceptFieldVisitor(this.schema, entity, field, {\n\t\t\t\tvisitColumn: () => where[field],\n\t\t\t\tvisitRelation: context => {\n\t\t\t\t\tconst relationWhere = where[field] as Input.OptionalWhere | null\n\t\t\t\t\tif (relationWhere === null) {\n\t\t\t\t\t\treturn null\n\t\t\t\t\t}\n\t\t\t\t\t// Check if this relation is a back-reference to somewhere in our ancestor path\n\t\t\t\t\tconst isBackReference = this.findBackReferencedAncestor(ancestorPath, context.relation.name, context.entity.name) !== undefined\n\t\t\t\t\t// Once we enter a back-reference context, stay in it for nested relations\n\t\t\t\t\tconst nestedIsBackReferenceContext = isBackReference || isBackReferenceContext\n\t\t\t\t\t// Build extended ancestor path for nested traversal\n\t\t\t\t\tconst nestedAncestorPath: Model.AnyRelationContext[] = [...ancestorPath, context]\n\t\t\t\t\treturn this.injectToWhere(relationWhere, context.targetEntity, false, context, nestedIsBackReferenceContext, nestedAncestorPath, isQueryRoot)\n\t\t\t\t},\n\t\t\t})\n\t\t}\n\t\tconst fieldsForPredicate = !isRoot\n\t\t\t? fields\n\t\t\t: fields.filter(it => this.predicateFactory.shouldApplyCellLevelPredicate(entity, Acl.Operation.read, it, isQueryRoot))\n\n\t\treturn this.createWhere(entity, fieldsForPredicate, resultWhere, relationContext, isBackReferenceContext, ancestorPath, isQueryRoot)\n\t}\n}\n"],"names":[],"mappings":";;AAIO,MAAM,mBAAmB;AAAA,EAC/B,YAA6B,QAAuC,kBAAoC;AAA3E,SAAA,SAAA;AAAuC,SAAA,mBAAA;AAAA,EAAqC;AAAA,EAElG,OACN,QACA,OACA,iBACA,cACsB;AACtB,UAAM,cAAc,CAAC,oBAAo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IAAI,QAAQ,QAAQ,iBAAiB,wBAAwB,cAAc,WAAW;AAAA,MAAA;AAAA,IAE3G;AACA,QAAI,MAAM,KAAK;AACd,kBAAY,MAAM,KAAK,cAAc,MAAM,KAAK,QAAQ,QAAQ,iBAAiB,wBAAwB,cAAc,WAAW;AAAA,IACnI;AAEA,UAAM,SAAS,OAAO,KAAK,KAAK,EAAE,OAAO,CAAA,OAAM,CAAC,CAAC,OAAO,MAAM,KAAK,EAAE,SAAS,EAAE,CAAC;AAEjF,QAAI,OAAO,WAAW,GAAG;AACxB,aAAO;AAAA,IACR;AACA,aAAS,SAAS,QAAQ;AACzB,kBAAY,KAAK,IAAI,mBAAmB,KAAK,QAAQ,QAAQ,OAAO;AAAA,QACnE,aAAa,MAAM,MAAM,KAAK;AAAA,QAC9B,eAAe,CAAA,YAAW;AACzB,gBAAM,gBAAgB,MAAM,KAAK;AACjC,cAAI,kBAAkB,MAAM;AAC3B,mBAAO;AAAA,UACR;AAEA,gBAAM,kBAAkB,KAAK,2BAA2B,cAAc,QAAQ,SAAS,MAAM,QAAQ,OAAO,IAAI,MAAM;AAEtH,gBAAM,+BAA+B,mBAAmB;AAExD,gBAAM,qBAAiD,CAAC,GAAG,cAAc,OAAO;AAChF,iBAAO,KAAK,cAAc,eAAe,QAAQ,cAAc,OAAO,SAAS,8BAA8B,oBAAoB,WAAW;AAAA,QAC7I;AAAA,MAAA,CACA;AAAA,IACF;AACA,UAAM,qBAAqB,CAAC,SACzB,SACA,OAAO,OAAO,CAAA,OAAM,KAAK,iBAAiB,8BAA8B,QAAQ,IAAI,UAAU,MAAM,IAAI,WAAW,CAAC;AAEvH,WAAO,KAAK,YAAY,QAAQ,oBAAoB,aAAa,iBAAiB,wBAAwB,cAAc,WAAW;AAAA,EACpI;AACD;"}
|
|
1
|
+
{"version":3,"file":"PredicatesInjector.js","sources":["../../../../../packages/engine-content-api/src/acl/PredicatesInjector.ts"],"sourcesContent":["import { Acl, Input, Model, Writable } from '@contember/schema'\nimport { acceptFieldVisitor } from '@contember/schema-utils'\nimport { PredicateFactory } from './PredicateFactory.js'\n\nexport class PredicatesInjector {\n\tconstructor(private readonly schema: Model.Schema, private readonly predicateFactory: PredicateFactory) {}\n\n\tpublic inject(\n\t\tentity: Model.Entity,\n\t\twhere: Input.OptionalWhere,\n\t\trelationContext?: Model.AnyRelationContext,\n\t\tancestorPath?: readonly Model.AnyRelationContext[],\n\t): Input.OptionalWhere {\n\t\tconst isQueryRoot = !relationContext && (!ancestorPath || ancestorPath.length === 0)\n\t\tconst restrictedWhere = this.injectToWhere(where, entity, true, relationContext, false, ancestorPath ?? [], isQueryRoot)\n\t\treturn this.createWhere(entity, undefined, restrictedWhere, relationContext, false, ancestorPath ?? [], isQueryRoot)\n\t}\n\n\t/**\n\t * Finds an ancestor in the path that matches the given relation as a back-reference.\n\t * A match occurs when:\n\t * 1. The relation we're traversing has the same name as the inverse (targetRelation) of a relation in the ancestor path\n\t * 2. AND the entity where our relation is defined matches the targetEntity in the path\n\t * (to prevent false positives when different entities have relations with the same name)\n\t */\n\tprivate findBackReferencedAncestor(\n\t\tancestorPath: readonly Model.AnyRelationContext[],\n\t\trelationName: string,\n\t\trelationSourceEntityName: string,\n\t): Model.AnyRelationContext | undefined {\n\t\treturn ancestorPath.find(ctx =>\n\t\t\tctx.targetRelation?.name === relationName\n\t\t\t&& ctx.targetEntity.name === relationSourceEntityName\n\t\t)\n\t}\n\n\tprivate createWhere(\n\t\tentity: Model.Entity,\n\t\tfieldNames: string[] | undefined,\n\t\twhere: Input.OptionalWhere,\n\t\trelationContext?: Model.AnyRelationContext,\n\t\tisBackReferenceContext?: boolean,\n\t\tancestorPath?: readonly Model.AnyRelationContext[],\n\t\tisQueryRoot?: boolean,\n\t): Input.OptionalWhere {\n\t\t// Simplify predicates when:\n\t\t// 1. We're in a back-reference context (inside a filter that traverses back)\n\t\t// 2. AND the relation we traversed to get here corresponds to a relation in our query path\n\t\t// This ensures we only simplify when the traversed relation actually corresponds\n\t\t// to a relation in our query path (not just any relation to the same entity type)\n\t\tconst shouldSimplify = isBackReferenceContext === true\n\t\t\t&& ancestorPath !== undefined\n\t\t\t&& relationContext !== undefined\n\t\t\t&& this.findBackReferencedAncestor(ancestorPath, relationContext.relation.name, relationContext.entity.name) !== undefined\n\n\t\tlet predicatesWhere: Input.OptionalWhere\n\t\tif (shouldSimplify) {\n\t\t\tpredicatesWhere = { [entity.primary]: { always: true } }\n\t\t} else {\n\t\t\tpredicatesWhere = this.predicateFactory.create(entity, Acl.Operation.read, fieldNames, relationContext, isQueryRoot)\n\t\t}\n\n\t\tconst and = [where, predicatesWhere].filter(it => Object.keys(it).length > 0)\n\t\tif (and.length === 0) {\n\t\t\treturn {}\n\t\t}\n\t\tif (and.length === 1) {\n\t\t\treturn and[0]\n\t\t}\n\t\treturn { and: and }\n\t}\n\n\tprivate injectToWhere(\n\t\twhere: Input.OptionalWhere,\n\t\tentity: Model.Entity,\n\t\tisRoot: boolean,\n\t\trelationContext: Model.AnyRelationContext | undefined,\n\t\tisBackReferenceContext: boolean,\n\t\tancestorPath: readonly Model.AnyRelationContext[],\n\t\tisQueryRoot?: boolean,\n\t): Input.OptionalWhere {\n\t\tconst resultWhere: Writable<Input.OptionalWhere> = {}\n\t\tif (where.and) {\n\t\t\tresultWhere.and = where.and.filter((it): it is Input.Where => !!it).map(it =>\n\t\t\t\tthis.injectToWhere(it, entity, isRoot, relationContext, isBackReferenceContext, ancestorPath, isQueryRoot)\n\t\t\t)\n\t\t}\n\t\tif (where.or) {\n\t\t\tresultWhere.or = where.or.filter((it): it is Input.Where => !!it).map(it =>\n\t\t\t\tthis.injectToWhere(it, entity, isRoot, relationContext, isBackReferenceContext, ancestorPath, isQueryRoot)\n\t\t\t)\n\t\t}\n\t\tif (where.not) {\n\t\t\tresultWhere.not = this.injectToWhere(where.not, entity, isRoot, relationContext, isBackReferenceContext, ancestorPath, isQueryRoot)\n\t\t}\n\n\t\tconst fields = Object.keys(where).filter(it => !['and', 'or', 'not'].includes(it))\n\n\t\tif (fields.length === 0) {\n\t\t\treturn resultWhere\n\t\t}\n\t\tfor (let field of fields) {\n\t\t\tresultWhere[field] = acceptFieldVisitor(this.schema, entity, field, {\n\t\t\t\tvisitColumn: () => where[field],\n\t\t\t\tvisitRelation: context => {\n\t\t\t\t\tconst relationWhere = where[field] as Input.OptionalWhere | null\n\t\t\t\t\tif (relationWhere === null) {\n\t\t\t\t\t\treturn null\n\t\t\t\t\t}\n\t\t\t\t\t// Check if this relation is a back-reference to somewhere in our ancestor path\n\t\t\t\t\tconst isBackReference = this.findBackReferencedAncestor(ancestorPath, context.relation.name, context.entity.name) !== undefined\n\t\t\t\t\t// Once we enter a back-reference context, stay in it for nested relations\n\t\t\t\t\tconst nestedIsBackReferenceContext = isBackReference || isBackReferenceContext\n\t\t\t\t\t// Build extended ancestor path for nested traversal\n\t\t\t\t\tconst nestedAncestorPath: Model.AnyRelationContext[] = [...ancestorPath, context]\n\t\t\t\t\treturn this.injectToWhere(relationWhere, context.targetEntity, false, context, nestedIsBackReferenceContext, nestedAncestorPath, isQueryRoot)\n\t\t\t\t},\n\t\t\t})\n\t\t}\n\t\tconst fieldsForPredicate = !isRoot\n\t\t\t? fields\n\t\t\t: fields.filter(it => this.predicateFactory.shouldApplyCellLevelPredicate(entity, Acl.Operation.read, it, isQueryRoot))\n\n\t\treturn this.createWhere(entity, fieldsForPredicate, resultWhere, relationContext, isBackReferenceContext, ancestorPath, isQueryRoot)\n\t}\n}\n"],"names":[],"mappings":";;AAIO,MAAM,mBAAmB;AAAA,EAC/B,YAA6B,QAAuC,kBAAoC;AAA3E,SAAA,SAAA;AAAuC,SAAA,mBAAA;AAAA,EAAqC;AAAA,EAElG,OACN,QACA,OACA,iBACA,cACsB;AACtB,UAAM,cAAc,CAAC,oBAAoB,CAAC,gBAAgB,aAAa,WAAW;AAClF,UAAM,kBAAkB,KAAK,cAAc,OAAO,QAAQ,MAAM,iBAAiB,OAAO,gBAAgB,CAAA,GAAI,WAA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
|
@@ -30,14 +30,7 @@ class PredicatesInjector {
|
|
|
30
30
|
if (shouldSimplify) {
|
|
31
31
|
predicatesWhere = { [entity.primary]: { always: true } };
|
|
32
32
|
} else {
|
|
33
|
-
|
|
34
|
-
predicatesWhere = this.injectPredicatesToPredicate(
|
|
35
|
-
rawPredicate,
|
|
36
|
-
entity,
|
|
37
|
-
isBackReferenceContext ?? false,
|
|
38
|
-
ancestorPath ?? [],
|
|
39
|
-
isQueryRoot
|
|
40
|
-
);
|
|
33
|
+
predicatesWhere = this.predicateFactory.create(entity, schema.Acl.Operation.read, fieldNames, relationContext, isQueryRoot);
|
|
41
34
|
}
|
|
42
35
|
const and = [where, predicatesWhere].filter((it) => Object.keys(it).length > 0);
|
|
43
36
|
if (and.length === 0) {
|
|
@@ -48,66 +41,6 @@ class PredicatesInjector {
|
|
|
48
41
|
}
|
|
49
42
|
return { and };
|
|
50
43
|
}
|
|
51
|
-
/**
|
|
52
|
-
* Processes a predicate and injects nested entity predicates for any relation traversals.
|
|
53
|
-
* This ensures that when a predicate like { department: { company: { name: 'Acme' } } }
|
|
54
|
-
* is used, the predicates of Department and Company are also applied.
|
|
55
|
-
*/
|
|
56
|
-
injectPredicatesToPredicate(where, entity, isBackReferenceContext, ancestorPath, isQueryRoot) {
|
|
57
|
-
const resultWhere = {};
|
|
58
|
-
if (where.and) {
|
|
59
|
-
resultWhere.and = where.and.filter((it) => !!it).map((it) => this.injectPredicatesToPredicate(it, entity, isBackReferenceContext, ancestorPath, isQueryRoot));
|
|
60
|
-
}
|
|
61
|
-
if (where.or) {
|
|
62
|
-
resultWhere.or = where.or.filter((it) => !!it).map((it) => this.injectPredicatesToPredicate(it, entity, isBackReferenceContext, ancestorPath, isQueryRoot));
|
|
63
|
-
}
|
|
64
|
-
if (where.not) {
|
|
65
|
-
resultWhere.not = this.injectPredicatesToPredicate(where.not, entity, isBackReferenceContext, ancestorPath, isQueryRoot);
|
|
66
|
-
}
|
|
67
|
-
const fields = Object.keys(where).filter((it) => !["and", "or", "not"].includes(it));
|
|
68
|
-
for (const field of fields) {
|
|
69
|
-
resultWhere[field] = schemaUtils.acceptFieldVisitor(this.schema, entity, field, {
|
|
70
|
-
visitColumn: () => where[field],
|
|
71
|
-
visitRelation: (context) => {
|
|
72
|
-
const relationWhere = where[field];
|
|
73
|
-
if (relationWhere === null) {
|
|
74
|
-
return null;
|
|
75
|
-
}
|
|
76
|
-
const isBackReference = this.findBackReferencedAncestor(
|
|
77
|
-
ancestorPath,
|
|
78
|
-
context.relation.name,
|
|
79
|
-
context.entity.name
|
|
80
|
-
) !== void 0;
|
|
81
|
-
const nestedIsBackReferenceContext = isBackReference || isBackReferenceContext;
|
|
82
|
-
const nestedAncestorPath = [...ancestorPath, context];
|
|
83
|
-
const processedNestedWhere = this.injectPredicatesToPredicate(
|
|
84
|
-
relationWhere,
|
|
85
|
-
context.targetEntity,
|
|
86
|
-
nestedIsBackReferenceContext,
|
|
87
|
-
nestedAncestorPath,
|
|
88
|
-
isQueryRoot
|
|
89
|
-
);
|
|
90
|
-
const shouldSimplifyNested = nestedIsBackReferenceContext && this.findBackReferencedAncestor(nestedAncestorPath, context.relation.name, context.entity.name) !== void 0;
|
|
91
|
-
const targetPredicate = shouldSimplifyNested ? { [context.targetEntity.primary]: { always: true } } : this.predicateFactory.create(context.targetEntity, schema.Acl.Operation.read, void 0, context, isQueryRoot);
|
|
92
|
-
const primaryKey = context.targetEntity.primary;
|
|
93
|
-
const nestedIsAlwaysTrue = Object.keys(processedNestedWhere).length === 1 && processedNestedWhere[primaryKey] !== void 0 && processedNestedWhere[primaryKey]?.always === true;
|
|
94
|
-
const targetIsAlwaysTrue = Object.keys(targetPredicate).length === 1 && targetPredicate[primaryKey] !== void 0 && targetPredicate[primaryKey]?.always === true;
|
|
95
|
-
if (nestedIsAlwaysTrue && targetIsAlwaysTrue) {
|
|
96
|
-
return processedNestedWhere;
|
|
97
|
-
}
|
|
98
|
-
const parts = [processedNestedWhere, targetPredicate].filter((it) => Object.keys(it).length > 0);
|
|
99
|
-
if (parts.length === 0) {
|
|
100
|
-
return {};
|
|
101
|
-
}
|
|
102
|
-
if (parts.length === 1) {
|
|
103
|
-
return parts[0];
|
|
104
|
-
}
|
|
105
|
-
return { and: parts };
|
|
106
|
-
}
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
return resultWhere;
|
|
110
|
-
}
|
|
111
44
|
injectToWhere(where, entity, isRoot, relationContext, isBackReferenceContext, ancestorPath, isQueryRoot) {
|
|
112
45
|
const resultWhere = {};
|
|
113
46
|
if (where.and) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PredicatesInjector.cjs","sources":["../../../../../packages/engine-content-api/src/acl/PredicatesInjector.ts"],"sourcesContent":["import { Acl, Input, Model, Writable } from '@contember/schema'\nimport { acceptFieldVisitor } from '@contember/schema-utils'\nimport { PredicateFactory } from './PredicateFactory.js'\n\nexport class PredicatesInjector {\n\tconstructor(private readonly schema: Model.Schema, private readonly predicateFactory: PredicateFactory) {}\n\n\tpublic inject(\n\t\tentity: Model.Entity,\n\t\twhere: Input.OptionalWhere,\n\t\trelationContext?: Model.AnyRelationContext,\n\t\tancestorPath?: readonly Model.AnyRelationContext[],\n\t): Input.OptionalWhere {\n\t\tconst isQueryRoot = !relationContext && (!ancestorPath || ancestorPath.length === 0)\n\t\tconst restrictedWhere = this.injectToWhere(where, entity, true, relationContext, false, ancestorPath ?? [], isQueryRoot)\n\t\treturn this.createWhere(entity, undefined, restrictedWhere, relationContext, false, ancestorPath ?? [], isQueryRoot)\n\t}\n\n\t/**\n\t * Finds an ancestor in the path that matches the given relation as a back-reference.\n\t * A match occurs when:\n\t * 1. The relation we're traversing has the same name as the inverse (targetRelation) of a relation in the ancestor path\n\t * 2. AND the entity where our relation is defined matches the targetEntity in the path\n\t * (to prevent false positives when different entities have relations with the same name)\n\t */\n\tprivate findBackReferencedAncestor(\n\t\tancestorPath: readonly Model.AnyRelationContext[],\n\t\trelationName: string,\n\t\trelationSourceEntityName: string,\n\t): Model.AnyRelationContext | undefined {\n\t\treturn ancestorPath.find(ctx =>\n\t\t\tctx.targetRelation?.name === relationName\n\t\t\t&& ctx.targetEntity.name === relationSourceEntityName\n\t\t)\n\t}\n\n\tprivate createWhere(\n\t\tentity: Model.Entity,\n\t\tfieldNames: string[] | undefined,\n\t\twhere: Input.OptionalWhere,\n\t\trelationContext?: Model.AnyRelationContext,\n\t\tisBackReferenceContext?: boolean,\n\t\tancestorPath?: readonly Model.AnyRelationContext[],\n\t\tisQueryRoot?: boolean,\n\t): Input.OptionalWhere {\n\t\t// Simplify predicates when:\n\t\t// 1. We're in a back-reference context (inside a filter that traverses back)\n\t\t// 2. AND the relation we traversed to get here corresponds to a relation in our query path\n\t\t// This ensures we only simplify when the traversed relation actually corresponds\n\t\t// to a relation in our query path (not just any relation to the same entity type)\n\t\tconst shouldSimplify = isBackReferenceContext === true\n\t\t\t&& ancestorPath !== undefined\n\t\t\t&& relationContext !== undefined\n\t\t\t&& this.findBackReferencedAncestor(ancestorPath, relationContext.relation.name, relationContext.entity.name) !== undefined\n\n\t\tlet predicatesWhere: Input.OptionalWhere\n\t\tif (shouldSimplify) {\n\t\t\tpredicatesWhere = { [entity.primary]: { always: true } }\n\t\t} else {\n\t\t\tconst rawPredicate = this.predicateFactory.create(entity, Acl.Operation.read, fieldNames, relationContext, isQueryRoot)\n\t\t\t// Process the predicate to inject nested entity predicates\n\t\t\tpredicatesWhere = this.injectPredicatesToPredicate(\n\t\t\t\trawPredicate,\n\t\t\t\tentity,\n\t\t\t\tisBackReferenceContext ?? false,\n\t\t\t\tancestorPath ?? [],\n\t\t\t\tisQueryRoot,\n\t\t\t)\n\t\t}\n\n\t\tconst and = [where, predicatesWhere].filter(it => Object.keys(it).length > 0)\n\t\tif (and.length === 0) {\n\t\t\treturn {}\n\t\t}\n\t\tif (and.length === 1) {\n\t\t\treturn and[0]\n\t\t}\n\t\treturn { and: and }\n\t}\n\n\t/**\n\t * Processes a predicate and injects nested entity predicates for any relation traversals.\n\t * This ensures that when a predicate like { department: { company: { name: 'Acme' } } }\n\t * is used, the predicates of Department and Company are also applied.\n\t */\n\tprivate injectPredicatesToPredicate(\n\t\twhere: Input.OptionalWhere,\n\t\tentity: Model.Entity,\n\t\tisBackReferenceContext: boolean,\n\t\tancestorPath: readonly Model.AnyRelationContext[],\n\t\tisQueryRoot?: boolean,\n\t): Input.OptionalWhere {\n\t\tconst resultWhere: Writable<Input.OptionalWhere> = {}\n\n\t\tif (where.and) {\n\t\t\tresultWhere.and = where.and\n\t\t\t\t.filter((it): it is Input.Where => !!it)\n\t\t\t\t.map(it => this.injectPredicatesToPredicate(it, entity, isBackReferenceContext, ancestorPath, isQueryRoot))\n\t\t}\n\t\tif (where.or) {\n\t\t\tresultWhere.or = where.or\n\t\t\t\t.filter((it): it is Input.Where => !!it)\n\t\t\t\t.map(it => this.injectPredicatesToPredicate(it, entity, isBackReferenceContext, ancestorPath, isQueryRoot))\n\t\t}\n\t\tif (where.not) {\n\t\t\tresultWhere.not = this.injectPredicatesToPredicate(where.not, entity, isBackReferenceContext, ancestorPath, isQueryRoot)\n\t\t}\n\n\t\tconst fields = Object.keys(where).filter(it => !['and', 'or', 'not'].includes(it))\n\n\t\tfor (const field of fields) {\n\t\t\tresultWhere[field] = acceptFieldVisitor(this.schema, entity, field, {\n\t\t\t\tvisitColumn: () => where[field],\n\t\t\t\tvisitRelation: context => {\n\t\t\t\t\tconst relationWhere = where[field] as Input.OptionalWhere | null\n\t\t\t\t\tif (relationWhere === null) {\n\t\t\t\t\t\treturn null\n\t\t\t\t\t}\n\n\t\t\t\t\t// Check if this relation is a back-reference to somewhere in our ancestor path\n\t\t\t\t\tconst isBackReference = this.findBackReferencedAncestor(\n\t\t\t\t\t\tancestorPath,\n\t\t\t\t\t\tcontext.relation.name,\n\t\t\t\t\t\tcontext.entity.name,\n\t\t\t\t\t) !== undefined\n\t\t\t\t\tconst nestedIsBackReferenceContext = isBackReference || isBackReferenceContext\n\t\t\t\t\tconst nestedAncestorPath: readonly Model.AnyRelationContext[] = [...ancestorPath, context]\n\n\t\t\t\t\t// Recursively process the nested where (always do this to handle deeper nesting)\n\t\t\t\t\tconst processedNestedWhere = this.injectPredicatesToPredicate(\n\t\t\t\t\t\trelationWhere,\n\t\t\t\t\t\tcontext.targetEntity,\n\t\t\t\t\t\tnestedIsBackReferenceContext,\n\t\t\t\t\t\tnestedAncestorPath,\n\t\t\t\t\t\tisQueryRoot,\n\t\t\t\t\t)\n\n\t\t\t\t\t// Check if we should simplify the target entity's predicate\n\t\t\t\t\tconst shouldSimplifyNested = nestedIsBackReferenceContext\n\t\t\t\t\t\t&& this.findBackReferencedAncestor(nestedAncestorPath, context.relation.name, context.entity.name) !== undefined\n\n\t\t\t\t\t// Get target entity's predicate (simplified if back-reference)\n\t\t\t\t\tconst targetPredicate = shouldSimplifyNested\n\t\t\t\t\t\t? { [context.targetEntity.primary]: { always: true } }\n\t\t\t\t\t\t: this.predicateFactory.create(context.targetEntity, Acl.Operation.read, undefined, context, isQueryRoot)\n\n\t\t\t\t\t// Optimization: avoid duplicate { id: always } when both are simplified\n\t\t\t\t\tconst primaryKey = context.targetEntity.primary\n\t\t\t\t\tconst nestedIsAlwaysTrue = Object.keys(processedNestedWhere).length === 1\n\t\t\t\t\t\t&& (processedNestedWhere as Record<string, unknown>)[primaryKey] !== undefined\n\t\t\t\t\t\t&& (processedNestedWhere as Record<string, Record<string, unknown>>)[primaryKey]?.always === true\n\t\t\t\t\tconst targetIsAlwaysTrue = Object.keys(targetPredicate).length === 1\n\t\t\t\t\t\t&& (targetPredicate as Record<string, unknown>)[primaryKey] !== undefined\n\t\t\t\t\t\t&& (targetPredicate as Record<string, Record<string, unknown>>)[primaryKey]?.always === true\n\n\t\t\t\t\tif (nestedIsAlwaysTrue && targetIsAlwaysTrue) {\n\t\t\t\t\t\treturn processedNestedWhere\n\t\t\t\t\t}\n\n\t\t\t\t\t// Combine the processed nested where with target entity's predicate\n\t\t\t\t\tconst parts = [processedNestedWhere, targetPredicate].filter(it => Object.keys(it).length > 0)\n\t\t\t\t\tif (parts.length === 0) {\n\t\t\t\t\t\treturn {}\n\t\t\t\t\t}\n\t\t\t\t\tif (parts.length === 1) {\n\t\t\t\t\t\treturn parts[0]\n\t\t\t\t\t}\n\t\t\t\t\treturn { and: parts }\n\t\t\t\t},\n\t\t\t})\n\t\t}\n\n\t\treturn resultWhere\n\t}\n\n\tprivate injectToWhere(\n\t\twhere: Input.OptionalWhere,\n\t\tentity: Model.Entity,\n\t\tisRoot: boolean,\n\t\trelationContext: Model.AnyRelationContext | undefined,\n\t\tisBackReferenceContext: boolean,\n\t\tancestorPath: readonly Model.AnyRelationContext[],\n\t\tisQueryRoot?: boolean,\n\t): Input.OptionalWhere {\n\t\tconst resultWhere: Writable<Input.OptionalWhere> = {}\n\t\tif (where.and) {\n\t\t\tresultWhere.and = where.and.filter((it): it is Input.Where => !!it).map(it =>\n\t\t\t\tthis.injectToWhere(it, entity, isRoot, relationContext, isBackReferenceContext, ancestorPath, isQueryRoot)\n\t\t\t)\n\t\t}\n\t\tif (where.or) {\n\t\t\tresultWhere.or = where.or.filter((it): it is Input.Where => !!it).map(it =>\n\t\t\t\tthis.injectToWhere(it, entity, isRoot, relationContext, isBackReferenceContext, ancestorPath, isQueryRoot)\n\t\t\t)\n\t\t}\n\t\tif (where.not) {\n\t\t\tresultWhere.not = this.injectToWhere(where.not, entity, isRoot, relationContext, isBackReferenceContext, ancestorPath, isQueryRoot)\n\t\t}\n\n\t\tconst fields = Object.keys(where).filter(it => !['and', 'or', 'not'].includes(it))\n\n\t\tif (fields.length === 0) {\n\t\t\treturn resultWhere\n\t\t}\n\t\tfor (let field of fields) {\n\t\t\tresultWhere[field] = acceptFieldVisitor(this.schema, entity, field, {\n\t\t\t\tvisitColumn: () => where[field],\n\t\t\t\tvisitRelation: context => {\n\t\t\t\t\tconst relationWhere = where[field] as Input.OptionalWhere | null\n\t\t\t\t\tif (relationWhere === null) {\n\t\t\t\t\t\treturn null\n\t\t\t\t\t}\n\t\t\t\t\t// Check if this relation is a back-reference to somewhere in our ancestor path\n\t\t\t\t\tconst isBackReference = this.findBackReferencedAncestor(ancestorPath, context.relation.name, context.entity.name) !== undefined\n\t\t\t\t\t// Once we enter a back-reference context, stay in it for nested relations\n\t\t\t\t\tconst nestedIsBackReferenceContext = isBackReference || isBackReferenceContext\n\t\t\t\t\t// Build extended ancestor path for nested traversal\n\t\t\t\t\tconst nestedAncestorPath: Model.AnyRelationContext[] = [...ancestorPath, context]\n\t\t\t\t\treturn this.injectToWhere(relationWhere, context.targetEntity, false, context, nestedIsBackReferenceContext, nestedAncestorPath, isQueryRoot)\n\t\t\t\t},\n\t\t\t})\n\t\t}\n\t\tconst fieldsForPredicate = !isRoot\n\t\t\t? fields\n\t\t\t: fields.filter(it => this.predicateFactory.shouldApplyCellLevelPredicate(entity, Acl.Operation.read, it, isQueryRoot))\n\n\t\treturn this.createWhere(entity, fieldsForPredicate, resultWhere, relationContext, isBackReferenceContext, ancestorPath, isQueryRoot)\n\t}\n}\n"],"names":["schema","Acl","acceptFieldVisitor"],"mappings":";;;;AAIO,MAAM,mBAAmB;AAAA,EAC/B,YAA6BA,SAAuC,kBAAoC;AAA3E,SAAA,SAAAA;AAAuC,SAAA,mBAAA;AAAA,EAAqC;AAAA,EAElG,OACN,QACA,OACA,iBACA,cACsB;AACtB,UAAM,cAAc,CAAC,oBAAoB,CAAC,gBAAgB,aAAa,WAAW;AAClF,UAAM,kBAAkB,KAAK,cAAc,OAAO,QAAQ,MAAM,iBAAiB,OAAO,gBAAgB,CAAA,GAAI,WAAW;AACvH,WAAO,KAAK,YAAY,QAAQ,QAAW,iBAAiB,iBAAiB,OAAO,gBAAgB,CAAA,GAAI,WAAW;AAAA,EACpH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASQ,2BACP,cACA,cACA,0BACuC;AACvC,WAAO,aAAa;AAAA,MAAK,SACxB,IAAI,gB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AO,WAAW,GAAG;AACxB,aAAO;AAAA,IACR;AACA,aAAS,SAAS,QAAQ;AACzB,kBAAY,KAAK,IAAIC,YAAAA,mBAAmB,KAAK,QAAQ,QAAQ,OAAO;AAAA,QACnE,aAAa,MAAM,MAAM,KAAK;AAAA,QAC9B,eAAe,CAAA,YAAW;AACzB,gBAAM,gBAAgB,MAAM,KAAK;AACjC,cAAI,kBAAkB,MAAM;AAC3B,mBAAO;AAAA,UACR;AAEA,gBAAM,kBAAkB,KAAK,2BAA2B,cAAc,QAAQ,SAAS,MAAM,QAAQ,OAAO,IAAI,MAAM;AAEtH,gBAAM,+BAA+B,mBAAmB;AAExD,gBAAM,qBAAiD,CAAC,GAAG,cAAc,OAAO;AAChF,iBAAO,KAAK,cAAc,eAAe,QAAQ,cAAc,OAAO,SAAS,8BAA8B,oBAAoB,WAAW;AAAA,QAC7I;AAAA,MAAA,CACA;AAAA,IACF;AACA,UAAM,qBAAqB,CAAC,SACzB,SACA,OAAO,OAAO,CAAA,OAAM,KAAK,iBAAiB,8BAA8B,QAAQD,OAAAA,IAAI,UAAU,MAAM,IAAI,WAAW,CAAC;AAEvH,WAAO,KAAK,YAAY,QAAQ,oBAAoB,aAAa,iBAAiB,wBAAwB,cAAc,WAAW;AAAA,EACpI;AACD;;"}
|
|
1
|
+
{"version":3,"file":"PredicatesInjector.cjs","sources":["../../../../../packages/engine-content-api/src/acl/PredicatesInjector.ts"],"sourcesContent":["import { Acl, Input, Model, Writable } from '@contember/schema'\nimport { acceptFieldVisitor } from '@contember/schema-utils'\nimport { PredicateFactory } from './PredicateFactory.js'\n\nexport class PredicatesInjector {\n\tconstructor(private readonly schema: Model.Schema, private readonly predicateFactory: PredicateFactory) {}\n\n\tpublic inject(\n\t\tentity: Model.Entity,\n\t\twhere: Input.OptionalWhere,\n\t\trelationContext?: Model.AnyRelationContext,\n\t\tancestorPath?: readonly Model.AnyRelationContext[],\n\t): Input.OptionalWhere {\n\t\tconst isQueryRoot = !relationContext && (!ancestorPath || ancestorPath.length === 0)\n\t\tconst restrictedWhere = this.injectToWhere(where, entity, true, relationContext, false, ancestorPath ?? [], isQueryRoot)\n\t\treturn this.createWhere(entity, undefined, restrictedWhere, relationContext, false, ancestorPath ?? [], isQueryRoot)\n\t}\n\n\t/**\n\t * Finds an ancestor in the path that matches the given relation as a back-reference.\n\t * A match occurs when:\n\t * 1. The relation we're traversing has the same name as the inverse (targetRelation) of a relation in the ancestor path\n\t * 2. AND the entity where our relation is defined matches the targetEntity in the path\n\t * (to prevent false positives when different entities have relations with the same name)\n\t */\n\tprivate findBackReferencedAncestor(\n\t\tancestorPath: readonly Model.AnyRelationContext[],\n\t\trelationName: string,\n\t\trelationSourceEntityName: string,\n\t): Model.AnyRelationContext | undefined {\n\t\treturn ancestorPath.find(ctx =>\n\t\t\tctx.targetRelation?.name === relationName\n\t\t\t&& ctx.targetEntity.name === relationSourceEntityName\n\t\t)\n\t}\n\n\tprivate createWhere(\n\t\tentity: Model.Entity,\n\t\tfieldNames: string[] | undefined,\n\t\twhere: Input.OptionalWhere,\n\t\trelationContext?: Model.AnyRelationContext,\n\t\tisBackReferenceContext?: boolean,\n\t\tancestorPath?: readonly Model.AnyRelationContext[],\n\t\tisQueryRoot?: boolean,\n\t): Input.OptionalWhere {\n\t\t// Simplify predicates when:\n\t\t// 1. We're in a back-reference context (inside a filter that traverses back)\n\t\t// 2. AND the relation we traversed to get here corresponds to a relation in our query path\n\t\t// This ensures we only simplify when the traversed relation actually corresponds\n\t\t// to a relation in our query path (not just any relation to the same entity type)\n\t\tconst shouldSimplify = isBackReferenceContext === true\n\t\t\t&& ancestorPath !== undefined\n\t\t\t&& relationContext !== undefined\n\t\t\t&& this.findBackReferencedAncestor(ancestorPath, relationContext.relation.name, relationContext.entity.name) !== undefined\n\n\t\tlet predicatesWhere: Input.OptionalWhere\n\t\tif (shouldSimplify) {\n\t\t\tpredicatesWhere = { [entity.primary]: { always: true } }\n\t\t} else {\n\t\t\tpredicatesWhere = this.predicateFactory.create(entity, Acl.Operation.read, fieldNames, relationContext, isQueryRoot)\n\t\t}\n\n\t\tconst and = [where, predicatesWhere].filter(it => Object.keys(it).length > 0)\n\t\tif (and.length === 0) {\n\t\t\treturn {}\n\t\t}\n\t\tif (and.length === 1) {\n\t\t\treturn and[0]\n\t\t}\n\t\treturn { and: and }\n\t}\n\n\tprivate injectToWhere(\n\t\twhere: Input.OptionalWhere,\n\t\tentity: Model.Entity,\n\t\tisRoot: boolean,\n\t\trelationContext: Model.AnyRelationContext | undefined,\n\t\tisBackReferenceContext: boolean,\n\t\tancestorPath: readonly Model.AnyRelationContext[],\n\t\tisQueryRoot?: boolean,\n\t): Input.OptionalWhere {\n\t\tconst resultWhere: Writable<Input.OptionalWhere> = {}\n\t\tif (where.and) {\n\t\t\tresultWhere.and = where.and.filter((it): it is Input.Where => !!it).map(it =>\n\t\t\t\tthis.injectToWhere(it, entity, isRoot, relationContext, isBackReferenceContext, ancestorPath, isQueryRoot)\n\t\t\t)\n\t\t}\n\t\tif (where.or) {\n\t\t\tresultWhere.or = where.or.filter((it): it is Input.Where => !!it).map(it =>\n\t\t\t\tthis.injectToWhere(it, entity, isRoot, relationContext, isBackReferenceContext, ancestorPath, isQueryRoot)\n\t\t\t)\n\t\t}\n\t\tif (where.not) {\n\t\t\tresultWhere.not = this.injectToWhere(where.not, entity, isRoot, relationContext, isBackReferenceContext, ancestorPath, isQueryRoot)\n\t\t}\n\n\t\tconst fields = Object.keys(where).filter(it => !['and', 'or', 'not'].includes(it))\n\n\t\tif (fields.length === 0) {\n\t\t\treturn resultWhere\n\t\t}\n\t\tfor (let field of fields) {\n\t\t\tresultWhere[field] = acceptFieldVisitor(this.schema, entity, field, {\n\t\t\t\tvisitColumn: () => where[field],\n\t\t\t\tvisitRelation: context => {\n\t\t\t\t\tconst relationWhere = where[field] as Input.OptionalWhere | null\n\t\t\t\t\tif (relationWhere === null) {\n\t\t\t\t\t\treturn null\n\t\t\t\t\t}\n\t\t\t\t\t// Check if this relation is a back-reference to somewhere in our ancestor path\n\t\t\t\t\tconst isBackReference = this.findBackReferencedAncestor(ancestorPath, context.relation.name, context.entity.name) !== undefined\n\t\t\t\t\t// Once we enter a back-reference context, stay in it for nested relations\n\t\t\t\t\tconst nestedIsBackReferenceContext = isBackReference || isBackReferenceContext\n\t\t\t\t\t// Build extended ancestor path for nested traversal\n\t\t\t\t\tconst nestedAncestorPath: Model.AnyRelationContext[] = [...ancestorPath, context]\n\t\t\t\t\treturn this.injectToWhere(relationWhere, context.targetEntity, false, context, nestedIsBackReferenceContext, nestedAncestorPath, isQueryRoot)\n\t\t\t\t},\n\t\t\t})\n\t\t}\n\t\tconst fieldsForPredicate = !isRoot\n\t\t\t? fields\n\t\t\t: fields.filter(it => this.predicateFactory.shouldApplyCellLevelPredicate(entity, Acl.Operation.read, it, isQueryRoot))\n\n\t\treturn this.createWhere(entity, fieldsForPredicate, resultWhere, relationContext, isBackReferenceContext, ancestorPath, isQueryRoot)\n\t}\n}\n"],"names":["schema","Acl","acceptFieldVisitor"],"mappings":";;;;AAIO,MAAM,mBAAmB;AAAA,EAC/B,YAA6BA,SAAuC,kBAAoC;AAA3E,SAAA,SAAAA;AAAuC,SAAA,mBAAA;AAAA,EAAqC;AAAA,EAElG,OACN,QACA,OACA,iBACA,cACsB;AACtB,UAAM,cAAc,CAAC,oBAAoB,CAAC,gBAAgB,aAAa,WAAW;AAClF,UAAM,kBAAkB,KAAK,cAAc,OAAO,QAAQ,MAAM,iBAAiB,OAAO,gBAAgB,CAAA,GAAI,WAAW;AACvH,WAAO,KAAK,YAAY,QAAQ,QAAW,iBAAiB,iBAAiB,OAAO,gBAAgB,CAAA,GAAI,WAAW;AAAA,EACpH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASQ,2BACP,cACA,cACA,0BACuC;AACvC,WAAO,aAAa;AAAA,MAAK,SACxB,IAAI,gBAAgB,SAAS,gBAC1B,IAAI,aAAa,SAAS;AAAA,IAAA;AAAA,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BAAiB,wBAAwB,cAAc,WAAW;AAAA,IACnI;AAEA,UAAM,SAAS,OAAO,KAAK,KAAK,EAAE,OAAO,CAAA,OAAM,CAAC,CAAC,OAAO,MAAM,KAAK,EAAE,SAAS,EAAE,CAAC;AAEjF,QAAI,OAAO,WAAW,GAAG;AACxB,aAAO;AAAA,IACR;AACA,aAAS,SAAS,QAAQ;AACzB,kBAAY,KAAK,IAAIC,YAAAA,mBAAmB,KAAK,QAAQ,QAAQ,OAAO;AAAA,QACnE,aAAa,MAAM,MAAM,KAAK;AAAA,QAC9B,eAAe,CAAA,YAAW;AACzB,gBAAM,gBAAgB,MAAM,KAAK;AACjC,cAAI,kBAAkB,MAAM;AAC3B,mBAAO;AAAA,UACR;AAEA,gBAAM,kBAAkB,KAAK,2BAA2B,cAAc,QAAQ,SAAS,MAAM,QAAQ,OAAO,IAAI,MAAM;AAEtH,gBAAM,+BAA+B,mBAAmB;AAExD,gBAAM,qBAAiD,CAAC,GAAG,cAAc,OAAO;AAChF,iBAAO,KAAK,cAAc,eAAe,QAAQ,cAAc,OAAO,SAAS,8BAA8B,oBAAoB,WAAW;AAAA,QAC7I;AAAA,MAAA,CACA;AAAA,IACF;AACA,UAAM,qBAAqB,CAAC,SACzB,SACA,OAAO,OAAO,CAAA,OAAM,KAAK,iBAAiB,8BAA8B,QAAQD,OAAAA,IAAI,UAAU,MAAM,IAAI,WAAW,CAAC;AAEvH,WAAO,KAAK,YAAY,QAAQ,oBAAoB,aAAa,iBAAiB,wBAAwB,cAAc,WAAW;AAAA,EACpI;AACD;;"}
|
|
@@ -28,14 +28,7 @@ class PredicatesInjector {
|
|
|
28
28
|
if (shouldSimplify) {
|
|
29
29
|
predicatesWhere = { [entity.primary]: { always: true } };
|
|
30
30
|
} else {
|
|
31
|
-
|
|
32
|
-
predicatesWhere = this.injectPredicatesToPredicate(
|
|
33
|
-
rawPredicate,
|
|
34
|
-
entity,
|
|
35
|
-
isBackReferenceContext ?? false,
|
|
36
|
-
ancestorPath ?? [],
|
|
37
|
-
isQueryRoot
|
|
38
|
-
);
|
|
31
|
+
predicatesWhere = this.predicateFactory.create(entity, Acl.Operation.read, fieldNames, relationContext, isQueryRoot);
|
|
39
32
|
}
|
|
40
33
|
const and = [where, predicatesWhere].filter((it) => Object.keys(it).length > 0);
|
|
41
34
|
if (and.length === 0) {
|
|
@@ -46,66 +39,6 @@ class PredicatesInjector {
|
|
|
46
39
|
}
|
|
47
40
|
return { and };
|
|
48
41
|
}
|
|
49
|
-
/**
|
|
50
|
-
* Processes a predicate and injects nested entity predicates for any relation traversals.
|
|
51
|
-
* This ensures that when a predicate like { department: { company: { name: 'Acme' } } }
|
|
52
|
-
* is used, the predicates of Department and Company are also applied.
|
|
53
|
-
*/
|
|
54
|
-
injectPredicatesToPredicate(where, entity, isBackReferenceContext, ancestorPath, isQueryRoot) {
|
|
55
|
-
const resultWhere = {};
|
|
56
|
-
if (where.and) {
|
|
57
|
-
resultWhere.and = where.and.filter((it) => !!it).map((it) => this.injectPredicatesToPredicate(it, entity, isBackReferenceContext, ancestorPath, isQueryRoot));
|
|
58
|
-
}
|
|
59
|
-
if (where.or) {
|
|
60
|
-
resultWhere.or = where.or.filter((it) => !!it).map((it) => this.injectPredicatesToPredicate(it, entity, isBackReferenceContext, ancestorPath, isQueryRoot));
|
|
61
|
-
}
|
|
62
|
-
if (where.not) {
|
|
63
|
-
resultWhere.not = this.injectPredicatesToPredicate(where.not, entity, isBackReferenceContext, ancestorPath, isQueryRoot);
|
|
64
|
-
}
|
|
65
|
-
const fields = Object.keys(where).filter((it) => !["and", "or", "not"].includes(it));
|
|
66
|
-
for (const field of fields) {
|
|
67
|
-
resultWhere[field] = acceptFieldVisitor(this.schema, entity, field, {
|
|
68
|
-
visitColumn: () => where[field],
|
|
69
|
-
visitRelation: (context) => {
|
|
70
|
-
const relationWhere = where[field];
|
|
71
|
-
if (relationWhere === null) {
|
|
72
|
-
return null;
|
|
73
|
-
}
|
|
74
|
-
const isBackReference = this.findBackReferencedAncestor(
|
|
75
|
-
ancestorPath,
|
|
76
|
-
context.relation.name,
|
|
77
|
-
context.entity.name
|
|
78
|
-
) !== void 0;
|
|
79
|
-
const nestedIsBackReferenceContext = isBackReference || isBackReferenceContext;
|
|
80
|
-
const nestedAncestorPath = [...ancestorPath, context];
|
|
81
|
-
const processedNestedWhere = this.injectPredicatesToPredicate(
|
|
82
|
-
relationWhere,
|
|
83
|
-
context.targetEntity,
|
|
84
|
-
nestedIsBackReferenceContext,
|
|
85
|
-
nestedAncestorPath,
|
|
86
|
-
isQueryRoot
|
|
87
|
-
);
|
|
88
|
-
const shouldSimplifyNested = nestedIsBackReferenceContext && this.findBackReferencedAncestor(nestedAncestorPath, context.relation.name, context.entity.name) !== void 0;
|
|
89
|
-
const targetPredicate = shouldSimplifyNested ? { [context.targetEntity.primary]: { always: true } } : this.predicateFactory.create(context.targetEntity, Acl.Operation.read, void 0, context, isQueryRoot);
|
|
90
|
-
const primaryKey = context.targetEntity.primary;
|
|
91
|
-
const nestedIsAlwaysTrue = Object.keys(processedNestedWhere).length === 1 && processedNestedWhere[primaryKey] !== void 0 && processedNestedWhere[primaryKey]?.always === true;
|
|
92
|
-
const targetIsAlwaysTrue = Object.keys(targetPredicate).length === 1 && targetPredicate[primaryKey] !== void 0 && targetPredicate[primaryKey]?.always === true;
|
|
93
|
-
if (nestedIsAlwaysTrue && targetIsAlwaysTrue) {
|
|
94
|
-
return processedNestedWhere;
|
|
95
|
-
}
|
|
96
|
-
const parts = [processedNestedWhere, targetPredicate].filter((it) => Object.keys(it).length > 0);
|
|
97
|
-
if (parts.length === 0) {
|
|
98
|
-
return {};
|
|
99
|
-
}
|
|
100
|
-
if (parts.length === 1) {
|
|
101
|
-
return parts[0];
|
|
102
|
-
}
|
|
103
|
-
return { and: parts };
|
|
104
|
-
}
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
return resultWhere;
|
|
108
|
-
}
|
|
109
42
|
injectToWhere(where, entity, isRoot, relationContext, isBackReferenceContext, ancestorPath, isQueryRoot) {
|
|
110
43
|
const resultWhere = {};
|
|
111
44
|
if (where.and) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PredicatesInjector.js","sources":["../../../../../packages/engine-content-api/src/acl/PredicatesInjector.ts"],"sourcesContent":["import { Acl, Input, Model, Writable } from '@contember/schema'\nimport { acceptFieldVisitor } from '@contember/schema-utils'\nimport { PredicateFactory } from './PredicateFactory.js'\n\nexport class PredicatesInjector {\n\tconstructor(private readonly schema: Model.Schema, private readonly predicateFactory: PredicateFactory) {}\n\n\tpublic inject(\n\t\tentity: Model.Entity,\n\t\twhere: Input.OptionalWhere,\n\t\trelationContext?: Model.AnyRelationContext,\n\t\tancestorPath?: readonly Model.AnyRelationContext[],\n\t): Input.OptionalWhere {\n\t\tconst isQueryRoot = !relationContext && (!ancestorPath || ancestorPath.length === 0)\n\t\tconst restrictedWhere = this.injectToWhere(where, entity, true, relationContext, false, ancestorPath ?? [], isQueryRoot)\n\t\treturn this.createWhere(entity, undefined, restrictedWhere, relationContext, false, ancestorPath ?? [], isQueryRoot)\n\t}\n\n\t/**\n\t * Finds an ancestor in the path that matches the given relation as a back-reference.\n\t * A match occurs when:\n\t * 1. The relation we're traversing has the same name as the inverse (targetRelation) of a relation in the ancestor path\n\t * 2. AND the entity where our relation is defined matches the targetEntity in the path\n\t * (to prevent false positives when different entities have relations with the same name)\n\t */\n\tprivate findBackReferencedAncestor(\n\t\tancestorPath: readonly Model.AnyRelationContext[],\n\t\trelationName: string,\n\t\trelationSourceEntityName: string,\n\t): Model.AnyRelationContext | undefined {\n\t\treturn ancestorPath.find(ctx =>\n\t\t\tctx.targetRelation?.name === relationName\n\t\t\t&& ctx.targetEntity.name === relationSourceEntityName\n\t\t)\n\t}\n\n\tprivate createWhere(\n\t\tentity: Model.Entity,\n\t\tfieldNames: string[] | undefined,\n\t\twhere: Input.OptionalWhere,\n\t\trelationContext?: Model.AnyRelationContext,\n\t\tisBackReferenceContext?: boolean,\n\t\tancestorPath?: readonly Model.AnyRelationContext[],\n\t\tisQueryRoot?: boolean,\n\t): Input.OptionalWhere {\n\t\t// Simplify predicates when:\n\t\t// 1. We're in a back-reference context (inside a filter that traverses back)\n\t\t// 2. AND the relation we traversed to get here corresponds to a relation in our query path\n\t\t// This ensures we only simplify when the traversed relation actually corresponds\n\t\t// to a relation in our query path (not just any relation to the same entity type)\n\t\tconst shouldSimplify = isBackReferenceContext === true\n\t\t\t&& ancestorPath !== undefined\n\t\t\t&& relationContext !== undefined\n\t\t\t&& this.findBackReferencedAncestor(ancestorPath, relationContext.relation.name, relationContext.entity.name) !== undefined\n\n\t\tlet predicatesWhere: Input.OptionalWhere\n\t\tif (shouldSimplify) {\n\t\t\tpredicatesWhere = { [entity.primary]: { always: true } }\n\t\t} else {\n\t\t\tconst rawPredicate = this.predicateFactory.create(entity, Acl.Operation.read, fieldNames, relationContext, isQueryRoot)\n\t\t\t// Process the predicate to inject nested entity predicates\n\t\t\tpredicatesWhere = this.injectPredicatesToPredicate(\n\t\t\t\trawPredicate,\n\t\t\t\tentity,\n\t\t\t\tisBackReferenceContext ?? false,\n\t\t\t\tancestorPath ?? [],\n\t\t\t\tisQueryRoot,\n\t\t\t)\n\t\t}\n\n\t\tconst and = [where, predicatesWhere].filter(it => Object.keys(it).length > 0)\n\t\tif (and.length === 0) {\n\t\t\treturn {}\n\t\t}\n\t\tif (and.length === 1) {\n\t\t\treturn and[0]\n\t\t}\n\t\treturn { and: and }\n\t}\n\n\t/**\n\t * Processes a predicate and injects nested entity predicates for any relation traversals.\n\t * This ensures that when a predicate like { department: { company: { name: 'Acme' } } }\n\t * is used, the predicates of Department and Company are also applied.\n\t */\n\tprivate injectPredicatesToPredicate(\n\t\twhere: Input.OptionalWhere,\n\t\tentity: Model.Entity,\n\t\tisBackReferenceContext: boolean,\n\t\tancestorPath: readonly Model.AnyRelationContext[],\n\t\tisQueryRoot?: boolean,\n\t): Input.OptionalWhere {\n\t\tconst resultWhere: Writable<Input.OptionalWhere> = {}\n\n\t\tif (where.and) {\n\t\t\tresultWhere.and = where.and\n\t\t\t\t.filter((it): it is Input.Where => !!it)\n\t\t\t\t.map(it => this.injectPredicatesToPredicate(it, entity, isBackReferenceContext, ancestorPath, isQueryRoot))\n\t\t}\n\t\tif (where.or) {\n\t\t\tresultWhere.or = where.or\n\t\t\t\t.filter((it): it is Input.Where => !!it)\n\t\t\t\t.map(it => this.injectPredicatesToPredicate(it, entity, isBackReferenceContext, ancestorPath, isQueryRoot))\n\t\t}\n\t\tif (where.not) {\n\t\t\tresultWhere.not = this.injectPredicatesToPredicate(where.not, entity, isBackReferenceContext, ancestorPath, isQueryRoot)\n\t\t}\n\n\t\tconst fields = Object.keys(where).filter(it => !['and', 'or', 'not'].includes(it))\n\n\t\tfor (const field of fields) {\n\t\t\tresultWhere[field] = acceptFieldVisitor(this.schema, entity, field, {\n\t\t\t\tvisitColumn: () => where[field],\n\t\t\t\tvisitRelation: context => {\n\t\t\t\t\tconst relationWhere = where[field] as Input.OptionalWhere | null\n\t\t\t\t\tif (relationWhere === null) {\n\t\t\t\t\t\treturn null\n\t\t\t\t\t}\n\n\t\t\t\t\t// Check if this relation is a back-reference to somewhere in our ancestor path\n\t\t\t\t\tconst isBackReference = this.findBackReferencedAncestor(\n\t\t\t\t\t\tancestorPath,\n\t\t\t\t\t\tcontext.relation.name,\n\t\t\t\t\t\tcontext.entity.name,\n\t\t\t\t\t) !== undefined\n\t\t\t\t\tconst nestedIsBackReferenceContext = isBackReference || isBackReferenceContext\n\t\t\t\t\tconst nestedAncestorPath: readonly Model.AnyRelationContext[] = [...ancestorPath, context]\n\n\t\t\t\t\t// Recursively process the nested where (always do this to handle deeper nesting)\n\t\t\t\t\tconst processedNestedWhere = this.injectPredicatesToPredicate(\n\t\t\t\t\t\trelationWhere,\n\t\t\t\t\t\tcontext.targetEntity,\n\t\t\t\t\t\tnestedIsBackReferenceContext,\n\t\t\t\t\t\tnestedAncestorPath,\n\t\t\t\t\t\tisQueryRoot,\n\t\t\t\t\t)\n\n\t\t\t\t\t// Check if we should simplify the target entity's predicate\n\t\t\t\t\tconst shouldSimplifyNested = nestedIsBackReferenceContext\n\t\t\t\t\t\t&& this.findBackReferencedAncestor(nestedAncestorPath, context.relation.name, context.entity.name) !== undefined\n\n\t\t\t\t\t// Get target entity's predicate (simplified if back-reference)\n\t\t\t\t\tconst targetPredicate = shouldSimplifyNested\n\t\t\t\t\t\t? { [context.targetEntity.primary]: { always: true } }\n\t\t\t\t\t\t: this.predicateFactory.create(context.targetEntity, Acl.Operation.read, undefined, context, isQueryRoot)\n\n\t\t\t\t\t// Optimization: avoid duplicate { id: always } when both are simplified\n\t\t\t\t\tconst primaryKey = context.targetEntity.primary\n\t\t\t\t\tconst nestedIsAlwaysTrue = Object.keys(processedNestedWhere).length === 1\n\t\t\t\t\t\t&& (processedNestedWhere as Record<string, unknown>)[primaryKey] !== undefined\n\t\t\t\t\t\t&& (processedNestedWhere as Record<string, Record<string, unknown>>)[primaryKey]?.always === true\n\t\t\t\t\tconst targetIsAlwaysTrue = Object.keys(targetPredicate).length === 1\n\t\t\t\t\t\t&& (targetPredicate as Record<string, unknown>)[primaryKey] !== undefined\n\t\t\t\t\t\t&& (targetPredicate as Record<string, Record<string, unknown>>)[primaryKey]?.always === true\n\n\t\t\t\t\tif (nestedIsAlwaysTrue && targetIsAlwaysTrue) {\n\t\t\t\t\t\treturn processedNestedWhere\n\t\t\t\t\t}\n\n\t\t\t\t\t// Combine the processed nested where with target entity's predicate\n\t\t\t\t\tconst parts = [processedNestedWhere, targetPredicate].filter(it => Object.keys(it).length > 0)\n\t\t\t\t\tif (parts.length === 0) {\n\t\t\t\t\t\treturn {}\n\t\t\t\t\t}\n\t\t\t\t\tif (parts.length === 1) {\n\t\t\t\t\t\treturn parts[0]\n\t\t\t\t\t}\n\t\t\t\t\treturn { and: parts }\n\t\t\t\t},\n\t\t\t})\n\t\t}\n\n\t\treturn resultWhere\n\t}\n\n\tprivate injectToWhere(\n\t\twhere: Input.OptionalWhere,\n\t\tentity: Model.Entity,\n\t\tisRoot: boolean,\n\t\trelationContext: Model.AnyRelationContext | undefined,\n\t\tisBackReferenceContext: boolean,\n\t\tancestorPath: readonly Model.AnyRelationContext[],\n\t\tisQueryRoot?: boolean,\n\t): Input.OptionalWhere {\n\t\tconst resultWhere: Writable<Input.OptionalWhere> = {}\n\t\tif (where.and) {\n\t\t\tresultWhere.and = where.and.filter((it): it is Input.Where => !!it).map(it =>\n\t\t\t\tthis.injectToWhere(it, entity, isRoot, relationContext, isBackReferenceContext, ancestorPath, isQueryRoot)\n\t\t\t)\n\t\t}\n\t\tif (where.or) {\n\t\t\tresultWhere.or = where.or.filter((it): it is Input.Where => !!it).map(it =>\n\t\t\t\tthis.injectToWhere(it, entity, isRoot, relationContext, isBackReferenceContext, ancestorPath, isQueryRoot)\n\t\t\t)\n\t\t}\n\t\tif (where.not) {\n\t\t\tresultWhere.not = this.injectToWhere(where.not, entity, isRoot, relationContext, isBackReferenceContext, ancestorPath, isQueryRoot)\n\t\t}\n\n\t\tconst fields = Object.keys(where).filter(it => !['and', 'or', 'not'].includes(it))\n\n\t\tif (fields.length === 0) {\n\t\t\treturn resultWhere\n\t\t}\n\t\tfor (let field of fields) {\n\t\t\tresultWhere[field] = acceptFieldVisitor(this.schema, entity, field, {\n\t\t\t\tvisitColumn: () => where[field],\n\t\t\t\tvisitRelation: context => {\n\t\t\t\t\tconst relationWhere = where[field] as Input.OptionalWhere | null\n\t\t\t\t\tif (relationWhere === null) {\n\t\t\t\t\t\treturn null\n\t\t\t\t\t}\n\t\t\t\t\t// Check if this relation is a back-reference to somewhere in our ancestor path\n\t\t\t\t\tconst isBackReference = this.findBackReferencedAncestor(ancestorPath, context.relation.name, context.entity.name) !== undefined\n\t\t\t\t\t// Once we enter a back-reference context, stay in it for nested relations\n\t\t\t\t\tconst nestedIsBackReferenceContext = isBackReference || isBackReferenceContext\n\t\t\t\t\t// Build extended ancestor path for nested traversal\n\t\t\t\t\tconst nestedAncestorPath: Model.AnyRelationContext[] = [...ancestorPath, context]\n\t\t\t\t\treturn this.injectToWhere(relationWhere, context.targetEntity, false, context, nestedIsBackReferenceContext, nestedAncestorPath, isQueryRoot)\n\t\t\t\t},\n\t\t\t})\n\t\t}\n\t\tconst fieldsForPredicate = !isRoot\n\t\t\t? fields\n\t\t\t: fields.filter(it => this.predicateFactory.shouldApplyCellLevelPredicate(entity, Acl.Operation.read, it, isQueryRoot))\n\n\t\treturn this.createWhere(entity, fieldsForPredicate, resultWhere, relationContext, isBackReferenceContext, ancestorPath, isQueryRoot)\n\t}\n}\n"],"names":[],"mappings":";;AAIO,MAAM,mBAAmB;AAAA,EAC/B,YAA6B,QAAuC,kBAAoC;AAA3E,SAAA,SAAA;AAAuC,SAAA,mBAAA;AAAA,EAAqC;AAAA,EAElG,OACN,QACA,OACA,iBACA,cACsB;AACtB,UAAM,cAAc,CAAC,oBAAoB,CAAC,gBAAgB,aAAa,WAAW;AAClF,UAAM,kBAAkB,KAAK,cAAc,OAAO,QAAQ,MAAM,iBAAiB,OAAO,gBAAgB,CAAA,GAAI,WAAW;AACvH,WAAO,KAAK,YAAY,QAAQ,QAAW,iBAAiB,iBAAiB,OAAO,gBAAgB,CAAA,GAAI,WAAW;AAAA,EACpH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASQ,2BACP,cACA,cACA,0BACuC;AACvC,WAAO,aAAa;AAAA,MAAK,SACxB,IAAI,gBAAgB,SAAS,gBAC1B,IAAI,aAAa,SAAS;AAAA,IAAA;AAAA,EAE/B;AAAA,EAEQ,YACP,QACA,YACA,OACA,iBACA,wBACA,cACA,aACsB;AAMtB,UAAM,iBAAiB,2BAA2B,QAC9C,iBAAiB,UACjB,oBAAoB,UACpB,KAAK,2BAA2B,cAAc,gBAAgB,SAAS,MAAM,gBAAgB,OAAO,IAAI,MAAM;AAElH,QAAI;AACJ,QAAI,gBAAgB;AACnB,wBAAkB,EAAE,C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,SAAS,MAAM,QAAQ,OAAO,IAAI,MAAM;AAEtH,gBAAM,+BAA+B,mBAAmB;AAExD,gBAAM,qBAAiD,CAAC,GAAG,cAAc,OAAO;AAChF,iBAAO,KAAK,cAAc,eAAe,QAAQ,cAAc,OAAO,SAAS,8BAA8B,oBAAoB,WAAW;AAAA,QAC7I;AAAA,MAAA,CACA;AAAA,IACF;AACA,UAAM,qBAAqB,CAAC,SACzB,SACA,OAAO,OAAO,CAAA,OAAM,KAAK,iBAAiB,8BAA8B,QAAQ,IAAI,UAAU,MAAM,IAAI,WAAW,CAAC;AAEvH,WAAO,KAAK,YAAY,QAAQ,oBAAoB,aAAa,iBAAiB,wBAAwB,cAAc,WAAW;AAAA,EACpI;AACD;"}
|
|
1
|
+
{"version":3,"file":"PredicatesInjector.js","sources":["../../../../../packages/engine-content-api/src/acl/PredicatesInjector.ts"],"sourcesContent":["import { Acl, Input, Model, Writable } from '@contember/schema'\nimport { acceptFieldVisitor } from '@contember/schema-utils'\nimport { PredicateFactory } from './PredicateFactory.js'\n\nexport class PredicatesInjector {\n\tconstructor(private readonly schema: Model.Schema, private readonly predicateFactory: PredicateFactory) {}\n\n\tpublic inject(\n\t\tentity: Model.Entity,\n\t\twhere: Input.OptionalWhere,\n\t\trelationContext?: Model.AnyRelationContext,\n\t\tancestorPath?: readonly Model.AnyRelationContext[],\n\t): Input.OptionalWhere {\n\t\tconst isQueryRoot = !relationContext && (!ancestorPath || ancestorPath.length === 0)\n\t\tconst restrictedWhere = this.injectToWhere(where, entity, true, relationContext, false, ancestorPath ?? [], isQueryRoot)\n\t\treturn this.createWhere(entity, undefined, restrictedWhere, relationContext, false, ancestorPath ?? [], isQueryRoot)\n\t}\n\n\t/**\n\t * Finds an ancestor in the path that matches the given relation as a back-reference.\n\t * A match occurs when:\n\t * 1. The relation we're traversing has the same name as the inverse (targetRelation) of a relation in the ancestor path\n\t * 2. AND the entity where our relation is defined matches the targetEntity in the path\n\t * (to prevent false positives when different entities have relations with the same name)\n\t */\n\tprivate findBackReferencedAncestor(\n\t\tancestorPath: readonly Model.AnyRelationContext[],\n\t\trelationName: string,\n\t\trelationSourceEntityName: string,\n\t): Model.AnyRelationContext | undefined {\n\t\treturn ancestorPath.find(ctx =>\n\t\t\tctx.targetRelation?.name === relationName\n\t\t\t&& ctx.targetEntity.name === relationSourceEntityName\n\t\t)\n\t}\n\n\tprivate createWhere(\n\t\tentity: Model.Entity,\n\t\tfieldNames: string[] | undefined,\n\t\twhere: Input.OptionalWhere,\n\t\trelationContext?: Model.AnyRelationContext,\n\t\tisBackReferenceContext?: boolean,\n\t\tancestorPath?: readonly Model.AnyRelationContext[],\n\t\tisQueryRoot?: boolean,\n\t): Input.OptionalWhere {\n\t\t// Simplify predicates when:\n\t\t// 1. We're in a back-reference context (inside a filter that traverses back)\n\t\t// 2. AND the relation we traversed to get here corresponds to a relation in our query path\n\t\t// This ensures we only simplify when the traversed relation actually corresponds\n\t\t// to a relation in our query path (not just any relation to the same entity type)\n\t\tconst shouldSimplify = isBackReferenceContext === true\n\t\t\t&& ancestorPath !== undefined\n\t\t\t&& relationContext !== undefined\n\t\t\t&& this.findBackReferencedAncestor(ancestorPath, relationContext.relation.name, relationContext.entity.name) !== undefined\n\n\t\tlet predicatesWhere: Input.OptionalWhere\n\t\tif (shouldSimplify) {\n\t\t\tpredicatesWhere = { [entity.primary]: { always: true } }\n\t\t} else {\n\t\t\tpredicatesWhere = this.predicateFactory.create(entity, Acl.Operation.read, fieldNames, relationContext, isQueryRoot)\n\t\t}\n\n\t\tconst and = [where, predicatesWhere].filter(it => Object.keys(it).length > 0)\n\t\tif (and.length === 0) {\n\t\t\treturn {}\n\t\t}\n\t\tif (and.length === 1) {\n\t\t\treturn and[0]\n\t\t}\n\t\treturn { and: and }\n\t}\n\n\tprivate injectToWhere(\n\t\twhere: Input.OptionalWhere,\n\t\tentity: Model.Entity,\n\t\tisRoot: boolean,\n\t\trelationContext: Model.AnyRelationContext | undefined,\n\t\tisBackReferenceContext: boolean,\n\t\tancestorPath: readonly Model.AnyRelationContext[],\n\t\tisQueryRoot?: boolean,\n\t): Input.OptionalWhere {\n\t\tconst resultWhere: Writable<Input.OptionalWhere> = {}\n\t\tif (where.and) {\n\t\t\tresultWhere.and = where.and.filter((it): it is Input.Where => !!it).map(it =>\n\t\t\t\tthis.injectToWhere(it, entity, isRoot, relationContext, isBackReferenceContext, ancestorPath, isQueryRoot)\n\t\t\t)\n\t\t}\n\t\tif (where.or) {\n\t\t\tresultWhere.or = where.or.filter((it): it is Input.Where => !!it).map(it =>\n\t\t\t\tthis.injectToWhere(it, entity, isRoot, relationContext, isBackReferenceContext, ancestorPath, isQueryRoot)\n\t\t\t)\n\t\t}\n\t\tif (where.not) {\n\t\t\tresultWhere.not = this.injectToWhere(where.not, entity, isRoot, relationContext, isBackReferenceContext, ancestorPath, isQueryRoot)\n\t\t}\n\n\t\tconst fields = Object.keys(where).filter(it => !['and', 'or', 'not'].includes(it))\n\n\t\tif (fields.length === 0) {\n\t\t\treturn resultWhere\n\t\t}\n\t\tfor (let field of fields) {\n\t\t\tresultWhere[field] = acceptFieldVisitor(this.schema, entity, field, {\n\t\t\t\tvisitColumn: () => where[field],\n\t\t\t\tvisitRelation: context => {\n\t\t\t\t\tconst relationWhere = where[field] as Input.OptionalWhere | null\n\t\t\t\t\tif (relationWhere === null) {\n\t\t\t\t\t\treturn null\n\t\t\t\t\t}\n\t\t\t\t\t// Check if this relation is a back-reference to somewhere in our ancestor path\n\t\t\t\t\tconst isBackReference = this.findBackReferencedAncestor(ancestorPath, context.relation.name, context.entity.name) !== undefined\n\t\t\t\t\t// Once we enter a back-reference context, stay in it for nested relations\n\t\t\t\t\tconst nestedIsBackReferenceContext = isBackReference || isBackReferenceContext\n\t\t\t\t\t// Build extended ancestor path for nested traversal\n\t\t\t\t\tconst nestedAncestorPath: Model.AnyRelationContext[] = [...ancestorPath, context]\n\t\t\t\t\treturn this.injectToWhere(relationWhere, context.targetEntity, false, context, nestedIsBackReferenceContext, nestedAncestorPath, isQueryRoot)\n\t\t\t\t},\n\t\t\t})\n\t\t}\n\t\tconst fieldsForPredicate = !isRoot\n\t\t\t? fields\n\t\t\t: fields.filter(it => this.predicateFactory.shouldApplyCellLevelPredicate(entity, Acl.Operation.read, it, isQueryRoot))\n\n\t\treturn this.createWhere(entity, fieldsForPredicate, resultWhere, relationContext, isBackReferenceContext, ancestorPath, isQueryRoot)\n\t}\n}\n"],"names":[],"mappings":";;AAIO,MAAM,mBAAmB;AAAA,EAC/B,YAA6B,QAAuC,kBAAoC;AAA3E,SAAA,SAAA;AAAuC,SAAA,mBAAA;AAAA,EAAqC;AAAA,EAElG,OACN,QACA,OACA,iBACA,cACsB;AACtB,UAAM,cAAc,CAAC,oBAAoB,CAAC,gBAAgB,aAAa,WAAW;AAClF,UAAM,kBAAkB,KAAK,cAAc,OAAO,QAAQ,MAAM,iBAAiB,OAAO,gBAAgB,CAAA,GAAI,WAAW;AACvH,WAAO,KAAK,YAAY,QAAQ,QAAW,iBAAiB,iBAAiB,OAAO,gBAAgB,CAAA,GAAI,WAAW;AAAA,EACpH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASQ,2BACP,cACA,cACA,0BACuC;AACvC,WAAO,aAAa;AAAA,MAAK,SACxB,IAAI,gBAAgB,SAAS,gBAC1B,IAAI,aAAa,SAAS;AAAA,IAAA;AAAA,EAE/B;AAAA,EAEQ,YACP,QACA,YACA,OACA,iBACA,wBACA,cACA,aACsB;AAMtB,UAAM,iBAAiB,2BAA2B,QAC9C,iBAAiB,UACjB,oBAAoB,UACpB,KAAK,2BAA2B,cAAc,gBAAgB,SAAS,MAAM,gBAAgB,OAAO,IAAI,MAAM;AAElH,QAAI;AACJ,QAAI,gBAAgB;AACnB,wBAAkB,EAAE,CAAC,OAAO,OAAO,GAAG,EAAE,QAAQ,OAAK;AAAA,IACtD,OAAO;AACN,wBAAkB,KAAK,iBAAiB,OAAO,QAAQ,IAAI,UAAU,MAAM,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