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