From e21805f20a15a40abd5f0f5908aef4ed9c0fb176 Mon Sep 17 00:00:00 2001 From: Igor Freire Date: Fri, 24 Jul 2015 16:58:41 -0300 Subject: [PATCH] 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', {