1 import {Component, OnInit, Input, Renderer2} from '@angular/core';
2 import {ToastService} from '@eg/share/toast/toast.service';
3 import {AuthService} from '@eg/core/auth.service';
4 import {DialogComponent} from '@eg/share/dialog/dialog.component';
5 import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
6 import {NetRequest, NetService} from '@eg/core/net.service';
9 selector: 'eg-op-change',
10 templateUrl: 'op-change.component.html'
13 export class OpChangeComponent
14 extends DialogComponent implements OnInit {
16 @Input() username: string;
17 @Input() password: string;
18 @Input() loginType = 'temp';
20 @Input() successMessage: string;
21 @Input() failMessage: string;
23 requestToEscalate: NetRequest;
26 private modal: NgbModal, // required for passing to parent
27 private renderer: Renderer2,
28 private toast: ToastService,
29 private net: NetService,
30 private auth: AuthService) {
35 // Focus the username any time the dialog is opened.
36 this.onOpen$.subscribe(
37 val => this.renderer.selectRootElement('#username').focus()
42 if (this.requestToEscalate) {
43 this.requestToEscalate.observer.error('Operation canceled');
44 delete this.requestToEscalate;
46 this.close(); // dialog close
49 login(): Promise<any> {
50 if (!(this.username && this.password)) {
51 return Promise.reject('Missing Params');
54 return this.auth.login(
55 { username : this.username,
56 password : this.password,
57 workstation : this.auth.workstation(),
65 // Fetch the user object
66 this.auth.testAuthToken().then(
69 this.toast.success(this.successMessage);
70 if (this.requestToEscalate) {
71 // Allow a breath for the dialog to clean up.
72 setTimeout(() => this.sendEscalatedRequest());
79 this.toast.danger(this.failMessage);
84 restore(): Promise<any> {
85 return this.auth.undoOpChange().then(
86 ok => this.toast.success(this.successMessage),
87 err => this.toast.danger(this.failMessage)
91 escalateRequest(req: NetRequest) {
92 this.requestToEscalate = req;
96 // Resend a net request using the credentials just created
97 // via operator change.
98 sendEscalatedRequest() {
99 const sourceReq = this.requestToEscalate;
100 delete this.requestToEscalate;
102 console.debug('Op-Change escalating request', sourceReq);
104 // Clone the source request, modifying the params to
105 // use the op-change'd authtoken
106 const req = new NetRequest(
109 [this.auth.token()].concat(sourceReq.params.splice(1))
112 // Relay responses received for our escalated request to
113 // the caller via the original request observer.
114 this.net.requestCompiled(req)
116 res => sourceReq.observer.next(res),
117 (err: unknown) => sourceReq.observer.error(err),
118 () => sourceReq.observer.complete()
119 ).add(() => this.auth.undoOpChange());