Programmatically Add Magento Admin User Roles and Resources


Hi all again,

Today i would like to share, how to add magento admin user roles and resources using code.


/* 4 Roles Creation Default*/
$roles = array('User Role 1','User Role 2','User Role 3','User Role 4');
$roleIds = array();
$resources = explode(',', '__root__,admin/sales,admin/sales/order,admin/sales/order/actions,admin/sales/order/actions/hold,admin/sales/order/actions/creditmemo,admin/sales/order/actions/unhold,admin/sales/order/actions/ship,admin/sales/order/actions/comment,admin/sales/order/actions/invoice,admin/sales/order/actions/capture,admin/sales/order/actions/email,admin/sales/order/actions/view,admin/sales/order/actions/reorder,admin/sales/order/actions/edit,admin/sales/order/actions/review_payment,admin/sales/order/actions/cancel,admin/sales/order/actions/create,admin/dashboard,admin/catalog,admin/catalog/products,admin/cms,admin/cms/page,admin/cms/page/save,admin/system,admin/system/acl,admin/system/acl/users');
foreach($roles as $role){
$col = Mage::getModel('admin/role')->setRoleName($role)->setRoleType('G')->setTreeLevel(1)->save();
if($col->getRoleId()){
if( in_array($role, $enabledRoles) )
$roleIds[] = $col->getRoleId();
$rules = Mage::getModel('admin/rules')->setRoleId($col->getRoleId())->setResources($resources);
$rules = Mage::getModel('admin/resource_rules')->saveRel($rules);
}
}

We are created a new user roles(User Role 1, User Role 2, User Role 3 and User Role 4). Then we have to update the Role Resources. I have checked with Magento Version 1.7.0.2.

You can get the Resources list in the backend of the magento roles area.

If any suggestions welcome with comments area below.

Thank you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s