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 | |
| 13 | |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame^] | 14 | app.controller('MainCtrl', ['$scope', function($scope) { |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 15 | |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame^] | 16 | }]); |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 17 | |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 18 | app.service('loginInterceptor', ["$injector", |
| 19 | function($injector) { |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 20 | var service = this; |
| 21 | |
| 22 | service.responseError = function(response) { |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 23 | var $state = $injector.get('$state'); |
| 24 | var AuthenticationService = $injector.get('AuthenticationService'); |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 25 | if (response.status == 401){ |
| 26 | console.log("Login required... "); |
| 27 | |
| 28 | var invalidate_reason = "Your user was logged out."; |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 29 | |
| 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 Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 34 | } else { |
| 35 | $state.after_login_state = $state.current.name; |
| 36 | $state.go('login'); |
| 37 | } |
| 38 | AuthenticationService.ClearCredentials(invalidate_reason); |
| 39 | } |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame^] | 40 | return response; |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 41 | }; |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 42 | }]) |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 43 | |
| 44 | app.config(['$httpProvider', function ($httpProvider) { |
| 45 | $httpProvider.interceptors.push('loginInterceptor'); |
| 46 | }]); |
| 47 | |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame^] | 48 | app.directive('windowSize', ['$window', function ($window) { |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 49 | 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 Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame^] | 72 | }]); |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 73 | |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 74 | app.run(['$rootScope', '$cookieStore', '$state', '$resource', 'AuthenticationService', |
| 75 | function($rootScope, $cookieStore, $state, $resource, AuthenticationService) { |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 76 | 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 Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 87 | // unless we're already trying to go to the login page (prevent a loop) |
| 88 | if (!$rootScope.globals.currentUser && toState.name !== 'login') { |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 89 | // If logged out and transitioning to a logged in page: |
| 90 | event.preventDefault(); |
| 91 | $state.go('login'); |
| 92 | } |
| 93 | }); |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 94 | } |
| 95 | ]); |
| 96 | |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 97 | app.config(['$stateProvider', '$urlRouterProvider', |
| 98 | function($stateProvider, $urlRouterProvider) { |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 99 | |
| 100 | $urlRouterProvider.otherwise('/systeminfo'); |
| 101 | |
| 102 | $stateProvider |
| 103 | // nested list with just some random string data |
| 104 | .state('login', { |
| 105 | url: '/login', |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame^] | 106 | templateUrl: 'static/partial-login.html', |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 107 | controller: 'LoginController', |
| 108 | }) |
| 109 | // systeminfo view ======================================== |
| 110 | .state( |
| 111 | 'systeminfo', |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 112 | {url: '/systeminfo', templateUrl: 'static/partial-systeminfo.html'}) |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 113 | |
| 114 | |
| 115 | // HOME STATES AND NESTED VIEWS ======================================== |
| 116 | .state( |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 117 | 'eventlog', {url: '/eventlog', templateUrl: 'static/partial-eventlog.html'}) |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 118 | |
| 119 | |
| 120 | .state( |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 121 | 'kvm', {url: '/kvm', templateUrl: 'static/partial-kvm.html'}) |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 122 | |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame^] | 123 | .state( |
| 124 | 'ipmi', {url: '/ipmi', templateUrl: 'static/partial-ipmi.html'}) |
| 125 | |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 126 | // ABOUT PAGE AND MULTIPLE NAMED VIEWS ================================= |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 127 | .state('about', {url: '/about', templateUrl: 'static/partial-fruinfo.html'}) |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 128 | |
| 129 | // nested list with custom controller |
| 130 | .state('about.list', { |
| 131 | url: '/list', |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 132 | templateUrl: 'static/partial-home-list.html', |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 133 | controller: function($scope) { |
| 134 | $scope.dogs = ['Bernese', 'Husky', 'Goldendoodle']; |
| 135 | } |
| 136 | }) |
| 137 | |
| 138 | |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 139 | }]); |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 140 | |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 141 | app.controller('PaginationDemoCtrl', ['$scope', '$log', function($scope, $log) { |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 142 | $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 Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 154 | }]); |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 155 | |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 156 | angular.module('Authentication').factory( |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 157 | 'AuthenticationService', |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 158 | ['$cookieStore', '$rootScope', '$timeout', '$resource', '$log', '$http', |
| 159 | function($cookieStore, $rootScope, $timeout, $resource, $log, $http) { |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 160 | var service = {}; |
| 161 | |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 162 | service.Login = function(username, password, success_callback, fail_callback) { |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 163 | |
| 164 | var user = {"username": username, "password": password}; |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 165 | 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 Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | service.SetCredentials = function(username, token) { |
| 173 | $rootScope.globals["currentUser"] = {username: username, authdata: token}; |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 174 | $http.defaults.headers.common['Authorization'] = 'Token ' + token; |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 175 | $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 Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 184 | $http.defaults.headers.common['Authorization'] = ''; |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 185 | }; |
| 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 Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame^] | 198 | ]); |