Added automatic TTS login for impersonation.

This commit is contained in:
Tom 2025-01-16 15:03:30 +00:00
parent a19a7a0217
commit e6f681219c
2 changed files with 18 additions and 4 deletions

View File

@ -1,6 +1,6 @@
import { isPlatformBrowser } from '@angular/common';
import { Component, OnInit, Inject, PLATFORM_ID, NgZone, OnDestroy, inject } from '@angular/core';
import { Router, RouterOutlet } from '@angular/router';
import { ActivatedRoute, Router, RouterOutlet } from '@angular/router';
import { HermesClientService } from './hermes-client.service';
import { AuthUserGuard } from './shared/auth/auth.user.guard'
import { first, Subscription, timeout } from 'rxjs';

View File

@ -1,4 +1,4 @@
import { Component, Inject, OnInit, PLATFORM_ID } from '@angular/core';
import { Component, inject, Inject, OnInit, PLATFORM_ID } from '@angular/core';
import { ApiAuthenticationService } from '../../shared/services/api/api-authentication.service';
import { MatCardModule } from '@angular/material/card';
import { MatSelectModule } from '@angular/material/select';
@ -8,6 +8,9 @@ import { environment } from '../../../environments/environment';
import EventService from '../../shared/services/EventService';
import { HermesClientService } from '../../hermes-client.service';
import { Router } from '@angular/router';
import { timeout, first } from 'rxjs';
import ApiKey from '../../shared/models/api-key';
import { ApiKeyService } from '../../shared/services/api/api-key.service';
@Component({
selector: 'impersonation',
@ -17,6 +20,8 @@ import { Router } from '@angular/router';
styleUrl: './impersonation.component.scss'
})
export class ImpersonationComponent implements OnInit {
private readonly keyService = inject(ApiKeyService);
impersonated: string | undefined;
users: { id: string, name: string }[];
@ -25,7 +30,7 @@ export class ImpersonationComponent implements OnInit {
}
ngOnInit(): void {
if (!isPlatformBrowser(this.platformId)) {
if (!isPlatformBrowser(this.platformId) || !this.auth.isAdmin()) {
return;
}
@ -40,6 +45,16 @@ export class ImpersonationComponent implements OnInit {
this.impersonated = id;
}
});
this.events.listen('impersonation', (userId) => {
this.keyService.fetch(true)
.pipe(timeout(3000), first())
.subscribe(async (d: ApiKey[]) => {
if (d.length > 0)
this.hermes.login(d[0].id);
await this.router.navigate([this.router.url.substring(1)]);
});
});
}
public isAdmin() {
@ -62,7 +77,6 @@ export class ImpersonationComponent implements OnInit {
}).subscribe(async (data: any) => {
this.hermes.disconnect();
this.events.emit('impersonation', e.value);
await this.router.navigate(['tts-login']);
});
} else {
this.http.put(environment.API_HOST + '/admin/impersonate', {