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