2

In Magento 2, We want to list all user email id's by using their role_name.

How we can achieve in one collection with optimal way?

Thanks!

  • 1
    What do you mean by role_name – Priyank Dec 22 '17 at 6:04
  • Yes @Priyank we want users email ids by role_name – Pramod Kharade Dec 22 '17 at 6:08
  • You mean admin users? – Priyank Dec 22 '17 at 6:11
  • @Priyank admin side, lets consider there are 2 roles like: HOD, Operator. suppose if my passing Operator role then related to same group user email should list. – Pramod Kharade Dec 22 '17 at 6:14
2

you can create a helper like below and pass the role name to filter it.

public function getUserByRole($roleName)
{
    /** @var $userCollection \Magento\User\Model\ResourceModel\User\Collection*/
    $userCollection->getSelect()->joinLeft(
        ['role' => $userCollection->getTable('authorization_role')],
        "main_table.user_id = role.user_id",
        ["role_name"]
        )->where("role.role_name = ". $roleName);
    return $userCollection;

}

By calling the above helper you will have collection of all the users of respective role.

Hope this helps.

|improve this answer|||||
3

try below code:

$roleName = "Administrators";
$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
$roleModel = $objectManager->create('Magento\Authorization\Model\Role');
$userModel = $objectManager->create('Magento\User\Model\User');
$roleModel = $roleModel->load($roleName, 'role_name');
if($roleModel->getId()) {
    $userIds = $roleModel->getRoleUsers();
    foreach($userIds as $userId) {
        $user = $userModel->load($userId);      
        print_r($user->getEmail());
        echo "<br>";                                   
    }
}

Multiples Role name

  $rolesModel = $objectManager->create('Magento\Authorization\Model\ResourceModel\Role\Collection');
  $rolesModel->addFieldToFilter('role_name', array('in' => array('Administrators','xyz')));

  if($rolesModel->getSize()) {
      foreach($rolesModel as $roleModel) {
          $userIds = $roleModel->getRoleUsers();
            foreach($userIds as $userId) {
                $user = $userModel->load($userId);      
                print_r($user->getEmail());
                echo "<br>";                                   
            }
      }
  }
|improve this answer|||||
  • if I want to use multiple roles, how could I use it? e.i. $roleName = ("Administrators",''xyz''); – Utsav Gupta Feb 1 '19 at 7:16
  • @Utsav Gupta Please check updates ans – Abdul Feb 1 '19 at 9:36
2

After digging , I got solution like this:

<?php
    namespace Custom\ProductApproval\Helper;

    use Magento\Authorization\Model\RoleFactory;
    use Magento\User\Model\UserFactory;


    class Data extends \Magento\Framework\App\Helper\AbstractHelper {

        protected $roles;
        protected $user;
        protected $objectManager;

        const ROLES_NAMES = "HOD Merchandising";

        public function __construct(\Magento\Framework\App\Helper\Context $context, \Magento\Backend\Model\Session $session, RoleFactory $roles, UserFactory $user
        ) {

            parent::__construct($context);
            $this->_session = $session;
            $this->roles = $roles;
            $this->user = $user;
            $this->objectManager = $objectManager;
        }


        public function getEmailUserByRole() {
            //$roleName = "HOD Merchandising";
            $userEmail = array();
            $roleModel = $this->roles->create();
            $userModel = $this->user->create();
            $roleModel = $roleModel->load(self::ROLES_NAMES, 'role_name');
            if ($roleModel->getId()) {
                $userIds = $roleModel->getRoleUsers();
                foreach ($userIds as $userId) {
                    $user = $userModel->load($userId);
                    $userEmail[]= $user->getEmail();
                }

            }
            return $userEmail;
        }

    }
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.