How can I retrieve the list of all admin that has the same role?

For example, I create a new role called "Seller" and assign some admin users to that role.

How to list all admin users with the role "Seller"?

Thank you very much


To get all the Roles

   $roles = Mage::getModel('admin/roles')->getCollection();
   foreach($roles as $role):
      echo '<br/>Role : '.$role->getId()." | ".$role->getRoleName();

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

Or in your database you can check in the table admin_role which role you want to check, for example id=1 is my administrator and then with the query

select * from admin_role where parent_id='1' 

You get all users with the Administrator role. And I have customer service with id=2 so the query would be

select * from admin_role where parent_id='2'

Which gives me all users with the role customer service and so on...

|improve this answer|||||
  • I´ve updated it for specific roles in mysql – Klettseb Jan 19 '16 at 8:56

There are two ways to get all the admin users belonging to a specific admin role. On the one hand, you can build the collection of admin users by first retrieving the user IDs belonging to the admin role and then loading a new Mage_Admin_Model_Role object per user ID:

$adminUserCollection = array();
$adminUserIds = Mage::getModel('admin/roles')
    ->load("Sellers", 'role_name')
foreach($adminUserIds as $userId) {
    $adminUser = Mage::getModel('admin/user')->load($adminUserId);
    $adminUserCollection[] = $adminUser;

Secondly, you can load the collection as follows:

$adminRoleId = Mage::getModel('admin/role')->load('Sellers', 'role_name')->getId();
$adminUserCollection = Mage::getModel('admin/role')->getCollection()
    ->addFieldToFilter('parent_id', ['eq'=>$adminRoleId])
    ->join(['user' => 'admin/user'], user.user_id=main_table.user_id');

I am not sure which method is faster, but IMO the second option is slightly more elegant.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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