Skip to content

Commit b8cfffd

Browse files
authored
[server] Cleanup adminBlockUser RPC (#17033)
* [server] Cleanup adminBlockUser RPC * Fix * log * fix * fix
1 parent 2c1ff94 commit b8cfffd

File tree

3 files changed

+35
-21
lines changed

3 files changed

+35
-21
lines changed

components/server/ee/src/workspace/gitpod-server-impl.ts

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -620,27 +620,19 @@ export class GitpodServerEEImpl extends GitpodServerImpl {
620620

621621
await this.guardAdminAccess("adminBlockUser", { req }, Permission.ADMIN_USERS);
622622

623-
let targetUser;
624-
try {
625-
targetUser = await this.userService.blockUser(req.id, req.blocked);
626-
} catch (error) {
627-
throw new ResponseError(ErrorCodes.NOT_FOUND, "not found");
628-
}
623+
const targetUser = await this.userService.blockUser(req.id, req.blocked);
629624

630-
const workspaceDb = this.workspaceDb.trace(ctx);
631-
const workspaces = await workspaceDb.findWorkspacesByUser(req.id);
632-
const isDefined = <T>(x: T | undefined): x is T => x !== undefined;
633-
(await Promise.all(workspaces.map((workspace) => workspaceDb.findRunningInstance(workspace.id))))
634-
.filter(isDefined)
635-
.forEach((instance) =>
636-
this.workspaceStarter.stopWorkspaceInstance(
637-
ctx,
638-
instance.id,
639-
instance.region,
640-
"user blocked by admin",
641-
StopWorkspacePolicy.IMMEDIATELY,
642-
),
643-
);
625+
const stoppedWorkspaces = await this.workspaceStarter.stopRunningWorkspacesForUser(
626+
ctx,
627+
req.id,
628+
"user blocked by admin",
629+
StopWorkspacePolicy.IMMEDIATELY,
630+
);
631+
632+
log.info(`Stopped ${stoppedWorkspaces.length} workspaces in response to admin initiated block.`, {
633+
userId: targetUser.id,
634+
workspaceIds: stoppedWorkspaces.map((w) => w.id),
635+
});
644636

645637
// For some reason, returning the result of `this.userDB.storeUser(target)` does not work. The response never arrives the caller.
646638
// Returning `target` instead (which should be equivalent).

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ export class UserService {
377377
async blockUser(targetUserId: string, block: boolean): Promise<User> {
378378
const target = await this.userDb.findUserById(targetUserId);
379379
if (!target) {
380-
throw new Error("Not found.");
380+
throw new ResponseError(ErrorCodes.NOT_FOUND, "not found");
381381
}
382382

383383
target.blocked = !!block;

components/server/src/workspace/workspace-starter.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,28 @@ export class WorkspaceStarter {
415415
await client.stopWorkspace(ctx, req);
416416
}
417417

418+
public async stopRunningWorkspacesForUser(
419+
ctx: TraceContext,
420+
userID: string,
421+
reason: string,
422+
policy?: StopWorkspacePolicy,
423+
): Promise<Workspace[]> {
424+
const workspaceDb = this.workspaceDb.trace(ctx);
425+
const instances = await workspaceDb.findRunningInstancesWithWorkspaces(undefined, userID);
426+
await Promise.all(
427+
instances.map((instance) =>
428+
this.stopWorkspaceInstance(
429+
ctx,
430+
instance.latestInstance.id,
431+
instance.latestInstance.region,
432+
reason,
433+
policy,
434+
),
435+
),
436+
);
437+
return instances.map((instance) => instance.workspace);
438+
}
439+
418440
protected async checkBlockedRepository(user: User, contextURL: string) {
419441
const blockedRepository = await this.blockedRepositoryDB.findBlockedRepositoryByURL(contextURL);
420442
if (!blockedRepository) return;

0 commit comments

Comments
 (0)