]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/staff.component.ts
LP1779158 Angular7 and ng-lint updates
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / staff.component.ts
1 import {Component, OnInit, NgZone, HostListener} from '@angular/core';
2 import {Router, ActivatedRoute, NavigationEnd} from '@angular/router';
3 import {AuthService, AuthWsState} from '@eg/core/auth.service';
4 import {NetService} from '@eg/core/net.service';
5 import {AccessKeyService} from '@eg/share/accesskey/accesskey.service';
6 import {AccessKeyInfoComponent} from '@eg/share/accesskey/accesskey-info.component';
7
8 const LOGIN_PATH = '/staff/login';
9 const WS_BASE_PATH = '/staff/admin/workstation/workstations/';
10 const WS_MANAGE_PATH = '/staff/admin/workstation/workstations/manage';
11
12 @Component({
13   templateUrl: 'staff.component.html',
14   styleUrls: ['staff.component.css']
15 })
16
17 export class StaffComponent implements OnInit {
18
19     constructor(
20         private router: Router,
21         private route: ActivatedRoute,
22         private zone: NgZone,
23         private net: NetService,
24         private auth: AuthService,
25         private keys: AccessKeyService
26     ) {}
27
28     ngOnInit() {
29
30         // Fires on all in-staff-app router navigation, but not initial
31         // page load.
32         this.router.events.subscribe(routeEvent => {
33             if (routeEvent instanceof NavigationEnd) {
34                 // console.debug(`StaffComponent routing to ${routeEvent.url}`);
35                 this.preventForbiddenNavigation(routeEvent.url);
36             }
37         });
38
39         // Redirect to the login page on any auth timeout events.
40         this.net.authExpired$.subscribe(expireEvent => {
41
42             // If the expired authtoken was identified locally (i.e.
43             // in this browser tab) notify all tabs of imminent logout.
44             if (!expireEvent.viaExternal) {
45                 this.auth.broadcastLogout();
46             }
47
48             console.debug('Auth session has expired. Redirecting to login');
49             this.auth.redirectUrl = this.router.url;
50
51             // https://github.com/angular/angular/issues/18254
52             // When a tab redirects to a login page as a result of
53             // another tab broadcasting a logout, ngOnInit() fails to
54             // fire in the login component, until the user interacts
55             // with the page.  Fix it by wrapping it in zone.run().
56             // This is the only navigate() where I have seen this happen.
57             this.zone.run(() => {
58                 this.router.navigate([LOGIN_PATH]);
59             });
60         });
61
62         this.route.data.subscribe((data: {staffResolver: any}) => {
63             // Data fetched via StaffResolver is available here.
64         });
65
66
67     }
68
69     /**
70      * Prevent the user from leaving the login page when they don't have
71      * a valid authoken.
72      *
73      * Prevent the user from leaving the workstation admin page when
74      * they don't have a valid workstation.
75      *
76      * This does not verify auth tokens with the server on every route,
77      * because that would be overkill.  This is only here to keep
78      * people boxed in with their authenication state was already
79      * known to be less then 100%.
80      */
81     preventForbiddenNavigation(url: string): void {
82
83         // No auth checks needed for login page.
84         if (url.startsWith(LOGIN_PATH)) {
85             return;
86         }
87
88         // We lost our authtoken, go back to the login page.
89         if (!this.auth.token()) {
90             this.router.navigate([LOGIN_PATH]);
91         }
92
93         // No workstation checks needed for workstation admin page.
94         if (url.startsWith(WS_BASE_PATH)) {
95             return;
96         }
97
98         if (this.auth.workstationState !== AuthWsState.VALID) {
99             this.router.navigate([WS_MANAGE_PATH]);
100         }
101     }
102
103     /**
104      * Listen for keyboard events here -- the root directive --  and pass
105      * events down to the key service for processing.
106      */
107     @HostListener('window:keydown', ['$event']) onKeyDown(evt: KeyboardEvent) {
108         this.keys.fire(evt);
109     }
110
111     /*
112     @ViewChild('egAccessKeyInfo')
113     private keyComponent: AccessKeyInfoComponent;
114     */
115
116 }
117
118