Skip to content

Commit d50ad10

Browse files
committed
fix
1 parent 6d98c72 commit d50ad10

File tree

2 files changed

+51
-23
lines changed

2 files changed

+51
-23
lines changed

components/openfga/scripts.hack

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,13 @@ curl -X POST "openfga:8080/stores/01GP4CRKXESH1JE5E0SNHMZYG1/authorization-model
151151
],
152152
"schema_version": "1.1"
153153
}'
154+
155+
curl -X POST "openfga:8080/stores/01GP4CRKXESH1JE5E0SNHMZYG1/check" \
156+
-H "content-type: application/json" \
157+
-d '{"tuple_key":{"user":"user:milan","relation":"maintainer","object":"project:project1"}}'
158+
159+
# Response: {"allowed":true}
160+
b23f24d7-47f7-4366-a642-ce46b61b499e
161+
162+
curl -X GET "localhost:8080/stores/01GP4CRKXESH1JE5E0SNHMZYG1/changes" \
163+
-H "content-type: application/json"

components/server/src/perms.ts

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* See License.AGPL.txt in the project root for license information.
55
*/
66

7+
import { del } from "@gitbeaker/core/dist/types/infrastructure";
78
import { ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";
89
import { OpenFgaApi, TupleKey } from "@openfga/sdk";
910
import { ResponseError } from "vscode-jsonrpc";
@@ -30,42 +31,59 @@ function proj(id: string): string {
3031
return `project:${id}`;
3132
}
3233

34+
export async function isTeamOwner(userID: string, teamID: string): Promise<boolean> {
35+
return (
36+
(
37+
await OpenFGA.check({
38+
tuple_key: tup(user(userID), "owner", team(teamID)),
39+
})
40+
).allowed || false
41+
);
42+
}
43+
44+
export async function isTeamMember(userID: string, teamID: string): Promise<boolean> {
45+
return (
46+
(
47+
await OpenFGA.check({
48+
tuple_key: tup(user(userID), "member", team(teamID)),
49+
})
50+
).allowed || false
51+
);
52+
}
53+
3354
export async function grantTeamOwner(userID: string, teamID: string) {
34-
await OpenFGA.write({
55+
const deletes: TupleKey[] = [];
56+
57+
const isMember = await isTeamMember(userID, teamID);
58+
if (isMember) {
59+
deletes.push(tup(user(userID), "member", team(teamID)));
60+
}
61+
62+
return await OpenFGA.write({
3563
writes: {
3664
tuple_keys: [tup(user(userID), "owner", team(teamID))],
3765
},
66+
deletes: {
67+
tuple_keys: deletes,
68+
},
3869
});
39-
40-
try {
41-
// also remove any existing member role, if it existed
42-
await OpenFGA.write({
43-
deletes: {
44-
tuple_keys: [tup(user(userID), "member", team(teamID))],
45-
},
46-
});
47-
} catch (e) {
48-
// if the member role did not exist, the delete we fail, but we do not need to do anything as we have the desired outcome.
49-
}
5070
}
5171

5272
export async function grantTeamMember(userID: string, teamID: string) {
73+
const deletes: TupleKey[] = [];
74+
75+
const isMember = await isTeamOwner(userID, teamID);
76+
if (isMember) {
77+
deletes.push(tup(user(userID), "owner", team(teamID)));
78+
}
5379
await OpenFGA.write({
5480
writes: {
5581
tuple_keys: [tup(user(userID), "member", team(teamID))],
5682
},
83+
deletes: {
84+
tuple_keys: deletes,
85+
},
5786
});
58-
59-
try {
60-
// also remove any existing owner role
61-
await OpenFGA.write({
62-
deletes: {
63-
tuple_keys: [tup(user(userID), "owner", team(teamID))],
64-
},
65-
});
66-
} catch (e) {
67-
// if the owner role did not exist, the delete we fail, but we do not need to do anything as we have the desired outcome.
68-
}
6987
}
7088

7189
export async function removeUserFromTeam(userID: string, teamID: string) {

0 commit comments

Comments
 (0)