From c8eca566905da47e27ffd634b1e866ceb684235d Mon Sep 17 00:00:00 2001 From: kevgliss Date: Mon, 4 Jul 2016 13:03:46 -0700 Subject: [PATCH] Closes #366 (#387) --- lemur/manage.py | 2 +- lemur/static/app/angular/app.js | 12 ++++- .../app/angular/authentication/login/login.js | 46 +++++++++++++++++-- .../app/angular/authentication/services.js | 38 ++------------- 4 files changed, 56 insertions(+), 42 deletions(-) diff --git a/lemur/manage.py b/lemur/manage.py index 86e6b8df..d0608f5f 100755 --- a/lemur/manage.py +++ b/lemur/manage.py @@ -864,7 +864,7 @@ class Rolling(Command): def main(): manager.add_command("start", LemurServer()) - manager.add_command("runserver", Server(host='127.0.0.1')) + manager.add_command("runserver", Server(host='127.0.0.1', threaded=True)) manager.add_command("clean", Clean()) manager.add_command("show_urls", ShowUrls()) manager.add_command("db", MigrateCommand) diff --git a/lemur/static/app/angular/app.js b/lemur/static/app/angular/app.js index a071a106..9a22eac9 100644 --- a/lemur/static/app/angular/app.js +++ b/lemur/static/app/angular/app.js @@ -149,9 +149,17 @@ }); }); - lemur.run(['$templateCache', function ($templateCache) { + lemur.run(function ($templateCache, $location, $rootScope, $auth, $state) { $templateCache.put('ng-table/pager.html', '
'); - }]); + $rootScope.$on('$stateChangeStart', function(event, toState, toParams) { + if (toState.name !== 'login') { + if (!$auth.isAuthenticated()) { + event.preventDefault(); + $state.go('login', {'toState': toState.name, 'toParams': toParams, notify: false}); + } + } + }); + }); }()); diff --git a/lemur/static/app/angular/authentication/login/login.js b/lemur/static/app/angular/authentication/login/login.js index 9c389737..e317e1b3 100644 --- a/lemur/static/app/angular/authentication/login/login.js +++ b/lemur/static/app/angular/authentication/login/login.js @@ -5,12 +5,48 @@ angular.module('lemur') $stateProvider.state('login', { url: '/login', templateUrl: '/angular/authentication/login/login.tpl.html', - controller: 'LoginController' + controller: 'LoginController', + params: { + 'toState': 'certificates', + 'toParams': {} + } }); }) - .controller('LoginController', function ($rootScope, $scope, AuthenticationService, UserService, providers) { - $scope.login = AuthenticationService.login; - $scope.authenticate = AuthenticationService.authenticate; + .controller('LoginController', function ($rootScope, $scope, $state, $auth, AuthenticationService, UserService, providers, toaster) { + $scope.login = function (username, password) { + return AuthenticationService.login(username, password).then( + function (user) { + $auth.setToken(user.token, true); + $rootScope.$emit('user:login'); + $state.go($state.params.toState, $state.params.toParams); + }, + function (response) { + toaster.pop({ + type: 'error', + title: 'Whoa there', + body: response.data.message, + showCloseButton: true + }); + }); + }; + + $scope.authenticate = function (provider) { + return AuthenticationService.authenticate(provider).then( + function (user) { + $auth.setToken(user.token, true); + $rootScope.$emit('user:login'); + $state.go($state.params.toState, $state.params.toParams); + }, + function (response) { + toaster.pop({ + type: 'error', + title: 'Whoa there', + body: response.data.message, + showCloseButton: true + }); + }); + }; + $scope.logout = AuthenticationService.logout; $scope.providers = providers; @@ -28,4 +64,4 @@ angular.module('lemur') $rootScope.$on('user:logout', function () { $scope.currentUser = null; }); - }); + }); \ No newline at end of file diff --git a/lemur/static/app/angular/authentication/services.js b/lemur/static/app/angular/authentication/services.js index dddffeb4..1a24acb2 100644 --- a/lemur/static/app/angular/authentication/services.js +++ b/lemur/static/app/angular/authentication/services.js @@ -7,40 +7,11 @@ angular.module('lemur') var AuthenticationService = this; AuthenticationService.login = function (username, password) { - AuthenticationApi.customPOST({'username': username, 'password': password}, 'login') - .then( - function (user) { - $auth.setToken(user.token, true); - $rootScope.$emit('user:login'); - $location.url('/certificates'); - }, - function (response) { - toaster.pop({ - type: 'error', - title: 'Whoa there', - body: response.data.message, - showCloseButton: true - }); - } - ); + return AuthenticationApi.customPOST({'username': username, 'password': password}, 'login'); }; AuthenticationService.authenticate = function (provider) { - $auth.authenticate(provider) - .then( - function () { - UserService.getCurrentUser(); - $rootScope.$emit('user:login'); - $location.url('/certificates'); - }, - function (response) { - toaster.pop({ - type: 'error', - title: 'Something went wrong', - body: response.data.message - }); - } - ); + return $auth.authenticate(provider); }; AuthenticationService.logout = function () { @@ -55,8 +26,7 @@ angular.module('lemur') title: 'Good job!', body: 'You have been successfully logged out.' }); - $location.path('/'); + $location.path('/login'); }); }; - - }); + }); \ No newline at end of file