blob: 55e4a425e20b05360d5b45f97130625479bbc993 [file] [log] [blame]
Ed Tanous904063f2017-03-02 16:48:24 -08001'use strict';
2angular.module('Authentication', []);
3var app = angular.module('bmcApp', [
Ed Tanous4758d5b2017-06-06 15:28:13 -07004 'ngCookies', 'ngAnimate', 'ngSanitize', 'ui.bootstrap',
5 'ui.router', 'ngWebSocket', 'Authentication', 'ui.router.modal',
Ed Tanous904063f2017-03-02 16:48:24 -08006]);
7
Ed Tanous4758d5b2017-06-06 15:28:13 -07008app.service('loginInterceptor', [
9 '$injector',
Ed Tanousc4771fb2017-03-13 13:39:49 -070010 function($injector) {
Ed Tanous9b65f1f2017-03-07 15:17:13 -080011 var service = this;
12
13 service.responseError = function(response) {
Ed Tanous4758d5b2017-06-06 15:28:13 -070014 var $state = $injector.get('$state');
15 var AuthenticationService = $injector.get('AuthenticationService');
16 if (response.status == 401) {
17 console.log('Login required... ');
Ed Tanous9b65f1f2017-03-07 15:17:13 -080018
Ed Tanous4758d5b2017-06-06 15:28:13 -070019 var invalidate_reason = 'Your user was logged out.';
Ed Tanous9b65f1f2017-03-07 15:17:13 -080020
Ed Tanous4758d5b2017-06-06 15:28:13 -070021 // 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 Tanous9b65f1f2017-03-07 15:17:13 -080028 }
Ed Tanous4758d5b2017-06-06 15:28:13 -070029 AuthenticationService.ClearCredentials(invalidate_reason);
30 }
31 return response;
Ed Tanous9b65f1f2017-03-07 15:17:13 -080032 };
Ed Tanousc4771fb2017-03-13 13:39:49 -070033 }
Ed Tanous4758d5b2017-06-06 15:28:13 -070034])
Ed Tanousc4771fb2017-03-13 13:39:49 -070035
Ed Tanous4758d5b2017-06-06 15:28:13 -070036app.config([
37 '$httpProvider',
38 function($httpProvider) {
39 $httpProvider.interceptors.push('loginInterceptor');
40 }
41]);
Ed Tanous9140a672017-04-24 17:01:32 -070042
Ed Tanous4758d5b2017-06-06 15:28:13 -070043app.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 Tanous9140a672017-04-24 17:01:32 -070061
Ed Tanous4758d5b2017-06-06 15:28:13 -070062 w.bind('resize', function() { scope.$apply(); });
63 }
64 }
65]);
66
67app.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 Tanous904063f2017-03-02 16:48:24 -080075 }
Ed Tanous9140a672017-04-24 17:01:32 -070076
Ed Tanous904063f2017-03-02 16:48:24 -080077 // 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 Tanous4758d5b2017-06-06 15:28:13 -070084 // unless we're already trying to go to the login page (prevent a
85 // loop)
Ed Tanousc4771fb2017-03-13 13:39:49 -070086 if (!$rootScope.globals.currentUser && toState.name !== 'login') {
Ed Tanous904063f2017-03-02 16:48:24 -080087 // If logged out and transitioning to a logged in page:
88 event.preventDefault();
89 $state.go('login');
90 }
91 });
Ed Tanous9140a672017-04-24 17:01:32 -070092
Ed Tanous904063f2017-03-02 16:48:24 -080093 }
94]);
95
Ed Tanous4758d5b2017-06-06 15:28:13 -070096app.config([
97 '$stateProvider', '$urlRouterProvider',
98 function($stateProvider, $urlRouterProvider) {
Ed Tanous904063f2017-03-02 16:48:24 -080099
Ed Tanous4758d5b2017-06-06 15:28:13 -0700100 $urlRouterProvider.otherwise('/systeminfo');
Ed Tanous904063f2017-03-02 16:48:24 -0800101
Ed Tanous4758d5b2017-06-06 15:28:13 -0700102 $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 Tanous904063f2017-03-02 16:48:24 -0800115
Ed Tanous4758d5b2017-06-06 15:28:13 -0700116 .state('kvm', {url : '/kvm', templateUrl : 'static/partial-kvm.html'})
Ed Tanous904063f2017-03-02 16:48:24 -0800117
Ed Tanous4758d5b2017-06-06 15:28:13 -0700118 .state('ipmi',
119 {url : '/ipmi', templateUrl : 'static/partial-ipmi.html'})
Ed Tanous904063f2017-03-02 16:48:24 -0800120
Ed Tanous4758d5b2017-06-06 15:28:13 -0700121 .state('sensor',
122 {url : '/sensor', templateUrl : 'static/partial-sensor.html'})
Ed Tanous904063f2017-03-02 16:48:24 -0800123
Ed Tanous4758d5b2017-06-06 15:28:13 -0700124 .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 Tanous904063f2017-03-02 16:48:24 -0800136
Ed Tanous4758d5b2017-06-06 15:28:13 -0700137 // 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 Tanous904063f2017-03-02 16:48:24 -0800143 }
Ed Tanous4758d5b2017-06-06 15:28:13 -0700144 });
145
146 }
147]);
148
149app.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
164app.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
182angular.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]);