|
|
@ -1,7 +1,5 @@ |
|
|
import { Injector } from '@angular/core'; |
|
|
import { Injector } from '@angular/core'; |
|
|
import clone from 'just-clone'; |
|
|
import clone from 'just-clone'; |
|
|
|
|
|
|
|
|
import { tap } from 'rxjs/operators'; |
|
|
|
|
|
import { Environment } from '../models/environment'; |
|
|
import { Environment } from '../models/environment'; |
|
|
|
|
|
|
|
|
import { FindTenantResultDto } from '../proxy/volo/abp/asp-net-core/mvc/multi-tenancy/models'; |
|
|
import { FindTenantResultDto } from '../proxy/volo/abp/asp-net-core/mvc/multi-tenancy/models'; |
|
|
@ -9,6 +7,8 @@ import { EnvironmentService } from '../services/environment.service'; |
|
|
import { MultiTenancyService } from '../services/multi-tenancy.service'; |
|
|
import { MultiTenancyService } from '../services/multi-tenancy.service'; |
|
|
import { createTokenParser } from './string-utils'; |
|
|
import { createTokenParser } from './string-utils'; |
|
|
import { firstValueFrom } from 'rxjs'; |
|
|
import { firstValueFrom } from 'rxjs'; |
|
|
|
|
|
import { TENANT_NOT_FOUND_BY_NAME } from '../tokens/tenant-not-found-by-name'; |
|
|
|
|
|
import { HttpErrorResponse } from '@angular/common/http'; |
|
|
|
|
|
|
|
|
const tenancyPlaceholder = '{0}'; |
|
|
const tenancyPlaceholder = '{0}'; |
|
|
|
|
|
|
|
|
@ -25,10 +25,10 @@ function getCurrentTenancyNameFromUrl(tenantKey: string) { |
|
|
return urlParams.get(tenantKey); |
|
|
return urlParams.get(tenantKey); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function parseTenantFromUrl(injector: Injector) { |
|
|
export async function parseTenantFromUrl(injector: Injector) { |
|
|
const environmentService = injector.get(EnvironmentService); |
|
|
const environmentService = injector.get(EnvironmentService); |
|
|
const multiTenancyService = injector.get(MultiTenancyService); |
|
|
const multiTenancyService = injector.get(MultiTenancyService); |
|
|
|
|
|
const tenantNotFoundHandler = injector.get(TENANT_NOT_FOUND_BY_NAME, null); |
|
|
|
|
|
|
|
|
const baseUrl = environmentService.getEnvironment()?.application?.baseUrl || ''; |
|
|
const baseUrl = environmentService.getEnvironment()?.application?.baseUrl || ''; |
|
|
const tenancyName = getCurrentTenancyName(baseUrl); |
|
|
const tenancyName = getCurrentTenancyName(baseUrl); |
|
|
@ -51,37 +51,39 @@ export async function parseTenantFromUrl(injector: Injector) { |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
if (tenancyName) { |
|
|
if (tenancyName) { |
|
|
debugger |
|
|
|
|
|
/** |
|
|
/** |
|
|
* We have to replace tenant name within the urls from environment, |
|
|
* We have to replace tenant name within the urls from environment, |
|
|
* because the code below will make a http request to find information about the domain tenant. |
|
|
* because the code below will make a http request to find information about the domain tenant. |
|
|
* Before this request takes place, we need to replace placeholders aka "{0}". |
|
|
* Before this request takes place, we need to replace placeholders aka "{0}". |
|
|
*/ |
|
|
*/ |
|
|
replaceTenantNameWithinEnvironment(injector, tenancyName); |
|
|
replaceTenantNameWithinEnvironment(injector, tenancyName); |
|
|
|
|
|
|
|
|
const tenant$ = multiTenancyService |
|
|
|
|
|
.setTenantByName(tenancyName) |
|
|
|
|
|
.pipe(tap(setEnvironmentWithDomainTenant)) |
|
|
|
|
|
try { |
|
|
|
|
|
const result = await firstValueFrom(tenant$) |
|
|
|
|
|
return result; |
|
|
|
|
|
} |
|
|
|
|
|
catch (error) { |
|
|
|
|
|
|
|
|
|
|
|
console.log(error) |
|
|
|
|
|
debugger |
|
|
|
|
|
} |
|
|
|
|
|
} else { |
|
|
|
|
|
/** |
|
|
|
|
|
* If there is no tenant, we still have to clean up {0}. from baseUrl to avoid incorrect http requests. |
|
|
|
|
|
*/ |
|
|
|
|
|
replaceTenantNameWithinEnvironment(injector, '', tenancyPlaceholder + '.'); |
|
|
|
|
|
|
|
|
|
|
|
const tenantIdFromQueryParams = getCurrentTenancyNameFromUrl(multiTenancyService.tenantKey); |
|
|
const tenant$ = multiTenancyService.setTenantByName(tenancyName); |
|
|
if (tenantIdFromQueryParams) { |
|
|
try { |
|
|
return multiTenancyService.setTenantById(tenantIdFromQueryParams).toPromise(); |
|
|
const result = await firstValueFrom(tenant$); |
|
|
|
|
|
setEnvironmentWithDomainTenant(result); |
|
|
|
|
|
return Promise.resolve(result); |
|
|
|
|
|
} catch (httpError: HttpErrorResponse | any) { |
|
|
|
|
|
if ( |
|
|
|
|
|
httpError instanceof HttpErrorResponse && |
|
|
|
|
|
httpError.status === 404 && |
|
|
|
|
|
tenantNotFoundHandler |
|
|
|
|
|
) { |
|
|
|
|
|
tenantNotFoundHandler(httpError); |
|
|
|
|
|
} |
|
|
|
|
|
return Promise.reject(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
|
|
|
|
* If there is no tenant, we still have to clean up {0}. from baseUrl to avoid incorrect http requests. |
|
|
|
|
|
*/ |
|
|
|
|
|
replaceTenantNameWithinEnvironment(injector, '', tenancyPlaceholder + '.'); |
|
|
|
|
|
|
|
|
|
|
|
const tenantIdFromQueryParams = getCurrentTenancyNameFromUrl(multiTenancyService.tenantKey); |
|
|
|
|
|
if (tenantIdFromQueryParams) { |
|
|
|
|
|
const tenantById$ = multiTenancyService.setTenantById(tenantIdFromQueryParams); |
|
|
|
|
|
return firstValueFrom(tenantById$); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return Promise.resolve(); |
|
|
return Promise.resolve(); |
|
|
} |
|
|
} |
|
|
@ -109,14 +111,20 @@ function replaceTenantNameWithinEnvironment( |
|
|
); |
|
|
); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if(!environment.oAuthConfig) { |
|
|
if (!environment.oAuthConfig) { |
|
|
environment.oAuthConfig = {}; |
|
|
environment.oAuthConfig = {}; |
|
|
} |
|
|
} |
|
|
environment.oAuthConfig.issuer = (environment.oAuthConfig.issuer || '').replace(placeholder, tenancyName); |
|
|
environment.oAuthConfig.issuer = (environment.oAuthConfig.issuer || '').replace( |
|
|
|
|
|
placeholder, |
|
|
|
|
|
tenancyName, |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
Object.keys(environment.apis).forEach(api => { |
|
|
Object.keys(environment.apis).forEach(api => { |
|
|
Object.keys(environment.apis[api]).forEach(key => { |
|
|
Object.keys(environment.apis[api]).forEach(key => { |
|
|
environment.apis[api][key] = (environment.apis[api][key] || '').replace(placeholder, tenancyName); |
|
|
environment.apis[api][key] = (environment.apis[api][key] || '').replace( |
|
|
|
|
|
placeholder, |
|
|
|
|
|
tenancyName, |
|
|
|
|
|
); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
|