import { Injectable } from '@angular/core' ; import { SessionServiceProxy , UserLoginInfoDto , TenantLoginInfoDto , ApplicationInfoDto , GetCurrentLoginInformationsOutput } from '@shared/service-proxies/service-proxies' ; import { AbpMultiTenancyService } from '@abp/multi-tenancy/abp-multi-tenancy.service' ; import { Abp } from '@abp' ; @ Injectable ( ) export class AppSessionService { private _user : UserLoginInfoDto ; private _tenant : TenantLoginInfoDto ; private _application : ApplicationInfoDto ; constructor ( private _sessionService , private _abpMultiTenancyService ) { } get application ( ) : ApplicationInfoDto { return this . _application ; } get user ( ) : UserLoginInfoDto { return this . _user ; } get userId ( ) : number { return this . user ? this . user . id : null ; } get tenant ( ) : TenantLoginInfoDto { return this . _tenant ; } get tenantId ( ) : number { return this . tenant ? this . tenant . id : null ; } getShownLoginName ( ) { let userName = this . _user . userName ; if ( ! this . _abpMultiTenancyService . isEnabled ) { return userName ; } return ( this . _tenant ? this . _tenant . tenancyName : "." ) + "\\" + userName ; } init ( ) { return new Promise < boolean > ( ( resolve , reject ) => { this . _sessionService . getCurrentLoginInformations ( ) . toPromise ( ) . then ( ( result ) => { this . _application = result . application ; this . _user = result . user ; this . _tenant = result . tenant ; resolve ( true ) ; } , ( err ) => { reject ( err ) ; } ) ; } ) ; } changeTenantIfNeeded ( tenantId ? ) { if ( this . isCurrentTenant ( tenantId ) ) { return false ; } Abp . multiTenancy . setTenantIdCookie ( tenantId ) ; location . reload ( ) ; return true ; } private isCurrentTenant ( tenantId ? ) { if ( ! tenantId && this . tenant ) { return false ; } else if ( tenantId && ( ! this . tenant || this . tenant . id !== tenantId ) ) { return false ; } return true ; } }