Skip to content

Commit 73533e4

Browse files
authored
[server] fix org-owned users without membership (#18463)
1 parent de4bbbe commit 73533e4

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

components/server/src/iam/iam-session-app.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { OIDCCreateSessionPayload } from "./iam-oidc-create-session-payload";
2121
import { TeamMemberInfo, TeamMemberRole, User } from "@gitpod/gitpod-protocol";
2222
import { OrganizationService } from "../orgs/organization-service";
2323
import { UserService } from "../user/user-service";
24-
import { UserDB } from "@gitpod/gitpod-db/lib";
24+
import { TeamDB, UserDB } from "@gitpod/gitpod-db/lib";
2525
const expect = chai.expect;
2626

2727
@suite(timeout(10000))
@@ -122,6 +122,7 @@ class TestIamSessionApp {
122122
return run();
123123
},
124124
}); // unused
125+
bind(TeamDB).toConstantValue(<any>{}); // unused
125126
}),
126127
);
127128
this.app = container.get(IamSessionApp);

components/server/src/iam/iam-session-app.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { reportJWTCookieIssued } from "../prometheus-metrics";
1616
import { ApplicationError } from "@gitpod/gitpod-protocol/lib/messaging/error";
1717
import { OrganizationService } from "../orgs/organization-service";
1818
import { UserService } from "../user/user-service";
19-
import { UserDB } from "@gitpod/gitpod-db/lib";
19+
import { BUILTIN_INSTLLATION_ADMIN_USER_ID, TeamDB, UserDB } from "@gitpod/gitpod-db/lib";
2020
import { SYSTEM_USER } from "../authorization/authorizer";
2121

2222
@injectable()
@@ -29,6 +29,7 @@ export class IamSessionApp {
2929
@inject(OrganizationService) private readonly orgService: OrganizationService,
3030
@inject(SessionHandler) private readonly session: SessionHandler,
3131
@inject(UserDB) private readonly userDb: UserDB,
32+
@inject(TeamDB) private readonly teamDb: TeamDB,
3233
) {}
3334

3435
public getMiddlewares() {
@@ -66,6 +67,26 @@ export class IamSessionApp {
6667
const existingUser = await this.findExistingOIDCUser(payload);
6768
if (existingUser) {
6869
await this.updateOIDCUserOnSignin(existingUser, payload);
70+
71+
try {
72+
//TODO we need to fix users without a team membership that happened because of a bug in the past
73+
// this is a workaround to fix the issue for now, but should be removed after a while
74+
if (existingUser.organizationId) {
75+
const result = await this.teamDb.addMemberToTeam(existingUser.id, existingUser.organizationId);
76+
if (result === "added") {
77+
const teamMemberships = await this.teamDb.findMembersByTeam(existingUser.organizationId);
78+
const otherOwners = teamMemberships.filter(
79+
(tm) => tm.userId !== BUILTIN_INSTLLATION_ADMIN_USER_ID && tm.role !== "member",
80+
);
81+
// if there is no owner on the team besides the admin user, we make this user an owner
82+
if (otherOwners.length === 0) {
83+
await this.teamDb.setTeamMemberRole(existingUser.id, existingUser.organizationId, "owner");
84+
}
85+
}
86+
}
87+
} catch (error) {
88+
log.error("Error fixing user team membership", error);
89+
}
6990
}
7091

7192
const user = existingUser || (await this.createNewOIDCUser(payload));

0 commit comments

Comments
 (0)