Skip to content

Commit cc72d04

Browse files
committed
[server] setAdmin impr
1 parent a744dbd commit cc72d04

File tree

5 files changed

+38
-24
lines changed

5 files changed

+38
-24
lines changed

components/server/src/authorization/authorizer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ export class Authorizer {
337337
);
338338
}
339339

340-
async removeAdminRole(userID: string) {
340+
async removeInstallationAdminRole(userID: string) {
341341
await this.authorizer.writeRelationships(
342342
v1.WriteRelationshipsRequest.create({
343343
updates: [

components/server/src/authorization/definitions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export type UserResourceType = "user";
1515

1616
export type UserRelation = "self" | "container";
1717

18-
export type UserPermission = "read_info" | "write_info" | "suspend";
18+
export type UserPermission = "read_info" | "write_info" | "suspend" | "make_admin";
1919

2020
export type InstallationResourceType = "installation";
2121

components/server/src/orgs/usage-service.spec.db.ts

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

7-
import { TypeORM, UserDB } from "@gitpod/gitpod-db/lib";
7+
import { BUILTIN_INSTLLATION_ADMIN_USER_ID, TypeORM } from "@gitpod/gitpod-db/lib";
88
import { Organization, User } from "@gitpod/gitpod-protocol";
99
import { AttributionId } from "@gitpod/gitpod-protocol/lib/attribution";
1010
import { Experiments } from "@gitpod/gitpod-protocol/lib/experiments/configcat-server";
@@ -47,21 +47,42 @@ describe("UsageService", async () => {
4747
centralizedPermissions: true,
4848
});
4949
os = container.get(OrganizationService);
50-
const userDB = container.get<UserDB>(UserDB);
51-
owner = await userDB.newUser();
50+
const userService = container.get<UserService>(UserService);
51+
owner = await userService.createUser({
52+
identity: {
53+
authName: "github",
54+
authProviderId: "github",
55+
authId: "1234",
56+
},
57+
});
5258
org = await os.createOrganization(owner.id, "myorg");
5359
const invite = await os.getOrCreateInvite(owner.id, org.id);
5460

55-
member = await userDB.newUser();
61+
member = await userService.createUser({
62+
identity: {
63+
authName: "github",
64+
authProviderId: "github",
65+
authId: "1234",
66+
},
67+
});
5668
await os.joinOrganization(member.id, invite.id);
5769

58-
stranger = await userDB.newUser();
70+
stranger = await userService.createUser({
71+
identity: {
72+
authName: "github",
73+
authProviderId: "github",
74+
authId: "1234",
75+
},
76+
});
5977

60-
const userService = container.get<UserService>(UserService);
61-
admin = await userDB.newUser();
62-
admin.rolesOrPermissions = ["admin"];
63-
await userDB.storeUser(admin);
64-
await userService.setAdminRole(admin.id, admin.id, true);
78+
admin = await userService.createUser({
79+
identity: {
80+
authName: "github",
81+
authProviderId: "github",
82+
authId: "1234",
83+
},
84+
});
85+
await userService.setAdminRole(BUILTIN_INSTLLATION_ADMIN_USER_ID, admin.id, true);
6586

6687
us = container.get<UsageService>(UsageService);
6788
await us.getCostCenter(owner.id, org.id);

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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,8 @@ export class UserService {
118118
}
119119

120120
async setAdminRole(userId: string, targetUserId: string, admin: boolean): Promise<User> {
121-
//TODO check if user has permission on targetUser to change admin role using auth system
122-
const user = await this.userDb.findUserById(userId);
123-
if (!user?.rolesOrPermissions || !user?.rolesOrPermissions.includes("admin")) {
124-
throw new ApplicationError(ErrorCodes.PERMISSION_DENIED, "permission denied");
125-
}
126-
127-
const target = await this.userDb.findUserById(targetUserId);
128-
if (!target) {
129-
throw new ApplicationError(ErrorCodes.NOT_FOUND, "not found");
130-
}
121+
await this.authorizer.checkUserPermissionAndThrow(userId, "make_admin", targetUserId);
122+
const target = await this.findUserById(userId, targetUserId);
131123
const rolesAndPermissions = target.rolesOrPermissions || [];
132124
const newRoles = [...rolesAndPermissions.filter((r) => r !== "admin")];
133125
if (admin) {
@@ -142,13 +134,13 @@ export class UserService {
142134
if (admin) {
143135
await this.authorizer.addInstallationAdminRole(target.id);
144136
} else {
145-
await this.authorizer.removeAdminRole(target.id);
137+
await this.authorizer.removeInstallationAdminRole(target.id);
146138
}
147139
return updatedUser;
148140
});
149141
} catch (err) {
150142
if (admin) {
151-
await this.authorizer.removeAdminRole(target.id);
143+
await this.authorizer.removeInstallationAdminRole(target.id);
152144
} else {
153145
await this.authorizer.addInstallationAdminRole(target.id);
154146
}

components/spicedb/schema/schema.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ schema: |-
1111
permission read_info = self + container->member + container->owner + container->admin
1212
permission write_info = self + container->owner + container->admin
1313
permission suspend = self + container->owner + container->admin
14+
permission make_admin = container->admin
1415
}
1516
1617
// There's only one global installation

0 commit comments

Comments
 (0)