@@ -12,7 +12,6 @@ import { UserAuthentication } from "../user/user-authentication";
12
12
import { OIDCCreateSessionPayload } from "./iam-oidc-create-session-payload" ;
13
13
import { log } from "@gitpod/gitpod-protocol/lib/util/logging" ;
14
14
import { Identity , User } from "@gitpod/gitpod-protocol" ;
15
- import { BUILTIN_INSTLLATION_ADMIN_USER_ID } from "@gitpod/gitpod-db/lib" ;
16
15
import { reportJWTCookieIssued } from "../prometheus-metrics" ;
17
16
import { ApplicationError } from "@gitpod/gitpod-protocol/lib/messaging/error" ;
18
17
import { OrganizationService } from "../orgs/organization-service" ;
@@ -98,17 +97,31 @@ export class IamSessionApp {
98
97
let existingUser = await this . userAuthentication . findUserForLogin ( {
99
98
candidate : this . mapOIDCProfileToIdentity ( payload ) ,
100
99
} ) ;
101
- if ( existingUser ) {
102
- return existingUser ;
100
+ if ( ! existingUser ) {
101
+ // Organizational account lookup by email address
102
+ existingUser = await this . userAuthentication . findOrgOwnedUser ( {
103
+ organizationId : payload . organizationId ,
104
+ email : payload . claims . email ,
105
+ } ) ;
106
+ if ( existingUser ) {
107
+ log . info ( "Found Org-owned user by email." , { email : payload ?. claims ?. email } ) ;
108
+ }
103
109
}
104
110
105
- // Organizational account lookup by email address
106
- existingUser = await this . userAuthentication . findOrgOwnedUser ( {
107
- organizationId : payload . organizationId ,
108
- email : payload . claims . email ,
109
- } ) ;
110
- if ( existingUser ) {
111
- log . info ( "Found Org-owned user by email." , { email : payload ?. claims ?. email } ) ;
111
+ if ( existingUser ?. organizationId ) {
112
+ const members = await this . orgService . listMembers ( existingUser . id , existingUser . organizationId ) ;
113
+ if ( ! members . some ( ( m ) => m . userId === existingUser ?. id ) ) {
114
+ // In case `createNewOIDCUser` failed to create a membership for this user,
115
+ // let's try to fix the situation on the fly.
116
+ // Also, if that step repeatedly fails, it would fail the login process earlier but
117
+ // in a more consistent state.
118
+ await this . orgService . addOrUpdateMember (
119
+ undefined ,
120
+ existingUser . organizationId ,
121
+ existingUser . id ,
122
+ "member" ,
123
+ ) ;
124
+ }
112
125
}
113
126
114
127
return existingUser ;
@@ -144,7 +157,7 @@ export class IamSessionApp {
144
157
} ,
145
158
} ) ;
146
159
147
- await this . orgService . addOrUpdateMember ( BUILTIN_INSTLLATION_ADMIN_USER_ID , organizationId , user . id , "member" ) ;
160
+ await this . orgService . addOrUpdateMember ( undefined , organizationId , user . id , "member" ) ;
148
161
return user ;
149
162
}
150
163
}
0 commit comments