Skip to content

Commit

Permalink
Merge pull request #52 from Narzerus/development
Browse files Browse the repository at this point in the history
Version 0.2
  • Loading branch information
Rafael Vidaurre authored and Rafael Vidaurre committed Mar 2, 2015
2 parents a4c8334 + 4c2e052 commit dab6314
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 15 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-permission",
"version": "0.1.7",
"version": "0.2.0",
"authors": [
"Rafael Vidaurre <[email protected]>"
],
Expand Down
16 changes: 9 additions & 7 deletions dist/angular-permission.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* angular-permission
* Route permission and access control as simple as it can get
* @version v0.1.7 - 2015-02-16
* @version v0.2.0 - 2015-03-02
* @link http://www.rafaelvidaurre.com
* @author Rafael Vidaurre <[email protected]>
* @license MIT License, http://www.opensource.org/licenses/MIT
Expand All @@ -14,6 +14,10 @@
.run(['$rootScope', 'Permission', '$state', function ($rootScope, Permission, $state) {
$rootScope.$on('$stateChangeStart',
function (event, toState, toParams, fromState, fromParams) {
if (toState.$$finishAuthorize) {
return;
}

// If there are permissions set then prevent default and attempt to authorize
var permissions;
if (toState.data && toState.data.permissions) {
Expand All @@ -31,12 +35,13 @@

if (permissions) {
event.preventDefault();
toState = angular.extend({'$$finishAuthorize': true}, toState);

Permission.authorize(permissions, toParams).then(function () {
// If authorized, use call state.go without triggering the event.
// Then trigger $stateChangeSuccess manually to resume the rest of the process
// Note: This is a pseudo-hacky fix which should be fixed in future ui-router versions
if (!$rootScope.$broadcast('$stateChangeStart', toState.name, toParams, fromState.name, fromParams).defaultPrevented) {
if (!$rootScope.$broadcast('$stateChangeStart', toState, toParams, fromState, fromParams).defaultPrevented) {
$rootScope.$broadcast('$stateChangePermissionAccepted', toState, toParams);

$state.go(toState.name, toParams, {notify: false}).then(function() {
Expand All @@ -45,16 +50,13 @@
});
}
}, function () {
if (!$rootScope.$broadcast('$stateChangeStart', toState.name, toParams, fromState.name, fromParams).defaultPrevented) {
if (!$rootScope.$broadcast('$stateChangeStart', toState, toParams, fromState, fromParams).defaultPrevented) {
$rootScope.$broadcast('$stateChangePermissionDenied', toState, toParams);

// If not authorized, redirect to wherever the route has defined, if defined at all
var redirectTo = permissions.redirectTo;
if (redirectTo) {
$state.go(redirectTo, toParams, {notify: false}).then(function() {
$rootScope
.$broadcast('$stateChangeSuccess', toState, toParams, fromState, fromParams);
});
$state.go(redirectTo, toParams);
}
}
});
Expand Down
14 changes: 8 additions & 6 deletions src/permission.mdl.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
.run(['$rootScope', 'Permission', '$state', function ($rootScope, Permission, $state) {
$rootScope.$on('$stateChangeStart',
function (event, toState, toParams, fromState, fromParams) {
if (toState.$$finishAuthorize) {
return;
}

// If there are permissions set then prevent default and attempt to authorize
var permissions;
if (toState.data && toState.data.permissions) {
Expand All @@ -22,12 +26,13 @@

if (permissions) {
event.preventDefault();
toState = angular.extend({'$$finishAuthorize': true}, toState);

Permission.authorize(permissions, toParams).then(function () {
// If authorized, use call state.go without triggering the event.
// Then trigger $stateChangeSuccess manually to resume the rest of the process
// Note: This is a pseudo-hacky fix which should be fixed in future ui-router versions
if (!$rootScope.$broadcast('$stateChangeStart', toState.name, toParams, fromState.name, fromParams).defaultPrevented) {
if (!$rootScope.$broadcast('$stateChangeStart', toState, toParams, fromState, fromParams).defaultPrevented) {
$rootScope.$broadcast('$stateChangePermissionAccepted', toState, toParams);

$state.go(toState.name, toParams, {notify: false}).then(function() {
Expand All @@ -36,16 +41,13 @@
});
}
}, function () {
if (!$rootScope.$broadcast('$stateChangeStart', toState.name, toParams, fromState.name, fromParams).defaultPrevented) {
if (!$rootScope.$broadcast('$stateChangeStart', toState, toParams, fromState, fromParams).defaultPrevented) {
$rootScope.$broadcast('$stateChangePermissionDenied', toState, toParams);

// If not authorized, redirect to wherever the route has defined, if defined at all
var redirectTo = permissions.redirectTo;
if (redirectTo) {
$state.go(redirectTo, toParams, {notify: false}).then(function() {
$rootScope
.$broadcast('$stateChangeSuccess', toState, toParams, fromState, fromParams);
});
$state.go(redirectTo, toParams);
}
}
});
Expand Down
52 changes: 51 additions & 1 deletion src/permission.mdl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,32 @@ describe('Module: Permission', function () {
changePermissionDeniedHasBeenCalled = true;
});


$rootScope.$digest();
expect($state.current.name).toBe('accepted');
expect(changePermissionAcceptedHasBeenCalled).toBeTruthy();
expect(changePermissionDeniedHasBeenCalled).not.toBeTruthy();
}));

it('should broadcast a $stateChangeStart with correct parameters(accepted state)', inject (function($rootScope) {
initStateTo('home');
$state.go('accepted');

var changeStartHasBeenCalled = false;
var toState = null;
var fromState = null;
$rootScope.$on('$stateChangeStart', function (event, _toState, toParams, _fromState, fromParams) {
changeStartHasBeenCalled = true;
toState = _toState;
fromState = _fromState;
});

$rootScope.$digest();
expect($state.current.name).toBe('accepted');
expect(changeStartHasBeenCalled).toBeTruthy();
expect(toState.name).toBe('accepted');
expect(fromState.name).toBe('home');
}));

it('should not go to the denied state', function () {
initStateTo('home');
$state.go('denied');
Expand All @@ -160,6 +179,26 @@ describe('Module: Permission', function () {
expect(changePermissionDeniedHasBeenCalled).toBeTruthy();
});

it('should broadcast a $stateChangeStart with correct parameters(denied state)', inject (function($rootScope) {
initStateTo('home');
$state.go('denied');

var changeStartHasBeenCalled = false;
var toState = null;
var fromState = null;
$rootScope.$on('$stateChangeStart', function (event, _toState, toParams, _fromState, fromParams) {
changeStartHasBeenCalled = true;
toState = _toState;
fromState = _fromState;
});

$rootScope.$digest();
expect($state.current.name).toBe('home');
expect(changeStartHasBeenCalled).toBeTruthy();
expect(toState.name).toBe('denied');
expect(fromState.name).toBe('home');
}));

it('should not go to the denied state but redirect to the provided state', function () {
initStateTo('home');
$state.go('deniedWithRedirect');
Expand All @@ -178,6 +217,17 @@ describe('Module: Permission', function () {
expect(changePermissionDeniedHasBeenCalled).toBeTruthy();
});

it('should trigger $stateChangeSuccess with the redirect state and not the denied one', function () {
initStateTo('home');
$state.go('deniedWithRedirect');

$rootScope.$on('$stateChangeSuccess', function (name, toState) {
expect(toState.name).not.toBe('deniedWithRedirect');
expect(toState.name).toBe('redirectToThisState');
});
$rootScope.$digest();
});

it('should pass state params on redirect', function () {
initStateTo('home');
$state.go('abstractTest.denied',{abstractValue: 'test'});
Expand Down

0 comments on commit dab6314

Please sign in to comment.