-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from Narzerus/development
Version 0.2
- Loading branch information
Showing
4 changed files
with
69 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]>" | ||
], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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) { | ||
|
@@ -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() { | ||
|
@@ -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); | ||
} | ||
} | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters