]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/services/startup.js
lp1653998 webstaff redirect to login page
[Evergreen.git] / Open-ILS / web / js / ui / default / staff / services / startup.js
1 /**
2  * Core Service - egStartup
3  *
4  * Coordinates all startup routines and consolidates them into
5  * a single startup promise.  Startup can be launched from multiple
6  * controllers, etc., but only one startup routine will be run.
7  *
8  * If no valid authtoken is found, startup will exit early and 
9  * change the page href to the login page.  Otherwise, the global
10  * promise returned by startup.go() will be resolved after all
11  * async data is arrived.
12  */
13
14 angular.module('egCoreMod')
15
16 .config(['$routeProvider','$locationProvider','$compileProvider',
17  function($routeProvider , $locationProvider , $compileProvider) {
18     $locationProvider.html5Mode(true);
19     $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|blob):/);
20 }])
21
22 .factory('egStartup', 
23        ['$q','$rootScope','$location','$window','egIDL','egAuth','egEnv',
24 function($q,  $rootScope,  $location,  $window,  egIDL,  egAuth,  egEnv) {
25
26     var service = { promise : null }
27
28     // returns true if we are staying on the current page
29     // false if we are redirecting to login
30     service.expiredAuthHandler = function() {
31         console.debug('egStartup.expiredAuthHandler()');
32         egAuth.logout(); // clean up
33
34         // no need to redirect if we're on the /login page
35         if ($location.path() == '/login') return true;
36
37         // change locations to the login page, using the current page
38         // as the 'route_to' destination on /login
39         $window.location.href = $location
40             .path('/login')
41             .search({route_to : 
42                 $window.location.pathname + $window.location.search})
43             .absUrl();
44
45         return false;
46     }
47
48     // if during startup or any time in the future we encounter an expired
49     // authtoken, call our epired token handler
50     // we handle this here instead egAuth, since it affects the flow
51     // of the startup routines when no valid token exists during startup.
52     $rootScope.$on('egAuthExpired', function() {service.expiredAuthHandler()});
53
54     service.go = function () {
55         if (service.promise) {
56             // startup already started, return our existing promise
57             return service.promise;
58         } 
59
60         // create a new promise and fire off startup
61         var deferred = $q.defer();
62         service.promise = deferred.promise;
63
64         // IDL parsing is sync.  No promises required
65         egIDL.parseIDL();
66         egAuth.testAuthToken().then(
67
68             // testAuthToken resolved
69             function() { 
70                 egEnv.load().then(
71                     function() { deferred.resolve() }, 
72                     function() { 
73                         deferred.reject('egEnv did not resolve')
74                     }
75                 );
76             },
77
78             // testAuthToken rejected
79             function() { 
80                 console.log('egAuth found no valid authtoken');
81                 if (service.expiredAuthHandler()) deferred.resolve();
82             }
83         );
84
85         return service.promise;
86     }
87     
88     return service;
89 }]);
90