3


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 ?

2 Answers 2

8

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();
}
2
  • after I tried, have a problem below: Fatal error: Call to a member function getRole() on null in
    – MrTo-Kane
    Mar 1, 2016 at 2:02
  • 1
    I want get admin session at front-end.
    – MrTo-Kane
    Mar 1, 2016 at 2:13
0
protected $_context ;

public function __construct(
\Magento\Framework\App\Action\Context $context
) {
$this->_context = $context;
}

public function customfucntion()
{
  $auth          = $this->_context->getAuth();
  $loginUser     = $auth->getUser();
  $loginUserRole = $loginUser->getRole();
  $loginUserRole->getRoleName();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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