@@ -16,7 +16,7 @@ import { reportJWTCookieIssued } from "../prometheus-metrics";
16
16
import { ApplicationError } from "@gitpod/gitpod-protocol/lib/messaging/error" ;
17
17
import { OrganizationService } from "../orgs/organization-service" ;
18
18
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" ;
20
20
import { SYSTEM_USER } from "../authorization/authorizer" ;
21
21
22
22
@injectable ( )
@@ -29,6 +29,7 @@ export class IamSessionApp {
29
29
@inject ( OrganizationService ) private readonly orgService : OrganizationService ,
30
30
@inject ( SessionHandler ) private readonly session : SessionHandler ,
31
31
@inject ( UserDB ) private readonly userDb : UserDB ,
32
+ @inject ( TeamDB ) private readonly teamDb : TeamDB ,
32
33
) { }
33
34
34
35
public getMiddlewares ( ) {
@@ -66,6 +67,26 @@ export class IamSessionApp {
66
67
const existingUser = await this . findExistingOIDCUser ( payload ) ;
67
68
if ( existingUser ) {
68
69
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
+ }
69
90
}
70
91
71
92
const user = existingUser || ( await this . createNewOIDCUser ( payload ) ) ;
0 commit comments