Skip to content

Commit 90428bb

Browse files
committed
Replace owner with creator
1 parent cef319c commit 90428bb

File tree

11 files changed

+27
-41
lines changed

11 files changed

+27
-41
lines changed

dbschema/comments.esdl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Comments {
33
commentThreads := .<container[is Thread];
44
}
55

6-
type Thread extending default::Resource, Mixin::Embedded, Mixin::Owned {
6+
type Thread extending default::Resource, Mixin::Embedded {
77
overloaded required single link container: Aware {
88
on target delete delete source;
99
};
@@ -12,7 +12,7 @@ module Comments {
1212
latestComment := (select .comments order by .createdAt desc limit 1);
1313
}
1414

15-
type Comment extending default::Resource, Mixin::Owned {
15+
type Comment extending default::Resource {
1616
required thread: Thread {
1717
on target delete delete source;
1818
};

dbschema/post.esdl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module default {
2-
type Post extending Resource, Mixin::Embedded, Mixin::Owned {
2+
type Post extending Resource, Mixin::Embedded {
33
overloaded required single link container: Mixin::Postable {
44
on target delete delete source;
55
};

dbschema/progress-report.esdl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ module ProgressReport {
5454
module Media {
5555
type VariantGroup;
5656
}
57-
type Media extending ProgressReport::Child, Mixin::Owned {
57+
type Media extending ProgressReport::Child {
5858
required file: default::File;
5959
required single media := assert_exists(.file.media);
6060

dbschema/user.esdl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module default {
2-
type User extending Resource, Mixin::Pinnable, Mixin::Owned {
2+
type User extending Resource, Mixin::Pinnable {
33
email: str {
44
constraint exclusive;
55
};

dbschema/z.owned.esdl

Lines changed: 0 additions & 8 deletions
This file was deleted.

dbschema/z.stamped.esdl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ module Mixin {
88
default := default::currentUser;
99
rewrite update using (default::currentUser);
1010
};
11+
12+
required isCreator := .createdBy ?= <default::User>(global default::currentUserId)
1113
}
1214

1315
abstract type Timestamped {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { owner, Policy } from '../util';
1+
import { creator, Policy } from '../util';
22

3-
@Policy('all', (r) => r.ProgressReportMedia.when(owner).edit.delete)
3+
@Policy('all', (r) => r.ProgressReportMedia.when(creator).edit.delete)
44
export class ProgressReportMediaOwnerPolicy {}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { owner, Policy } from '../util';
1+
import { creator, Policy } from '../util';
22

3-
@Policy('all', (r) => [
4-
r.Post.when(owner).edit.delete,
5-
r.CommentThread.when(owner).edit.delete,
6-
r.Comment.when(owner).edit.delete,
7-
])
3+
@Policy('all', (r) =>
4+
[r.Post, r.CommentThread, r.Comment].flatMap(
5+
(it) => it.when(creator).edit.delete,
6+
),
7+
)
88
export class UserCanManageOwnCommentsPolicy {}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { owner, Policy } from '../util';
1+
import { creator, Policy } from '../util';
22

33
@Policy('all', (r) => [
44
[
55
r.ProgressReportCommunityStory,
66
r.ProgressReportHighlight,
77
r.ProgressReportTeamNews,
8-
].map((it) => it.specifically((p) => p.prompt.when(owner).edit)),
8+
].map((it) => it.specifically((p) => p.prompt.when(creator).edit)),
99
])
1010
export class UserCanManageOwnPromptsPolicy {}

src/components/authorization/policies/conditions/owner.condition.ts renamed to src/components/authorization/policies/conditions/creator.condition.ts

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
unwrapSecured,
1111
} from '~/common';
1212
import { type LinkTo } from '~/core/resources';
13-
import { User } from '../../../user/dto';
1413
import {
1514
AsCypherParams,
1615
Condition,
@@ -23,19 +22,15 @@ export interface HasCreator {
2322
creator: MaybeSecuredProp<ID | LinkTo<'User'>>;
2423
}
2524

26-
class OwnerCondition<
27-
TResourceStatic extends ResourceShape<HasCreator> | typeof User,
28-
> implements Condition<TResourceStatic>
25+
class CreatorCondition<TResourceStatic extends ResourceShape<HasCreator>>
26+
implements Condition<TResourceStatic>
2927
{
30-
isAllowed({ object, resource, session }: IsAllowedParams<TResourceStatic>) {
28+
isAllowed({ object, session }: IsAllowedParams<TResourceStatic>) {
3129
if (!object) {
3230
throw new Error("Needed object but wasn't given");
3331
}
3432

3533
const creator = (() => {
36-
if (resource.is(User)) {
37-
return (object as MaybeSecured<User>).id;
38-
}
3934
const o = object as MaybeSecured<HasCreator>;
4035
const creator = unwrapSecured(o.creator);
4136
if (!creator) {
@@ -58,10 +53,10 @@ class OwnerCondition<
5853
prevApplied: Set<any>,
5954
other: AsCypherParams<TResourceStatic>,
6055
) {
61-
if (prevApplied.has('owner')) {
56+
if (prevApplied.has('creator')) {
6257
return query;
6358
}
64-
prevApplied.add('owner');
59+
prevApplied.add('creator');
6560

6661
const param = query.params.addParam(other.session.userId, CQL_VAR);
6762
Reflect.set(other, CQL_VAR, param);
@@ -71,9 +66,6 @@ class OwnerCondition<
7166

7267
asCypherCondition(_query: Query, other: AsCypherParams<TResourceStatic>) {
7368
const requester = String(Reflect.get(other, CQL_VAR));
74-
if (other.resource.is(User)) {
75-
return `node:User AND node.id = ${requester}`;
76-
}
7769
return [
7870
`node.creator = ${requester}`,
7971
`exists((node)-[:creator { active: true }]->(:Property { value: ${requester} }))`,
@@ -82,7 +74,7 @@ class OwnerCondition<
8274
}
8375

8476
asEdgeQLCondition() {
85-
return '(.isOwner ?? false)';
77+
return '.isCreator';
8678
}
8779

8880
union(this: void, conditions: this[]) {
@@ -94,11 +86,11 @@ class OwnerCondition<
9486
}
9587

9688
[inspect.custom](_depth: number, _options: InspectOptionsStylized) {
97-
return `Owner`;
89+
return `Creator`;
9890
}
9991
}
10092

10193
/**
102-
* The following actions only apply if the requester is the "owner" of the given object.
94+
* The following actions only apply if the requester is the "creator" of the given object.
10395
*/
104-
export const owner = new OwnerCondition();
96+
export const creator = new CreatorCondition();

0 commit comments

Comments
 (0)