Skip to content

Instantly share code, notes, and snippets.

@gupta-pratik
Last active April 27, 2017 07:23
Show Gist options
  • Save gupta-pratik/759a692d1f7df173624d92f0d5feea43 to your computer and use it in GitHub Desktop.
Save gupta-pratik/759a692d1f7df173624d92f0d5feea43 to your computer and use it in GitHub Desktop.
AngularJs Service for adding authorization / permission to the pages.
'use strict';
angular.module('permission-module')
.factory('permissions', function ($rootScope) {
var permissionList;
return {
setPermissions: function (permissions) {
permissionList = permissions;
$rootScope.$broadcast('permissionsChanged');
},
hasPermission: function (permissions) {
var hasPermission = false;
if (!permissionList) {
if (localStorage.roles && typeof(localStorage.roles).toLowerCase() == 'string')
permissionList = [localStorage.roles];
else
permissionList = localStorage.roles;
}
if (!permissionList) return false;
angular.forEach(permissions, function (permission, index) {
if (!hasPermission) {
permission = permission.trim();
return angular.forEach(permissionList, function (item) {
if (item) {
hasPermission = hasPermission || item.trim() === permission;
}
});
}
});
return hasPermission;
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment