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()
41 login(): Promise<any> {
42 if (!(this.username && this.password)) {
43 return Promise.reject('Missing Params');
46 return this.auth.login(
47 { username : this.username,
48 password : this.password,
49 workstation : this.auth.workstation(),
57 // Fetch the user object
58 this.auth.testAuthToken().then(
61 this.toast.success(this.successMessage);
62 if (this.requestToEscalate) {
63 // Allow a breath for the dialog to clean up.
64 setTimeout(() => this.sendEscalatedRequest());
71 this.toast.danger(this.failMessage);
76 restore(): Promise<any> {
77 return this.auth.undoOpChange().then(
78 ok => this.toast.success(this.successMessage),
79 err => this.toast.danger(this.failMessage)
83 escalateRequest(req: NetRequest) {
84 this.requestToEscalate = req;
88 // Resend a net request using the credentials just created
89 // via operator change.
90 sendEscalatedRequest() {
91 const sourceReq = this.requestToEscalate;
92 delete this.requestToEscalate;
94 console.debug('Op-Change escalating request', sourceReq);
96 // Clone the source request, modifying the params to
97 // use the op-change'd authtoken
98 const req = new NetRequest(
101 [this.auth.token()].concat(sourceReq.params.splice(1))
104 // Relay responses received for our escalated request to
105 // the caller via the original request observer.
106 this.net.requestCompiled(req)
108 res => sourceReq.observer.next(res),
109 err => sourceReq.observer.error(err),
110 () => sourceReq.observer.complete()
111 ).add(_ => this.auth.undoOpChange());