6c7b64d94ff6ffc6a3f07d9d1b119f5d0ae7f694
[evergreen-equinox.git] / Open-ILS / src / eg2 / src / app / staff / login.component.ts
1 import {Component, OnInit, Renderer2} from '@angular/core';
2 import {Location} from '@angular/common';
3 import {Router, ActivatedRoute} from '@angular/router';
4 import {AuthService, AuthWsState} from '@eg/core/auth.service';
5 import {StoreService} from '@eg/core/store.service';
6 import {OrgService} from '@eg/core/org.service';
7 import {OfflineService} from '@eg/staff/share/offline.service';
8
9 // Direct users to the AngJS splash page when no routeTo is provided.
10 const SPLASH_PAGE_PATH = '/eg/staff/splash';
11
12 @Component({
13   templateUrl : './login.component.html'
14 })
15 export class StaffLoginComponent implements OnInit {
16
17     workstations: any[];
18     loginFailed: boolean;
19     routeTo: string;
20
21     args = {
22       username : '',
23       password : '',
24       workstation : '',
25       type : 'staff'
26     };
27
28     constructor(
29       private router: Router,
30       private route: ActivatedRoute,
31       private ngLocation: Location,
32       private renderer: Renderer2,
33       private auth: AuthService,
34       private org: OrgService,
35       private store: StoreService,
36       private offline: OfflineService
37     ) {}
38
39     ngOnInit() {
40         this.routeTo = this.route.snapshot.queryParamMap.get('routeTo');
41
42         if (this.routeTo) {
43             if (this.routeTo.match(/^[a-z]+:\/\//i)) {
44                 console.warn(
45                     'routeTo must contain only path information: ', this.routeTo);
46                 this.routeTo = null;
47             }
48         }
49
50         // clear out any stale auth data
51         this.auth.logout();
52
53         // Focus username
54         this.renderer.selectRootElement('#username').focus();
55
56         this.store.getWorkstations()
57         .then(wsList => {
58             this.workstations = wsList;
59             return this.store.getDefaultWorkstation();
60         }).then(def => {
61             this.args.workstation = def;
62             this.applyWorkstation();
63         });
64     }
65
66     applyWorkstation() {
67         const wanted = this.route.snapshot.queryParamMap.get('workstation');
68         if (!wanted) { return; } // use the default
69
70         const exists = this.workstations.filter(w => w.name === wanted)[0];
71         if (exists) {
72             this.args.workstation = wanted;
73         } else {
74             console.error(`Unknown workstation requested: ${wanted}`);
75         }
76     }
77
78     handleSubmit() {
79
80         // post-login URL
81         let url: string = this.routeTo || SPLASH_PAGE_PATH;
82
83         // prevent sending the user back to the login page
84         if (url.match('/staff/login')) { url = SPLASH_PAGE_PATH; }
85
86         const workstation: string = this.args.workstation;
87
88         this.loginFailed = false;
89         this.auth.login(this.args).then(
90             ok => {
91
92                 if (this.auth.workstationState === AuthWsState.NOT_FOUND_SERVER) {
93                     // User attempted to login with a workstation that is
94                     // unknown to the server. Redirect to the WS admin page.
95                     // Reset the WS state to avoid looping back to WS removal
96                     // page before the new workstation can be activated.
97                     this.auth.workstationState = AuthWsState.PENDING;
98                     this.router.navigate(
99                         [`/staff/admin/workstation/workstations/remove/${workstation}`]);
100
101                 } else {
102
103                     this.offline.refreshOfflineData()
104                     // Initial login clears cached org unit settings.
105                     .then(_ => this.org.clearCachedSettings())
106                     .then(_ => {
107
108                         // Force reload of the app after a successful login.
109                         // This allows the route resolver to re-run with a
110                         // valid auth token and workstation.
111                         window.location.href =
112                             this.ngLocation.prepareExternalUrl(url);
113                     });
114                 }
115             },
116             notOk => {
117                 this.loginFailed = true;
118             }
119         );
120     }
121 }
122
123
124