]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/resolver.service.ts
LP1825896 Store workstations in Hatch when available
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / resolver.service.ts
1 import {Injectable} from '@angular/core';
2 import {Location} from '@angular/common';
3 import {Observable, Observer, of} from 'rxjs';
4 import {Router, Resolve, RouterStateSnapshot,
5         ActivatedRoute, ActivatedRouteSnapshot} from '@angular/router';
6 import {StoreService} from '@eg/core/store.service';
7 import {NetService} from '@eg/core/net.service';
8 import {AuthService, AuthWsState} from '@eg/core/auth.service';
9 import {PermService} from '@eg/core/perm.service';
10 import {OrgService} from '@eg/core/org.service';
11 import {FormatService} from '@eg/core/format.service';
12 import {HatchService} from '@eg/core/hatch.service';
13
14 const LOGIN_PATH = '/staff/login';
15 const WS_MANAGE_PATH = '/staff/admin/workstation/workstations/manage';
16
17 /**
18  * Load data used by all staff modules.
19  */
20 @Injectable()
21 export class StaffResolver implements Resolve<Observable<any>> {
22
23     // Tracks the primary resolve observable.
24     observer: Observer<any>;
25
26     constructor(
27         private router: Router,
28         private route: ActivatedRoute,
29         private ngLocation: Location,
30         private hatch: HatchService,
31         private store: StoreService,
32         private org: OrgService,
33         private net: NetService,
34         private auth: AuthService,
35         private perm: PermService,
36         private format: FormatService
37     ) {}
38
39     resolve(
40         route: ActivatedRouteSnapshot,
41         state: RouterStateSnapshot): Observable<any> {
42
43         this.hatch.connect();
44
45         // Staff cookies stay in /$base/staff/
46         // NOTE: storing session data at '/' so it can be shared by
47         // Angularjs apps.
48         this.store.loginSessionBasePath = '/';
49         // ^-- = this.ngLocation.prepareExternalUrl('/staff');
50
51         // Not sure how to get the path without params... using this for now.
52         const path = state.url.split('?')[0];
53         if (path === '/staff/login') {
54             return of(true);
55         }
56
57         const observable: Observable<any>
58             = Observable.create(o => this.observer = o);
59
60         this.auth.testAuthToken().then(
61             tokenOk => {
62                 this.confirmStaffPerms().then(
63                     hasPerms => {
64                         this.auth.verifyWorkstation().then(
65                             wsOk => {
66                                 this.loadStartupData()
67                                 .then(ok => this.observer.complete());
68                             },
69                             wsNotOk => this.handleInvalidWorkstation(path)
70                         );
71                     },
72                     hasNotPerms => {
73                         this.observer.error(
74                             'User does not have staff permissions');
75                     }
76                 );
77             },
78             tokenNotOk => this.handleInvalidToken(state)
79         );
80
81         return observable;
82     }
83
84
85     // Confirm the user has the STAFF_LOGIN permission anywhere before
86     // allowing the staff sub-tree to load. This will prevent users
87     // with valid, non-staff authtokens from attempting to connect and
88     // subsequently getting redirected to the workstation admin page
89     // (since they won't have a valid WS either).
90     confirmStaffPerms(): Promise<any> {
91         return new Promise((resolve, reject) => {
92             this.perm.hasWorkPermAt(['STAFF_LOGIN']).then(
93                 permMap => {
94                     if (permMap.STAFF_LOGIN.length) {
95                         resolve('perm check OK');
96                     } else {
97                         reject('perm check faield');
98                     }
99                 }
100             );
101         });
102     }
103
104
105     // A page that's not the login page was requested without a
106     // valid auth token.  Send the caller back to the login page.
107     handleInvalidToken(state: RouterStateSnapshot): void {
108         console.debug('StaffResolver: authtoken is not valid');
109         this.auth.redirectUrl = state.url;
110         this.router.navigate([LOGIN_PATH]);
111         this.observer.error('invalid or no auth token');
112     }
113
114     handleInvalidWorkstation(path: string): void {
115
116         if (path.startsWith(WS_MANAGE_PATH)) {
117             // user is navigating to the WS admin page.
118             this.observer.complete();
119         } else {
120             this.router.navigate([WS_MANAGE_PATH]);
121             this.observer.error(`Auth session linked to no
122                 workstation or a workstation unknown to this browser`);
123         }
124     }
125
126     /**
127      * Fetches data common to all staff interfaces.
128      */
129     loadStartupData(): Promise<void> {
130
131         // Fetch settings needed globally.  This will cache the values
132         // in the org service.
133         return this.org.settings([
134             'lib.timezone',
135             'webstaff.format.dates',
136             'webstaff.format.date_and_time',
137             'ui.staff.max_recent_patrons',
138             'ui.staff.angular_catalog.enabled' // navbar
139         ]).then(settings => {
140             // Avoid clobbering defaults
141             if (settings['lib.timezone']) {
142                 this.format.wsOrgTimezone = settings['lib.timezone'];
143             }
144             if (settings['webstaff.format.dates']) {
145                 this.format.dateFormat = settings['webstaff.format.dates'];
146             }
147             if (settings['webstaff.format.date_and_time']) {
148                 this.format.dateTimeFormat =
149                     settings['webstaff.format.date_and_time'];
150             }
151         });
152     }
153 }
154