Question

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?

Était-ce utile?

La solution

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!

Autres conseils

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().

Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange
scroll top