]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/staff.component.ts
LP1869906 Angular staff cat browse links
[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      * Prevent the user from leaving the login page when they don't have
69      * a valid authoken.
70      *
71      * Prevent the user from leaving the workstation admin page when
72      * they don't have a valid workstation.
73      *
74      * This does not verify auth tokens with the server on every route,
75      * because that would be overkill.  This is only here to keep
76      * people boxed in with their authenication state was already
77      * known to be less then 100%.
78      */
79     preventForbiddenNavigation(url: string): void {
80
81         // No auth checks needed for login page.
82         if (url.startsWith(LOGIN_PATH)) {
83             return;
84         }
85
86         // We lost our authtoken, go back to the login page.
87         if (!this.auth.token()) {
88             this.router.navigate([LOGIN_PATH]);
89         }
90
91         // No workstation checks needed for workstation admin page.
92         if (url.startsWith(WS_BASE_PATH)) {
93             return;
94         }
95
96         if (this.auth.workstationState !== AuthWsState.VALID) {
97             this.router.navigate([WS_MANAGE_PATH]);
98         }
99     }
100
101     /**
102      * Listen for keyboard events here -- the root directive --  and pass
103      * events down to the key service for processing.
104      */
105     @HostListener('window:keydown', ['$event']) onKeyDown(evt: KeyboardEvent) {
106         this.keys.fire(evt);
107     }
108
109     /**
110      * Make sure to fire the contextmenu Event on Shift+F10
111      */
112     fireContextMenuEvent(): void {
113         const event = new MouseEvent('contextmenu', {
114             bubbles: true,
115             cancelable: false,
116             view: window,
117             button: 2,
118             buttons: 0,
119         });
120         document.activeElement.dispatchEvent(event);
121     }
122
123     /*
124     @ViewChild('egAccessKeyInfo')
125     private keyComponent: AccessKeyInfoComponent;
126     */
127
128 }
129