Last active
April 27, 2017 07:23
-
-
Save gupta-pratik/759a692d1f7df173624d92f0d5feea43 to your computer and use it in GitHub Desktop.
AngularJs Service for adding authorization / permission to the pages.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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