How To Get Administrators Role’s User Ids programmatically in Magento 2

Written by Jigar Patel

Jan 08, 2023

How To Get Administrators Role’s User Ids programmatically in Magento 2

In the Magento store, the user role is used to define the permission of specific admins. This is useful for a Magento store with a huge product catalog, multiple admin users, and numerous store views. In case, store owners just want to give permission to certain users so that each user role is in charge of different tasks and work.

Magento function is pretty limited in that it allows permission on store pages only. Then after accessing to a certain webpage, these admin users have all permissions on that page.

There are a couple of ways to go about creating a new Admin User in Magento 2:

  1. Directly via the Magento Admin
  2. Via the command line

Here I’ll show you How To Get the administrator’s Role’s User Ids programmatically in Magento 2.

I have created a method getAdminUserIds in the following Helper class file.

You can add this method to your required class file and call on the needed places in your code.

<?php

namespace Dolphin\CustomModule\Helper;

use Magento\User\Model\ResourceModel\User\CollectionFactory as UserCollectionFactory;

/**
 * Dolphin CustomModule Helper Data.
 */
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    /**
     * const ADMIN_ROLE
     */
    private const ADMIN_ROLE = "Administrators";

    /**
     * @var UserCollectionFactory
     */
    private $userCollFactory;

    /**
     * Constructor
     *
     * @param UserCollectionFactory $userCollFactory
     * @param \Magento\Framework\App\Helper\Context $context
     * @return void
     */
    public function __construct(
        UserCollectionFactory $userCollFactory,
        \Magento\Framework\App\Helper\Context $context
    ) {
        $this->userCollFactory = $userCollFactory;

        parent::__construct($context);
    }

    /**
     * Get Administrator Role's user ids
     *
     * @return array
     */
    public function getAdminUserIds()
    {
        $ids = [];
        try {
            $userColl = $this->userCollFactory->create()
                ->addFieldToSelect("user_id");

            $userColl->getSelect()
                ->where("`detail_role`.`role_name` = '".(self::ADMIN_ROLE).
                    "' AND user_role.parent_id=detail_role.role_id");

            $userIdsColl = (!empty($userColl))?$userColl->getData():[];

            foreach ($userIdsColl as $each) {
                $ids[] = $each["user_id"] ?? 0;
            }
        } catch (\Exception $e) {
            echo $e->getMessage();exit;
        }
        return $ids;
    }
}

Follow the above class to get the administrator’s Role’s User Ids.

I Hope, This instruction will be helpful for you.

Happy Coding with magento2!! ?

If you have any difficulties regarding this blog, do consider them posting in the Comments section below!

I’m here to help.

Thank you!

Jigar Patel

Author

We can help you with

  • Dedicated Team
  • Setup Extended Team
  • Product Development
  • Custom App Development

Schedule a Developer Interview And Get 7 Days Risk-Free Trial

Fill out the form below, and one of our technical experts will reach out to you within 12 hours.

    Google
    |

    4.8

    Google
    |

    4.8

    Google
    |

    4.9

    Google
    |

    4.8

    Google
    |

    4.9

    Copyright © 2024 DOLPHIN WEB SOLUTION. All rights reserved.

    ×

    Hello!

    Click one of our contacts below to chat on WhatsApp

    × How can I help you?
    TO TOP