Add Tenant-not found handler by token

pull/17569/head
Mahmut Gundogdu 2 years ago
parent d2fb511415
commit 0df76b87f3

@ -12,4 +12,5 @@ export * from './pipe-to-login.token';
export * from './set-token-response-to-storage.token';
export * from './check-authentication-state';
export * from './http-context.token';
export * from './others-group.token'
export * from './others-group.token';
export * from './tenant-not-found-by-name';

@ -1,3 +1,6 @@
import { InjectionToken } from "@angular/core";
import { InjectionToken } from '@angular/core';
import { HttpErrorResponse } from '@angular/common/http';
export const TENANT_NOT_FOUND_BY_NAME = new InjectionToken<string>('TENANT_NOT_FOUND_BY_NAME');
export const TENANT_NOT_FOUND_BY_NAME = new InjectionToken<
(HttpErrorResponse: HttpErrorResponse) => void
>('TENANT_NOT_FOUND_BY_NAME');

@ -1,7 +1,5 @@
import { Injector } from '@angular/core';
import clone from 'just-clone';
import { tap } from 'rxjs/operators';
import { Environment } from '../models/environment';
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 { createTokenParser } from './string-utils';
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}';
@ -25,10 +25,10 @@ function getCurrentTenancyNameFromUrl(tenantKey: string) {
return urlParams.get(tenantKey);
}
export async function parseTenantFromUrl(injector: Injector) {
const environmentService = injector.get(EnvironmentService);
const multiTenancyService = injector.get(MultiTenancyService);
const tenantNotFoundHandler = injector.get(TENANT_NOT_FOUND_BY_NAME, null);
const baseUrl = environmentService.getEnvironment()?.application?.baseUrl || '';
const tenancyName = getCurrentTenancyName(baseUrl);
@ -51,37 +51,39 @@ export async function parseTenantFromUrl(injector: Injector) {
};
if (tenancyName) {
debugger
/**
* 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.
* Before this request takes place, we need to replace placeholders aka "{0}".
*/
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);
if (tenantIdFromQueryParams) {
return multiTenancyService.setTenantById(tenantIdFromQueryParams).toPromise();
const tenant$ = multiTenancyService.setTenantByName(tenancyName);
try {
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();
}
@ -109,14 +111,20 @@ function replaceTenantNameWithinEnvironment(
);
}
if(!environment.oAuthConfig) {
if (!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[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,
);
});
});

@ -1,2 +1,3 @@
export * from './ng-bootstrap-config.provider';
export * from './route.provider';
export * from './tenant-not-found.provider';

@ -0,0 +1,20 @@
import { TENANT_NOT_FOUND_BY_NAME } from '@abp/ng.core';
import { inject, Provider } from '@angular/core';
import { ConfirmationService } from '../services';
import { HttpErrorResponse } from '@angular/common/http';
export const tenantNotFoundProvider: Provider = {
provide: TENANT_NOT_FOUND_BY_NAME,
useFactory: function () {
const confirm = inject(ConfirmationService);
return (response: HttpErrorResponse) => {
const { error } = response.error;
// hide loading donut
const appRoot = document.querySelector('app-root div.donut');
if (appRoot) {
appRoot.remove();
}
confirm.error(error.details, error.message, { hideCancelBtn: true, hideYesBtn: true });
};
},
};

@ -40,6 +40,7 @@ import { CardModule } from './components/card/card.module';
import { AbpVisibleDirective } from './directives';
import { FormInputComponent } from './components/form-input/form-input.component';
import { FormCheckboxComponent } from './components/checkbox/checkbox.component';
import { tenantNotFoundProvider } from './providers/tenant-not-found.provider';
const declarationsWithExports = [
BreadcrumbComponent,
@ -58,7 +59,7 @@ const declarationsWithExports = [
ModalCloseDirective,
AbpVisibleDirective,
FormInputComponent,
FormCheckboxComponent
FormCheckboxComponent,
];
@NgModule({
@ -69,7 +70,6 @@ const declarationsWithExports = [
NgbPaginationModule,
EllipsisModule,
CardModule,
],
declarations: [...declarationsWithExports, HttpErrorWrapperComponent],
exports: [
@ -77,11 +77,11 @@ const declarationsWithExports = [
EllipsisModule,
NgxValidateCoreModule,
CardModule,
...declarationsWithExports
...declarationsWithExports,
],
providers: [DatePipe],
})
export class BaseThemeSharedModule { }
export class BaseThemeSharedModule {}
@NgModule({
imports: [BaseThemeSharedModule],
@ -144,6 +144,7 @@ export class ThemeSharedModule {
...(confirmationIcons || {}),
},
},
tenantNotFoundProvider,
],
};
}

Loading…
Cancel
Save