Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Commit

Permalink
Merge pull request #700 from trainerbill/requireLogin
Browse files Browse the repository at this point in the history
Require login
  • Loading branch information
lirantal committed Jul 24, 2015
2 parents 0e3f194 + 1b54d35 commit c8880ea
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 8 deletions.
5 changes: 4 additions & 1 deletion modules/articles/client/config/articles.client.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ angular.module('articles').config(['$stateProvider',
state('articles', {
abstract: true,
url: '/articles',
template: '<ui-view/>'
template: '<ui-view/>',
data: {
roles: ['user']
}
}).
state('articles.list', {
url: '',
Expand Down
5 changes: 4 additions & 1 deletion modules/chat/client/config/chat.client.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']
}
});
}
]);
24 changes: 24 additions & 0 deletions modules/core/client/app/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,30 @@ 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.roles && toState.data.roles.length > 0) {
var allowed = false;
toState.data.roles.forEach(function (role) {
if (Authentication.user.roles !== undefined && Authentication.user.roles.indexOf(role) !== -1) {
allowed = true;
return true;
}
});

if (!allowed) {
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
Expand Down
5 changes: 4 additions & 1 deletion modules/users/client/config/users.client.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
roles: ['user']
}
}).
state('settings.profile', {
url: '/profile',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +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;

// If user is not signed in then redirect back home
if (!$scope.user) $location.path('/');
}
]);

0 comments on commit c8880ea

Please sign in to comment.