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();
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;
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...
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')
->getRoleUsers();
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.
select admin_user.user_id,firstname,lastname,email,authorization_role.parent_id as pid,(select role_name from authorization_role where role_id = pid) as admin_rule_name,(select GROUP_CONCAT( resource_id SEPARATOR "||") from authorization_rule where permission = 'allow' and role_id = pid ) from admin_user
left join authorization_role On admin_user.user_id = authorization_role.user_id
Use below mysql query
select admin_user.user_id,firstname,lastname,email,authorization_role.parent_id as pid,(select role_name from authorization_role where role_id = pid) as admin_rule_name,(select GROUP_CONCAT( resource_id SEPARATOR "||") from authorization_rule where permission = 'allow' and role_id = pid ) from admin_user left join authorization_role On admin_user.user_id = authorization_role.user_id
-- View Users
SELECT
u.user_id 'ID',
u.username 'User Name',
u.firstname 'First Name',
u.lastname 'Last Name',
u.email 'Email',
u.logdate 'Last Logged In',
a2.role_name 'Role',
IF(u.is_active=1,'Active','Inactive') 'Status'
FROM admin_user u
JOIN authorization_role a1 ON u.user_id = a1.user_id
JOIN authorization_role a2 ON a2.role_id = a1.parent_id;