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 | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 13 | app.controller('MainCtrl', ['$scope', function($scope) { |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 14 | |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 15 | }]); |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 16 | |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 17 | app.service('loginInterceptor', ["$injector", |
| 18 | function($injector) { |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 19 | var service = this; |
| 20 | |
| 21 | service.responseError = function(response) { |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 22 | var $state = $injector.get('$state'); |
| 23 | var AuthenticationService = $injector.get('AuthenticationService'); |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 24 | if (response.status == 401){ |
| 25 | console.log("Login required... "); |
| 26 | |
| 27 | var invalidate_reason = "Your user was logged out."; |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 28 | |
| 29 | // if we're attempting to log in, we need to |
| 30 | // continue the promise chain to make sure the user is informed |
| 31 | if ($state.current.name === "login") { |
| 32 | invalidate_reason = "Your username and password was incorrect"; |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 33 | } else { |
| 34 | $state.after_login_state = $state.current.name; |
| 35 | $state.go('login'); |
| 36 | } |
| 37 | AuthenticationService.ClearCredentials(invalidate_reason); |
| 38 | } |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 39 | return response; |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 40 | }; |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 41 | }]) |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 42 | |
| 43 | app.config(['$httpProvider', function ($httpProvider) { |
| 44 | $httpProvider.interceptors.push('loginInterceptor'); |
| 45 | }]); |
| 46 | |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 47 | app.directive('windowSize', ['$window', function ($window) { |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 48 | return function (scope, element) { |
| 49 | var w = angular.element($window); |
| 50 | scope.getWindowDimensions = function () { |
| 51 | return { |
| 52 | 'h': w.height(), |
| 53 | 'w': w.width() |
| 54 | }; |
| 55 | }; |
| 56 | scope.$watch(scope.getWindowDimensions, function (newValue, oldValue) { |
| 57 | scope.windowHeight = newValue.h; |
| 58 | scope.windowWidth = newValue.w; |
| 59 | scope.style = function () { |
| 60 | return { |
| 61 | 'height': (newValue.h - 100) + 'px', |
| 62 | 'width': (newValue.w - 100) + 'px' |
| 63 | }; |
| 64 | }; |
| 65 | }, true); |
| 66 | |
| 67 | w.bind('resize', function () { |
| 68 | scope.$apply(); |
| 69 | }); |
| 70 | } |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 71 | }]); |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 72 | |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 73 | app.run(['$rootScope', '$cookieStore', '$state', '$resource', 'AuthenticationService', '$http', '$templateCache', |
| 74 | function($rootScope, $cookieStore, $state, $resource, AuthenticationService, $http, $templateCache) { |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 75 | if ($rootScope.globals == undefined){ |
| 76 | $rootScope.globals = {}; |
| 77 | } |
| 78 | |
| 79 | // keep user logged in after page refresh |
| 80 | AuthenticationService.RestoreCredientials(); |
| 81 | |
| 82 | $rootScope.$on( |
| 83 | '$stateChangeStart', |
| 84 | function(event, toState, toParams, fromState, fromParams, options) { |
| 85 | // redirect to login page if not logged in |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 86 | // unless we're already trying to go to the login page (prevent a loop) |
| 87 | if (!$rootScope.globals.currentUser && toState.name !== 'login') { |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 88 | // If logged out and transitioning to a logged in page: |
| 89 | event.preventDefault(); |
| 90 | $state.go('login'); |
| 91 | } |
| 92 | }); |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame] | 93 | |
| 94 | $http.get('static/partial-kvm.html', { cache: $templateCache }); |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 95 | } |
| 96 | ]); |
| 97 | |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 98 | app.config(['$stateProvider', '$urlRouterProvider', |
| 99 | function($stateProvider, $urlRouterProvider) { |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 100 | |
| 101 | $urlRouterProvider.otherwise('/systeminfo'); |
| 102 | |
| 103 | $stateProvider |
| 104 | // nested list with just some random string data |
| 105 | .state('login', { |
| 106 | url: '/login', |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 107 | templateUrl: 'static/partial-login.html', |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 108 | controller: 'LoginController', |
| 109 | }) |
| 110 | // systeminfo view ======================================== |
| 111 | .state( |
| 112 | 'systeminfo', |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 113 | {url: '/systeminfo', templateUrl: 'static/partial-systeminfo.html'}) |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 114 | |
| 115 | |
| 116 | // HOME STATES AND NESTED VIEWS ======================================== |
| 117 | .state( |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 118 | 'eventlog', {url: '/eventlog', templateUrl: 'static/partial-eventlog.html'}) |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 119 | |
| 120 | |
| 121 | .state( |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 122 | 'kvm', {url: '/kvm', templateUrl: 'static/partial-kvm.html'}) |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 123 | |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 124 | .state( |
| 125 | 'ipmi', {url: '/ipmi', templateUrl: 'static/partial-ipmi.html'}) |
| 126 | |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 127 | // ABOUT PAGE AND MULTIPLE NAMED VIEWS ================================= |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 128 | .state('about', {url: '/about', templateUrl: 'static/partial-fruinfo.html'}) |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 129 | |
| 130 | // nested list with custom controller |
| 131 | .state('about.list', { |
| 132 | url: '/list', |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 133 | templateUrl: 'static/partial-home-list.html', |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 134 | controller: function($scope) { |
| 135 | $scope.dogs = ['Bernese', 'Husky', 'Goldendoodle']; |
| 136 | } |
| 137 | }) |
| 138 | |
| 139 | |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 140 | }]); |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 141 | |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 142 | app.controller('PaginationDemoCtrl', ['$scope', '$log', function($scope, $log) { |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 143 | $scope.totalItems = 64; |
| 144 | $scope.currentPage = 4; |
| 145 | |
| 146 | $scope.setPage = function(pageNo) { $scope.currentPage = pageNo; }; |
| 147 | |
| 148 | $scope.pageChanged = function() { |
| 149 | $log.log('Page changed to: ' + $scope.currentPage); |
| 150 | }; |
| 151 | |
| 152 | $scope.maxSize = 5; |
| 153 | $scope.bigTotalItems = 175; |
| 154 | $scope.bigCurrentPage = 1; |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 155 | }]); |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 156 | |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 157 | angular.module('Authentication').factory( |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 158 | 'AuthenticationService', |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 159 | ['$cookieStore', '$rootScope', '$timeout', '$resource', '$log', '$http', |
| 160 | function($cookieStore, $rootScope, $timeout, $resource, $log, $http) { |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 161 | var service = {}; |
| 162 | |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 163 | service.Login = function(username, password, success_callback, fail_callback) { |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 164 | |
| 165 | var user = {"username": username, "password": password}; |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 166 | var UserLogin = $resource("/login"); |
| 167 | var this_login = new UserLogin(); |
| 168 | this_login.data = {"username": username, "password": password}; |
| 169 | UserLogin.save(user, success_callback, fail_callback); |
| 170 | |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 171 | }; |
| 172 | |
| 173 | service.SetCredentials = function(username, token) { |
| 174 | $rootScope.globals["currentUser"] = {username: username, authdata: token}; |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 175 | $http.defaults.headers.common['Authorization'] = 'Token ' + token; |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 176 | $cookieStore.put('globals', $rootScope.globals); |
| 177 | }; |
| 178 | |
| 179 | service.ClearCredentials = function(reason) { |
| 180 | $rootScope.globals["currentUser"] = null; |
| 181 | if (reason !== null) { |
| 182 | service.logoutreason = reason; |
| 183 | } |
| 184 | $cookieStore.remove('globals'); |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 185 | $http.defaults.headers.common['Authorization'] = ''; |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | service.RestoreCredientials = function() { |
| 189 | var globals = $cookieStore.get('globals') || {}; |
| 190 | if (globals.currentUser) { |
| 191 | service.SetCredentials( |
| 192 | globals.currentUser.username, globals.currentUser.authdata); |
| 193 | } |
| 194 | }; |
| 195 | |
| 196 | service.logoutreason = ""; |
| 197 | return service; |
| 198 | } |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 199 | ]); |