Magento: Get Roles and Users

How can I get a collection of all roles (System-> Permission-> Roles) and users who have this role?

Thanks.

+6
source share
3 answers

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 user role

  $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; 
+12
source

You can also get all role user identifiers using this

 $roles = Mage::getModel('admin/roles')->load($roleId)->getRoleUsers(); 
+3
source

I came across this post when I was looking for a way to select all admin users belonging to a specific user role. (See this answer and this question .) The question here can be read as if Alex wants admin users for each role. Since the answer is not sorted by administrator role, I would like to suggest 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; } 
0
source

All Articles