@@ -12,6 +12,7 @@ import {
1212 type UserFeatures ,
1313 type UserSettingsConfig ,
1414 type UserSettingsData ,
15+ type CloudUserInfo ,
1516 OrganizationAllowList ,
1617 OrganizationSettings ,
1718 organizationSettingsSchema ,
@@ -23,6 +24,7 @@ import { getRooCodeApiUrl } from "./config.js"
2324import { RefreshTimer } from "./RefreshTimer.js"
2425
2526const ORGANIZATION_SETTINGS_CACHE_KEY = "organization-settings"
27+ const ORGANIZATION_ID_CACHE_KEY = "organization-settings-org-id"
2628const USER_SETTINGS_CACHE_KEY = "user-settings"
2729
2830const parseExtensionSettingsResponse = ( data : unknown ) => {
@@ -55,6 +57,7 @@ export class CloudSettingsService extends EventEmitter<SettingsServiceEvents> im
5557 private authService : AuthService
5658 private settings : OrganizationSettings | undefined = undefined
5759 private userSettings : UserSettingsData | undefined = undefined
60+ private currentOrganizationId : string | null = null
5861 private timer : RefreshTimer
5962 private log : ( ...args : unknown [ ] ) => void
6063
@@ -99,6 +102,24 @@ export class CloudSettingsService extends EventEmitter<SettingsServiceEvents> im
99102 }
100103 } )
101104
105+ this . authService . on ( "user-info" , async ( data : { userInfo : CloudUserInfo } ) => {
106+ try {
107+ const newOrgId = data . userInfo ?. organizationId || null
108+ if ( newOrgId !== this . currentOrganizationId ) {
109+ this . currentOrganizationId = newOrgId
110+ this . settings = undefined
111+ await this . context . globalState . update ( ORGANIZATION_SETTINGS_CACHE_KEY , undefined )
112+ await this . context . globalState . update ( ORGANIZATION_ID_CACHE_KEY , newOrgId )
113+ this . emit ( "settings-updated" , { } as Record < string , never > )
114+ if ( this . authService . hasActiveSession ( ) ) {
115+ await this . fetchSettings ( )
116+ }
117+ }
118+ } catch ( error ) {
119+ this . log ( `[cloud-settings] error processing user-info: ${ error } ` , error )
120+ }
121+ } )
122+
102123 if ( this . authService . hasActiveSession ( ) ) {
103124 this . timer . start ( )
104125 }
@@ -170,24 +191,44 @@ export class CloudSettingsService extends EventEmitter<SettingsServiceEvents> im
170191 // Store settings in separate globalState values
171192 if ( this . settings ) {
172193 await this . context . globalState . update ( ORGANIZATION_SETTINGS_CACHE_KEY , this . settings )
194+ await this . context . globalState . update ( ORGANIZATION_ID_CACHE_KEY , this . currentOrganizationId )
173195 }
174196
175197 if ( this . userSettings ) {
176198 await this . context . globalState . update ( USER_SETTINGS_CACHE_KEY , this . userSettings )
177199 }
178200 }
179201
202+ private getCurrentOrganizationId ( ) : string | null {
203+ return this . authService . getUserInfo ?.( ) ?. organizationId || this . authService . getStoredOrganizationId ?.( ) || null
204+ }
205+
180206 private loadCachedSettings ( ) : void {
181- // Load settings from separate globalState values
182207 this . settings = this . context . globalState . get < OrganizationSettings > ( ORGANIZATION_SETTINGS_CACHE_KEY )
183208 this . userSettings = this . context . globalState . get < UserSettingsData > ( USER_SETTINGS_CACHE_KEY )
209+
210+ const cachedOrgId = this . context . globalState . get < string | null > ( ORGANIZATION_ID_CACHE_KEY ) ?? null
211+ const currentOrgId = this . getCurrentOrganizationId ( )
212+ this . currentOrganizationId = currentOrgId
213+
214+ if ( currentOrgId && cachedOrgId !== currentOrgId ) {
215+ this . settings = undefined
216+ }
184217 }
185218
186219 public getAllowList ( ) : OrganizationAllowList {
220+ const currentOrgId = this . getCurrentOrganizationId ( )
221+ if ( currentOrgId && ( ! this . settings || this . currentOrganizationId !== currentOrgId ) ) {
222+ return { allowAll : false , providers : { } }
223+ }
187224 return this . settings ?. allowList || ORGANIZATION_ALLOW_ALL
188225 }
189226
190227 public getSettings ( ) : OrganizationSettings | undefined {
228+ const currentOrgId = this . getCurrentOrganizationId ( )
229+ if ( currentOrgId && this . currentOrganizationId !== currentOrgId ) {
230+ return undefined
231+ }
191232 return this . settings
192233 }
193234
@@ -287,6 +328,7 @@ export class CloudSettingsService extends EventEmitter<SettingsServiceEvents> im
287328
288329 // Clear both cache keys
289330 await this . context . globalState . update ( ORGANIZATION_SETTINGS_CACHE_KEY , undefined )
331+ await this . context . globalState . update ( ORGANIZATION_ID_CACHE_KEY , undefined )
290332 await this . context . globalState . update ( USER_SETTINGS_CACHE_KEY , undefined )
291333 }
292334
0 commit comments