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';
14 const LOGIN_PATH = '/staff/login';
15 const WS_MANAGE_PATH = '/staff/admin/workstation/workstations/manage';
18 * Load data used by all staff modules.
21 export class StaffResolver implements Resolve<Observable<any>> {
23 // Tracks the primary resolve observable.
24 observer: Observer<any>;
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
40 route: ActivatedRouteSnapshot,
41 state: RouterStateSnapshot): Observable<any> {
45 // Staff cookies stay in /$base/staff/
46 // NOTE: storing session data at '/' so it can be shared by
48 this.store.loginSessionBasePath = '/';
49 // ^-- = this.ngLocation.prepareExternalUrl('/staff');
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') {
57 const observable: Observable<any>
58 = Observable.create(o => this.observer = o);
60 this.auth.testAuthToken().then(
62 this.confirmStaffPerms().then(
64 this.auth.verifyWorkstation().then(
66 this.loadStartupData()
67 .then(ok => this.observer.complete());
69 wsNotOk => this.handleInvalidWorkstation(path)
74 'User does not have staff permissions');
78 tokenNotOk => this.handleInvalidToken(state)
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(
94 if (permMap.STAFF_LOGIN.length) {
95 resolve('perm check OK');
97 reject('perm check faield');
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');
114 handleInvalidWorkstation(path: string): void {
116 if (path.startsWith(WS_MANAGE_PATH)) {
117 // user is navigating to the WS admin page.
118 this.observer.complete();
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`);
127 * Fetches data common to all staff interfaces.
129 loadStartupData(): Promise<void> {
131 // Fetch settings needed globally. This will cache the values
132 // in the org service.
133 return this.org.settings([
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'];
144 if (settings['webstaff.format.dates']) {
145 this.format.dateFormat = settings['webstaff.format.dates'];
147 if (settings['webstaff.format.date_and_time']) {
148 this.format.dateTimeFormat =
149 settings['webstaff.format.date_and_time'];