From 4dc764a5e441fd0795d6760b57d727fe39caf56d Mon Sep 17 00:00:00 2001 From: Maksym Tsymbarov Date: Mon, 16 Nov 2026 16:44:05 +0100 Subject: [PATCH] Fixed dashboard "Upload new version" accepting invalid files and losing images and entity aliases (#16700) * Fixed dashboard "Upload new version" silently ignoring invalid dashboard file * PR comments fixes * Additional fix * Standardized Cancel and Discard --- .../home/dialogs/home-dialogs.service.ts | 19 ---- .../dashboard-group-config.factory.ts | 5 +- .../home/pages/dashboard/dashboard.module.ts | 4 +- .../dashboards-table-config.resolver.ts | 5 +- ...mport-dashboard-file-dialog.component.html | 50 --------- .../import-dashboard-file-dialog.component.ts | 85 --------------- .../import-export/import-export.service.ts | 102 ++++++++++-------- 7 files changed, 68 insertions(+), 202 deletions(-) delete mode 100644 ui-ngx/src/app/modules/home/pages/dashboard/import-dashboard-file-dialog.component.html delete mode 100644 ui-ngx/src/app/modules/home/pages/dashboard/import-dashboard-file-dialog.component.ts diff --git a/ui-ngx/src/app/modules/home/dialogs/home-dialogs.service.ts b/ui-ngx/src/app/modules/home/dialogs/home-dialogs.service.ts index dfeb9a9669..b9e968bc0b 100644 --- a/ui-ngx/src/app/modules/home/dialogs/home-dialogs.service.ts +++ b/ui-ngx/src/app/modules/home/dialogs/home-dialogs.service.ts @@ -36,11 +36,6 @@ import { EntityGroupWizardDialogData, EntityGroupWizardDialogResult } from '@home/components/wizard/entity-group-wizard-dialog.component'; -import { DashboardInfo } from '@shared/models/dashboard.models'; -import { - DashboardInfoDialogData, - ImportDashboardFileDialogComponent -} from '@home/pages/dashboard/import-dashboard-file-dialog.component'; @Injectable() export class HomeDialogsService { @@ -141,20 +136,6 @@ export class HomeDialogsService { }).afterClosed(); } - public importDashboardFile($event: Event, dashboard: DashboardInfo): Observable { - if ($event) { - $event.stopPropagation(); - } - return this.dialog.open(ImportDashboardFileDialogComponent, { - disableClose: true, - panelClass: ['tb-dialog', 'tb-fullscreen-dialog'], - data: { - dashboard - } - }).afterClosed(); - } - public selectOwner($event: Event, selectOwnerTitle: string, confirmSelectTitle: string, placeholderText: string, notFoundText: string, requiredText: string, onOwnerSelected?: (targetOwnerId: EntityId) => Observable, diff --git a/ui-ngx/src/app/modules/home/pages/dashboard/dashboard-group-config.factory.ts b/ui-ngx/src/app/modules/home/pages/dashboard/dashboard-group-config.factory.ts index ec56f4b220..4b6e7a93f0 100644 --- a/ui-ngx/src/app/modules/home/pages/dashboard/dashboard-group-config.factory.ts +++ b/ui-ngx/src/app/modules/home/pages/dashboard/dashboard-group-config.factory.ts @@ -254,7 +254,10 @@ export class DashboardGroupConfigFactory implements EntityGroupStateConfigFactor } importDashboardFile($event: Event, dashboard: DashboardInfo, config: GroupEntityTableConfig) { - this.homeDialogs.importDashboardFile($event, dashboard).subscribe( + if ($event) { + $event.stopPropagation(); + } + this.importExport.importDashboardVersion(dashboard, this.editMissingAliases.bind(this)).subscribe( (res) => { if (res) { config.updateData(); diff --git a/ui-ngx/src/app/modules/home/pages/dashboard/dashboard.module.ts b/ui-ngx/src/app/modules/home/pages/dashboard/dashboard.module.ts index 10c0b1b2f5..4388b15cf5 100644 --- a/ui-ngx/src/app/modules/home/pages/dashboard/dashboard.module.ts +++ b/ui-ngx/src/app/modules/home/pages/dashboard/dashboard.module.ts @@ -12,14 +12,12 @@ import { DASHBOARD_GROUP_CONFIG_FACTORY } from '@home/models/group/group-entitie import { DashboardGroupConfigFactory } from '@home/pages/dashboard/dashboard-group-config.factory'; import { DashboardRoutingModule } from '@home/pages/dashboard/dashboard-routing.module'; import { DashboardTableHeaderComponent } from '@home/pages/dashboard/dashboard-table-header.component'; -import { ImportDashboardFileDialogComponent } from "@home/pages/dashboard/import-dashboard-file-dialog.component"; @NgModule({ declarations: [ DashboardFormComponent, DashboardTableHeaderComponent, - PublicDashboardLinkDialogComponent, - ImportDashboardFileDialogComponent + PublicDashboardLinkDialogComponent ], imports: [ CommonModule, diff --git a/ui-ngx/src/app/modules/home/pages/dashboard/dashboards-table-config.resolver.ts b/ui-ngx/src/app/modules/home/pages/dashboard/dashboards-table-config.resolver.ts index 2432a7bbea..c63b23a9ce 100644 --- a/ui-ngx/src/app/modules/home/pages/dashboard/dashboards-table-config.resolver.ts +++ b/ui-ngx/src/app/modules/home/pages/dashboard/dashboards-table-config.resolver.ts @@ -286,7 +286,10 @@ export class DashboardsTableConfigResolver { } importDashboardFile($event: Event, dashboard: DashboardInfo, config: EntityTableConfig) { - this.homeDialogs.importDashboardFile($event, dashboard).subscribe( + if ($event) { + $event.stopPropagation(); + } + this.importExport.importDashboardVersion(dashboard, this.editMissingAliases.bind(this)).subscribe( (res) => { if (res) { config.updateData(); diff --git a/ui-ngx/src/app/modules/home/pages/dashboard/import-dashboard-file-dialog.component.html b/ui-ngx/src/app/modules/home/pages/dashboard/import-dashboard-file-dialog.component.html deleted file mode 100644 index 298014fa74..0000000000 --- a/ui-ngx/src/app/modules/home/pages/dashboard/import-dashboard-file-dialog.component.html +++ /dev/null @@ -1,50 +0,0 @@ - -
- -

{{ 'dashboard.update-new-version' | translate }}

- - -
- @if (isLoading$ | async) { - - - } @else { -
- } -
- - -
- -
- - -
-
diff --git a/ui-ngx/src/app/modules/home/pages/dashboard/import-dashboard-file-dialog.component.ts b/ui-ngx/src/app/modules/home/pages/dashboard/import-dashboard-file-dialog.component.ts deleted file mode 100644 index ecad7891dd..0000000000 --- a/ui-ngx/src/app/modules/home/pages/dashboard/import-dashboard-file-dialog.component.ts +++ /dev/null @@ -1,85 +0,0 @@ -// SPDX-FileCopyrightText: Copyright The ThingsBoard Authors -// SPDX-FileCopyrightText: Modifications Copyright ThingsBoard, Inc. -// SPDX-License-Identifier: Apache-2.0 AND BUSL-1.1 -import { Component, Inject, OnInit } from '@angular/core'; -import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog'; -import { Store } from '@ngrx/store'; -import { AppState } from '@core/core.state'; -import { FormBuilder, FormGroup } from '@angular/forms'; -import { DashboardService } from '@core/http/dashboard.service'; -import { DashboardInfo } from '@app/shared/models/dashboard.models'; -import { ActionNotificationShow } from '@core/notification/notification.actions'; -import { DialogComponent } from '@shared/components/dialog.component'; -import { Router } from '@angular/router'; -import { HttpErrorResponse, HttpStatusCode } from '@angular/common/http'; - -export interface DashboardInfoDialogData { - dashboard: DashboardInfo; -} - -@Component({ - selector: 'tb-import-dashboard-file-dialog', - templateUrl: './import-dashboard-file-dialog.component.html', - styleUrls: [], - standalone: false -}) -export class ImportDashboardFileDialogComponent extends DialogComponent implements OnInit { - - private dashboard: DashboardInfo; - currentFileName: string = ''; - uploadFileFormGroup: FormGroup; - - constructor(protected store: Store, - protected router: Router, - @Inject(MAT_DIALOG_DATA) public data: DashboardInfoDialogData, - private dashboardService: DashboardService, - protected dialogRef: MatDialogRef, - private fb: FormBuilder) { - super(store, router, dialogRef); - this.dashboard = data.dashboard; - } - - ngOnInit(): void { - this.uploadFileFormGroup = this.fb.group({ - file: [null] - }); - } - - cancel(): void { - this.dialogRef.close(); - } - - save() { - const fileControl = this.uploadFileFormGroup.get('file'); - if (!fileControl || !fileControl.value) { - return; - } - - const dashboard: DashboardInfo = { - ...this.dashboard, - configuration: { - ...fileControl.value, - description: this.dashboard.configuration.description - } - }; - - this.dashboardService.saveDashboard(dashboard).subscribe({ - next: () => this.dialogRef.close(true), - error: (err: HttpErrorResponse) => { - if (err.status === HttpStatusCode.Conflict) { - this.dialogRef.close(true); - } - } - }); - } - - loadDataFromJsonContent(content: string): any { - try { - const importData = JSON.parse(content); - return importData ? importData['configuration'] : importData; - } catch (err) { - this.store.dispatch(new ActionNotificationShow({message: err.message, type: 'error'})); - return null; - } - } -} diff --git a/ui-ngx/src/app/shared/import-export/import-export.service.ts b/ui-ngx/src/app/shared/import-export/import-export.service.ts index 38b4ab0ce4..888a267071 100644 --- a/ui-ngx/src/app/shared/import-export/import-export.service.ts +++ b/ui-ngx/src/app/shared/import-export/import-export.service.ts @@ -7,9 +7,9 @@ import { TranslateService } from '@ngx-translate/core'; import { select, Store } from '@ngrx/store'; import { AppState } from '@core/core.state'; import { ActionNotificationShow } from '@core/notification/notification.actions'; -import { BreakpointId, Dashboard, DashboardLayoutId } from '@shared/models/dashboard.models'; +import { BreakpointId, Dashboard, DashboardInfo, DashboardLayoutId } from '@shared/models/dashboard.models'; import { DatePipe } from '@angular/common'; -import { deepClone, guid, isDate, isDefined, isNotEmptyStr, isNumber, isObject, isString, isUndefined, unwrapModule } from '@core/utils'; +import { deepClone, guid, isDate, isDefined, isNotEmptyStr, isObject, isString, isUndefined, unwrapModule } from '@core/utils'; import { AliasesInfo, @@ -22,7 +22,8 @@ import { import { MatDialog } from '@angular/material/dialog'; import { ImportDialogComponent, ImportDialogData } from '@shared/import-export/import-dialog.component'; import { forkJoin, Observable, of, Subject } from 'rxjs'; -import { catchError, map, mergeMap, switchMap, take, tap } from 'rxjs/operators'; +import { catchError, defaultIfEmpty, map, mergeMap, switchMap, take, tap } from 'rxjs/operators'; +import { HttpErrorResponse, HttpStatusCode } from '@angular/common/http'; import { DashboardUtilsService } from '@core/services/dashboard-utils.service'; import { EntityService } from '@core/http/entity.service'; import { ExportRow, Widget, WidgetActionsMap, WidgetActionType, WidgetSize, WidgetTypeDetails } from '@shared/models/widget.models'; @@ -266,48 +267,33 @@ export class ImportExportService { onEditMissingAliases: editMissingAliasesFunction, entityGroupId?: string): Observable { return this.openImportDialog('dashboard.import', 'dashboard.dashboard-file').pipe( mergeMap((dashboard: Dashboard) => { - if (!this.validateImportedDashboard(dashboard)) { - this.store.dispatch(new ActionNotificationShow( - {message: this.translate.instant('dashboard.invalid-dashboard-file-error'), - type: 'error'})); - throw new Error('Invalid dashboard file'); - } else { - dashboard = this.prepareImport(dashboard); - dashboard = this.dashboardUtils.validateAndUpdateDashboard(dashboard); - dashboard.customerId = customerId; - let aliasIds = null; - const entityAliases = dashboard.configuration.entityAliases; - if (entityAliases) { - aliasIds = Object.keys(entityAliases); - } - if (aliasIds && aliasIds.length > 0) { - return this.processEntityAliases(entityAliases, aliasIds).pipe( - mergeMap((missingEntityAliases) => { - if (Object.keys(missingEntityAliases).length > 0) { - return onEditMissingAliases(this.dashboardUtils.getWidgetsArray(dashboard), - false, 'dashboard.dashboard-import-missing-aliases-title', - missingEntityAliases).pipe( - mergeMap((updatedEntityAliases) => { - for (const aliasId of Object.keys(updatedEntityAliases)) { - entityAliases[aliasId] = updatedEntityAliases[aliasId]; - } - return this.saveImportedDashboard(dashboard, entityGroupId); - }) - ); - } else { - return this.saveImportedDashboard(dashboard, entityGroupId); - } - } - )); - } else { - return this.saveImportedDashboard(dashboard, entityGroupId); - } - } + dashboard = this.prepareImportedDashboard(dashboard); + dashboard.customerId = customerId; + return this.resolveMissingAliases(dashboard, onEditMissingAliases); }), + mergeMap((dashboard) => this.saveImportedDashboard(dashboard, entityGroupId)), catchError(() => of(null)) ); } + public importDashboardVersion(dashboard: DashboardInfo, + onEditMissingAliases: editMissingAliasesFunction): Observable { + return this.openImportDialog('dashboard.update-new-version', 'dashboard.upload-file-to-update').pipe( + mergeMap((imported: Dashboard) => + this.resolveMissingAliases(this.prepareImportedDashboard(imported), onEditMissingAliases)), + mergeMap((imported) => this.dashboardService.saveDashboard({ + ...dashboard, + configuration: {...imported.configuration, description: dashboard.configuration?.description}, + resources: imported.resources + }).pipe( + map(() => true), + defaultIfEmpty(true), + catchError((err) => of(err instanceof HttpErrorResponse && err.status === HttpStatusCode.Conflict)) + )), + catchError(() => of(false)) + ); + } + public exportWidget(dashboard: Dashboard, sourceState: string, sourceLayout: DashboardLayoutId, widget: Widget, widgetTitle: string, breakpoint: BreakpointId) { const widgetItem = this.itembuffer.prepareWidgetItem(dashboard, sourceState, sourceLayout, widget, breakpoint); @@ -1315,10 +1301,40 @@ export class ImportExportService { } private validateImportedDashboard(dashboard: Dashboard): boolean { - if (isUndefined(dashboard.title) || isUndefined(dashboard.configuration)) { - return false; + return isObject(dashboard) && !Array.isArray(dashboard) && isDefined(dashboard.title) + && isObject(dashboard.configuration) && !Array.isArray(dashboard.configuration); + } + + private prepareImportedDashboard(dashboard: Dashboard): Dashboard { + if (!this.validateImportedDashboard(dashboard)) { + this.store.dispatch(new ActionNotificationShow( + {message: this.translate.instant('dashboard.invalid-dashboard-file-error'), + type: 'error'})); + throw new Error('Invalid dashboard file'); } - return true; + return this.dashboardUtils.validateAndUpdateDashboard(this.prepareImport(dashboard)); + } + + private resolveMissingAliases(dashboard: Dashboard, onEditMissingAliases: editMissingAliasesFunction): Observable { + const entityAliases = dashboard.configuration.entityAliases; + const aliasIds = entityAliases ? Object.keys(entityAliases) : []; + if (!aliasIds.length) { + return of(dashboard); + } + return this.processEntityAliases(entityAliases, aliasIds).pipe( + mergeMap((missingEntityAliases) => { + if (!Object.keys(missingEntityAliases).length) { + return of(dashboard); + } + return onEditMissingAliases(this.dashboardUtils.getWidgetsArray(dashboard), + false, 'dashboard.dashboard-import-missing-aliases-title', missingEntityAliases).pipe( + map((updatedEntityAliases) => { + Object.assign(entityAliases, updatedEntityAliases); + return dashboard; + }) + ); + }) + ); } private validateImportedWidget(widgetItem: WidgetItem): boolean {