Dynamic Adding Permissions
Closed this issue · 6 comments
Hello ,
I am dynamically adding permissions. I defined I runner js class
var permissionRunner = function (PermPermissionStore, permissionService, userService, $log) {
var permissions = [];
var setPermPermission = function (permissionList) {
PermPermissionStore.defineManyPermissions(permissionList, function (permissionName) {
return _.contains(permissionList, permissionName);
});
};
//another logic and after it finishes calls setPermPermission method
// setPermPermission(permissions);
}
But when I call
<button permission permission-only="'somename'" ..
It does not work
But
var permissions = ["somename"];
PermPermissionStore.defineManyPermissions(permissionList, function (permissionName) {
return _.contains(permissionList, permissionName);
});
This works
At first glance var permissions = [];
creates empty list. Maybe that is your source of the problem if you asynchronously populate it and setPermPermission is invoked before the data are fetched from the server?
I am trying to set inside successCallBack
var promiseGetUserRoles = userService.getRolesOfCurrentUser();
promiseGetUserRoles.then(function (response) {
....
setPermPermission(permissions);
....
This code is to drafty to say what might be wrong. But double check for async implementation of your code. For sure the problem lies there.
app.js
appModule.run(permissionRunner);
permission-runner.js
/*global define */
define([], function () {
'use strict';
var permissionRunner = function (PermPermissionStore, permissionService, userService, $log) {
var permissions = [];
var userInfo = null;
var userRoleList = [];
var rolePermissionList = [];
var setPermPermission = function () {
PermPermissionStore.defineManyPermissions(permissions, function (permissionName) {
return _.contains(permissions, permissionName);
});
};
var setPermissions = function (roleName) {
var permissionsByRole = rolePermissionList[roleName];
if (permissionsByRole) {
for (var index = 0; index < permissionsByRole.length; index++) {
if (permissions.indexOf(permissionsByRole[index]) === -1) {
permissions.push(permissionsByRole[index]);
}
}
}
}
var getRolesOfCurrentUser = function () {
var promiseGetUserRoles = userService.getRolesOfCurrentUser();
promiseGetUserRoles.then(function (response) {
var userRoleInfo = response;
for (var index = 0; index < userRoleInfo.length; index++) {
if (userRoleInfo[index] && userRoleInfo[index].name && userRoleList.indexOf(userRoleInfo[index].name) === -1) {
userRoleList.push(userRoleInfo[index].name);
setPermissions(userRoleInfo[index].name);
}
}
setPermPermission();
}, function (error) {
$log.error("An error occured while getting user role info", error);
});
};
var getRolePermissions = function () {
var promisePermissions = permissionService.getRolePermissions();
promisePermissions.then(function (response) {
rolePermissionList = response.data;
getRolesOfCurrentUser();
}, function (error) {
$log.error("An error occured while getting permission-role info", error);
});
}
getRolePermissions();
};
permissionRunner.$inject = ['PermPermissionStore', 'permissionService', 'userService', '$log'];
return permissionRunner;
});
user-service
var getRolesOfCurrentUser = function(){
var deferred = $q.defer();
if(!$rootScope.rolesOfUser){
$http({
method: 'GET',
url: ''
}).then(function successCallback(response) {
$rootScope.rolesOfUser = response.data;
deferred.resolve(response.data);
}, function errorCallback(response) {
$log.error("Error in getRolesOfCurrentUser "+ response);
deferred.reject(response);
})
}else{
deferred.resolve($rootScope.rolesOfUser);
}
return deferred.promise;
}
permission-service
var getRolePermissions = function () {
var deferred = $q.defer();
$http({
method: 'GET',
url: ''
}).then(function successCallback(response) {
if (response) {
deferred.resolve(response);
} else {
deferred.reject(response);
}
}, function errorCallback(error) {
deferred.reject(error);
$log.error(error);
});
return deferred.promise;
};
Do you reload your ui-router states after adding new permissions? Cuz maybe this the source of the problem. Your views don't magically gets that your permissions in services have changed.
http://stackoverflow.com/questions/21714655/reloading-current-state-refresh-data
I solved it with using state provider resolver. And getting necessary async data before state is loaded. I have not used $state.reload(); maybe it can be other solution for me. Thanks for your efforts.