rsanchez
2015-01-28 da889d489da5d7fa8c71d9f21f24b1dc2e29d8e1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
(function() {
   'use strict';
   var m = angular.module('securis', [ 'ngRoute', 'ngResource', 'toaster', 'localytics.directives', 'catalogs', 'i18n' ]);
   m.service('$store', function() {
       this.get = function(key, defaultValue) {
           return store.get(key) || defaultValue;
       }
       this.set = this.put = function(key, value) {
           store.set(key, value);
       }
       this.remove = this.delete = function(key) {
           return store.remove(key);
       }
       this.clear = this.clearAll = function() {
           store.clear();
       }
       this.getAll = function() {
           return store.getAll();
       }
   });
   m.factory('securisHttpInterceptor', function($q, $location, $store, toaster) {
       var isUnauthorizedAccess = function(rejection) {
           return rejection.status === 401 /* Unauthorized */;
       } 
       return {
           'request': function(config) {
               var token = $store.get('token');
               if (token) {
                   var la = $store.get('last_access');
                   var now = new Date().getTime();
                   if (la !== null) {
                       if (now > (la + 1800000)) { // Session timeout is 1/2
                                                   // hour
                           $store.clear();
                           $location.path('/login');
                           toaster.pop('warning', 'Session has expired', null, 4000);
                       } else {
                           console.debug('Last access recent');
                       }
                   }
                   $store.set('last_access', now);
               }
               return config || $q.when(config);
           },
           'responseError': function(rejection) {
               // do something on error
               if (isUnauthorizedAccess(rejection)) {
                   if ($location.path() !== '/login') {
                       $store.clear();
                       $location.path('/login');
                       console.error('There was an unathorized access to url {0}, method: {1}'.$i18n(rejection.config.url, rejection.config.method));
                   } else {
                       // console.log('Error on login ...')
                   }
               }
               return $q.reject(rejection);
           }
       };
   });
   m.config(function($routeProvider, $locationProvider, $httpProvider) {
       console.debug('Configuring routes...');
       $routeProvider.when('/login', {
           templateUrl: 'login.html',
           controller: 'LoginCtrl'
       });
       $routeProvider.when('/licenses', {
           templateUrl: 'licenses.html',
           controller: 'PackAndLicensesCtrl'
       });
       $routeProvider.when('/admin', {
           templateUrl: 'admin.html',
           controller: 'AdminCtrl'
       });
       // configure html5 to get links working on jsfiddle
       $locationProvider.html5Mode(true);
       $httpProvider.interceptors.push('securisHttpInterceptor');
   });
   m.controller('MainCtrl', ['$scope', '$http', '$location', '$L', '$store',
                             function($scope, $http, $location, $L, $store) {
       $scope.$location = $location;
       
       $location.path('/login');
       if ($store.get('token') != null) {
           $http.get('check', {
               headers: {
                   'X-SECURIS-TOKEN': $store.get('token')
               }
           }).success(function(data) {
               if (data.valid) {
                   $http.defaults.headers.common['X-SECURIS-TOKEN'] = $store.get('token');
                   var location = $store.get('location') || '/licenses';
                   $location.path(location);
                   $store.set('user', data.user);
               }
           });
       }
       $scope.logout = function() {
           $store.remove('user');
           $store.remove('token');
           $location.path('/login');
       }
   }]);    
})();