From 28ec0c83ee6ad65ca4cf8a116b6a96423f50859f Mon Sep 17 00:00:00 2001 From: gulnaz Date: Thu, 24 Apr 2025 12:42:36 +0300 Subject: [PATCH] SUPPORT-9136: move socket subscription to auth service --- frontend/src/ts/modules/app/app.module.ts | 3 ++- .../app/component/app-header.component.ts | 19 +------------- .../app/service/authorization.service.ts | 25 ++++++++++++++++--- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/frontend/src/ts/modules/app/app.module.ts b/frontend/src/ts/modules/app/app.module.ts index ad0409d5..a86ac57a 100644 --- a/frontend/src/ts/modules/app/app.module.ts +++ b/frontend/src/ts/modules/app/app.module.ts @@ -26,6 +26,7 @@ import {DropdownTreeviewSelectComponent} from "../../account_applications/compon import {TreeviewModule} from "ngx-treeview"; import {ErvuStaticGrid} from "../../account_applications/component/grid/ErvuStaticGrid"; import {TemporaryFileUpload} from "../../account_applications/component/field/TemporaryFileUpload"; +import {AuthorizationService} from "./service/authorization.service"; registerLocaleData(localeRu); export const DIRECTIVES = [ @@ -62,7 +63,7 @@ export const DIRECTIVES = [ DIRECTIVES ], providers: [ - TokenInterceptor, + TokenInterceptor, AuthorizationService, {provide: ProgressIndicationService, useClass: AppProgressIndicationService } ], bootstrap: [], diff --git a/frontend/src/ts/modules/app/component/app-header.component.ts b/frontend/src/ts/modules/app/component/app-header.component.ts index 2f60d6df..49037cfa 100644 --- a/frontend/src/ts/modules/app/component/app-header.component.ts +++ b/frontend/src/ts/modules/app/component/app-header.component.ts @@ -1,7 +1,5 @@ import {ChangeDetectionStrategy, ChangeDetectorRef, Component, OnDestroy} from "@angular/core"; import {AuthorizationService} from "../service/authorization.service"; -import {WebsocketService} from "../websocket/websocket.service"; -import {StatusUpdateService} from "../service/status-update.service"; import {Subscription} from "rxjs"; @Component({ @@ -17,31 +15,16 @@ export class AppHeaderComponent implements OnDestroy { private subscription: Subscription; - constructor(protected authService: AuthorizationService, protected cd: ChangeDetectorRef, - protected websocketService: WebsocketService, - protected statusUpdateService: StatusUpdateService) { + constructor(protected authService: AuthorizationService, protected cd: ChangeDetectorRef) { this.subscription = authService.onSessionUpdate .subscribe(session => { this.name = session.name; this.realm = session.realm; cd.markForCheck(); - - if (this.authService.hasRole('security_administrator')) { - this.websocketService.subscribe(({data}) => { - let parsedObj = JSON.parse(data); - - if (parsedObj && parsedObj.traceId) { - if (parsedObj.className === 'update' || parsedObj.className === 'processError') { - this.statusUpdateService.update(parsedObj); - } - } - }); - } }) } ngOnDestroy(): void { - this.websocketService.unsubscribe(); this.subscription.unsubscribe(); } } diff --git a/frontend/src/ts/modules/app/service/authorization.service.ts b/frontend/src/ts/modules/app/service/authorization.service.ts index 9aedf3ac..25fb045b 100644 --- a/frontend/src/ts/modules/app/service/authorization.service.ts +++ b/frontend/src/ts/modules/app/service/authorization.service.ts @@ -1,6 +1,8 @@ -import {Injectable} from "@angular/core"; +import {Injectable, OnDestroy} from "@angular/core"; import {Subject} from "rxjs"; import {HttpClient} from "@angular/common/http"; +import {WebsocketService} from "../websocket/websocket.service"; +import {StatusUpdateService} from "./status-update.service"; export interface UserSession { userId: string, @@ -11,13 +13,14 @@ export interface UserSession { } @Injectable({providedIn: 'root'}) -export class AuthorizationService { +export class AuthorizationService implements OnDestroy { private session: UserSession; public onSessionUpdate: Subject = new Subject(); - constructor(protected httpClient: HttpClient) {} + constructor(protected httpClient: HttpClient, protected websocketService: WebsocketService, + protected statusUpdateService: StatusUpdateService) {} public getCurrentSession(): Promise { if (this.session) return new Promise(resolve => resolve(this.session)); @@ -26,6 +29,18 @@ export class AuthorizationService { .then((session: UserSession) => { this.session = session; this.onSessionUpdate.next(session); + + if (this.hasRole('security_administrator')) { + this.websocketService.subscribe(({data}) => { + let parsedObj = JSON.parse(data); + + if (parsedObj && parsedObj.traceId) { + if (parsedObj.className === 'update' || parsedObj.className === 'processError') { + this.statusUpdateService.update(parsedObj); + } + } + }); + } return session; }) } @@ -61,4 +76,8 @@ export class AuthorizationService { getRoles(): string[] { return this.isAuthorized() ? this.session.roles : null; } + + ngOnDestroy(): void { + this.websocketService.unsubscribe(); + } }