2

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.

3
  • Need to create new rule programtically and this rule assign to give use id?
    – Abdul
    Jan 7, 2016 at 9:15
  • Pls explain more details
    – Abdul
    Jan 7, 2016 at 9:15
  • I want to copy role resources of any role id to other role id.
    – Mathew
    Jan 7, 2016 at 11:10

2 Answers 2

4

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();
    }
}
0

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;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.