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';
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';
13 templateUrl: 'staff.component.html',
14 styleUrls: ['staff.component.css']
17 export class StaffComponent implements OnInit {
20 private router: Router,
21 private route: ActivatedRoute,
23 private net: NetService,
24 private auth: AuthService,
25 private keys: AccessKeyService
30 // Fires on all in-staff-app router navigation, but not initial
32 this.router.events.subscribe(routeEvent => {
33 if (routeEvent instanceof NavigationEnd) {
34 // console.debug(`StaffComponent routing to ${routeEvent.url}`);
35 this.preventForbiddenNavigation(routeEvent.url);
39 // Redirect to the login page on any auth timeout events.
40 this.net.authExpired$.subscribe(expireEvent => {
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();
48 console.debug('Auth session has expired. Redirecting to login');
49 this.auth.redirectUrl = this.router.url;
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.
58 this.router.navigate([LOGIN_PATH]);
62 this.route.data.subscribe((data: {staffResolver: any}) => {
63 // Data fetched via StaffResolver is available here.
68 * Prevent the user from leaving the login page when they don't have
71 * Prevent the user from leaving the workstation admin page when
72 * they don't have a valid workstation.
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%.
79 preventForbiddenNavigation(url: string): void {
81 // No auth checks needed for login page.
82 if (url.startsWith(LOGIN_PATH)) {
86 // We lost our authtoken, go back to the login page.
87 if (!this.auth.token()) {
88 this.router.navigate([LOGIN_PATH]);
91 // No workstation checks needed for workstation admin page.
92 if (url.startsWith(WS_BASE_PATH)) {
96 if (this.auth.workstationState !== AuthWsState.VALID) {
97 this.router.navigate([WS_MANAGE_PATH]);
102 * Listen for keyboard events here -- the root directive -- and pass
103 * events down to the key service for processing.
105 @HostListener('window:keydown', ['$event']) onKeyDown(evt: KeyboardEvent) {
110 * Make sure to fire the contextmenu Event on Shift+F10
112 fireContextMenuEvent(): void {
113 const event = new MouseEvent('contextmenu', {
120 document.activeElement.dispatchEvent(event);
124 @ViewChild('egAccessKeyInfo')
125 private keyComponent: AccessKeyInfoComponent;