Alex
Alex

Reputation: 468

Magento: get roles and users

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

Thanks.

Upvotes: 6

Views: 6705

Answers (3)

Vincent_v_E
Vincent_v_E

Reputation: 101

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;
}

Upvotes: 0

Oskars Tūns
Oskars Tūns

Reputation: 41

You can also get all role user ids with this

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

Upvotes: 3

Haijerome
Haijerome

Reputation: 1550

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;

Upvotes: 12

Related Questions