blob: 1812261882db6297e50be9906b80e3d05b63cd56 [file] [log] [blame]
Ed Tanous904063f2017-03-02 16:48:24 -08001'use strict';
2angular.module('Authentication', []);
3var app = angular.module('bmcApp', [
Ed Tanous5fceeb42017-06-28 09:43:09 -07004 'ngCookies',
5 'ngAnimate',
6 'ngSanitize',
7 'ui.bootstrap',
8 'ui.router',
9 'ngWebSocket',
10 'Authentication',
11 'ui.router.modal',
12 'smart-table',
Ed Tanous904063f2017-03-02 16:48:24 -080013]);
14
Ed Tanous4758d5b2017-06-06 15:28:13 -070015app.service('loginInterceptor', [
16 '$injector',
Ed Tanousc4771fb2017-03-13 13:39:49 -070017 function($injector) {
Ed Tanous9b65f1f2017-03-07 15:17:13 -080018 var service = this;
19
20 service.responseError = function(response) {
Ed Tanous4758d5b2017-06-06 15:28:13 -070021 var $state = $injector.get('$state');
22 var AuthenticationService = $injector.get('AuthenticationService');
23 if (response.status == 401) {
24 console.log('Login required... ');
Ed Tanous9b65f1f2017-03-07 15:17:13 -080025
Ed Tanous4758d5b2017-06-06 15:28:13 -070026 var invalidate_reason = 'Your user was logged out.';
Ed Tanous9b65f1f2017-03-07 15:17:13 -080027
Ed Tanous4758d5b2017-06-06 15:28:13 -070028 // 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 Tanous9b65f1f2017-03-07 15:17:13 -080035 }
Ed Tanous4758d5b2017-06-06 15:28:13 -070036 AuthenticationService.ClearCredentials(invalidate_reason);
37 }
38 return response;
Ed Tanous9b65f1f2017-03-07 15:17:13 -080039 };
Ed Tanousc4771fb2017-03-13 13:39:49 -070040 }
Ed Tanous4758d5b2017-06-06 15:28:13 -070041])
Ed Tanousc4771fb2017-03-13 13:39:49 -070042
Ed Tanous4758d5b2017-06-06 15:28:13 -070043app.config([
44 '$httpProvider',
45 function($httpProvider) {
46 $httpProvider.interceptors.push('loginInterceptor');
47 }
48]);
Ed Tanous9140a672017-04-24 17:01:32 -070049
Ed Tanous4758d5b2017-06-06 15:28:13 -070050app.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 Tanous9140a672017-04-24 17:01:32 -070068
Ed Tanous4758d5b2017-06-06 15:28:13 -070069 w.bind('resize', function() { scope.$apply(); });
70 }
71 }
72]);
73
74app.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 Tanous904063f2017-03-02 16:48:24 -080082 }
Ed Tanous9140a672017-04-24 17:01:32 -070083
Ed Tanous904063f2017-03-02 16:48:24 -080084 // 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 Tanous4758d5b2017-06-06 15:28:13 -070091 // unless we're already trying to go to the login page (prevent a
92 // loop)
Ed Tanousc4771fb2017-03-13 13:39:49 -070093 if (!$rootScope.globals.currentUser && toState.name !== 'login') {
Ed Tanous904063f2017-03-02 16:48:24 -080094 // If logged out and transitioning to a logged in page:
95 event.preventDefault();
96 $state.go('login');
97 }
98 });
Ed Tanous9140a672017-04-24 17:01:32 -070099
Ed Tanous904063f2017-03-02 16:48:24 -0800100 }
101]);
102
Ed Tanous4758d5b2017-06-06 15:28:13 -0700103app.config([
104 '$stateProvider', '$urlRouterProvider',
105 function($stateProvider, $urlRouterProvider) {
Ed Tanous904063f2017-03-02 16:48:24 -0800106
Ed Tanous4758d5b2017-06-06 15:28:13 -0700107 $urlRouterProvider.otherwise('/systeminfo');
Ed Tanous904063f2017-03-02 16:48:24 -0800108
Ed Tanous4758d5b2017-06-06 15:28:13 -0700109 $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 Tanous904063f2017-03-02 16:48:24 -0800122
Ed Tanous4758d5b2017-06-06 15:28:13 -0700123 .state('kvm', {url : '/kvm', templateUrl : 'static/partial-kvm.html'})
Ed Tanous904063f2017-03-02 16:48:24 -0800124
Ed Tanous4758d5b2017-06-06 15:28:13 -0700125 .state('ipmi',
126 {url : '/ipmi', templateUrl : 'static/partial-ipmi.html'})
Ed Tanous904063f2017-03-02 16:48:24 -0800127
Ed Tanous4758d5b2017-06-06 15:28:13 -0700128 .state('sensor',
129 {url : '/sensor', templateUrl : 'static/partial-sensor.html'})
Ed Tanous904063f2017-03-02 16:48:24 -0800130
Ed Tanous4758d5b2017-06-06 15:28:13 -0700131 .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 Tanous5fceeb42017-06-28 09:43:09 -0700138 modal : true
Ed Tanous4758d5b2017-06-06 15:28:13 -0700139 })
140 // ABOUT PAGE AND MULTIPLE NAMED VIEWS =================================
141 .state('about',
142 {url : '/about', templateUrl : 'static/partial-fruinfo.html'})
Ed Tanous904063f2017-03-02 16:48:24 -0800143
Ed Tanous4758d5b2017-06-06 15:28:13 -0700144 // 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 Tanous904063f2017-03-02 16:48:24 -0800150 }
Ed Tanous4758d5b2017-06-06 15:28:13 -0700151 });
152
153 }
154]);
155
156app.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
171app.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
189angular.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 Tanous5fceeb42017-06-28 09:43:09 -0700226 if ($rootScope.globals['currentUser']) {
Ed Tanous4758d5b2017-06-06 15:28:13 -0700227 return true;
228 } else {
229 return false;
230 }
231 };
232
233 service.logoutreason = '';
234 return service;
235 }
236]);