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