Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

... #297

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

... #297

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/env bash
#!/bin/zsh
set -euxo pipefail
rimraf dist
tsc --skipLibCheck
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cruddl",
"version": "3.2.0",
"version": "3.2.0-beta.1",
"description": "",
"license": "MIT",
"main": "dist/index.js",
Expand Down
41 changes: 31 additions & 10 deletions src/authorization/permission-descriptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import {
BinaryOperationQueryNode,
BinaryOperator,
ConstBoolQueryNode,
ConstIntQueryNode,
CountQueryNode,
FieldQueryNode,
IntersectionQueryNode,
LiteralQueryNode,
QueryNode,
UnknownValueQueryNode,
Expand Down Expand Up @@ -215,6 +218,20 @@ export class ProfileBasedPermissionDescriptor extends PermissionDescriptor {
accessGroupConditionNode = ConstBoolQueryNode.TRUE;
}

const intersectBinaryOperationQueryNode = (
fieldNode: QueryNode,
values: ReadonlyArray<string>,
): QueryNode => {
const node = new BinaryOperationQueryNode(
new CountQueryNode(
new IntersectionQueryNode([fieldNode, new LiteralQueryNode(values)]),
),
BinaryOperator.GREATER_THAN_OR_EQUAL,
ConstIntQueryNode.ONE,
);
return node;
};

const restrictionNodes = permission.restrictions.map((restriction) => {
if (!this.rootEntityType) {
throw new Error(
Expand All @@ -239,11 +256,13 @@ export class ProfileBasedPermissionDescriptor extends PermissionDescriptor {

if (restriction.valueTemplate !== undefined) {
const values = permission.evaluateTemplate(restriction.valueTemplate, authContext);
return new BinaryOperationQueryNode(
fieldNode,
BinaryOperator.IN,
new LiteralQueryNode(values),
);
return fieldPath.isList
? intersectBinaryOperationQueryNode(fieldNode, values)
: new BinaryOperationQueryNode(
fieldNode,
BinaryOperator.IN,
new LiteralQueryNode(values),
);
}

if (restriction.claim !== undefined) {
Expand All @@ -255,11 +274,13 @@ export class ProfileBasedPermissionDescriptor extends PermissionDescriptor {
if (!sanitizedClaimValues.length) {
return ConstBoolQueryNode.FALSE;
}
return new BinaryOperationQueryNode(
fieldNode,
BinaryOperator.IN,
new LiteralQueryNode(sanitizedClaimValues),
);
return fieldPath.isList
? intersectBinaryOperationQueryNode(fieldNode, sanitizedClaimValues)
: new BinaryOperationQueryNode(
fieldNode,
BinaryOperator.IN,
new LiteralQueryNode(sanitizedClaimValues),
);
}

throw new Error(`Invalid permission restriction (field: ${restriction.field})`);
Expand Down
13 changes: 12 additions & 1 deletion src/database/arangodb/aql-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
FieldQueryNode,
FirstOfListQueryNode,
FollowEdgeQueryNode,
IntersectionQueryNode,
ListItemQueryNode,
ListQueryNode,
LiteralQueryNode,
Expand Down Expand Up @@ -504,6 +505,12 @@ register(FieldPathQueryNode, (node, context) => {
return aql`${object}${getFieldPathAccessFragment(node.path)}`;
});

register(IntersectionQueryNode, (node, context) => {
const listNodes = node.listNodes.map((node) => processNode(node, context));
const listNodeStr = aql.join(listNodes, aql`, `);
return aql`INTERSECTION(${listNodeStr})`;
});

function getPropertyAccessFragment(propertyName: string) {
if (aql.isSafeIdentifier(propertyName)) {
return aql`.${aql.identifier(propertyName)}`;
Expand Down Expand Up @@ -1383,6 +1390,8 @@ register(TraversalQueryNode, (node, context) => {
}
}

// console.log('sourceFrag', sourceFrag);

const frag = getRelationTraversalFragment({
segments: node.relationSegments,
sourceFrag: fixedSourceFrag,
Expand Down Expand Up @@ -1486,7 +1495,9 @@ function getRelationTraversalFragment({
segment.vertexFilter.lhs.objectNode === segment.vertexFilterVariable
)
) {
throw new Error(`Unsupported filter pattern for graph traversal`);
throw new Error(
`Unsupported filter pattern for graph traversal - ${segment.vertexFilter.describe()}`,
);
}
const vertexInPathFrag = aql`${pathVar}.vertices[*]`;
const pathFilterContext = context.introduceVariableAlias(
Expand Down
13 changes: 13 additions & 0 deletions src/query-tree/lists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,16 @@ export class AggregationQueryNode extends QueryNode {
return `${this.operator}(${this.listNode.describe()})`;
}
}

export class IntersectionQueryNode extends QueryNode {
constructor(public readonly listNodes: ReadonlyArray<QueryNode>) {
super();
}

describe() {
if (!this.listNodes.length) {
return `[]`;
}
return `intersection(${this.listNodes.map((listNode) => listNode.describe()).join(',')})`;
}
}