获取magento中特定角色的用户

Gar*_*rry 1 php magento magento-1.9

有没有办法让Magento的特定角色(比如说员工)的用户?我试过这个

$roles_users = Mage::getResourceModel('admin/roles_user_collection');
Run Code Online (Sandbox Code Playgroud)

但是不知道如何为特定角色添加过滤器.

提前致谢

awo*_*yin 6

如果仔细查看magento如何存储管理员角色和用户,您会更好地理解这一点.

假设您创建了一个角色staff,magento将此角色存储在admin_role表中.创建新用户时,用户数据存储在admin_user表中,该表与表没有任何关联admin_role.但是,当您为此用户分配角色时staff,此分配会再次创建一个新的管理员角色.实质上,用户本身被视为管理员角色.

这应该完美:

$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;
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.