import { computed , makeObservable , runInAction } from 'mobx' ; import { injectable } from '@cloudbeaver/core-di' ; import { ServerConfigResource } from '@cloudbeaver/core-root' ; import { GraphQLService , AuthProviderInfo , CachedMapResource , ResourceKey , resourceKeyList , ResourceKeyUtils , isResourceKeyList , CachedMapAllKey , AuthProviderConfiguration as BaseAuthProviderConfiguration , } from '@cloudbeaver/core-sdk' ; import { AuthConfigurationsResource } from './AuthConfigurationsResource' ; import { AuthSettingsService } from './AuthSettingsService' ; export type AuthProvider = AuthProviderInfo ; export type AuthProviderConfiguration = BaseAuthProviderConfiguration ; @ injectable ( ) export class AuthProvidersResource extends CachedMapResource < string , AuthProvider > { get configurable ( ) : AuthProvider [ ] { return this . values . filter ( provider => provider . configurable ) ; } constructor ( private readonly authSettingsService , private readonly graphQLService , private readonly serverConfigResource , private readonly authConfigurationsResource ) { super ( ) ; this . preloadResource ( serverConfigResource , ( ) => undefined ) ; this . serverConfigResource . outdateResource ( this ) ; this . authConfigurationsResource . onItemAdd . addHandler ( this . updateConfigurations . bind ( this ) ) ; this . authConfigurationsResource . onItemDelete . addHandler ( this . deleteConfigurations . bind ( this ) ) ; makeObservable ( this , { configurable : computed , } ) ; } getEnabledProviders ( ) { return this . get ( resourceKeyList ( this . serverConfigResource . enabledAuthProviders ) ) as AuthProvider [ ] ; } getBase ( ) { return this . authSettingsService . settings . getValue ( 'baseAuthProvider' ) ; } getPrimary ( ) { return this . authSettingsService . settings . getValue ( 'primaryAuthProvider' ) ; } isEnabled ( id ) { return this . isAuthEnabled ( id ) ; } isBase ( id ) { return id === this . getBase ( ) ; } isPrimary ( id ) { return id === this . getPrimary ( ) ; } isAuthEnabled ( id ) { return this . serverConfigResource . enabledAuthProviders . includes ( id ) ; } async loadAll ( ) { await this . load ( CachedMapAllKey ) ; return this . values ; } async refreshAll ( ) { this . resetIncludes ( ) ; await this . refresh ( CachedMapAllKey ) ; return this . values ; } refreshAllLazy ( ) { this . resetIncludes ( ) ; this . markOutdated ( CachedMapAllKey ) ; } protected async loader ( key ) { const all = ResourceKeyUtils . includes ( key , CachedMapAllKey ) ; const { providers } = await this . graphQLService . sdk . getAuthProviders ( ) ; runInAction ( ( ) => { if ( all ) { this . data . clear ( ) ; } for ( const provider of providers ) { this . data . set ( provider . id , provider as AuthProvider ) ; } } ) ; return this . data ; } private updateConfigurations ( key ) { const configurations = isResourceKeyList ( key ) ? this . authConfigurationsResource . get ( key ) : [ this . authConfigurationsResource . get ( key ) ] ; const providerIds = resourceKeyList ( configurations . filter ( Boolean ) . map ( configuration => configuration ! . providerId ) ) ; this . markOutdated ( providerIds ) ; } private deleteConfigurations ( key ) { runInAction ( ( ) => { for ( const provider of this . values ) { if ( provider . configurable && provider . configurations ?. length ) { ResourceKeyUtils . forEach ( key , id => { const index = provider . configurations ! . findIndex ( configuration => configuration . id === id ) ; if ( index >= 0 ) { provider . configurations ! . splice ( index , 1 ) ; } } ) ; } } } ) ; } }