Magento: get roles and users
Asked Answered
F

3

6

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

Thanks.

Fichtean answered 4/3, 2013 at 11:20 Comment(0)
T
12

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;
Toweling answered 4/3, 2013 at 11:36 Comment(2)
Thanks! Maybe you also now how to get users with this roles? :)Fichtean
@AlexK kindly find my updated answer ;-) Let me know if you have any doubts with this.Toweling
R
3

You can also get all role user ids with this

$roles = Mage::getModel('admin/roles')->load($roleId)->getRoleUsers();
Rivera answered 13/5, 2014 at 17:17 Comment(0)
A
0

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;
}
Amphioxus answered 17/8, 2017 at 15:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.