SUPPORT-9136: move socket subscription to auth service
This commit is contained in:
parent
9cc7dff3be
commit
28ec0c83ee
3 changed files with 25 additions and 22 deletions
|
|
@ -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: [],
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<UserSession> = new Subject<UserSession>();
|
||||
|
||||
constructor(protected httpClient: HttpClient) {}
|
||||
constructor(protected httpClient: HttpClient, protected websocketService: WebsocketService,
|
||||
protected statusUpdateService: StatusUpdateService) {}
|
||||
|
||||
public getCurrentSession(): Promise<any> {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue