I would like to get the Role of the user logged in.

I'm trying to use the \vendor\magento\module-user\Model\User.php class, but all results give me null or empty.

I'm trying to use \vendor\magento\module-authorization\Model\Role.php, but the same happens, all empty.

I have achieved getting the userName of the user logged in, which might help, with this class: \vendor\magento\module-backend\Model\Auth\Session.php

I have checked the database, and in the authoritzation_role I have:

enter image description here

Where the parent_id refers to the 3 roles I have created:

enter image description here

What am I doing wrong here? Am I calling the right classes? Is there a better way to do it?


You can do this using below code.

public function __construct(\Magento\User\Model\UserFactory $userFactory)
    $this->userFactory = $userFactory;

public function getAdminRole()
 $roleName = $this->userFactory->create()->getRole()->getRoleName();

Let me know if you need further help.

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.