Magento 2: Come ottenere il dettaglio degli utenti amministratore corrente?
-
29-09-2020 - |
Domanda
Come ottenere il dettaglio dell'utente di amministratore corrente in back-end?
Soluzione
È necessario aggiungere questo al costruttore della tua classe
protected $authSession;
public function __construct(
....
\Magento\Backend\Model\Auth\Session $authSession,
....
) {
....
$this->authSession = $authSession;
....
}
.
Quindi creare questo metodo
public function getCurrentUser()
{
return $this->authSession->getUser();
}
.
Questo ti darà l'attuale amministratore registrato.
È possibile ottenere in seguito i dettagli come $user->getUsername()
o $user->getEmail()
.
Altri suggerimenti
Come ottenere il dettaglio Utente Amministratore corrente?
Inject Backend Session nel tuo controller
public function __construct(
....
\Magento\Backend\Model\Auth\Session $authSession,
....
) {
....
$this->authSession = $authSession;
....
.
}
E usa questo per ottenere il nome utente o e-mail
$this->authSession->getUser()->getUsername();
$this->authSession->getUser()->getEmail();
. Il tuo controller estende già \Magento\Backend\App\Action
in modo che abbia già l'oggetto di autorizzazione.Non sono necessarie iniezioni aggiuntive.
Per ottenere l'utente semplicemente utilizzare questa funzione:
/** @var \Magento\User\Model\User $user*/
$user = $this->_auth->getUser();
.
Altre risposte suggeriscono iniezioni duplicate, che non sono necessarie.