import { computed , makeObservable , runInAction } from 'mobx' ; import { injectable } from '@cloudbeaver/core-di' ; import { SyncExecutor , ISyncExecutor } from '@cloudbeaver/core-executor' ; import { SessionDataResource , SessionResource } from '@cloudbeaver/core-root' ; import { CachedDataResource , GetActiveUserQueryVariables , GraphQLService , isResourceKeyList , ObjectOrigin , ResourceKey , UserAuthToken , UserInfo } from '@cloudbeaver/core-sdk' ; import { AUTH_PROVIDER_LOCAL_ID } from './AUTH_PROVIDER_LOCAL_ID' ; import { AuthProviderService } from './AuthProviderService' ; import type { IAuthCredentials } from './IAuthCredentials' ; export type UserInfoIncludes = GetActiveUserQueryVariables ; @ injectable ( ) export class UserInfoResource extends CachedDataResource < UserInfo | null , void , void , UserInfoIncludes > { readonly userChange : ISyncExecutor < string > ; get parametersAvailable ( ) { return this . data !== null ; } constructor ( private readonly graphQLService , private readonly authProviderService , sessionResource , private readonly sessionDataResource ) { super ( null , [ 'customIncludeOriginDetails' , 'includeConfigurationParameters' ] ) ; this . userChange = new SyncExecutor ( ) ; this . sync ( sessionResource ) ; makeObservable ( this , { parametersAvailable : computed , } ) ; } isLinked ( provideId ) { return this . data ?. linkedAuthProviders . includes ( provideId ) || false ; } getId ( ) { return this . data ?. userId || 'anonymous' ; } hasOrigin ( origin ) { if ( ! this . data ) { return false ; } return this . hasToken ( origin . type , origin . subType ) ; } hasToken ( type , subType ? ) { if ( type === AUTH_PROVIDER_LOCAL_ID ) { return true ; } if ( ! this . data ) { return false ; } return ( this . data . authTokens . some ( token => token . origin . type === ( subType ?? type ) ) || this . data . authTokens . some ( token => token . origin . type === type && token . origin . subType === subType ) ) ; } async login ( provider , credentials , link ? ) { await this . performUpdate ( undefined , [ ] , async ( ) => { const processedCredentials = await this . authProviderService . processCredentials ( provider , credentials ) ; const { authToken } = await this . graphQLService . sdk . authLogin ( { provider , credentials : processedCredentials . credentials , linkUser : link , customIncludeOriginDetails : true , } ) ; this . resetIncludes ( ) ; if ( this . data === null || link ) { this . setData ( await this . loader ( ) ) ; } else { this . data . authTokens . push ( authToken as UserAuthToken ) ; } } ) ; this . sessionDataResource . markOutdated ( ) ; return this . data ; } async logout ( ) { await this . performUpdate ( undefined , [ ] , async ( ) => { if ( this . data ) { await this . graphQLService . sdk . authLogout ( ) ; this . setData ( null ) ; this . resetIncludes ( ) ; } } ) ; this . sessionDataResource . markOutdated ( ) ; } async setConfigurationParameter ( key , value ) { await this . graphQLService . sdk . setUserConfigurationParameter ( { name : key , value , } ) ; if ( this . data ) { this . data . configurationParameters [ key ] = value ; } return this . data ; } async deleteConfigurationParameter ( key ) { if ( isResourceKeyList ( key ) ) { const keyList = [ ] ; for ( const item of key . list ) { await this . graphQLService . sdk . setUserConfigurationParameter ( { name : item , value : null , } ) ; keyList . push ( item ) ; } runInAction ( ( ) => { for ( const item of keyList ) { delete this . data ?. configurationParameters [ item ] ; } } ) ; } else { await this . graphQLService . sdk . setUserConfigurationParameter ( { name : key , value : null , } ) ; delete this . data ?. configurationParameters [ key ] ; } return this . data ; } getConfigurationParameter ( key ) { return this . data ?. configurationParameters [ key ] ; } protected async loader ( key , includes ? ) { const { user } = await this . graphQLService . sdk . getActiveUser ( { ... this . getDefaultIncludes ( ) , ... this . getIncludesMap ( key , includes ) , } ) ; return ( user as UserInfo | null ) ?? null ; } protected setData ( data ) { const prevUserId = this . getId ( ) ; this . data = data ; const currentUserId = this . getId ( ) ; if ( prevUserId !== currentUserId ) { this . userChange . execute ( currentUserId ) ; } } private getDefaultIncludes ( ) { return { customIncludeOriginDetails : true , includeConfigurationParameters : false , includeMetaParameters : false , } ; } protected resetIncludes ( ) { const metadata = this . getMetadata ( ) ; metadata . includes = [ ... this . defaultIncludes ] ; } getIncludes ( key ) { key = this . transformParam ( key ) ; const metadata = this . getMetadata ( key ) ; return metadata . includes ; } }