Comparing version 0.1.1 to 0.1.2
{ | ||
"name": "js-acl", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Provides a lightweight and flexible ACL implementation for privileges management in JS/NodeJS", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/StyleT/js-acl", |
133
README.md
@@ -24,4 +24,4 @@ # JS ACL | ||
- `npm install --save js-acl` | ||
- `bower install --save js-acl` | ||
and add a `<script>` to your `index.html`: | ||
- `bower install --save js-acl` and add a `<script>` to your `index.html`: | ||
```html | ||
@@ -34,113 +34,42 @@ <!-- For bower --> | ||
``` | ||
```js | ||
const Acl = require('js-acl'); | ||
``` | ||
#### Set Data | ||
Add `ng-acl` to your app module's dependencies & setup the `AclService` in `run()` block. | ||
```js | ||
angular.module('myApp', ['ng-acl']); | ||
const AclService = require('js-acl'); | ||
app.run(['AclService', function (AclService) { | ||
//All these actions you also can do in the middle of app execution | ||
AclService.addRole('guest'); | ||
AclService.addRole('user', 'guest'); | ||
AclService.addRole('admin', 'user'); | ||
//All these actions you also can do in the middle of app execution | ||
AclService.addRole('guest'); | ||
AclService.addRole('user', 'guest'); | ||
AclService.addRole('admin', 'user'); | ||
AclService.addResource('Post'); | ||
AclService.addResource('Users'); | ||
AclService.addResource('AdminPanel'); | ||
AclService.addResource('Post'); | ||
AclService.addResource('Users'); | ||
AclService.addResource('AdminPanel'); | ||
AclService.allow('guest', 'Post', 'view'); | ||
AclService.allow('guest', 'Post', 'view'); | ||
//Users can edit edit their own posts & view it because user inherits all guest permissions | ||
AclService.allow('user', 'Post', 'edit', function (role, resource, privilege) { | ||
return resource.authorId === role.id; | ||
}); | ||
//Users can edit edit their own posts & view it because user inherits all guest permissions | ||
AclService.allow('user', 'Post', 'edit', function (role, resource, privilege) { | ||
return resource.authorId === role.id; | ||
}); | ||
//Full access to all actions that available for Post | ||
AclService.allow('admin', 'Post'); | ||
AclService.allow('admin', 'AdminPanel'); | ||
//Full access to all actions that available for Post | ||
AclService.allow('admin', 'Post'); | ||
AclService.allow('admin', 'AdminPanel'); | ||
//Let's assume that you have some user object that implements AclRoleInterface. This is optional feature. | ||
var user = { | ||
id: 1, | ||
name: 'Duck', | ||
getRoles: function () { | ||
return ['user']; | ||
}, | ||
}; | ||
AclService.setUserIdentity(user); | ||
}]); | ||
//Let's assume that you have some user object that implements AclRoleInterface. This is optional feature. | ||
var user = { | ||
id: 1, | ||
name: 'Duck', | ||
getRoles: function () { | ||
return ['user']; | ||
}, | ||
}; | ||
AclService.setUserIdentity(user); | ||
``` | ||
#### Protect a route | ||
If the current user tries to go to the `/admin_panel` route, they will be redirected because the current user is a `user`, and `AdminPanel` is not one of a member role's abilities. | ||
However, when the user goes to `/posts/2`, route will work as normal, since the user has permission. | ||
```js | ||
app.config(['$routeProvider', function ($routeProvider) { | ||
$routeProvider | ||
.when('/admin_panel', { | ||
resolve : { | ||
'acl' : ['$q', 'AclService', function($q, AclService){ | ||
if(AclService.can('AdminPanel')){ | ||
// Has proper permissions | ||
return true; | ||
} else { | ||
// Does not have permission | ||
return $q.reject('Unauthorized'); | ||
} | ||
}] | ||
} | ||
}); | ||
.when('/posts/:id', { | ||
resolve : { | ||
'acl' : ['$q', 'AclService', function($q, AclService){ | ||
if (AclService.can('Post', 'view')) { | ||
return true; | ||
} else { | ||
return $q.reject('Unauthorized'); | ||
} | ||
}] | ||
} | ||
}); | ||
}]); | ||
app.run(['$rootScope', '$location', function ($rootScope, $location) { | ||
// If the route change failed due to our "Unauthorized" error, redirect them | ||
$rootScope.$on('$routeChangeError', function(current, previous, rejection){ | ||
if(rejection === 'Unauthorized'){ | ||
$location.path('/'); | ||
} | ||
}) | ||
}]); | ||
``` | ||
#### Manipulate a Template | ||
The edit link in the template below will be shown, because the current user is a `user`, and `Post` which was created by our user is one of a his role's abilities. | ||
###### Controller | ||
```js | ||
app.controller('DemoCtrl', ['$scope', 'AclService', function ($scope, AclService) { | ||
$scope.can = AclService.can; | ||
$scope.post = { | ||
id: 1, | ||
authorId: 1, | ||
name: 'Demo post', | ||
getResourceId: function () { //AclResourceInterface implementation | ||
return 'Post'; | ||
} | ||
}; | ||
}]); | ||
``` | ||
###### Template | ||
```html | ||
<h1>{{ post.name }}</h1> | ||
<a ng-href="posts/{{ post.id }}/edit" ng-show="can(post, 'edit')">Edit</a> | ||
``` | ||
## How secure is this if I'm using it in browser? | ||
@@ -154,3 +83,3 @@ | ||
The current user has a role of "guest". A guest is not able to "create_users". However, this sneaky guest is clever | ||
The current user has a role of "guest". A guest is not able to "create_users". However, this sneaky guest is clever | ||
enough to tamper with the system and give themselves that privilege. So, now that guest is at the "Create Users" page, | ||
@@ -157,0 +86,0 @@ and submits the form. The form data is sent the the server and the user is greeted with an "Access Denied: Unauthorized" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
113184
87