@@ -16,7 +16,7 @@ import { reportJWTCookieIssued } from "../prometheus-metrics";
1616import { ApplicationError } from "@gitpod/gitpod-protocol/lib/messaging/error" ;
1717import { OrganizationService } from "../orgs/organization-service" ;
1818import { 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" ;
2020import { 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