Skip to content

Commit 850cc04

Browse files
authored
[spicedb] Small schema adjustments (#18380)
* [spicedb] Validate schema on build * [spicedb] Small schema adjustments
1 parent eebaae4 commit 850cc04

File tree

6 files changed

+35
-25
lines changed

6 files changed

+35
-25
lines changed

components/server/src/authorization/authorizer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ export class Authorizer {
161161
set(rel.user(userId).self.user(userId)), //
162162
set(
163163
owningOrgId
164-
? rel.user(userId).container.organization(owningOrgId)
165-
: rel.user(userId).container.installation,
164+
? rel.user(userId).organization.organization(owningOrgId)
165+
: rel.user(userId).installation.installation,
166166
),
167167
);
168168
}

components/server/src/authorization/definitions.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ export type Permission = UserPermission | InstallationPermission | OrganizationP
1818

1919
export type UserResourceType = "user";
2020

21-
export type UserRelation = "self" | "container";
21+
export type UserRelation = "self" | "organization" | "installation";
2222

23-
export type UserPermission = "read_info" | "write_info" | "suspend" | "make_admin";
23+
export type UserPermission = "read_info" | "write_info" | "make_admin";
2424

2525
export type InstallationResourceType = "installation";
2626

@@ -85,10 +85,10 @@ export const rel = {
8585
};
8686
},
8787

88-
get container() {
88+
get organization() {
8989
const result2 = {
9090
...result,
91-
relation: "container",
91+
relation: "organization",
9292
};
9393
return {
9494
organization(objectId: string) {
@@ -102,6 +102,15 @@ export const rel = {
102102
},
103103
} as v1.Relationship;
104104
},
105+
};
106+
},
107+
108+
get installation() {
109+
const result2 = {
110+
...result,
111+
relation: "installation",
112+
};
113+
return {
105114
get installation() {
106115
return {
107116
...result2,

components/server/src/authorization/relationship-updater.spec.db.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ describe("RelationshipUpdater", async () => {
4747

4848
it("should update a simple user", async () => {
4949
let user = await userDB.newUser();
50-
await notExpected(rel.user(user.id).container.installation);
50+
await notExpected(rel.user(user.id).installation.installation);
5151
await notExpected(rel.user(user.id).self.user(user.id));
5252
await notExpected(rel.installation.member.user(user.id));
5353

5454
user = await migrate(user);
5555

56-
await expected(rel.user(user.id).container.installation);
56+
await expected(rel.user(user.id).installation.installation);
5757
await expected(rel.user(user.id).self.user(user.id));
5858
await notExpected(rel.installation.admin.user(user.id));
5959
await expected(rel.installation.member.user(user.id));
@@ -65,7 +65,7 @@ describe("RelationshipUpdater", async () => {
6565
user = await userDB.storeUser(user);
6666
user = await migrate(user);
6767

68-
await expected(rel.user(user.id).container.installation);
68+
await expected(rel.user(user.id).installation.installation);
6969
await expected(rel.user(user.id).self.user(user.id));
7070
await expected(rel.installation.admin.user(user.id));
7171
await expected(rel.installation.member.user(user.id));
@@ -77,7 +77,7 @@ describe("RelationshipUpdater", async () => {
7777
user = await userDB.storeUser(user);
7878
user = await migrate(user);
7979

80-
await expected(rel.user(user.id).container.installation);
80+
await expected(rel.user(user.id).installation.installation);
8181
await expected(rel.user(user.id).self.user(user.id));
8282
await notExpected(rel.installation.admin.user(user.id));
8383
await expected(rel.installation.member.user(user.id));
@@ -92,7 +92,7 @@ describe("RelationshipUpdater", async () => {
9292
user = await migrate(user);
9393

9494
await expected(rel.user(user.id).self.user(user.id));
95-
await expected(rel.user(user.id).container.organization(org.id));
95+
await expected(rel.user(user.id).organization.organization(org.id));
9696
await expected(rel.organization(org.id).installation.installation);
9797
await expected(rel.organization(org.id).member.user(user.id));
9898
await expected(rel.organization(org.id).owner.user(user.id));
@@ -109,10 +109,10 @@ describe("RelationshipUpdater", async () => {
109109
user = await migrate(user);
110110

111111
await expected(rel.user(user.id).self.user(user.id));
112-
await expected(rel.user(user.id).container.organization(org.id));
112+
await expected(rel.user(user.id).organization.organization(org.id));
113113

114114
// we haven't called migrate on user2, so we don't expect any relationships
115-
await notExpected(rel.user(user2.id).container.installation);
115+
await notExpected(rel.user(user2.id).installation.installation);
116116
await notExpected(rel.user(user2.id).self.user(user2.id));
117117

118118
// but on the org user2 is a member
@@ -124,7 +124,7 @@ describe("RelationshipUpdater", async () => {
124124

125125
user2 = await migrate(user2);
126126

127-
await expected(rel.user(user2.id).container.installation);
127+
await expected(rel.user(user2.id).installation.installation);
128128
await expected(rel.user(user2.id).self.user(user2.id));
129129

130130
// rest should be the same

components/server/src/user/user-service.spec.db.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,15 @@ describe("UserService", async () => {
7979
it("createUser", async () => {
8080
expect(await auth.hasPermissionOnUser(user.id, "read_info", user.id)).to.be.true;
8181
expect(await auth.hasPermissionOnUser(user.id, "write_info", user.id)).to.be.true;
82-
expect(await auth.hasPermissionOnUser(user.id, "suspend", user.id)).to.be.true;
8382

8483
expect(await auth.hasPermissionOnUser(user2.id, "read_info", user.id)).to.be.true;
8584
expect(await auth.hasPermissionOnUser(user2.id, "write_info", user.id)).to.be.false;
86-
expect(await auth.hasPermissionOnUser(user2.id, "suspend", user.id)).to.be.false;
8785

8886
expect(await auth.hasPermissionOnUser(nonOrgUser.id, "read_info", user.id)).to.be.false;
8987
expect(await auth.hasPermissionOnUser(nonOrgUser.id, "write_info", user.id)).to.be.false;
90-
expect(await auth.hasPermissionOnUser(nonOrgUser.id, "suspend", user.id)).to.be.false;
9188

9289
expect(await auth.hasPermissionOnUser(admin.id, "read_info", user.id)).to.be.true;
93-
expect(await auth.hasPermissionOnUser(admin.id, "write_info", user.id)).to.be.true;
94-
expect(await auth.hasPermissionOnUser(admin.id, "suspend", user.id)).to.be.true;
90+
expect(await auth.hasPermissionOnUser(admin.id, "write_info", user.id)).to.be.false;
9591
});
9692

9793
it("updateLoggedInUser_avatarUrlNotUpdatable", async () => {

components/spicedb/BUILD.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ packages:
33
type: generic
44
srcs:
55
- "schema/*.yaml"
6+
config:
7+
test:
8+
- ["zed", "validate", "./schema/schema.yaml"]
69

710
- name: lib
811
type: go

components/spicedb/schema/schema.yaml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
schema: |-
66
definition user {
77
relation self: user
8-
relation container: organization | installation
8+
9+
// Only ONE of the following relations is ever present for a given user (XOR)
10+
relation organization: organization
11+
relation installation: installation
912
1013
// permissions
11-
permission read_info = self + container->member + container->owner + container->admin
12-
permission write_info = self + container->owner + container->admin
13-
permission suspend = self + container->owner + container->admin
14-
permission make_admin = container->admin
14+
permission read_info = self + organization->member + organization->owner + installation->admin
15+
permission write_info = self
16+
permission make_admin = installation->admin
1517
}
1618
1719
// There's only one global installation
@@ -81,7 +83,7 @@ schema: |-
8183
relationships: |-
8284
// we have one installation
8385
installation:installation_0#member@user:user_0
84-
user:user_0#container@installation:installation_0
86+
user:user_0#installation@installation:installation_0
8587
8688
installation:installation_0#admin@user:user_admin
8789

0 commit comments

Comments
 (0)