Browse Source

Add Tenant-not found handler by token

pull/17569/head
Mahmut Gundogdu 3 years ago
parent
commit
0df76b87f3
  1. 3
      npm/ng-packs/packages/core/src/lib/tokens/index.ts
  2. 7
      npm/ng-packs/packages/core/src/lib/tokens/tenant-not-found-by-name.ts
  3. 64
      npm/ng-packs/packages/core/src/lib/utils/multi-tenancy-utils.ts
  4. 1
      npm/ng-packs/packages/theme-shared/src/lib/providers/index.ts
  5. 20
      npm/ng-packs/packages/theme-shared/src/lib/providers/tenant-not-found.provider.ts
  6. 9
      npm/ng-packs/packages/theme-shared/src/lib/theme-shared.module.ts

3
npm/ng-packs/packages/core/src/lib/tokens/index.ts

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

7
npm/ng-packs/packages/core/src/lib/tokens/tenant-not-found-by-name.ts

@ -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');

64
npm/ng-packs/packages/core/src/lib/utils/multi-tenancy-utils.ts

@ -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,
);
}); });
}); });

1
npm/ng-packs/packages/theme-shared/src/lib/providers/index.ts

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

20
npm/ng-packs/packages/theme-shared/src/lib/providers/tenant-not-found.provider.ts

@ -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 });
};
},
};

9
npm/ng-packs/packages/theme-shared/src/lib/theme-shared.module.ts

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

Loading…
Cancel
Save