How can I get collection of all roles (System->Permission->Roles) and users which has this role?
Thanks.
How can I get collection of all roles (System->Permission->Roles) and users which has this role?
Thanks.
To get all the Roles
$roles = Mage::getModel('admin/roles')->getCollection();
foreach($roles as $role):
echo '<br/>Role : '.$role->getId()." | ".$role->getRoleName();
endforeach;
To get the Role users
$roles_users = Mage::getResourceModel('admin/roles_user_collection');
foreach($roles_users as $roleuser):
$user = Mage::getModel('admin/user')->load($roleuser->getUserId());
echo '<br/>User : '.$user->getUsername()." | ".$user->getFirstname();
endforeach;
You can also get all role user ids with this
$roles = Mage::getModel('admin/roles')->load($roleId)->getRoleUsers();
I came across this post when I looked for a way to select all admin users belonging to a specific user role. (See this answer and this question.) The question here could be read as if Alex wants the admin users for each role. Since the answer does not sort by admin role, I would like to offer the following solution:
$usersByRole = array();
$adminRoles = Mage::getModel('admin/roles')->getCollection();
foreach($adminRoles as $adminRole) {
$adminRoleId = $adminRole->getId();
$adminRoleUserCollection = Mage::getModel('admin/role')->getCollection()
->addFieldToFilter('parent_id', ['eq'=>$adminRoleId])
->join(['user' => 'admin/user'], 'user.user_id=main_table.user_id');
$usersByRole[$adminRole->getRoleName()] = $adminRoleUserCollection;
}
© 2022 - 2024 — McMap. All rights reserved.