1 import {Component, OnInit, NgZone, HostListener} from '@angular/core';
2 import {Location} from '@angular/common';
3 import {Router, ActivatedRoute, NavigationEnd} from '@angular/router';
4 import {AuthService, AuthWsState} from '@eg/core/auth.service';
5 import {NetService} from '@eg/core/net.service';
6 import {AccessKeyService} from '@eg/share/accesskey/accesskey.service';
7 import {AccessKeyInfoComponent} from '@eg/share/accesskey/accesskey-info.component';
9 const LOGIN_PATH = '/staff/login';
10 const WS_BASE_PATH = '/staff/admin/workstation/workstations/';
11 const WS_MANAGE_PATH = '/staff/admin/workstation/workstations/manage';
14 templateUrl: 'staff.component.html',
15 styleUrls: ['staff.component.css']
18 export class StaffComponent implements OnInit {
21 private router: Router,
22 private route: ActivatedRoute,
23 private ngLocation: Location,
25 private net: NetService,
26 private auth: AuthService,
27 private keys: AccessKeyService
32 // Fires on all in-staff-app router navigation, but not initial
34 this.router.events.subscribe(routeEvent => {
35 if (routeEvent instanceof NavigationEnd) {
36 // console.debug(`StaffComponent routing to ${routeEvent.url}`);
37 this.preventForbiddenNavigation(routeEvent.url);
41 // Redirect to the login page on any auth timeout events.
42 this.net.authExpired$.subscribe(expireEvent => {
44 // If the expired authtoken was identified locally (i.e.
45 // in this browser tab) notify all tabs of imminent logout.
46 if (!expireEvent.viaExternal) {
47 this.auth.broadcastLogout();
50 console.debug('Auth session has expired. Redirecting to login');
51 const url = this.ngLocation.prepareExternalUrl(this.router.url);
53 // https://github.com/angular/angular/issues/18254
54 // When a tab redirects to a login page as a result of
55 // another tab broadcasting a logout, ngOnInit() fails to
56 // fire in the login component, until the user interacts
57 // with the page. Fix it by wrapping it in zone.run().
58 // This is the only navigate() where I have seen this happen.
60 this.router.navigate([LOGIN_PATH], {queryParams: {routeTo: url}});
64 this.route.data.subscribe((data: {staffResolver: any}) => {
65 // Data fetched via StaffResolver is available here.
70 * Prevent the user from leaving the login page when they don't have
73 * Prevent the user from leaving the workstation admin page when
74 * they don't have a valid workstation.
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%.
81 preventForbiddenNavigation(url: string): void {
83 // No auth checks needed for login page.
84 if (url.startsWith(LOGIN_PATH)) {
88 // We lost our authtoken, go back to the login page.
89 if (!this.auth.token()) {
90 this.router.navigate([LOGIN_PATH]);
93 // No workstation checks needed for workstation admin page.
94 if (url.startsWith(WS_BASE_PATH)) {
98 if (this.auth.workstationState !== AuthWsState.VALID) {
99 this.router.navigate([WS_MANAGE_PATH]);
104 * Listen for keyboard events here -- the root directive -- and pass
105 * events down to the key service for processing.
107 @HostListener('window:keydown', ['$event']) onKeyDown(evt: KeyboardEvent) {
112 * Make sure to fire the contextmenu Event on Shift+F10
114 fireContextMenuEvent(): void {
115 const event = new MouseEvent('contextmenu', {
122 document.activeElement.dispatchEvent(event);
126 @ViewChild('egAccessKeyInfo')
127 private keyComponent: AccessKeyInfoComponent;