blob: 2678ac0a567459fbf769fdfff643a42bb4fa3bc5 [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
Ed Tanous9140a672017-04-24 17:01:32 -070013
14
Ed Tanous1ccd57c2017-03-21 13:15:58 -070015app.controller('MainCtrl', ['$scope', function($scope) {
Ed Tanous904063f2017-03-02 16:48:24 -080016
Ed Tanous1ccd57c2017-03-21 13:15:58 -070017}]);
Ed Tanous904063f2017-03-02 16:48:24 -080018
Ed Tanousc4771fb2017-03-13 13:39:49 -070019app.service('loginInterceptor', ["$injector",
20 function($injector) {
Ed Tanous9b65f1f2017-03-07 15:17:13 -080021 var service = this;
22
23 service.responseError = function(response) {
Ed Tanousc4771fb2017-03-13 13:39:49 -070024 var $state = $injector.get('$state');
25 var AuthenticationService = $injector.get('AuthenticationService');
Ed Tanous9b65f1f2017-03-07 15:17:13 -080026 if (response.status == 401){
27 console.log("Login required... ");
28
29 var invalidate_reason = "Your user was logged out.";
Ed Tanous9b65f1f2017-03-07 15:17:13 -080030
31 // if we're attempting to log in, we need to
32 // continue the promise chain to make sure the user is informed
33 if ($state.current.name === "login") {
34 invalidate_reason = "Your username and password was incorrect";
Ed Tanous9b65f1f2017-03-07 15:17:13 -080035 } else {
36 $state.after_login_state = $state.current.name;
37 $state.go('login');
38 }
39 AuthenticationService.ClearCredentials(invalidate_reason);
40 }
Ed Tanous1ccd57c2017-03-21 13:15:58 -070041 return response;
Ed Tanous9b65f1f2017-03-07 15:17:13 -080042 };
Ed Tanousc4771fb2017-03-13 13:39:49 -070043}])
Ed Tanous9b65f1f2017-03-07 15:17:13 -080044
45app.config(['$httpProvider', function ($httpProvider) {
46 $httpProvider.interceptors.push('loginInterceptor');
47}]);
48
Ed Tanous1ccd57c2017-03-21 13:15:58 -070049app.directive('windowSize', ['$window', function ($window) {
Ed Tanousc4771fb2017-03-13 13:39:49 -070050 return function (scope, element) {
51 var w = angular.element($window);
52 scope.getWindowDimensions = function () {
53 return {
54 'h': w.height(),
55 'w': w.width()
56 };
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);
68
69 w.bind('resize', function () {
70 scope.$apply();
71 });
72 }
Ed Tanous1ccd57c2017-03-21 13:15:58 -070073}]);
Ed Tanousc4771fb2017-03-13 13:39:49 -070074
Ed Tanousb4d29f42017-03-24 16:39:25 -070075app.run(['$rootScope', '$cookieStore', '$state', '$resource', 'AuthenticationService', '$http', '$templateCache',
76 function($rootScope, $cookieStore, $state, $resource, AuthenticationService, $http, $templateCache) {
Ed Tanous9140a672017-04-24 17:01:32 -070077
78 $http.get('static/partial-login.html', {cache:$templateCache});
79 $http.get('static/partial-kvm.html', {cache: $templateCache});
80
Ed Tanous904063f2017-03-02 16:48:24 -080081 if ($rootScope.globals == undefined){
82 $rootScope.globals = {};
83 }
Ed Tanous9140a672017-04-24 17:01:32 -070084
Ed Tanous904063f2017-03-02 16:48:24 -080085
86 // keep user logged in after page refresh
87 AuthenticationService.RestoreCredientials();
88
89 $rootScope.$on(
90 '$stateChangeStart',
91 function(event, toState, toParams, fromState, fromParams, options) {
92 // redirect to login page if not logged in
Ed Tanousc4771fb2017-03-13 13:39:49 -070093 // unless we're already trying to go to the login page (prevent a loop)
94 if (!$rootScope.globals.currentUser && toState.name !== 'login') {
Ed Tanous904063f2017-03-02 16:48:24 -080095 // If logged out and transitioning to a logged in page:
96 event.preventDefault();
97 $state.go('login');
98 }
99 });
Ed Tanous9140a672017-04-24 17:01:32 -0700100
Ed Tanous904063f2017-03-02 16:48:24 -0800101 }
102]);
103
Ed Tanousc4771fb2017-03-13 13:39:49 -0700104app.config(['$stateProvider', '$urlRouterProvider',
105 function($stateProvider, $urlRouterProvider) {
Ed Tanous904063f2017-03-02 16:48:24 -0800106
107 $urlRouterProvider.otherwise('/systeminfo');
108
109 $stateProvider
110 // nested list with just some random string data
111 .state('login', {
112 url: '/login',
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700113 templateUrl: 'static/partial-login.html',
Ed Tanous904063f2017-03-02 16:48:24 -0800114 controller: 'LoginController',
115 })
116 // systeminfo view ========================================
117 .state(
118 'systeminfo',
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800119 {url: '/systeminfo', templateUrl: 'static/partial-systeminfo.html'})
Ed Tanous904063f2017-03-02 16:48:24 -0800120
121
122 // HOME STATES AND NESTED VIEWS ========================================
123 .state(
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800124 'eventlog', {url: '/eventlog', templateUrl: 'static/partial-eventlog.html'})
Ed Tanous904063f2017-03-02 16:48:24 -0800125
126
127 .state(
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800128 'kvm', {url: '/kvm', templateUrl: 'static/partial-kvm.html'})
Ed Tanous904063f2017-03-02 16:48:24 -0800129
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700130 .state(
131 'ipmi', {url: '/ipmi', templateUrl: 'static/partial-ipmi.html'})
132
Ed Tanous904063f2017-03-02 16:48:24 -0800133 // ABOUT PAGE AND MULTIPLE NAMED VIEWS =================================
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800134 .state('about', {url: '/about', templateUrl: 'static/partial-fruinfo.html'})
Ed Tanous904063f2017-03-02 16:48:24 -0800135
136 // nested list with custom controller
137 .state('about.list', {
138 url: '/list',
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800139 templateUrl: 'static/partial-home-list.html',
Ed Tanous904063f2017-03-02 16:48:24 -0800140 controller: function($scope) {
141 $scope.dogs = ['Bernese', 'Husky', 'Goldendoodle'];
142 }
143 })
144
145
Ed Tanousc4771fb2017-03-13 13:39:49 -0700146}]);
Ed Tanous904063f2017-03-02 16:48:24 -0800147
Ed Tanousc4771fb2017-03-13 13:39:49 -0700148app.controller('PaginationDemoCtrl', ['$scope', '$log', function($scope, $log) {
Ed Tanous904063f2017-03-02 16:48:24 -0800149 $scope.totalItems = 64;
150 $scope.currentPage = 4;
151
152 $scope.setPage = function(pageNo) { $scope.currentPage = pageNo; };
153
154 $scope.pageChanged = function() {
155 $log.log('Page changed to: ' + $scope.currentPage);
156 };
157
158 $scope.maxSize = 5;
159 $scope.bigTotalItems = 175;
160 $scope.bigCurrentPage = 1;
Ed Tanousc4771fb2017-03-13 13:39:49 -0700161}]);
Ed Tanous904063f2017-03-02 16:48:24 -0800162
Ed Tanousc4771fb2017-03-13 13:39:49 -0700163angular.module('Authentication').factory(
Ed Tanous904063f2017-03-02 16:48:24 -0800164 'AuthenticationService',
Ed Tanousc4771fb2017-03-13 13:39:49 -0700165 ['$cookieStore', '$rootScope', '$timeout', '$resource', '$log', '$http',
166 function($cookieStore, $rootScope, $timeout, $resource, $log, $http) {
Ed Tanous904063f2017-03-02 16:48:24 -0800167 var service = {};
168
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800169 service.Login = function(username, password, success_callback, fail_callback) {
Ed Tanous904063f2017-03-02 16:48:24 -0800170
171 var user = {"username": username, "password": password};
Ed Tanous9b65f1f2017-03-07 15:17:13 -0800172 var UserLogin = $resource("/login");
173 var this_login = new UserLogin();
174 this_login.data = {"username": username, "password": password};
175 UserLogin.save(user, success_callback, fail_callback);
176
Ed Tanous904063f2017-03-02 16:48:24 -0800177 };
178
179 service.SetCredentials = function(username, token) {
180 $rootScope.globals["currentUser"] = {username: username, authdata: token};
Ed Tanousc4771fb2017-03-13 13:39:49 -0700181 $http.defaults.headers.common['Authorization'] = 'Token ' + token;
Ed Tanous904063f2017-03-02 16:48:24 -0800182 $cookieStore.put('globals', $rootScope.globals);
183 };
184
185 service.ClearCredentials = function(reason) {
186 $rootScope.globals["currentUser"] = null;
187 if (reason !== null) {
188 service.logoutreason = reason;
189 }
190 $cookieStore.remove('globals');
Ed Tanousc4771fb2017-03-13 13:39:49 -0700191 $http.defaults.headers.common['Authorization'] = '';
Ed Tanous904063f2017-03-02 16:48:24 -0800192 };
193
194 service.RestoreCredientials = function() {
195 var globals = $cookieStore.get('globals') || {};
196 if (globals.currentUser) {
197 service.SetCredentials(
198 globals.currentUser.username, globals.currentUser.authdata);
199 }
200 };
201
202 service.logoutreason = "";
203 return service;
204 }
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700205 ]);