As title says i want copy role resources from one to other.
For example:
I give one of my role id which is dynamically and then i get all resouces for that role id and assign that resources to other roles.
As title says i want copy role resources from one to other.
For example:
I give one of my role id which is dynamically and then i get all resouces for that role id and assign that resources to other roles.
Here we go:
function copyResources($sourceRoleId, $destinationRoleId) {
// Delete old resources for destination role
Mage::getResourceModel('admin/rules_collection')
->addFieldToFilter('role_id', $destinationRoleId)
->walk('delete');
// Copy resources
$resources = Mage::getResourceModel('admin/rules_collection')
->addFieldToFilter('role_id', $sourceRoleId);
foreach ($resources as $resource) {
$resource
->unsetData('rule_id')
->setData('role_id', $destinationRoleId)
->save();
}
}
If you are looking for a quick way to copy all of the permissions from one role to another, here is a quick Javascript snippet you can use in the console:
// Execute the following in the console from the role resources tab you want to copy:
var elems = document.querySelectorAll('input[type="checkbox"]');
var checkedPerms = [];
for (var i = 0; i < elems.length; i++) {
var isChecked = elems[i].checked;
if(isChecked && elems[i].id) {
checkedPerms.push(elems[i].id);
}
}
console.log(JSON.stringify(checkedPerms));
// Place the output of console.log into the var below and execute the following on the role resources tab you want to copy to:
var checkedPerms = ''; // Place the output of the above console.log into this var
checkedPerms = JSON.parse(checkedPerms);
for (var i = 0; i < checkedPerms.length; i++) {
var el = document.getElementById(checkedPerms[i]);
if (el) {
el.checked = true;
}
}