From 9fc88e6e5b5bd05c1e532246356725a3fa16a223 Mon Sep 17 00:00:00 2001 From: Igor Freire Date: Fri, 17 Jul 2015 14:39:32 -0300 Subject: [PATCH 1/5] Filter states for which login is required during state change A state parameter was added for the routes that require user authentication. Now, everytime a statechange occurs, the destination state is checked and user is redirected to signin page if necessary. Note the state parameter is added within `data`, so that nested states can inherent its value. --- .../client/config/articles.client.routes.js | 10 ++++++++-- modules/core/client/app/init.js | 14 ++++++++++++++ modules/users/client/config/users.client.routes.js | 5 ++++- .../settings/settings.client.controller.js | 3 --- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/modules/articles/client/config/articles.client.routes.js b/modules/articles/client/config/articles.client.routes.js index c6890be3ef..c29de18814 100644 --- a/modules/articles/client/config/articles.client.routes.js +++ b/modules/articles/client/config/articles.client.routes.js @@ -16,7 +16,10 @@ angular.module('articles').config(['$stateProvider', }). state('articles.create', { url: '/create', - templateUrl: 'modules/articles/views/create-article.client.view.html' + templateUrl: 'modules/articles/views/create-article.client.view.html', + data: { + requiresLogin: true + } }). state('articles.view', { url: '/:articleId', @@ -24,7 +27,10 @@ angular.module('articles').config(['$stateProvider', }). state('articles.edit', { url: '/:articleId/edit', - templateUrl: 'modules/articles/views/edit-article.client.view.html' + templateUrl: 'modules/articles/views/edit-article.client.view.html', + data: { + requiresLogin: true + } }); } ]); diff --git a/modules/core/client/app/init.js b/modules/core/client/app/init.js index 5e144afce5..df0f7ffe14 100644 --- a/modules/core/client/app/init.js +++ b/modules/core/client/app/init.js @@ -10,6 +10,20 @@ angular.module(ApplicationConfiguration.applicationModuleName).config(['$locatio } ]); +angular.module(ApplicationConfiguration.applicationModuleName).run(function($rootScope, $state, Authentication) { + // Check authentication before changing state + $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) { + if (toState.data && toState.data.requiresLogin && Authentication.user === '') { + event.preventDefault(); + $state.go('authentication.signin', {}, { + notify: false + }).then(function() { + $rootScope.$broadcast('$stateChangeSuccess', 'authentication.signin', {}, toState, toParams); + }); + } + }); +}); + //Then define the init function for starting up the application angular.element(document).ready(function() { //Fixing facebook bug with redirect diff --git a/modules/users/client/config/users.client.routes.js b/modules/users/client/config/users.client.routes.js index 4c00bc0651..69c01b55b7 100644 --- a/modules/users/client/config/users.client.routes.js +++ b/modules/users/client/config/users.client.routes.js @@ -8,7 +8,10 @@ angular.module('users').config(['$stateProvider', state('settings', { abstract: true, url: '/settings', - templateUrl: 'modules/users/views/settings/settings.client.view.html' + templateUrl: 'modules/users/views/settings/settings.client.view.html', + data: { + requiresLogin: true + } }). state('settings.profile', { url: '/profile', diff --git a/modules/users/client/controllers/settings/settings.client.controller.js b/modules/users/client/controllers/settings/settings.client.controller.js index 090abc86c4..7c8d1b6569 100644 --- a/modules/users/client/controllers/settings/settings.client.controller.js +++ b/modules/users/client/controllers/settings/settings.client.controller.js @@ -3,8 +3,5 @@ angular.module('users').controller('SettingsController', ['$scope', '$location', 'Authentication', function($scope, $location, Authentication) { $scope.user = Authentication.user; - - // If user is not signed in then redirect back home - if (!$scope.user) $location.path('/'); } ]); From e21805f20a15a40abd5f0f5908aef4ed9c0fb176 Mon Sep 17 00:00:00 2001 From: Igor Freire Date: Fri, 24 Jul 2015 16:58:41 -0300 Subject: [PATCH 2/5] Prepare for role-based access control of states --- .../client/config/articles.client.routes.js | 4 ++-- modules/core/client/app/init.js | 17 ++++++++++------- .../users/client/config/users.client.routes.js | 2 +- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/modules/articles/client/config/articles.client.routes.js b/modules/articles/client/config/articles.client.routes.js index c29de18814..9d3990d6fe 100644 --- a/modules/articles/client/config/articles.client.routes.js +++ b/modules/articles/client/config/articles.client.routes.js @@ -18,7 +18,7 @@ angular.module('articles').config(['$stateProvider', url: '/create', templateUrl: 'modules/articles/views/create-article.client.view.html', data: { - requiresLogin: true + forbiddenRoles: ['guest'] } }). state('articles.view', { @@ -29,7 +29,7 @@ angular.module('articles').config(['$stateProvider', url: '/:articleId/edit', templateUrl: 'modules/articles/views/edit-article.client.view.html', data: { - requiresLogin: true + forbiddenRoles: ['guest'] } }); } diff --git a/modules/core/client/app/init.js b/modules/core/client/app/init.js index df0f7ffe14..917fe4a989 100644 --- a/modules/core/client/app/init.js +++ b/modules/core/client/app/init.js @@ -13,13 +13,16 @@ angular.module(ApplicationConfiguration.applicationModuleName).config(['$locatio angular.module(ApplicationConfiguration.applicationModuleName).run(function($rootScope, $state, Authentication) { // Check authentication before changing state $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) { - if (toState.data && toState.data.requiresLogin && Authentication.user === '') { - event.preventDefault(); - $state.go('authentication.signin', {}, { - notify: false - }).then(function() { - $rootScope.$broadcast('$stateChangeSuccess', 'authentication.signin', {}, toState, toParams); - }); + if (toState.data && toState.data.forbiddenRoles) { + // If access of guest user is forbidden: + if (toState.data.forbiddenRoles.indexOf('guest') !== -1 && Authentication.user === '') { + event.preventDefault(); + $state.go('authentication.signin', {}, { + notify: false + }).then(function() { + $rootScope.$broadcast('$stateChangeSuccess', 'authentication.signin', {}, toState, toParams); + }); + } } }); }); diff --git a/modules/users/client/config/users.client.routes.js b/modules/users/client/config/users.client.routes.js index 69c01b55b7..0c720f0eeb 100644 --- a/modules/users/client/config/users.client.routes.js +++ b/modules/users/client/config/users.client.routes.js @@ -10,7 +10,7 @@ angular.module('users').config(['$stateProvider', url: '/settings', templateUrl: 'modules/users/views/settings/settings.client.view.html', data: { - requiresLogin: true + forbiddenRoles: ['guest'] } }). state('settings.profile', { From fb71619e0cc6166fe35c6d1e0f34637a279b20c5 Mon Sep 17 00:00:00 2001 From: Andrew Throener Date: Fri, 24 Jul 2015 15:37:26 -0500 Subject: [PATCH 3/5] Refactor --- .../articles/client/config/articles.client.routes.js | 4 ++-- modules/core/client/app/init.js | 12 +++++++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/modules/articles/client/config/articles.client.routes.js b/modules/articles/client/config/articles.client.routes.js index 9d3990d6fe..4dcc759f8b 100644 --- a/modules/articles/client/config/articles.client.routes.js +++ b/modules/articles/client/config/articles.client.routes.js @@ -18,7 +18,7 @@ angular.module('articles').config(['$stateProvider', url: '/create', templateUrl: 'modules/articles/views/create-article.client.view.html', data: { - forbiddenRoles: ['guest'] + roles: ['admin'] } }). state('articles.view', { @@ -29,7 +29,7 @@ angular.module('articles').config(['$stateProvider', url: '/:articleId/edit', templateUrl: 'modules/articles/views/edit-article.client.view.html', data: { - forbiddenRoles: ['guest'] + roles: ['user'] } }); } diff --git a/modules/core/client/app/init.js b/modules/core/client/app/init.js index 917fe4a989..327bc19cd5 100644 --- a/modules/core/client/app/init.js +++ b/modules/core/client/app/init.js @@ -13,9 +13,15 @@ angular.module(ApplicationConfiguration.applicationModuleName).config(['$locatio angular.module(ApplicationConfiguration.applicationModuleName).run(function($rootScope, $state, Authentication) { // Check authentication before changing state $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) { - if (toState.data && toState.data.forbiddenRoles) { - // If access of guest user is forbidden: - if (toState.data.forbiddenRoles.indexOf('guest') !== -1 && Authentication.user === '') { + if (toState.data && toState.data.roles && toState.data.roles.length > 0) { + var allowed = false; + toState.data.roles.forEach(function (role) { + if (Authentication.user.roles.indexOf(role) !== -1) { + allowed = true; + } + }); + + if (!allowed) { event.preventDefault(); $state.go('authentication.signin', {}, { notify: false From 03a4042a33600de428a468e4e4477109f9eae611 Mon Sep 17 00:00:00 2001 From: Andrew Throener Date: Fri, 24 Jul 2015 16:43:46 -0500 Subject: [PATCH 4/5] Updated routes and a logic fix --- .../client/config/articles.client.routes.js | 16 +++++++--------- modules/chat/client/config/chat.client.routes.js | 5 ++++- modules/core/client/app/init.js | 2 +- .../users/client/config/users.client.routes.js | 2 +- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/modules/articles/client/config/articles.client.routes.js b/modules/articles/client/config/articles.client.routes.js index 4dcc759f8b..fab0368c35 100644 --- a/modules/articles/client/config/articles.client.routes.js +++ b/modules/articles/client/config/articles.client.routes.js @@ -8,18 +8,19 @@ angular.module('articles').config(['$stateProvider', state('articles', { abstract: true, url: '/articles', - template: '' + template: '', + data: { + roles: ['user'] + } }). state('articles.list', { url: '', templateUrl: 'modules/articles/views/list-articles.client.view.html' + }). state('articles.create', { url: '/create', - templateUrl: 'modules/articles/views/create-article.client.view.html', - data: { - roles: ['admin'] - } + templateUrl: 'modules/articles/views/create-article.client.view.html' }). state('articles.view', { url: '/:articleId', @@ -27,10 +28,7 @@ angular.module('articles').config(['$stateProvider', }). state('articles.edit', { url: '/:articleId/edit', - templateUrl: 'modules/articles/views/edit-article.client.view.html', - data: { - roles: ['user'] - } + templateUrl: 'modules/articles/views/edit-article.client.view.html' }); } ]); diff --git a/modules/chat/client/config/chat.client.routes.js b/modules/chat/client/config/chat.client.routes.js index c688ac096f..490a11da88 100644 --- a/modules/chat/client/config/chat.client.routes.js +++ b/modules/chat/client/config/chat.client.routes.js @@ -6,7 +6,10 @@ angular.module('chat').config(['$stateProvider', $stateProvider. state('chat', { url: '/chat', - templateUrl: 'modules/chat/views/chat.client.view.html' + templateUrl: 'modules/chat/views/chat.client.view.html', + data: { + roles: ['user'] + } }); } ]); diff --git a/modules/core/client/app/init.js b/modules/core/client/app/init.js index 327bc19cd5..84b64d4bc8 100644 --- a/modules/core/client/app/init.js +++ b/modules/core/client/app/init.js @@ -16,7 +16,7 @@ angular.module(ApplicationConfiguration.applicationModuleName).run(function($roo if (toState.data && toState.data.roles && toState.data.roles.length > 0) { var allowed = false; toState.data.roles.forEach(function (role) { - if (Authentication.user.roles.indexOf(role) !== -1) { + if (Authentication.user.roles !== undefined && Authentication.user.roles.indexOf(role) !== -1) { allowed = true; } }); diff --git a/modules/users/client/config/users.client.routes.js b/modules/users/client/config/users.client.routes.js index 0c720f0eeb..1646d50717 100644 --- a/modules/users/client/config/users.client.routes.js +++ b/modules/users/client/config/users.client.routes.js @@ -10,7 +10,7 @@ angular.module('users').config(['$stateProvider', url: '/settings', templateUrl: 'modules/users/views/settings/settings.client.view.html', data: { - forbiddenRoles: ['guest'] + roles: ['user'] } }). state('settings.profile', { From 1b54d353056957395a4f07f67f6d956aa5a05253 Mon Sep 17 00:00:00 2001 From: Andrew Throener Date: Fri, 24 Jul 2015 17:06:55 -0500 Subject: [PATCH 5/5] Final Clean Up after code review --- modules/articles/client/config/articles.client.routes.js | 1 - modules/core/client/app/init.js | 1 + .../client/controllers/settings/settings.client.controller.js | 4 ++-- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/articles/client/config/articles.client.routes.js b/modules/articles/client/config/articles.client.routes.js index fab0368c35..ab0337a695 100644 --- a/modules/articles/client/config/articles.client.routes.js +++ b/modules/articles/client/config/articles.client.routes.js @@ -16,7 +16,6 @@ angular.module('articles').config(['$stateProvider', state('articles.list', { url: '', templateUrl: 'modules/articles/views/list-articles.client.view.html' - }). state('articles.create', { url: '/create', diff --git a/modules/core/client/app/init.js b/modules/core/client/app/init.js index 84b64d4bc8..94b23b80d5 100644 --- a/modules/core/client/app/init.js +++ b/modules/core/client/app/init.js @@ -18,6 +18,7 @@ angular.module(ApplicationConfiguration.applicationModuleName).run(function($roo toState.data.roles.forEach(function (role) { if (Authentication.user.roles !== undefined && Authentication.user.roles.indexOf(role) !== -1) { allowed = true; + return true; } }); diff --git a/modules/users/client/controllers/settings/settings.client.controller.js b/modules/users/client/controllers/settings/settings.client.controller.js index 7c8d1b6569..46c217cb0e 100644 --- a/modules/users/client/controllers/settings/settings.client.controller.js +++ b/modules/users/client/controllers/settings/settings.client.controller.js @@ -1,7 +1,7 @@ 'use strict'; -angular.module('users').controller('SettingsController', ['$scope', '$location', 'Authentication', - function($scope, $location, Authentication) { +angular.module('users').controller('SettingsController', ['$scope', 'Authentication', + function($scope, Authentication) { $scope.user = Authentication.user; } ]);