Magento 2 – How to Get Admin User Role

adminmagento2sessionuser-roles

I want to get all information of user(admin) and I find this answer for M1:
To get an admin user role try (assuming that user is log in)

$admin_user_session = Mage::getSingleton('admin/session');
$adminuserId = $admin_user_session->getUser()->getUserId();
$role_data = Mage::getModel('admin/user')->load($adminuserId)->getRole()->getData();

Now I want get this for Magento 2.0. And how to do ?

Best Answer

You need to use DI in your class -
1. add property of AdminSession class to class 2. add including of the variable using dependensy injection:

/**
 * @var \Magento\Backend\Model\Auth\Session
 */
protected $_adminSession;

public function __construct(
    \Magento\Backend\Model\Auth\Session $adminSession
) {
    $this->_adminSession = $adminSession;
}

and then where you need to use it:

public function someMethod()
{
    $roleData = $this->_adminSession->getUser()->getRole()->getData();
    $userData = $this->_adminSession->getUser()->getData();
}
Related Topic