Angular ACL (Access Control List) is a service that allows you to protect/show content based on the current user's assigned role(s), and those role(s) permissions (abilities). So, if the current user has a "moderator" role, and a moderator can "ban_users", then the current user can "ban_users".
Common uses include:
- Manipulate templates based on role/permissions
- Prevent routes that should not be viewable to user
A great analogy to ACL's in JavaScript would be form validation in JavaScript. Just like form validation, ACL's in the browser can be tampered with. However, just like form validation, ACL's are really useful and provide a better experience for the user and the developer. Just remember, any sensitive data or actions should require a server (or similar) as the final authority.
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, and submits the form. The form data is sent to the server and the user is greeted with an "Access Denied: Unauthorized" message, because the server also checked to make sure that the user had the correct permissions.
Any sensitive data or actions should integrate a server check like this example.
Setup the AclService
in your app module's run()
block.
app.run(['AclService', function (AclService) {
// Set the ACL data. Normally, you'd fetch this from an API or something.
// The data should have the roles as the property names,
// with arrays listing their permissions as their value.
var aclData = {
guest: ['login'],
member: ['logout', 'view_content'],
admin: ['logout', 'view_content', 'manage_content']
}
AclService.setAbilities(aclData);
// Attach the member role to the current user
AclService.attachRole('member');
}]);
If the current user tries to go to the /manage
route, they will be redirected because the current user is a member
, and manage_content
is not one of a member role's abilities.
However, when the user goes to /content
, route will work as normal, since the user has permission. If the user was not a member
, but a guest
, then they would not be able to see the content
route either, based on the data we set above.
app.config(['$routeProvider', function ($routeProvider) {
$routeProvider
.when('/manage', {
resolve : {
'acl' : ['$q', 'AclService', function($q, AclService){
if(AclService.can('manage_content')){
// Has proper permissions
return true;
} else {
// Does not have permission
return $q.reject('Unauthorized');
}
}]
}
});
.when('/content', {
resolve : {
'acl' : ['$q', 'AclService', function($q, AclService){
if(AclService.can('view_content')){
// Has proper permissions
return true;
} else {
// Does not have permission
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(event, current, previous, rejection){
if(rejection === 'Unauthorized'){
$location.path('/');
}
})
}]);
The edit link in the template below will not show, because the current user is a member
, and manage_content
is not one of a member role's abilities.
app.controller('DemoCtrl', ['$scope', 'AclService', function ($scope, AclService) {
$scope.can = AclService.can;
$scope.id = 22;
$scope.title = 'My Demo Title';
}]);
<h1>{{ title }}</h1>
<a ng-href="edit/{{ id }}" ng-show="can('manage_content')">Edit</a>
Install with bower
:
bower install angular-acl
Add a <script>
to your index.html
:
<script src="/bower_components/angular-acl/angular-acl.js"></script>
And add mm.acl
as a dependency for your app:
angular.module('myApp', ['mm.acl']);
You can modify the configuration by extending the config object during the Angular configuration phase using the config()
method on the AclServiceProvider
.
app.config(['AclServiceProvider', function (AclServiceProvider) {
var myConfig = {
storage: 'localStorage',
storageKey: 'AppAcl'
};
AclServiceProvider.config(myConfig);
}]);
Property | Default | Description |
---|---|---|
storage |
"sessionStorage" |
"sessionStorage" , "localStorage" , false . Where you want to persist your ACL data. If you would prefer not to use web storage, then you can pass a value of false , and data will be reset on next page refresh (next time the Angular app has to bootstrap) |
storageKey |
"AclService" |
The key that will be used when storing data in web storage |
Restore data from web storage.
boolean - true if web storage existed, false if it didn't
app.run(['AclService', function (AclService) {
// Attempt to load from web storage
if (!AclService.resume()) {
// Web storage record did not exist, we'll have to build it from scratch
// Get the user role, and add it to AclService
var userRole = fetchUserRoleFromSomewhere();
AclService.addRole(userRole);
// Get ACL data, and add it to AclService
var aclData = fetchAclFromSomewhere();
AclService.setAbilities(aclData);
}
}]);
You can also run resume()
in the config
phase, if you need the app to load the ACL data from web storage earlier in the app bootstrap process (e.g. before $routeProvider
resolves the first route).
app.config(['AclServiceProvider', function (AclServiceProvider) {
AclServiceProvider.resume();
}]);
Remove all data from web storage.
Attach a role to the current user. A user can have multiple roles.
Param | Type | Example | Details |
---|---|---|---|
role |
string | "admin" |
The role label |
Remove a role from the current user
Param | Type | Example | Details |
---|---|---|---|
role |
string | "admin" |
The role label |
Remove all roles from current user
Get all of the roles attached to the user
array
Check if the current user has role(s) attached. If an array is given, all roles must be attached. To check if any roles in an array are attached see the hasAnyRole()
method.
Param | Type | Example | Details |
---|---|---|---|
role |
string/array | "admin" |
The role label, or an array of role labels |
boolean
Check if the current user has any of the given roles attached. To check if all roles in an array are attached see the hasRole()
method.
Param | Type | Example | Details |
---|---|---|---|
roles |
array | ["admin","user"] |
Array of role labels |
boolean
Set the abilities object (overwriting previous abilities).
Param | Type | Details |
---|---|---|
abilities |
object | Each property on the abilities object should be a role. Each role should have a value of an array. The array should contain a list of all of the role's abilities. |
var abilities = {
guest: ['login'],
user: ['logout', 'view_content'],
admin: ['logout', 'view_content', 'manage_content']
}
AclService.setAbilities(abilities);
Add an ability to a role
Param | Type | Example | Details |
---|---|---|---|
role |
string | "admin" |
The role label |
ability |
string | "create_users" |
The ability/permission label |
Does current user have permission to do the given ability?
boolean
// Setup some abilities
AclService.addAbility('moderator', 'ban_users');
AclService.addAbility('admin', 'create_users');
// Add moderator role to the current user
AclService.attachRole('moderator');
// Check if the current user has these permissions
AclService.can('ban_users'); // returns true
AclService.can('create_users'); // returns false
Show and element if truthy, otherwise hide it.
Only user's that have the edit_posts
permission would see the button.
<button acl-show="edit_posts">Edit Post</button>
This is essentially a shortcut instead of having to type out an ngShow
like this...
<button ng-show="$ctrl.AclService.can('edit_posts')">Edit Post</button>
The MIT License
Angular ACL Copyright (c) 2016 Mike McLin
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.