7b854ab5999020daddbcdd82f65b5b68ce589050
[evergreen-equinox.git] / Open-ILS / src / eg2 / src / app / staff / share / op-change / op-change.component.ts
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';
7
8 @Component({
9   selector: 'eg-op-change',
10   templateUrl: 'op-change.component.html'
11 })
12
13 export class OpChangeComponent
14     extends DialogComponent implements OnInit {
15
16     @Input() username: string;
17     @Input() password: string;
18     @Input() loginType = 'temp';
19
20     @Input() successMessage: string;
21     @Input() failMessage: string;
22
23     requestToEscalate: NetRequest;
24
25     constructor(
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) {
31         super(modal);
32     }
33
34     ngOnInit() {
35         // Focus the username any time the dialog is opened.
36         this.onOpen$.subscribe(
37             val => this.renderer.selectRootElement('#username').focus()
38         );
39     }
40
41     login(): Promise<any> {
42         if (!(this.username && this.password)) {
43             return Promise.reject('Missing Params');
44         }
45
46         return this.auth.login(
47             {   username    : this.username,
48                 password    : this.password,
49                 workstation : this.auth.workstation(),
50                 type        : this.loginType
51             },  true        // isOpChange
52         ).then(
53             ok => {
54                 this.password = '';
55                 this.username = '';
56
57                 // Fetch the user object
58                 this.auth.testAuthToken().then(
59                     ok2 => {
60                         this.close();
61                         this.toast.success(this.successMessage);
62                         if (this.requestToEscalate) {
63                             // Allow a breath for the dialog to clean up.
64                             setTimeout(() => this.sendEscalatedRequest());
65                         }
66                     }
67                 );
68             },
69             notOk => {
70                 this.password = '';
71                 this.toast.danger(this.failMessage);
72             }
73         );
74     }
75
76     restore(): Promise<any> {
77         return this.auth.undoOpChange().then(
78             ok => this.toast.success(this.successMessage),
79             err => this.toast.danger(this.failMessage)
80         );
81     }
82
83     escalateRequest(req: NetRequest) {
84         this.requestToEscalate = req;
85         this.open({});
86     }
87
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;
93
94         console.debug('Op-Change escalating request', sourceReq);
95
96         // Clone the source request, modifying the params to
97         // use the op-change'd authtoken
98         const req = new NetRequest(
99             sourceReq.service,
100             sourceReq.method,
101             [this.auth.token()].concat(sourceReq.params.splice(1))
102         );
103
104         // Relay responses received for our escalated request to
105         // the caller via the original request observer.
106         this.net.requestCompiled(req)
107         .subscribe(
108             res => sourceReq.observer.next(res),
109             err => sourceReq.observer.error(err),
110             ()  => sourceReq.observer.complete()
111         ).add(_ => this.auth.undoOpChange());
112     }
113 }
114
115