Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 1 | 'use strict'; |
| 2 | angular.module('Authentication', []); |
| 3 | var app = angular.module('bmcApp', [ |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 4 | 'Authentication', |
| 5 | 'ngCookies', |
| 6 | 'ui.bootstrap', |
| 7 | 'ui.router', |
| 8 | 'ngSanitize', |
| 9 | 'ngWebSocket', |
| 10 | 'ngResource' |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 11 | ]); |
| 12 | |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame^] | 13 | |
| 14 | |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 15 | app.controller('MainCtrl', ['$scope', function($scope) { |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 16 | |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 17 | }]); |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 18 | |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 19 | app.service('loginInterceptor', ["$injector", |
| 20 | function($injector) { |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 21 | var service = this; |
| 22 | |
| 23 | service.responseError = function(response) { |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 24 | var $state = $injector.get('$state'); |
| 25 | var AuthenticationService = $injector.get('AuthenticationService'); |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 26 | if (response.status == 401){ |
| 27 | console.log("Login required... "); |
| 28 | |
| 29 | var invalidate_reason = "Your user was logged out."; |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 30 | |
| 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 Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 35 | } else { |
| 36 | $state.after_login_state = $state.current.name; |
| 37 | $state.go('login'); |
| 38 | } |
| 39 | AuthenticationService.ClearCredentials(invalidate_reason); |
| 40 | } |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 41 | return response; |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 42 | }; |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 43 | }]) |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 44 | |
| 45 | app.config(['$httpProvider', function ($httpProvider) { |
| 46 | $httpProvider.interceptors.push('loginInterceptor'); |
| 47 | }]); |
| 48 | |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 49 | app.directive('windowSize', ['$window', function ($window) { |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 50 | 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 Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 73 | }]); |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 74 | |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 75 | app.run(['$rootScope', '$cookieStore', '$state', '$resource', 'AuthenticationService', '$http', '$templateCache', |
| 76 | function($rootScope, $cookieStore, $state, $resource, AuthenticationService, $http, $templateCache) { |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame^] | 77 | |
| 78 | $http.get('static/partial-login.html', {cache:$templateCache}); |
| 79 | $http.get('static/partial-kvm.html', {cache: $templateCache}); |
| 80 | |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 81 | if ($rootScope.globals == undefined){ |
| 82 | $rootScope.globals = {}; |
| 83 | } |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame^] | 84 | |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 85 | |
| 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 Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 93 | // unless we're already trying to go to the login page (prevent a loop) |
| 94 | if (!$rootScope.globals.currentUser && toState.name !== 'login') { |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 95 | // If logged out and transitioning to a logged in page: |
| 96 | event.preventDefault(); |
| 97 | $state.go('login'); |
| 98 | } |
| 99 | }); |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame^] | 100 | |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 101 | } |
| 102 | ]); |
| 103 | |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 104 | app.config(['$stateProvider', '$urlRouterProvider', |
| 105 | function($stateProvider, $urlRouterProvider) { |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 106 | |
| 107 | $urlRouterProvider.otherwise('/systeminfo'); |
| 108 | |
| 109 | $stateProvider |
| 110 | // nested list with just some random string data |
| 111 | .state('login', { |
| 112 | url: '/login', |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 113 | templateUrl: 'static/partial-login.html', |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 114 | controller: 'LoginController', |
| 115 | }) |
| 116 | // systeminfo view ======================================== |
| 117 | .state( |
| 118 | 'systeminfo', |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 119 | {url: '/systeminfo', templateUrl: 'static/partial-systeminfo.html'}) |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 120 | |
| 121 | |
| 122 | // HOME STATES AND NESTED VIEWS ======================================== |
| 123 | .state( |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 124 | 'eventlog', {url: '/eventlog', templateUrl: 'static/partial-eventlog.html'}) |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 125 | |
| 126 | |
| 127 | .state( |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 128 | 'kvm', {url: '/kvm', templateUrl: 'static/partial-kvm.html'}) |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 129 | |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 130 | .state( |
| 131 | 'ipmi', {url: '/ipmi', templateUrl: 'static/partial-ipmi.html'}) |
| 132 | |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 133 | // ABOUT PAGE AND MULTIPLE NAMED VIEWS ================================= |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 134 | .state('about', {url: '/about', templateUrl: 'static/partial-fruinfo.html'}) |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 135 | |
| 136 | // nested list with custom controller |
| 137 | .state('about.list', { |
| 138 | url: '/list', |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 139 | templateUrl: 'static/partial-home-list.html', |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 140 | controller: function($scope) { |
| 141 | $scope.dogs = ['Bernese', 'Husky', 'Goldendoodle']; |
| 142 | } |
| 143 | }) |
| 144 | |
| 145 | |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 146 | }]); |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 147 | |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 148 | app.controller('PaginationDemoCtrl', ['$scope', '$log', function($scope, $log) { |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 149 | $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 Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 161 | }]); |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 162 | |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 163 | angular.module('Authentication').factory( |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 164 | 'AuthenticationService', |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 165 | ['$cookieStore', '$rootScope', '$timeout', '$resource', '$log', '$http', |
| 166 | function($cookieStore, $rootScope, $timeout, $resource, $log, $http) { |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 167 | var service = {}; |
| 168 | |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 169 | service.Login = function(username, password, success_callback, fail_callback) { |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 170 | |
| 171 | var user = {"username": username, "password": password}; |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 172 | 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 Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 177 | }; |
| 178 | |
| 179 | service.SetCredentials = function(username, token) { |
| 180 | $rootScope.globals["currentUser"] = {username: username, authdata: token}; |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 181 | $http.defaults.headers.common['Authorization'] = 'Token ' + token; |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 182 | $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 Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 191 | $http.defaults.headers.common['Authorization'] = ''; |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 192 | }; |
| 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 Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 205 | ]); |