Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 获取magento中特定角色的用户_Php_Magento_Magento 1.9 - Fatal编程技术网

Php 获取magento中特定角色的用户

Php 获取magento中特定角色的用户,php,magento,magento-1.9,Php,Magento,Magento 1.9,有没有办法让Magento中特定角色的用户(比如员工)参与进来? 我试过这个 $roles_users = Mage::getResourceModel('admin/roles_user_collection'); 但不知道如何为特定角色添加筛选器 提前感谢如果您仔细了解magento如何存储管理员角色和用户,您会更好地理解这一点 假设您创建了一个角色staff,magento将此角色存储在管理角色表中。创建新用户时,用户数据存储在admin\u user表中,该表与admin\u角色表完全

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

$roles_users = Mage::getResourceModel('admin/roles_user_collection');
但不知道如何为特定角色添加筛选器


提前感谢

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

假设您创建了一个角色
staff
,magento将此角色存储在
管理角色
表中。创建新用户时,用户数据存储在
admin\u user
表中,该表与
admin\u角色
表完全没有关联。但是,当您将
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;

希望能有所帮助。

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

假设您创建了一个角色
staff
,magento将此角色存储在
管理角色
表中。创建新用户时,用户数据存储在
admin\u user
表中,该表与
admin\u角色
表完全没有关联。但是,当您将
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;

希望有帮助。

在magento 2中,您可以使用自定义查询来实现这一点

$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));

在magento 2中,您可以使用自定义查询来实现这一点

$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));

我如何通过网站限制管理员用户,更多详细信息有人在statck overflow中发布-@awoyotyin谢谢我如何通过网站限制管理员用户,更多详细信息有人在statck overflow中发布-@awoyotyin谢谢