Get users of a particular role in magento

北城余情 提交于 2020-01-06 02:03:30

问题


Is there any way to get users of a particular role (say staff) in Magento? I tried with this

$roles_users = Mage::getResourceModel('admin/roles_user_collection');

But dont know how to add filter for a particular role.

thanks in advance


回答1:


If you look carefully at how magento stores admin roles and users, you would understand this better.

Say you create a role staff, magento stores this role in the admin_role table. When you create a new user, the user data is stored in the admin_user table, which has no association at all to the admin_role table. But, when you assign this user the role of staff, this assignment creates a new admin role in itself again. Essentially, the user itself is treated as an admin role.

This should work perfectly:

$output = []; // just an array to hold all the users, you may not need this

// instance of the admin_role
$model = Mage::getModel('admin/role');

// fetch all roles with name of 'Staff', but get only the first item since two roles cannot have same name
$role = $model->getCollection()
    ->addFieldToFilter('role_name', ['eq' => 'Staff'])
    ->getFirstItem();

// check to make sure the role exists
if ($roleId = $role->getId())
{
    // get a collection of all the user roles having the Staff role id as a parent_id
    $staffUsers = $model->getCollection()
        ->addFieldToFilter('parent_id', ['eq' => $roleId]);

    // ensure the collection has size
    if ($staffUsers->getSize())
    {
        // loop through each object and get the user_id values
        foreach ($staffUsers as $staffUser)
        {
            // you can still check to make sure the user_id field is not null
            if ($staffUser->getUserId())
            {
                // get the user object and do anything with it
                $user = Mage::getModel('admin/user')->load($staffUser->getUserId());
                $output[$user->getId()] = $user->getFirstname() . " " . $user->getLastname();
            }
        }
    }
}


var_dump($output); die;

Hope it helps.




回答2:


In magento 2 you can achieve this using custom query.

$shippercollection = $objectManager->create('Magento\User\Model\ResourceModel\User\Collection');
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$tableName = $resource->getTableName('authorization_role'); 
$filter = 'Shipper';

//Select Shipper users id from table
$sql = "Select user_id FROM " . $tableName." WHERE parent_id = (Select role_id FROM " . $tableName." WHERE role_name = '$filter' LIMIT 1)";
$shipper_users = $connection->fetchAll($sql);

$shippermodel = $objectManager->create('Magento\User\Model\User');

$deliveryBoys = $shippermodel->getCollection()
             ->addFieldToFilter('user_id', array('in' => $shipper_users));


来源:https://stackoverflow.com/questions/35925121/get-users-of-a-particular-role-in-magento

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!