From 3b35e18baaaf0342ee74ec8d471a4588282e4c99 Mon Sep 17 00:00:00 2001 From: Gero Posmyk-Leinemann Date: Thu, 2 Mar 2023 16:36:03 +0000 Subject: [PATCH] [server] Implement tsAddMembersToOrg --- .../gitpod-protocol/src/gitpod-service.ts | 1 + .../ee/src/workspace/gitpod-server-impl.ts | 39 +++++++++++++++++++ components/server/src/auth/rate-limiter.ts | 1 + .../src/workspace/gitpod-server-impl.ts | 3 ++ 4 files changed, 44 insertions(+) diff --git a/components/gitpod-protocol/src/gitpod-service.ts b/components/gitpod-protocol/src/gitpod-service.ts index 398876f10d45ea..dab6c8c3cb59f8 100644 --- a/components/gitpod-protocol/src/gitpod-service.ts +++ b/components/gitpod-protocol/src/gitpod-service.ts @@ -278,6 +278,7 @@ export interface GitpodServer extends JsonRpcServer, AdminServer, tsReassignSlot(teamSubscriptionId: string, teamSubscriptionSlotId: string, newIdentityStr: string): Promise; tsDeactivateSlot(teamSubscriptionId: string, teamSubscriptionSlotId: string): Promise; tsReactivateSlot(teamSubscriptionId: string, teamSubscriptionSlotId: string): Promise; + tsAddMembersToOrg(teamSubscriptionId: string, organizationId: string): Promise; getGithubUpgradeUrls(): Promise; diff --git a/components/server/ee/src/workspace/gitpod-server-impl.ts b/components/server/ee/src/workspace/gitpod-server-impl.ts index 40ca1de8f5e709..c10676f692c599 100644 --- a/components/server/ee/src/workspace/gitpod-server-impl.ts +++ b/components/server/ee/src/workspace/gitpod-server-impl.ts @@ -1731,6 +1731,45 @@ export class GitpodServerEEImpl extends GitpodServerImpl { return this.teamSubscriptionService.findTeamSubscriptionSlotsBy(user.id, new Date()); } + async tsAddMembersToOrg(ctx: TraceContext, teamSubscriptionId: string, organizationId: string): Promise { + const user = this.checkUser("tsAddMembersToOrg"); + + // Add logging to help us identify trouble with this operation very easily + try { + log.info({ userId: user.id }, "tsAddMembersToOrg: Started", { teamSubscriptionId, organizationId }); + + // TeamSubscription + const ts = await this.teamSubscriptionDB.findTeamSubscriptionById(teamSubscriptionId); + if (!ts || ts.userId !== user.id) { + throw new ResponseError(ErrorCodes.NOT_FOUND, "Cannot find TeamSubscription"); + } + + // Organization + const { team, members } = await this.guardTeamOperation(organizationId, "update", "org_write"); + const teamOwner = members.find((m) => m.userId === user.id && m.role === "owner"); + if (!teamOwner) { + throw new ResponseError(ErrorCodes.PERMISSION_DENIED, "You are not an owner of this Organization"); + } + + // Take all slots, identify the members, and add to the Organization + const slots = await this.teamSubscriptionDB.findSlotsByTeamSubscriptionId(teamSubscriptionId); + const adds = []; + for (const slot of slots) { + if (!slot.assigneeId) { + // Totally ok, it's just not assigned atm. + continue; + } + adds.push(this.teamDB.addMemberToTeam(slot.assigneeId, team.id)); + } + await Promise.all(adds); + + log.info({ userId: user.id }, "tsAddMembersToOrg: DONE", { teamSubscriptionId, organizationId }); + } catch (err) { + log.error({ userId: user.id }, "tsAddMembersToOrg: ERROR", err, { teamSubscriptionId, organizationId }); + throw err; + } + } + async tsGetUnassignedSlot( ctx: TraceContext, teamSubscriptionId: string, diff --git a/components/server/src/auth/rate-limiter.ts b/components/server/src/auth/rate-limiter.ts index 3bb38eecdc3c73..358b143e8f5e70 100644 --- a/components/server/src/auth/rate-limiter.ts +++ b/components/server/src/auth/rate-limiter.ts @@ -221,6 +221,7 @@ const defaultFunctions: FunctionsConfig = { listUsage: { group: "default", points: 1 }, getBillingModeForTeam: { group: "default", points: 1 }, getBillingModeForUser: { group: "default", points: 1 }, + tsAddMembersToOrg: { group: "default", points: 1 }, trackEvent: { group: "default", points: 1 }, trackLocation: { group: "default", points: 1 }, diff --git a/components/server/src/workspace/gitpod-server-impl.ts b/components/server/src/workspace/gitpod-server-impl.ts index f3ca228f919142..4ad37bb1ebf4a6 100644 --- a/components/server/src/workspace/gitpod-server-impl.ts +++ b/components/server/src/workspace/gitpod-server-impl.ts @@ -3403,6 +3403,9 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable { async tsGetSlots(ctx: TraceContext): Promise { throw new ResponseError(ErrorCodes.SAAS_FEATURE, `Not implemented in this version`); } + async tsAddMembersToOrg(ctx: TraceContext, teamSubscriptionId: string, organizationId: string): Promise { + throw new ResponseError(ErrorCodes.SAAS_FEATURE, `Not implemented in this version`); + } async tsGetUnassignedSlot( ctx: TraceContext, teamSubscriptionId: string,