Skip to main content
added 1702 characters in body
Source Link
mansi
  • 411
  • 2
  • 8

you can use this to get current admin details :-

$this->authSession->getUser()->getUsername();//get admin username
$this->authSession->getUser()->getEmail();//get admin email

To save admin username in database change controller file of save action to below

<?php

namespace OX\HomeSlider\Controller\Adminhtml\Post;

class Save extends \Magento\Backend\App\Action
{

    public function __construct(
    \Magento\Backend\App\Action\Context $context, 
    \Magento\Backend\Model\View\Result\RedirectFactory $resultRedirectFactory,
    \Magento\Backend\Model\Auth\Session $authSession
    )
    {
        $this->resultRedirectFactory = $resultRedirectFactory;
        $this->authSession = $authSession;
        parent::__construct($context);
    }

    public function execute()
    {
        $id = $this->getRequest()->getParam('id');
        $postModel = $this->_objectManager->create('OX\HomeSlider\Model\Post');

        if ($id) {
            $postModel = $postModel->load($id);
        }
        $post = $this->getRequest()->getParam('general');
        $user = $this->getCurrentUser();
        $post['username'] = $user;
        if (empty($post['id'])) {
            $post['id'] = null;
        }

        if (isset($post['image'])) {
            $post['image'] = $post['image'][0]['name'];
        } else {
            $post['image'] = null;
        }


        $postModel->setData($post);
        $postModel->save();
        return $this->resultRedirectFactory->create()->setPath('homeslider/post/index');
    }
    public function getCurrentUser()
    {
        return $this->authSession->getUser()->getUsername();
    }

}

you can use this to get current admin details :-

$this->authSession->getUser()->getUsername();//get admin username
$this->authSession->getUser()->getEmail();//get admin email

you can use this to get current admin details :-

$this->authSession->getUser()->getUsername();//get admin username
$this->authSession->getUser()->getEmail();//get admin email

To save admin username in database change controller file of save action to below

<?php

namespace OX\HomeSlider\Controller\Adminhtml\Post;

class Save extends \Magento\Backend\App\Action
{

    public function __construct(
    \Magento\Backend\App\Action\Context $context, 
    \Magento\Backend\Model\View\Result\RedirectFactory $resultRedirectFactory,
    \Magento\Backend\Model\Auth\Session $authSession
    )
    {
        $this->resultRedirectFactory = $resultRedirectFactory;
        $this->authSession = $authSession;
        parent::__construct($context);
    }

    public function execute()
    {
        $id = $this->getRequest()->getParam('id');
        $postModel = $this->_objectManager->create('OX\HomeSlider\Model\Post');

        if ($id) {
            $postModel = $postModel->load($id);
        }
        $post = $this->getRequest()->getParam('general');
        $user = $this->getCurrentUser();
        $post['username'] = $user;
        if (empty($post['id'])) {
            $post['id'] = null;
        }

        if (isset($post['image'])) {
            $post['image'] = $post['image'][0]['name'];
        } else {
            $post['image'] = null;
        }


        $postModel->setData($post);
        $postModel->save();
        return $this->resultRedirectFactory->create()->setPath('homeslider/post/index');
    }
    public function getCurrentUser()
    {
        return $this->authSession->getUser()->getUsername();
    }

}
Source Link
mansi
  • 411
  • 2
  • 8

you can use this to get current admin details :-

$this->authSession->getUser()->getUsername();//get admin username
$this->authSession->getUser()->getEmail();//get admin email