2016-03-10 6 views
1

Есть ли способ получить пользователей определенной роли (например, персонал) в Magento? Я пытался с этимПолучить пользователей определенной роли в magento

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

Но не знаю, как добавить фильтр для той или иной роли.

заранее спасибо

ответ

5

Если вы внимательно посмотрите на то, как 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; 

Надеется, что это помогает.

0

В 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)); 
Смежные вопросы