Skip to content

Commit dab6572

Browse files
committed
Use currentUser in policy conditions
1 parent 073baeb commit dab6572

File tree

3 files changed

+6
-64
lines changed

3 files changed

+6
-64
lines changed

src/components/authorization/policies/conditions/creator.condition.ts

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Logger } from '@nestjs/common';
2-
import { type Query } from 'cypher-query-builder';
32
import { inspect, type InspectOptionsStylized } from 'util';
43
import {
54
type ID,
@@ -11,14 +10,11 @@ import {
1110
} from '~/common';
1211
import { type LinkTo } from '~/core/resources';
1312
import {
14-
type AsCypherParams,
1513
type Condition,
1614
type IsAllowedParams,
1715
MissingContextException,
1816
} from '../../policy/conditions';
1917

20-
const CQL_VAR = 'requestingUser';
21-
2218
export interface HasCreator {
2319
creator: MaybeSecuredProp<ID | LinkTo<'User'>>;
2420
}
@@ -49,24 +45,8 @@ class CreatorCondition<TResourceStatic extends ResourceShape<HasCreator>>
4945
return creator === session.userId;
5046
}
5147

52-
setupCypherContext(
53-
query: Query,
54-
prevApplied: Set<any>,
55-
other: AsCypherParams<TResourceStatic>,
56-
) {
57-
if (prevApplied.has('creator')) {
58-
return query;
59-
}
60-
prevApplied.add('creator');
61-
62-
const param = query.params.addParam(other.session.userId, CQL_VAR);
63-
Reflect.set(other, CQL_VAR, param);
64-
65-
return query;
66-
}
67-
68-
asCypherCondition(_query: Query, other: AsCypherParams<TResourceStatic>) {
69-
const requester = String(Reflect.get(other, CQL_VAR));
48+
asCypherCondition() {
49+
const requester = '$currentUser';
7050
return [
7151
`node.creator = ${requester}`,
7252
`exists((node)-[:creator { active: true }]->(:Property { value: ${requester} }))`,

src/components/authorization/policies/conditions/member.condition.ts

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { type ResourceShape, type Role } from '~/common';
55
import { matchProjectScopedRoles } from '~/core/database/query';
66
import { rolesForScope, type ScopedRole, splitScope } from '../../dto/role.dto';
77
import {
8-
type AsCypherParams,
98
type AsEdgeQLParams,
109
type Condition,
1110
eqlDoesIntersect,
@@ -32,24 +31,8 @@ class MemberCondition<TResourceStatic extends ResourceWithScope>
3231
return getScope(object).includes('member:true');
3332
}
3433

35-
setupCypherContext(
36-
query: Query,
37-
prevApplied: Set<any>,
38-
other: AsCypherParams<TResourceStatic>,
39-
) {
40-
if (prevApplied.has('membership')) {
41-
return query;
42-
}
43-
prevApplied.add('membership');
44-
45-
const param = query.params.addParam(other.session.userId, 'requestingUser');
46-
Reflect.set(other, CQL_VAR, param);
47-
return query;
48-
}
49-
50-
asCypherCondition(query: Query, other: AsCypherParams<TResourceStatic>) {
51-
const requester = String(Reflect.get(other, CQL_VAR));
52-
return `exists((project)-[:member { active: true }]->(:ProjectMember)-[:user]->(:User { id: ${requester} }))`;
34+
asCypherCondition() {
35+
return 'exists((project)-[:member { active: true }]->(:ProjectMember)-[:user]->(:User { id: $currentUser }))';
5336
}
5437

5538
setupEdgeQLContext({

src/components/authorization/policies/conditions/self.condition.ts

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
import { type Query } from 'cypher-query-builder';
21
import { inspect, type InspectOptionsStylized } from 'util';
32
import { type User } from '../../../user/dto';
43
import {
5-
type AsCypherParams,
64
type AsEdgeQLParams,
75
type Condition,
86
fqnRelativeTo,
97
type IsAllowedParams,
108
MissingContextException,
119
} from '../../policy/conditions';
1210

13-
const CQL_VAR = 'requestingUser';
14-
1511
class SelfCondition<TResourceStatic extends typeof User>
1612
implements Condition<TResourceStatic>
1713
{
@@ -22,25 +18,8 @@ class SelfCondition<TResourceStatic extends typeof User>
2218
return object.id === session.userId;
2319
}
2420

25-
setupCypherContext(
26-
query: Query,
27-
prevApplied: Set<any>,
28-
other: AsCypherParams<TResourceStatic>,
29-
) {
30-
if (prevApplied.has('self')) {
31-
return query;
32-
}
33-
prevApplied.add('self');
34-
35-
const param = query.params.addParam(other.session.userId, CQL_VAR);
36-
Reflect.set(other, CQL_VAR, param);
37-
38-
return query;
39-
}
40-
41-
asCypherCondition(_query: Query, other: AsCypherParams<TResourceStatic>) {
42-
const requester = String(Reflect.get(other, CQL_VAR));
43-
return `node:User AND node.id = ${requester}`;
21+
asCypherCondition() {
22+
return 'node:User AND node.id = $currentUser';
4423
}
4524

4625
asEdgeQLCondition({ namespace }: AsEdgeQLParams<any>) {

0 commit comments

Comments
 (0)