Skip to content

Commit 1a3a3c9

Browse files
committed
README improved
1 parent 588ab4f commit 1a3a3c9

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed

README.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,129 @@ then the parking attendant is the requesting role, and the car is the resource,
1717

1818
Through the specification and use of an ACL, an application may control how roles are granted access to resources.
1919

20+
## Quick Examples
21+
First you need to install this library :) It's available via bower or npm:
22+
- `npm install --save angularjs-acl`
23+
- `bower install --save angularjs-acl`
24+
and add a `<script>` to your `index.html`:
25+
```html
26+
<!-- For bower -->
27+
<script src="/bower_components/angularjs-acl/dist/acl.js"></script>
28+
29+
<!-- For npm -->
30+
<script src="/node_modules/angularjs-acl/dist/acl.js"></script>
31+
```
32+
33+
#### Set Data
34+
Add `ng-acl` to your app module's dependencies & setup the `AclService` in `run()` block.
35+
```js
36+
angular.module('myApp', ['ng-acl']);
37+
38+
app.run(['AclService', function (AclService) {
39+
//All these actions you also can do in the middle of app execution
40+
AclService.addRole('guest');
41+
AclService.addRole('user', 'guest');
42+
AclService.addRole('admin', 'user');
43+
44+
AclService.addResource('Post');
45+
AclService.addResource('Users');
46+
AclService.addResource('AdminPanel');
47+
48+
AclService.allow('guest', 'Post', 'view');
49+
50+
//Users can edit edit their own posts & view it because user inherits all guest permissions
51+
AclService.allow('user', 'Post', 'edit', function (role, resource, privilege) {
52+
return resource.authorId === role.id;
53+
});
54+
55+
//Full access to all actions that available for Post
56+
AclService.allow('admin', 'Post');
57+
AclService.allow('admin', 'AdminPanel');
58+
59+
//Let's assume that you have some user object that implements AclRoleInterface. This is optional feature.
60+
var user = {
61+
id: 1,
62+
name: 'Duck',
63+
getRoles: function () {
64+
return ['user'];
65+
},
66+
};
67+
AclService.setUserIdentity(user);
68+
}]);
69+
```
70+
71+
#### Protect a route
72+
73+
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.
74+
75+
However, when the user goes to `/posts/2`, route will work as normal, since the user has permission.
76+
77+
```js
78+
app.config(['$routeProvider', function ($routeProvider) {
79+
$routeProvider
80+
.when('/admin_panel', {
81+
resolve : {
82+
'acl' : ['$q', 'AclService', function($q, AclService){
83+
if(AclService.can('AdminPanel')){
84+
// Has proper permissions
85+
return true;
86+
} else {
87+
// Does not have permission
88+
return $q.reject('Unauthorized');
89+
}
90+
}]
91+
}
92+
});
93+
.when('/posts/:id', {
94+
resolve : {
95+
'acl' : ['$q', 'AclService', function($q, AclService){
96+
if (AclService.can('Post', 'view')) {
97+
return true;
98+
} else {
99+
return $q.reject('Unauthorized');
100+
}
101+
}]
102+
}
103+
});
104+
}]);
105+
106+
app.run(['$rootScope', '$location', function ($rootScope, $location) {
107+
// If the route change failed due to our "Unauthorized" error, redirect them
108+
$rootScope.$on('$routeChangeError', function(current, previous, rejection){
109+
if(rejection === 'Unauthorized'){
110+
$location.path('/');
111+
}
112+
})
113+
}]);
114+
```
115+
116+
#### Manipulate a Template
117+
118+
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.
119+
120+
###### Controller
121+
122+
```js
123+
app.controller('DemoCtrl', ['$scope', 'AclService', function ($scope, AclService) {
124+
$scope.can = AclService.can;
125+
$scope.post = {
126+
id: 1,
127+
authorId: 1,
128+
name: 'Demo post',
129+
getResourceId: function () { //AclResourceInterface implementation
130+
return 'Post';
131+
}
132+
};
133+
}]);
134+
```
135+
136+
###### Template
137+
138+
```html
139+
<h1>{{ post.name }}</h1>
140+
<a ng-href="posts/{{ post.id }}/edit" ng-show="can(post, 'edit')">Edit</a>
141+
```
142+
20143
## How secure is this?
21144

22145
A great analogy to ACL's in JavaScript would be form validation in JavaScript. Just like form validation, ACL's in the

0 commit comments

Comments
 (0)