2
username = 'admin';
$user_data = Mage::getModel('admin/user')->getCollection()->addFieldToFilter('username',$username)->getData();
var_dump($user_data);

//By Username get admin User's Role Details
$username = 'admin';
$role_data = Mage::getModel('admin/user')->getCollection()->addFieldToFilter('username',$username)->getFirstItem()->getRole()->getData();
var_dump($role_data);

//By ID get admin User's Details
$id = 1;
$user_data = Mage::getModel('admin/user')->load($id)->getData();
var_dump($user_data);


//By ID get admin User's Role Details
$id = 1;
$role_data = Mage::getModel('admin/user')->load($id)->getRole()->getData();
var_dump($role_data);

this code for magento1 . am working on magento 2 . can anyone guide me how get user detail of specific user_id.

2

In Magento 2 you can do it by instantiating model object using dependency injection to inject its factory object. Please refer to this article, Factory Object for model in Magento 2, to understand more.

Check code below:

/** @var \Magento\User\Model\UserFactory  */
protected $userFactory;

/**
 * constructor.
 * @param \Magento\User\Model\UserFactory $userFactory
 */
public function __construct(
    \Magento\User\Model\UserFactory $userFactory
)
{
   $this->userFactory = $userFactory;
}

In your public method you can get admin detail like this:

public function getAdminDetails()
{
    //By username get admin User's Details
    $username = 'admin';
    $user = $this->userFactory->create()->loadByUsername($username);
    $user->getStoredData();
    //get specific data like username
    //$user->getUserName();
    var_dump($user);

    //By id get admin User's Details
    $id = 1;
    $user = $this->userFactory->create()->load($id);
    $user->getStoredData();
    //get specific data like username
    //$user->getUserName();
    var_dump($user);

}
  • ! sorry is not work . i create log. and check dubug.log . return null – HaFiz Umer Apr 4 at 7:27
  • try to change line var_dump($user); to return print_r($user); – fmsthird Apr 4 at 7:28
  • 1
    please it $userid=28; $userdetail=$this->userFactory->create()->load($userid); $this->logger->debug(' User Email is '.$userdetail->getEmail()); in debug.log file main.DEBUG: User Email is [] [] null value return. i also try loadByUsername($username) but nor work. – HaFiz Umer Apr 4 at 7:30
  • 1
    not i don't run upgrade. let me run then i will . actually var_dump or print_r() is not work because i work on place order event fire – HaFiz Umer Apr 4 at 7:32
  • 1
    Yes it work thanks – HaFiz Umer Apr 4 at 8:08

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.