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 | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame^] | 4 | 'Authentication', |
| 5 | 'ngCookies', |
| 6 | 'ui.bootstrap', |
| 7 | 'ui.router', |
| 8 | 'ngSanitize', |
| 9 | 'ngWebSocket', |
| 10 | 'ngResource' |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 11 | ]); |
| 12 | |
| 13 | |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 14 | app.controller('MainCtrl', function($scope) { |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 15 | |
| 16 | }); |
| 17 | |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame^] | 18 | app.service('loginInterceptor', ["$injector", |
| 19 | function($injector) { |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 20 | var service = this; |
| 21 | |
| 22 | service.responseError = function(response) { |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame^] | 23 | var $state = $injector.get('$state'); |
| 24 | var AuthenticationService = $injector.get('AuthenticationService'); |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 25 | if (response.status == 401){ |
| 26 | console.log("Login required... "); |
| 27 | |
| 28 | var invalidate_reason = "Your user was logged out."; |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 29 | |
| 30 | // if we're attempting to log in, we need to |
| 31 | // continue the promise chain to make sure the user is informed |
| 32 | if ($state.current.name === "login") { |
| 33 | invalidate_reason = "Your username and password was incorrect"; |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 34 | } else { |
| 35 | $state.after_login_state = $state.current.name; |
| 36 | $state.go('login'); |
| 37 | } |
| 38 | AuthenticationService.ClearCredentials(invalidate_reason); |
| 39 | } |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame^] | 40 | |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 41 | }; |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame^] | 42 | }]) |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 43 | |
| 44 | app.config(['$httpProvider', function ($httpProvider) { |
| 45 | $httpProvider.interceptors.push('loginInterceptor'); |
| 46 | }]); |
| 47 | |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame^] | 48 | app.directive('windowSize', function ($window) { |
| 49 | return function (scope, element) { |
| 50 | var w = angular.element($window); |
| 51 | scope.getWindowDimensions = function () { |
| 52 | return { |
| 53 | 'h': w.height(), |
| 54 | 'w': w.width() |
| 55 | }; |
| 56 | }; |
| 57 | scope.$watch(scope.getWindowDimensions, function (newValue, oldValue) { |
| 58 | scope.windowHeight = newValue.h; |
| 59 | scope.windowWidth = newValue.w; |
| 60 | scope.style = function () { |
| 61 | return { |
| 62 | 'height': (newValue.h - 100) + 'px', |
| 63 | 'width': (newValue.w - 100) + 'px' |
| 64 | }; |
| 65 | }; |
| 66 | }, true); |
| 67 | |
| 68 | w.bind('resize', function () { |
| 69 | scope.$apply(); |
| 70 | }); |
| 71 | } |
| 72 | }); |
| 73 | |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 74 | app.run(['$rootScope', '$cookieStore', '$state', '$resource', 'AuthenticationService', |
| 75 | function($rootScope, $cookieStore, $state, $resource, AuthenticationService) { |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 76 | if ($rootScope.globals == undefined){ |
| 77 | $rootScope.globals = {}; |
| 78 | } |
| 79 | |
| 80 | // keep user logged in after page refresh |
| 81 | AuthenticationService.RestoreCredientials(); |
| 82 | |
| 83 | $rootScope.$on( |
| 84 | '$stateChangeStart', |
| 85 | function(event, toState, toParams, fromState, fromParams, options) { |
| 86 | // redirect to login page if not logged in |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame^] | 87 | // unless we're already trying to go to the login page (prevent a loop) |
| 88 | if (!$rootScope.globals.currentUser && toState.name !== 'login') { |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 89 | // If logged out and transitioning to a logged in page: |
| 90 | event.preventDefault(); |
| 91 | $state.go('login'); |
| 92 | } |
| 93 | }); |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 94 | } |
| 95 | ]); |
| 96 | |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame^] | 97 | app.config(['$stateProvider', '$urlRouterProvider', |
| 98 | function($stateProvider, $urlRouterProvider) { |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 99 | |
| 100 | $urlRouterProvider.otherwise('/systeminfo'); |
| 101 | |
| 102 | $stateProvider |
| 103 | // nested list with just some random string data |
| 104 | .state('login', { |
| 105 | url: '/login', |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 106 | templateUrl: 'static/login.html', |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 107 | controller: 'LoginController', |
| 108 | }) |
| 109 | // systeminfo view ======================================== |
| 110 | .state( |
| 111 | 'systeminfo', |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 112 | {url: '/systeminfo', templateUrl: 'static/partial-systeminfo.html'}) |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 113 | |
| 114 | |
| 115 | // HOME STATES AND NESTED VIEWS ======================================== |
| 116 | .state( |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 117 | 'eventlog', {url: '/eventlog', templateUrl: 'static/partial-eventlog.html'}) |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 118 | |
| 119 | |
| 120 | .state( |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 121 | 'kvm', {url: '/kvm', templateUrl: 'static/partial-kvm.html'}) |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 122 | |
| 123 | // ABOUT PAGE AND MULTIPLE NAMED VIEWS ================================= |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 124 | .state('about', {url: '/about', templateUrl: 'static/partial-fruinfo.html'}) |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 125 | |
| 126 | // nested list with custom controller |
| 127 | .state('about.list', { |
| 128 | url: '/list', |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 129 | templateUrl: 'static/partial-home-list.html', |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 130 | controller: function($scope) { |
| 131 | $scope.dogs = ['Bernese', 'Husky', 'Goldendoodle']; |
| 132 | } |
| 133 | }) |
| 134 | |
| 135 | |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame^] | 136 | }]); |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 137 | |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame^] | 138 | app.controller('PaginationDemoCtrl', ['$scope', '$log', function($scope, $log) { |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 139 | $scope.totalItems = 64; |
| 140 | $scope.currentPage = 4; |
| 141 | |
| 142 | $scope.setPage = function(pageNo) { $scope.currentPage = pageNo; }; |
| 143 | |
| 144 | $scope.pageChanged = function() { |
| 145 | $log.log('Page changed to: ' + $scope.currentPage); |
| 146 | }; |
| 147 | |
| 148 | $scope.maxSize = 5; |
| 149 | $scope.bigTotalItems = 175; |
| 150 | $scope.bigCurrentPage = 1; |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame^] | 151 | }]); |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 152 | |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame^] | 153 | angular.module('Authentication').factory( |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 154 | 'AuthenticationService', |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame^] | 155 | ['$cookieStore', '$rootScope', '$timeout', '$resource', '$log', '$http', |
| 156 | function($cookieStore, $rootScope, $timeout, $resource, $log, $http) { |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 157 | var service = {}; |
| 158 | |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 159 | service.Login = function(username, password, success_callback, fail_callback) { |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 160 | |
| 161 | var user = {"username": username, "password": password}; |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 162 | var UserLogin = $resource("/login"); |
| 163 | var this_login = new UserLogin(); |
| 164 | this_login.data = {"username": username, "password": password}; |
| 165 | UserLogin.save(user, success_callback, fail_callback); |
| 166 | |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | service.SetCredentials = function(username, token) { |
| 170 | $rootScope.globals["currentUser"] = {username: username, authdata: token}; |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame^] | 171 | $http.defaults.headers.common['Authorization'] = 'Token ' + token; |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 172 | $cookieStore.put('globals', $rootScope.globals); |
| 173 | }; |
| 174 | |
| 175 | service.ClearCredentials = function(reason) { |
| 176 | $rootScope.globals["currentUser"] = null; |
| 177 | if (reason !== null) { |
| 178 | service.logoutreason = reason; |
| 179 | } |
| 180 | $cookieStore.remove('globals'); |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame^] | 181 | $http.defaults.headers.common['Authorization'] = ''; |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 182 | }; |
| 183 | |
| 184 | service.RestoreCredientials = function() { |
| 185 | var globals = $cookieStore.get('globals') || {}; |
| 186 | if (globals.currentUser) { |
| 187 | service.SetCredentials( |
| 188 | globals.currentUser.username, globals.currentUser.authdata); |
| 189 | } |
| 190 | }; |
| 191 | |
| 192 | service.logoutreason = ""; |
| 193 | return service; |
| 194 | } |
| 195 | ]) |
| 196 | |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 197 | .factory('Base64', function() { |
| 198 | /* jshint ignore:start */ |
| 199 | |
| 200 | var keyStr = |
| 201 | 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; |
| 202 | |
| 203 | return { |
| 204 | encode: function(input) { |
| 205 | var output = ""; |
| 206 | var chr1, chr2, chr3 = ""; |
| 207 | var enc1, enc2, enc3, enc4 = ""; |
| 208 | var i = 0; |
| 209 | |
| 210 | do { |
| 211 | chr1 = input.charCodeAt(i++); |
| 212 | chr2 = input.charCodeAt(i++); |
| 213 | chr3 = input.charCodeAt(i++); |
| 214 | |
| 215 | enc1 = chr1 >> 2; |
| 216 | enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); |
| 217 | enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); |
| 218 | enc4 = chr3 & 63; |
| 219 | |
| 220 | if (isNaN(chr2)) { |
| 221 | enc3 = enc4 = 64; |
| 222 | } else if (isNaN(chr3)) { |
| 223 | enc4 = 64; |
| 224 | } |
| 225 | |
| 226 | output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + |
| 227 | keyStr.charAt(enc3) + keyStr.charAt(enc4); |
| 228 | chr1 = chr2 = chr3 = ""; |
| 229 | enc1 = enc2 = enc3 = enc4 = ""; |
| 230 | } while (i < input.length); |
| 231 | |
| 232 | return output; |
| 233 | }, |
| 234 | |
| 235 | decode: function(input) { |
| 236 | var output = ""; |
| 237 | var chr1, chr2, chr3 = ""; |
| 238 | var enc1, enc2, enc3, enc4 = ""; |
| 239 | var i = 0; |
| 240 | |
| 241 | // remove all characters that are not A-Z, a-z, 0-9, +, /, or = |
| 242 | var base64test = /[^A-Za-z0-9\+\/\=]/g; |
| 243 | if (base64test.exec(input)) { |
| 244 | window.alert( |
| 245 | "There were invalid base64 characters in the input text.\n" + |
| 246 | "Valid base64 characters are A-Z, a-z, 0-9, '+', '/',and '='\n" + |
| 247 | "Expect errors in decoding."); |
| 248 | } |
| 249 | input = input.replace(/[^A-Za-z0-9\+\/\=]/g, ""); |
| 250 | |
| 251 | do { |
| 252 | enc1 = keyStr.indexOf(input.charAt(i++)); |
| 253 | enc2 = keyStr.indexOf(input.charAt(i++)); |
| 254 | enc3 = keyStr.indexOf(input.charAt(i++)); |
| 255 | enc4 = keyStr.indexOf(input.charAt(i++)); |
| 256 | |
| 257 | chr1 = (enc1 << 2) | (enc2 >> 4); |
| 258 | chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); |
| 259 | chr3 = ((enc3 & 3) << 6) | enc4; |
| 260 | |
| 261 | output = output + String.fromCharCode(chr1); |
| 262 | |
| 263 | if (enc3 != 64) { |
| 264 | output = output + String.fromCharCode(chr2); |
| 265 | } |
| 266 | if (enc4 != 64) { |
| 267 | output = output + String.fromCharCode(chr3); |
| 268 | } |
| 269 | |
| 270 | chr1 = chr2 = chr3 = ""; |
| 271 | enc1 = enc2 = enc3 = enc4 = ""; |
| 272 | |
| 273 | } while (i < input.length); |
| 274 | |
| 275 | return output; |
| 276 | } |
| 277 | }; |
| 278 | |
| 279 | /* jshint ignore:end */ |
| 280 | }); |