Comparing version 0.5.1 to 0.5.2
303
cr-acl.js
@@ -1,157 +0,184 @@ | ||
angular.module("cr.acl", []) | ||
.constant("cr-acl.config", { | ||
"redirect": "unauthorized", | ||
"roles": { | ||
"ROLE_USER": ["ROLE_USER"], | ||
"ROLE_GUEST": ["ROLE_GUEST"] | ||
} | ||
}) | ||
.provider("crAcl", ['cr-acl.config', function(config){ | ||
var self = {}; | ||
self.roles = config.roles; | ||
self.redirect = config.redirect; | ||
angular.module('cr.acl', []) | ||
.constant('cr-acl.config', { | ||
'redirect': 'unauthorized', | ||
'roles': { | ||
'ROLE_USER': ['ROLE_USER'], | ||
'ROLE_GUEST': ['ROLE_GUEST'] | ||
} | ||
}) | ||
.provider('crAcl', ['cr-acl.config', function (config) { | ||
var self = {}; | ||
self.roles = config.roles; | ||
self.redirect = config.redirect; | ||
/** | ||
* Your role is granted for this route? | ||
* @param string identityRole | ||
* @param array sstateRolesGranted | ||
* @return bool | ||
*/ | ||
self.isGranted = function(identityRole, stateRolesGranted){ | ||
var granted = false; | ||
/** | ||
* Your role is granted for this route? | ||
* @param string identityRole | ||
* @param array sstateRolesGranted | ||
* @return bool | ||
*/ | ||
self.isGranted = function (identityRole, stateRolesGranted) { | ||
var granted = false; | ||
if((identityRole in self.roles) === false){ | ||
throw "This role["+identityRole+"] not exist into InheritanceRoles declaration"; | ||
} | ||
if ((identityRole in self.roles) === false) { | ||
throw 'This role[' + identityRole + '] not exist into InheritanceRoles declaration'; | ||
} | ||
if(stateRolesGranted.indexOf(identityRole) !== -1){ | ||
granted = true; | ||
} | ||
if (stateRolesGranted.indexOf(identityRole) !== -1) { | ||
granted = true; | ||
} | ||
for(var ii in self.roles[identityRole]) { | ||
if(stateRolesGranted.indexOf(self.roles[identityRole][ii]) !== -1){ | ||
granted = true; | ||
} | ||
} | ||
for (var ii in self.roles[identityRole]) { | ||
if (stateRolesGranted.indexOf(self.roles[identityRole][ii]) !== -1) { | ||
granted = true; | ||
} | ||
} | ||
return granted; | ||
}; | ||
return granted; | ||
}; | ||
this.$get = ['$q', '$rootScope', '$injector', function($q, $rootScope, $injector){ | ||
var crAcl = {}; | ||
this.$get = ['$q', '$rootScope', '$injector', function ($q, $rootScope, $injector) { | ||
var crAcl = {}; | ||
/** | ||
* Configure roles tree | ||
* @param arrat roles | ||
*/ | ||
crAcl.setInheritanceRoles = function(roles){ | ||
angular.forEach(roles, function(inheritance, roleName){ | ||
if(roleName == "ROLE_USER" && roleName == "ROLE_GUEST"){ | ||
throw roleName+" is a reserved world because is a father of ROLE, you can not override it"; | ||
} | ||
self.roles[roleName] = inheritance; | ||
}); | ||
}; | ||
/** | ||
* Configure roles tree | ||
* @param arrat roles | ||
*/ | ||
crAcl.setInheritanceRoles = function (roles) { | ||
angular.forEach(roles, function (inheritance, roleName) { | ||
if (roleName == 'ROLE_USER' && roleName == 'ROLE_GUEST') { | ||
throw roleName + ' is a reserved world because is a father of ROLE, you can not override it'; | ||
} | ||
self.roles[roleName] = inheritance; | ||
}); | ||
}; | ||
/** | ||
* Set route name for redirect after unauthorized operation | ||
*/ | ||
crAcl.setRedirect = function(redirectStateName){ | ||
self.redirect = redirectStateName; | ||
}; | ||
/** | ||
* Set route name for redirect after unauthorized operation | ||
*/ | ||
crAcl.setRedirect = function (redirectStateName) { | ||
self.redirect = redirectStateName; | ||
}; | ||
/** | ||
* Set Role | ||
* @param string role | ||
*/ | ||
crAcl.setRole = function(role){ | ||
self.identityRole = role; | ||
}; | ||
/** | ||
* Set Role | ||
* @param string role | ||
*/ | ||
crAcl.setRole = function (role) { | ||
self.identityRole = role; | ||
}; | ||
/** | ||
* Return your role | ||
* @return string | ||
*/ | ||
crAcl.getRole = function(){ | ||
if(self.identityRole === undefined){ | ||
return "ROLE_GUEST"; | ||
} | ||
return self.identityRole; | ||
}; | ||
/** | ||
* Get all Roles | ||
* @param array | ||
*/ | ||
crAcl.getRoles = function () { | ||
return self.roles; | ||
}; | ||
var afterChangeStart = function(event, toState, toParams, fromState, fromParams) { | ||
if(!toState.data || !toState.data.is_granted){ | ||
return crAcl; | ||
} | ||
if(toState.data.is_granted[0] === "*"){ | ||
return crAcl; | ||
} | ||
var is_allowed = (toState.data.is_granted !== undefined) ? toState.data.is_granted : ["ROLE_GUEST"]; | ||
var isGranted = self.isGranted(crAcl.getRole(), is_allowed); | ||
return isGranted; | ||
}; | ||
/** | ||
* Return your role | ||
* @return string | ||
*/ | ||
crAcl.getRole = function () { | ||
if (self.identityRole === undefined) { | ||
return 'ROLE_GUEST'; | ||
} | ||
return self.identityRole; | ||
}; | ||
$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) { | ||
$injector.invoke(["$state", function($state) { | ||
var isGranted = afterChangeStart(event, toState, toParams, fromState, fromParams); | ||
var afterChangeStart = function (event, toState, toParams, fromState, fromParams) { | ||
if (!toState.data || !toState.data.is_granted) { | ||
return crAcl; | ||
} | ||
if (toState.data.is_granted[0] === '*') { | ||
return crAcl; | ||
} | ||
var is_allowed = (toState.data.is_granted !== undefined) ? toState.data.is_granted : ['ROLE_GUEST']; | ||
var isGranted = self.isGranted(crAcl.getRole(), is_allowed); | ||
return isGranted; | ||
}; | ||
if(!isGranted && self.redirect !== false){ | ||
event.preventDefault(); | ||
if(self.redirect != toState.name) { | ||
$state.go(self.redirect); | ||
} | ||
} | ||
}]); | ||
}); | ||
$rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) { | ||
$injector.invoke(['$state', function ($state) { | ||
var isGranted = afterChangeStart(event, toState, toParams, fromState, fromParams); | ||
$rootScope.$on('$routeChangeStart', function(event, toState, toParams, fromState, fromParams) { | ||
$injector.invoke(["$location", function($location) { | ||
var isGranted = afterChangeStart(event, toState, toParams, fromState, fromParams); | ||
if (!isGranted && self.redirect !== false) { | ||
event.preventDefault(); | ||
if (self.redirect != toState.name) { | ||
$state.go(self.redirect); | ||
} | ||
} | ||
}]); | ||
}); | ||
if(!isGranted && self.redirect !== false){ | ||
event.preventDefault(); | ||
if(self.redirect != toState.name) { | ||
$location.path(self.redirect); | ||
} | ||
} | ||
}]); | ||
}); | ||
$rootScope.$on('$routeChangeStart', function (event, toState, toParams, fromState, fromParams) { | ||
$injector.invoke(['$location', function ($location) { | ||
var isGranted = afterChangeStart(event, toState, toParams, fromState, fromParams); | ||
return crAcl; | ||
if (!isGranted && self.redirect !== false) { | ||
event.preventDefault(); | ||
if (self.redirect != toState.name) { | ||
$location.path(self.redirect); | ||
} | ||
} | ||
}]); | ||
}); | ||
}]; | ||
}]) | ||
.directive("crGranted", ['crAcl', '$animate', function(acl, $animate){ | ||
return { | ||
restrict: "A", | ||
replace: false, | ||
transclude: 'element', | ||
terminal: true, | ||
link: function(scope, elem, attr, ctrl, $transclude){ | ||
var content = false; | ||
$transclude(function(clone, newScope) { | ||
childScope = newScope; | ||
clone[clone.length++] = document.createComment(' end crGranted: ' + attr.crGranted + ' '); | ||
block = { | ||
clone: clone | ||
}; | ||
content = clone; | ||
}); | ||
return crAcl; | ||
}]; | ||
}]) | ||
.directive('crGranted', ['crAcl', '$animate', function (acl, $animate) { | ||
return { | ||
restrict: 'A', | ||
replace: false, | ||
transclude: 'element', | ||
terminal: true, | ||
link: function (scope, elem, attr, ctrl, $transclude) { | ||
var content = false; | ||
$transclude(function (clone, newScope) { | ||
childScope = newScope; | ||
clone[clone.length++] = document.createComment(' end crGranted: ' + attr.crGranted + ' '); | ||
block = { | ||
clone: clone | ||
}; | ||
content = clone; | ||
}); | ||
scope.$watch(function() { | ||
return acl.getRole(); | ||
}, function(newV, oldV){ | ||
var allowedRoles = attr.crGranted.split(","); | ||
if(allowedRoles.indexOf(acl.getRole()) != -1) { | ||
$animate.enter(content, elem.parent(), elem); | ||
} | ||
else { | ||
if(content) { | ||
content.remove(); | ||
} | ||
} | ||
}); | ||
} | ||
}; | ||
}]); | ||
scope.$watch(function () { | ||
return acl.getRole(); | ||
}, function (newV, oldV) { | ||
if (shouldBeShown()) { | ||
$animate.enter(content, elem.parent(), elem); | ||
} else { | ||
if (content) { | ||
content.remove(); | ||
} | ||
} | ||
}); | ||
var shouldBeShown = function () { | ||
var allowedRoles = attr.crGranted.split(','); | ||
var roles = acl.getRoles(); | ||
var currentRole = acl.getRole(); | ||
if (allowedRoles.indexOf(currentRole) != -1) { | ||
return true; | ||
} else { | ||
var granted = false; | ||
angular.forEach(allowedRoles, function (role) { | ||
if (angular.isDefined(roles[currentRole])) { | ||
var inheritedRoles = roles[currentRole]; | ||
if (inheritedRoles.indexOf(role) > -1) { | ||
granted = true; | ||
} | ||
} | ||
}); | ||
return granted; | ||
} | ||
}; | ||
} | ||
}; | ||
}]); |
@@ -1,21 +0,21 @@ | ||
module.exports = function(config) { | ||
config.set({ | ||
basePath: '', | ||
frameworks: ['jasmine'], | ||
files: [ | ||
'bower_components/angular/angular.js', | ||
'bower_components/angular-animate/angular-animate.js', | ||
'bower_components/angular-ui-router/release/angular-ui-router.js', | ||
'bower_components/angular-mocks/angular-mocks.js', | ||
'cr-acl.js', | ||
'spec/*.js' | ||
], | ||
reporters: ['progress'], | ||
port: 9876, | ||
colors: true, | ||
logLevel: config.LOG_INFO, | ||
autoWatch: true, | ||
browsers: ['PhantomJS'], | ||
singleRun: false | ||
}); | ||
module.exports = function (config) { | ||
config.set({ | ||
basePath: '', | ||
frameworks: ['jasmine'], | ||
files: [ | ||
'node_modules/angular/angular.js', | ||
'node_modules/angular-animate/angular-animate.js', | ||
'node_modules/@uirouter/angularjs/release/angular-ui-router.js', | ||
'node_modules/angular-mocks/angular-mocks.js', | ||
'cr-acl.js', | ||
'spec/*.js' | ||
], | ||
reporters: ['progress'], | ||
port: 9876, | ||
colors: true, | ||
logLevel: config.LOG_INFO, | ||
autoWatch: true, | ||
browsers: ['PhantomJS'], | ||
singleRun: false | ||
}); | ||
}; |
{ | ||
"name": "cr-acl", | ||
"version": "0.5.1", | ||
"version": "0.5.2", | ||
"dependencies": { | ||
"angular": "^1.5", | ||
"angular-animate": "^1.5" | ||
}, | ||
"devDependencies": { | ||
"karma": "~0.12", | ||
"karma-cli": "~0", | ||
"karma-phantomjs-launcher": "~0", | ||
"karma-jasmine": "~0.2.*", | ||
"phantomjs": "~1.9", | ||
"bower": "~1" | ||
"@uirouter/angularjs": "^1.0.6", | ||
"angular-mocks": "^1.5", | ||
"jasmine-core": "^2.8.0", | ||
"karma": "^1.3", | ||
"karma-jasmine": "^1.0", | ||
"karma-phantomjs-launcher": "^1.0" | ||
}, | ||
@@ -12,0 +16,0 @@ "scripts": { |
@@ -10,5 +10,5 @@ # crAcl | ||
You can use bower | ||
You can use npm | ||
```bash | ||
bower install cr-acl | ||
npm install cr-acl | ||
``` | ||
@@ -18,4 +18,4 @@ add to your html: | ||
```html | ||
<script src="bower_components/angular-ui-router/release/angular-ui-router.js"></script> | ||
<script src="bower_components/cr-acl/cr-acl.js"></script> | ||
<script src="node_modules/@uirouter/angularjs/release/angular-ui-router.js"></script> | ||
<script src="node_modules/cr-acl/cr-acl.js"></script> | ||
``` | ||
@@ -25,9 +25,6 @@ then inject it in your app: | ||
```javascript | ||
angular.module( | ||
'ngtest', | ||
[ | ||
'ui.router', | ||
'cr.acl' | ||
] | ||
) | ||
angular.module('ngtest', [ | ||
'ui.router', | ||
'cr.acl' | ||
]) | ||
``` | ||
@@ -68,3 +65,3 @@ | ||
.run(['crAcl', function run(crAcl) { | ||
crAcl.setRedirct("your-login-state-name"); | ||
crAcl.setRedirect("your-login-state-name"); | ||
}]) | ||
@@ -71,0 +68,0 @@ ``` |
@@ -1,32 +0,32 @@ | ||
describe('crGranted directive', function() { | ||
var $compile, | ||
element, | ||
$scope; | ||
describe('crGranted directive', function () { | ||
var $compile, | ||
element, | ||
$scope; | ||
beforeEach(module('ui.router')); | ||
beforeEach(module('ngAnimate')); | ||
beforeEach(module('cr.acl')); | ||
beforeEach(module('ui.router')); | ||
beforeEach(module('ngAnimate')); | ||
beforeEach(module('cr.acl')); | ||
afterEach(function() { | ||
delete element; | ||
}); | ||
afterEach(function () { | ||
delete element; | ||
}); | ||
beforeEach(inject(function(_$compile_, _$rootScope_, _crAcl_){ | ||
$compile = _$compile_; | ||
$scope = _$rootScope_.$new(); | ||
element = $compile('<div></div>')($scope); | ||
})); | ||
beforeEach(inject(function (_$compile_, _$rootScope_, _crAcl_) { | ||
$compile = _$compile_; | ||
$scope = _$rootScope_.$new(); | ||
element = $compile('<div></div>')($scope); | ||
})); | ||
it("My role is granted", function(){ | ||
element.append($compile("<div cr-granted='ROLE_GUEST'>Ciao</div>")($scope)); | ||
$scope.$apply(); | ||
expect(element.html()).toContain("Ciao"); | ||
}); | ||
it("My role is granted", function () { | ||
element.append($compile("<div cr-granted='ROLE_GUEST'>Ciao</div>")($scope)); | ||
$scope.$apply(); | ||
expect(element.html()).toContain("Ciao"); | ||
}); | ||
it("I am not granted", function(){ | ||
element.append($compile("<div cr-granted='ROLE_USER'>Ciao</div>")($scope)); | ||
$scope.$apply(); | ||
expect(element.html()).not.toContain("Ciao"); | ||
}); | ||
it("I am not granted", function () { | ||
element.append($compile("<div cr-granted='ROLE_USER'>Ciao</div>")($scope)); | ||
$scope.$apply(); | ||
expect(element.html()).not.toContain("Ciao"); | ||
}); | ||
}); |
@@ -1,10 +0,10 @@ | ||
describe("CrAcl is initializable", function(){ | ||
describe("CrAcl is initializable", function () { | ||
beforeEach(function(){ | ||
module('cr.acl'); | ||
}); | ||
beforeEach(function () { | ||
module('cr.acl'); | ||
}); | ||
it('Exists a config object', inject(["cr-acl.config", function(config) { | ||
expect(config).toBeDefined(); | ||
}])); | ||
it('Exists a config object', inject(["cr-acl.config", function (config) { | ||
expect(config).toBeDefined(); | ||
}])); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
11881
217
2
8
117
+ Addedangular@^1.5
+ Addedangular-animate@^1.5
+ Addedangular@1.8.3(transitive)
+ Addedangular-animate@1.8.3(transitive)