Skip to content

Magento 2 – Getting user role

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

I’m trying to use the vendormagentomodule-userModelUser.php class, but all results give me null or empty.

I’m trying to use vendormagentomodule-authorizationModelRole.php, but the same happens, all empty.

I have achieved getting the userName of the user logged in, which might help, with this class: vendormagentomodule-backendModelAuthSession.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?