blob: d4282ba0d63d697089faf8e12ed683800a8beb66 [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 Tanous1ccd57c2017-03-21 13:15:58 -070014app.controller('MainCtrl', ['$scope', function($scope) {
Ed Tanous904063f2017-03-02 16:48:24 -080015
Ed Tanous1ccd57c2017-03-21 13:15:58 -070016}]);
Ed Tanous904063f2017-03-02 16:48:24 -080017
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 Tanous1ccd57c2017-03-21 13:15:58 -070040 return response;
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 Tanous1ccd57c2017-03-21 13:15:58 -070048app.directive('windowSize', ['$window', function ($window) {
Ed Tanousc4771fb2017-03-13 13:39:49 -070049 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 }
Ed Tanous1ccd57c2017-03-21 13:15:58 -070072}]);
Ed Tanousc4771fb2017-03-13 13:39:49 -070073
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 Tanous1ccd57c2017-03-21 13:15:58 -0700106 templateUrl: 'static/partial-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
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700123 .state(
124 'ipmi', {url: '/ipmi', templateUrl: 'static/partial-ipmi.html'})
125
Ed Tanous904063f2017-03-02 16:48:24 -0800126 // ABOUT PAGE AND MULTIPLE NAMED VIEWS =================================
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800127 .state('about', {url: '/about', templateUrl: 'static/partial-fruinfo.html'})
Ed Tanous904063f2017-03-02 16:48:24 -0800128
129 // nested list with custom controller
130 .state('about.list', {
131 url: '/list',
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800132 templateUrl: 'static/partial-home-list.html',
Ed Tanous904063f2017-03-02 16:48:24 -0800133 controller: function($scope) {
134 $scope.dogs = ['Bernese', 'Husky', 'Goldendoodle'];
135 }
136 })
137
138
Ed Tanousc4771fb2017-03-13 13:39:49 -0700139}]);
Ed Tanous904063f2017-03-02 16:48:24 -0800140
Ed Tanousc4771fb2017-03-13 13:39:49 -0700141app.controller('PaginationDemoCtrl', ['$scope', '$log', function($scope, $log) {
Ed Tanous904063f2017-03-02 16:48:24 -0800142 $scope.totalItems = 64;
143 $scope.currentPage = 4;
144
145 $scope.setPage = function(pageNo) { $scope.currentPage = pageNo; };
146
147 $scope.pageChanged = function() {
148 $log.log('Page changed to: ' + $scope.currentPage);
149 };
150
151 $scope.maxSize = 5;
152 $scope.bigTotalItems = 175;
153 $scope.bigCurrentPage = 1;
Ed Tanousc4771fb2017-03-13 13:39:49 -0700154}]);
Ed Tanous904063f2017-03-02 16:48:24 -0800155
Ed Tanousc4771fb2017-03-13 13:39:49 -0700156angular.module('Authentication').factory(
Ed Tanous904063f2017-03-02 16:48:24 -0800157 'AuthenticationService',
Ed Tanousc4771fb2017-03-13 13:39:49 -0700158 ['$cookieStore', '$rootScope', '$timeout', '$resource', '$log', '$http',
159 function($cookieStore, $rootScope, $timeout, $resource, $log, $http) {
Ed Tanous904063f2017-03-02 16:48:24 -0800160 var service = {};
161
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800162 service.Login = function(username, password, success_callback, fail_callback) {
Ed Tanous904063f2017-03-02 16:48:24 -0800163
164 var user = {"username": username, "password": password};
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800165 var UserLogin = $resource("/login");
166 var this_login = new UserLogin();
167 this_login.data = {"username": username, "password": password};
168 UserLogin.save(user, success_callback, fail_callback);
169
Ed Tanous904063f2017-03-02 16:48:24 -0800170 };
171
172 service.SetCredentials = function(username, token) {
173 $rootScope.globals["currentUser"] = {username: username, authdata: token};
Ed Tanousc4771fb2017-03-13 13:39:49 -0700174 $http.defaults.headers.common['Authorization'] = 'Token ' + token;
Ed Tanous904063f2017-03-02 16:48:24 -0800175 $cookieStore.put('globals', $rootScope.globals);
176 };
177
178 service.ClearCredentials = function(reason) {
179 $rootScope.globals["currentUser"] = null;
180 if (reason !== null) {
181 service.logoutreason = reason;
182 }
183 $cookieStore.remove('globals');
Ed Tanousc4771fb2017-03-13 13:39:49 -0700184 $http.defaults.headers.common['Authorization'] = '';
Ed Tanous904063f2017-03-02 16:48:24 -0800185 };
186
187 service.RestoreCredientials = function() {
188 var globals = $cookieStore.get('globals') || {};
189 if (globals.currentUser) {
190 service.SetCredentials(
191 globals.currentUser.username, globals.currentUser.authdata);
192 }
193 };
194
195 service.logoutreason = "";
196 return service;
197 }
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700198 ]);