blob: 64a480ac836f9eb59f32f4d949a22c43eecd5c19 [file] [log] [blame]
Ed Tanous904063f2017-03-02 16:48:24 -08001'use strict';
2angular.module('Authentication', []);
3var app = angular.module('bmcApp', [
Ed Tanousc4771fb2017-03-13 13:39:49 -07004 'Authentication',
5 'ngCookies',
6 'ui.bootstrap',
7 'ui.router',
8 'ngSanitize',
9 'ngWebSocket',
10 'ngResource'
Ed Tanous904063f2017-03-02 16:48:24 -080011]);
12
13
Ed Tanous9b65f1f2017-03-07 15:17:13 -080014app.controller('MainCtrl', function($scope) {
Ed Tanous904063f2017-03-02 16:48:24 -080015
16});
17
Ed Tanousc4771fb2017-03-13 13:39:49 -070018app.service('loginInterceptor', ["$injector",
19 function($injector) {
Ed Tanous9b65f1f2017-03-07 15:17:13 -080020 var service = this;
21
22 service.responseError = function(response) {
Ed Tanousc4771fb2017-03-13 13:39:49 -070023 var $state = $injector.get('$state');
24 var AuthenticationService = $injector.get('AuthenticationService');
Ed Tanous9b65f1f2017-03-07 15:17:13 -080025 if (response.status == 401){
26 console.log("Login required... ");
27
28 var invalidate_reason = "Your user was logged out.";
Ed Tanous9b65f1f2017-03-07 15:17:13 -080029
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 Tanous9b65f1f2017-03-07 15:17:13 -080034 } else {
35 $state.after_login_state = $state.current.name;
36 $state.go('login');
37 }
38 AuthenticationService.ClearCredentials(invalidate_reason);
39 }
Ed Tanousc4771fb2017-03-13 13:39:49 -070040
Ed Tanous9b65f1f2017-03-07 15:17:13 -080041 };
Ed Tanousc4771fb2017-03-13 13:39:49 -070042}])
Ed Tanous9b65f1f2017-03-07 15:17:13 -080043
44app.config(['$httpProvider', function ($httpProvider) {
45 $httpProvider.interceptors.push('loginInterceptor');
46}]);
47
Ed Tanousc4771fb2017-03-13 13:39:49 -070048app.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 Tanous9b65f1f2017-03-07 15:17:13 -080074app.run(['$rootScope', '$cookieStore', '$state', '$resource', 'AuthenticationService',
75 function($rootScope, $cookieStore, $state, $resource, AuthenticationService) {
Ed Tanous904063f2017-03-02 16:48:24 -080076 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 Tanousc4771fb2017-03-13 13:39:49 -070087 // unless we're already trying to go to the login page (prevent a loop)
88 if (!$rootScope.globals.currentUser && toState.name !== 'login') {
Ed Tanous904063f2017-03-02 16:48:24 -080089 // If logged out and transitioning to a logged in page:
90 event.preventDefault();
91 $state.go('login');
92 }
93 });
Ed Tanous904063f2017-03-02 16:48:24 -080094 }
95]);
96
Ed Tanousc4771fb2017-03-13 13:39:49 -070097app.config(['$stateProvider', '$urlRouterProvider',
98 function($stateProvider, $urlRouterProvider) {
Ed Tanous904063f2017-03-02 16:48:24 -080099
100 $urlRouterProvider.otherwise('/systeminfo');
101
102 $stateProvider
103 // nested list with just some random string data
104 .state('login', {
105 url: '/login',
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800106 templateUrl: 'static/login.html',
Ed Tanous904063f2017-03-02 16:48:24 -0800107 controller: 'LoginController',
108 })
109 // systeminfo view ========================================
110 .state(
111 'systeminfo',
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800112 {url: '/systeminfo', templateUrl: 'static/partial-systeminfo.html'})
Ed Tanous904063f2017-03-02 16:48:24 -0800113
114
115 // HOME STATES AND NESTED VIEWS ========================================
116 .state(
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800117 'eventlog', {url: '/eventlog', templateUrl: 'static/partial-eventlog.html'})
Ed Tanous904063f2017-03-02 16:48:24 -0800118
119
120 .state(
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800121 'kvm', {url: '/kvm', templateUrl: 'static/partial-kvm.html'})
Ed Tanous904063f2017-03-02 16:48:24 -0800122
123 // ABOUT PAGE AND MULTIPLE NAMED VIEWS =================================
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800124 .state('about', {url: '/about', templateUrl: 'static/partial-fruinfo.html'})
Ed Tanous904063f2017-03-02 16:48:24 -0800125
126 // nested list with custom controller
127 .state('about.list', {
128 url: '/list',
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800129 templateUrl: 'static/partial-home-list.html',
Ed Tanous904063f2017-03-02 16:48:24 -0800130 controller: function($scope) {
131 $scope.dogs = ['Bernese', 'Husky', 'Goldendoodle'];
132 }
133 })
134
135
Ed Tanousc4771fb2017-03-13 13:39:49 -0700136}]);
Ed Tanous904063f2017-03-02 16:48:24 -0800137
Ed Tanousc4771fb2017-03-13 13:39:49 -0700138app.controller('PaginationDemoCtrl', ['$scope', '$log', function($scope, $log) {
Ed Tanous904063f2017-03-02 16:48:24 -0800139 $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 Tanousc4771fb2017-03-13 13:39:49 -0700151}]);
Ed Tanous904063f2017-03-02 16:48:24 -0800152
Ed Tanousc4771fb2017-03-13 13:39:49 -0700153angular.module('Authentication').factory(
Ed Tanous904063f2017-03-02 16:48:24 -0800154 'AuthenticationService',
Ed Tanousc4771fb2017-03-13 13:39:49 -0700155 ['$cookieStore', '$rootScope', '$timeout', '$resource', '$log', '$http',
156 function($cookieStore, $rootScope, $timeout, $resource, $log, $http) {
Ed Tanous904063f2017-03-02 16:48:24 -0800157 var service = {};
158
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800159 service.Login = function(username, password, success_callback, fail_callback) {
Ed Tanous904063f2017-03-02 16:48:24 -0800160
161 var user = {"username": username, "password": password};
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800162 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 Tanous904063f2017-03-02 16:48:24 -0800167 };
168
169 service.SetCredentials = function(username, token) {
170 $rootScope.globals["currentUser"] = {username: username, authdata: token};
Ed Tanousc4771fb2017-03-13 13:39:49 -0700171 $http.defaults.headers.common['Authorization'] = 'Token ' + token;
Ed Tanous904063f2017-03-02 16:48:24 -0800172 $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 Tanousc4771fb2017-03-13 13:39:49 -0700181 $http.defaults.headers.common['Authorization'] = '';
Ed Tanous904063f2017-03-02 16:48:24 -0800182 };
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 Tanous904063f2017-03-02 16:48:24 -0800197 .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 });