2

Hi I have created a new column in the admin users table

using this answers Magento2: How to add new column in admin_user table?

Now I want to get the column value when admin user logged in

How i can get this?

2
  • My solution is working for you? let me know if you face any issue. Commented Jun 10, 2019 at 11:44
  • i will check @ChiragPatel bro.... Commented Jun 10, 2019 at 11:49

2 Answers 2

2

Inject UserModel class to your construct and you can fetch details.

protected $userFactory;
public function __construct(
    ...
    \Magento\User\Model\ResourceModel\User\Collection $userCollection,
    ...
)
{
    ...
    $this->userCollection = $userCollection;
    ...
}

public function getUserData()
{
    $user = $this->userCollection->create();
    print_r($user->getData);
}

For apply condition is customer is logged in or not you can check from customer session.

Inject \Magento\Customer\Model\Session in your cunstruct and get value using $this->customerSession->isLoggedIn()

I hope it helps!

9
  • can i use in controller Commented Jun 10, 2019 at 11:49
  • You can use a block as well controller where you want to get data. Commented Jun 10, 2019 at 11:58
  • Just remove ->create and see. Commented Jun 17, 2019 at 6:23
  • how i can get the logged in user details i'm getting all users data Commented Jun 17, 2019 at 6:29
  • If can get using resource model collection & model class factory. But if you are using resource model collection then you don't need to use ->create() Commented Jun 17, 2019 at 6:40
1

I tried this works for me

need to add this to the constructor of your class

protected $authSession;
public function __construct(
    ....
    \Magento\Backend\Model\Auth\Session $authSession, 
    ....
) {
    ....
    $this->authSession = $authSession;
    ....
}

Then create this method

public function getCurrentUser()
{
    return $this->authSession->getUser();
}

this will give you the current logged in admin.

You can later get the details like $user->getUsername() or $user->getEmail().

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.