Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 1 | 'use strict'; |
| 2 | angular.module('Authentication', []); |
| 3 | var app = angular.module('bmcApp', [ |
Ed Tanous | 5fceeb4 | 2017-06-28 09:43:09 -0700 | [diff] [blame] | 4 | 'ngCookies', |
| 5 | 'ngAnimate', |
| 6 | 'ngSanitize', |
| 7 | 'ui.bootstrap', |
| 8 | 'ui.router', |
| 9 | 'ngWebSocket', |
| 10 | 'Authentication', |
| 11 | 'ui.router.modal', |
| 12 | 'smart-table', |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 13 | ]); |
| 14 | |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 15 | app.service('loginInterceptor', [ |
| 16 | '$injector', |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 17 | function($injector) { |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 18 | var service = this; |
| 19 | |
| 20 | service.responseError = function(response) { |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 21 | var $state = $injector.get('$state'); |
| 22 | var AuthenticationService = $injector.get('AuthenticationService'); |
| 23 | if (response.status == 401) { |
| 24 | console.log('Login required... '); |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 25 | |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 26 | var invalidate_reason = 'Your user was logged out.'; |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 27 | |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 28 | // if we're attempting to log in, we need to |
| 29 | // continue the promise chain to make sure the user is informed |
| 30 | if ($state.current.name === 'login') { |
| 31 | invalidate_reason = 'Your username and password was incorrect'; |
| 32 | } else { |
| 33 | $state.after_login_state = $state.current.name; |
| 34 | $state.go('login'); |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 35 | } |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 36 | AuthenticationService.ClearCredentials(invalidate_reason); |
| 37 | } |
| 38 | return response; |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 39 | }; |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 40 | } |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 41 | ]) |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 42 | |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 43 | app.config([ |
| 44 | '$httpProvider', |
| 45 | function($httpProvider) { |
| 46 | $httpProvider.interceptors.push('loginInterceptor'); |
| 47 | } |
| 48 | ]); |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 49 | |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 50 | app.directive('windowSize', [ |
| 51 | '$window', |
| 52 | function($window) { |
| 53 | return function(scope, element) { |
| 54 | var w = angular.element($window); |
| 55 | scope.getWindowDimensions = function() { |
| 56 | return {'h' : w.height(), 'w' : w.width()}; |
| 57 | }; |
| 58 | scope.$watch(scope.getWindowDimensions, function(newValue, oldValue) { |
| 59 | scope.windowHeight = newValue.h; |
| 60 | scope.windowWidth = newValue.w; |
| 61 | scope.style = function() { |
| 62 | return { |
| 63 | 'height' : (newValue.h - 100) + 'px', |
| 64 | 'width' : (newValue.w - 100) + 'px' |
| 65 | }; |
| 66 | }; |
| 67 | }, true); |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 68 | |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 69 | w.bind('resize', function() { scope.$apply(); }); |
| 70 | } |
| 71 | } |
| 72 | ]); |
| 73 | |
| 74 | app.run([ |
| 75 | '$rootScope', '$cookieStore', '$state', 'AuthenticationService', '$http', |
| 76 | '$templateCache', |
| 77 | function($rootScope, $cookieStore, $state, AuthenticationService, $http, |
| 78 | $templateCache) { |
| 79 | |
| 80 | if ($rootScope.globals == undefined) { |
| 81 | $rootScope.globals = {}; |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 82 | } |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 83 | |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 84 | // keep user logged in after page refresh |
| 85 | AuthenticationService.RestoreCredientials(); |
| 86 | |
| 87 | $rootScope.$on( |
| 88 | '$stateChangeStart', |
| 89 | function(event, toState, toParams, fromState, fromParams, options) { |
| 90 | // redirect to login page if not logged in |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 91 | // unless we're already trying to go to the login page (prevent a |
| 92 | // loop) |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 93 | if (!$rootScope.globals.currentUser && toState.name !== 'login') { |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 94 | // If logged out and transitioning to a logged in page: |
| 95 | event.preventDefault(); |
| 96 | $state.go('login'); |
| 97 | } |
| 98 | }); |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 99 | |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 100 | } |
| 101 | ]); |
| 102 | |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 103 | app.config([ |
| 104 | '$stateProvider', '$urlRouterProvider', |
| 105 | function($stateProvider, $urlRouterProvider) { |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 106 | |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 107 | $urlRouterProvider.otherwise('/systeminfo'); |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 108 | |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 109 | $stateProvider |
| 110 | .state('login', { |
| 111 | url : '/login', |
| 112 | templateUrl : 'static/partial-login.html', |
| 113 | controller : 'LoginController', |
| 114 | }) |
| 115 | .state('systeminfo', { |
| 116 | url : '/systeminfo', |
| 117 | templateUrl : 'static/partial-systeminfo.html' |
| 118 | }) |
| 119 | .state( |
| 120 | 'eventlog', |
| 121 | {url : '/eventlog', templateUrl : 'static/partial-eventlog.html'}) |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 122 | |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 123 | .state('kvm', {url : '/kvm', templateUrl : 'static/partial-kvm.html'}) |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 124 | |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 125 | .state('ipmi', |
| 126 | {url : '/ipmi', templateUrl : 'static/partial-ipmi.html'}) |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 127 | |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 128 | .state('sensor', |
| 129 | {url : '/sensor', templateUrl : 'static/partial-sensor.html'}) |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 130 | |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 131 | .state( |
| 132 | 'fwupdate', |
| 133 | {url : '/fwupdate', templateUrl : 'static/partial-fwupdate.html'}) |
| 134 | // nested list with custom controller |
| 135 | .state('fwupdate.confirm', { |
| 136 | url : '/confirm', |
| 137 | templateUrl : 'static/partial-fwupdateconfirm.html', |
Ed Tanous | 5fceeb4 | 2017-06-28 09:43:09 -0700 | [diff] [blame] | 138 | modal : true |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 139 | }) |
| 140 | // ABOUT PAGE AND MULTIPLE NAMED VIEWS ================================= |
| 141 | .state('about', |
| 142 | {url : '/about', templateUrl : 'static/partial-fruinfo.html'}) |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 143 | |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 144 | // nested list with custom controller |
| 145 | .state('about.list', { |
| 146 | url : '/list', |
| 147 | templateUrl : 'static/partial-home-list.html', |
| 148 | controller : function($scope) { |
| 149 | $scope.dogs = [ 'Bernese', 'Husky', 'Goldendoodle' ]; |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 150 | } |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 151 | }); |
| 152 | |
| 153 | } |
| 154 | ]); |
| 155 | |
| 156 | app.directive('fileread', [ function() { |
| 157 | return { |
| 158 | scope: {fileread : '='}, |
| 159 | link: function(scope, element, attributes) { |
| 160 | element.bind('change', function(changeEvent) { |
| 161 | scope.$apply(function() { |
| 162 | scope.fileread = changeEvent.target.files[0]; |
| 163 | // or all selected files: |
| 164 | // scope.fileread = changeEvent.target.files; |
| 165 | }); |
| 166 | }); |
| 167 | } |
| 168 | } |
| 169 | } ]); |
| 170 | |
| 171 | app.controller('PaginationDemoCtrl', [ |
| 172 | '$scope', '$log', |
| 173 | function($scope, $log) { |
| 174 | $scope.totalItems = 64; |
| 175 | $scope.currentPage = 4; |
| 176 | |
| 177 | $scope.setPage = function(pageNo) { $scope.currentPage = pageNo; }; |
| 178 | |
| 179 | $scope.pageChanged = function() { |
| 180 | $log.log('Page changed to: ' + $scope.currentPage); |
| 181 | }; |
| 182 | |
| 183 | $scope.maxSize = 5; |
| 184 | $scope.bigTotalItems = 175; |
| 185 | $scope.bigCurrentPage = 1; |
| 186 | } |
| 187 | ]); |
| 188 | |
| 189 | angular.module('Authentication').factory('AuthenticationService', [ |
| 190 | '$cookieStore', '$rootScope', '$timeout', '$log', '$http', |
| 191 | function($cookieStore, $rootScope, $timeout, $log, $http) { |
| 192 | var service = {}; |
| 193 | |
| 194 | service.Login = function(username, password) { |
| 195 | var user = {'username' : username, 'password' : password}; |
| 196 | return $http.post('/login', user); |
| 197 | }; |
| 198 | |
| 199 | service.SetCredentials = function(username, token) { |
| 200 | $rootScope.globals['currentUser'] = { |
| 201 | username : username, |
| 202 | authdata : token |
| 203 | }; |
| 204 | $http.defaults.headers.common['Authorization'] = 'Token ' + token; |
| 205 | $cookieStore.put('globals', $rootScope.globals); |
| 206 | }; |
| 207 | |
| 208 | service.ClearCredentials = function(reason) { |
| 209 | $rootScope.globals['currentUser'] = null; |
| 210 | if (reason !== null) { |
| 211 | service.logoutreason = reason; |
| 212 | } |
| 213 | $cookieStore.remove('globals'); |
| 214 | $http.defaults.headers.common['Authorization'] = ''; |
| 215 | }; |
| 216 | |
| 217 | service.RestoreCredientials = function() { |
| 218 | var globals = $cookieStore.get('globals') || {}; |
| 219 | if (globals.currentUser) { |
| 220 | service.SetCredentials(globals.currentUser.username, |
| 221 | globals.currentUser.authdata); |
| 222 | } |
| 223 | }; |
| 224 | |
| 225 | service.IsLoggedIn = function() { |
Ed Tanous | 5fceeb4 | 2017-06-28 09:43:09 -0700 | [diff] [blame] | 226 | if ($rootScope.globals['currentUser']) { |
Ed Tanous | 4758d5b | 2017-06-06 15:28:13 -0700 | [diff] [blame] | 227 | return true; |
| 228 | } else { |
| 229 | return false; |
| 230 | } |
| 231 | }; |
| 232 | |
| 233 | service.logoutreason = ''; |
| 234 | return service; |
| 235 | } |
| 236 | ]); |