I am writing a permission function in angular js factory method which checks user role. if user object have such role then result should be true else it should return false. my user object is:
administrator:true
company_admin:true
registered:true
manager:true
contact:"987654321" email:"abcd@gmail.com" name:"Anand"
And My Permission function in app model factory is:
$appModel.checkPermission:function($role, $requireAll) {
var data = $rootScope.authUser;
if (angular.isArray($role)) {
angular.forEach($role, function (value, key) {
var hasRole = $appModel.checkPermission(value); //Recursive Function
if (hasRole && !$requireAll) {
return true;
} else if (!hasRole && $requireAll) {
return false;
}
});
return $requireAll;
} else {
if (data.hasOwnProperty($role) && data[$role])
return true;
}
return false;
}
There are following cases which i am checking: 1. If there is multiple role in that array with 'required all' condition, then check that multiple roles present in user object and return true otherwise false.
$result= $appModel.checkPermission([registered, company_admin], true);
2. If there is multiple role in that array, then check those multiple roles in are present in user object and if any role get matched, then return true otherwise return false.
$result= $appModel.checkPermission([administrator, company_admin,manager,agent], false);
3. If there is single role in that array, then check that role is present in user object and if that role get matched, then return true otherwise return false.
$result= $appModel.checkPermission(register, false);
The above code is running fine for single role checking but in case of multiple role checking with 'required all' or not 'required all' condition, it does not work properly and return false.