Skip to content

[server] fix org-owned users without membership #18463

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion components/server/src/iam/iam-session-app.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { OIDCCreateSessionPayload } from "./iam-oidc-create-session-payload";
import { TeamMemberInfo, TeamMemberRole, User } from "@gitpod/gitpod-protocol";
import { OrganizationService } from "../orgs/organization-service";
import { UserService } from "../user/user-service";
import { UserDB } from "@gitpod/gitpod-db/lib";
import { TeamDB, UserDB } from "@gitpod/gitpod-db/lib";
const expect = chai.expect;

@suite(timeout(10000))
Expand Down Expand Up @@ -122,6 +122,7 @@ class TestIamSessionApp {
return run();
},
}); // unused
bind(TeamDB).toConstantValue(<any>{}); // unused
}),
);
this.app = container.get(IamSessionApp);
Expand Down
23 changes: 22 additions & 1 deletion components/server/src/iam/iam-session-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { reportJWTCookieIssued } from "../prometheus-metrics";
import { ApplicationError } from "@gitpod/gitpod-protocol/lib/messaging/error";
import { OrganizationService } from "../orgs/organization-service";
import { UserService } from "../user/user-service";
import { UserDB } from "@gitpod/gitpod-db/lib";
import { BUILTIN_INSTLLATION_ADMIN_USER_ID, TeamDB, UserDB } from "@gitpod/gitpod-db/lib";
import { SYSTEM_USER } from "../authorization/authorizer";

@injectable()
Expand All @@ -29,6 +29,7 @@ export class IamSessionApp {
@inject(OrganizationService) private readonly orgService: OrganizationService,
@inject(SessionHandler) private readonly session: SessionHandler,
@inject(UserDB) private readonly userDb: UserDB,
@inject(TeamDB) private readonly teamDb: TeamDB,
) {}

public getMiddlewares() {
Expand Down Expand Up @@ -66,6 +67,26 @@ export class IamSessionApp {
const existingUser = await this.findExistingOIDCUser(payload);
if (existingUser) {
await this.updateOIDCUserOnSignin(existingUser, payload);

try {
//TODO we need to fix users without a team membership that happened because of a bug in the past
// this is a workaround to fix the issue for now, but should be removed after a while
if (existingUser.organizationId) {
const result = await this.teamDb.addMemberToTeam(existingUser.id, existingUser.organizationId);
if (result === "added") {
const teamMemberships = await this.teamDb.findMembersByTeam(existingUser.organizationId);
const otherOwners = teamMemberships.filter(
(tm) => tm.userId !== BUILTIN_INSTLLATION_ADMIN_USER_ID && tm.role !== "member",
);
// if there is no owner on the team besides the admin user, we make this user an owner
if (otherOwners.length === 0) {
await this.teamDb.setTeamMemberRole(existingUser.id, existingUser.organizationId, "owner");
}
}
}
} catch (error) {
log.error("Error fixing user team membership", error);
}
}

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