Log Actions of All Admin Users
This will allow you to log all actions performed in-store backend by admin users who have the backend access to your e-store. For this create a module of AP=> LogAction
Step 1 :
Step 2 :
Create a model of above table.
Step 3:
Create File inside etc=>adminhtml=>di.xml
Step 4:
Create file inside AP=>LogAction=>Plugin
Logout.php
Step 5:
Create File inside etc=>events.xml
Step 6 :
Create file inside Ap=>LogAction=>Observer
LoginFailed.php
This will allow you to log all actions performed in-store backend by admin users who have the backend access to your e-store. For this create a module of AP=> LogAction
Step 1 :
namespace Ap\LogAction\Setup;
use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
/**
* Class InstallSchema
* @package Mageplaza\Security\Setup
*/
class InstallSchema implements InstallSchemaInterface
{
public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$installer = $setup;
$installer->startSetup();
$table = $installer->getConnection()
->newTable($installer->getTable('log_action'))
->addColumn(
'entity_id',
\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
null,
['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
'Entity Id'
)
->addColumn(
'user_id',
\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
null,
[],
'Admin User Id'
)
->addColumn(
'user_name',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'64K',
[],
'User Name'
)
->addColumn(
'ip_address',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'64K',
[],
'Ip Address'
)
->addColumn(
'url',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'64K',
[],
'Url'
)
->addColumn(
'controller_name',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'64K',
[],
'Controller Name'
)
->addColumn(
'browser_agent',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'64K',
[],
'Browser Agent'
)
->addColumn(
'status_code',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'64K',
[],
'Status'
)
->addColumn(
'time',
\Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
null,
[],
'Time'
)
->setComment('Log Table');
$installer->getConnection()->createTable($table);
$installer->endSetup();
}
}
Step 2 :
Create a model of above table.
Step 3:
Create File inside etc=>adminhtml=>di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="\Magento\Backend\Controller\Adminhtml\Auth\Logout">
<plugin name="ap_logout" type="Ap\LogAction\Plugin\Logout"/>
</type>
<type name="Magento\Security\Model\AdminSessionsManager">
<plugin name="ap_admin_sessions_prolong" type="Ap\LogAction\Plugin\AuthSessionCheck" sortOrder="1" />
</type>
</config>
Step 4:
Create file inside AP=>LogAction=>Plugin
Logout.php
namespace Ap\LogAction\Plugin;
use Magento\Backend\Model\Auth\Session;
use Magento\Framework\App\Response\RedirectInterface;
use Ap\LogAction\Model\LoginLogFactory;
use Magento\Framework\HTTP\PhpEnvironment\Request;
use Magento\Backend\Model\Session as BackendSession;
class Logout
{
protected $authSession;
protected $_redirect;
protected $_request;
protected $_backendsession;
protected $_loginLogFactory;
public function __construct(
Session $session,
BackendSession $backendsession,
Request $request,
RedirectInterface $redirect,
LoginLogFactory $loginLogFactory
)
{
$this->authSession = $session;
$this->_redirect = $redirect;
$this->_request = $request;
$this->_backendsession = $backendsession;
$this->_loginLogFactory = $loginLogFactory;
}
public function beforeExecute(\Magento\Backend\Controller\Adminhtml\Auth\Logout $logout)
{
$loginLog = [
'time' => time(),
'user_id' => $this->authSession->getUser()->getUserId(),
'ip_address' => $this->_request->getClientIp(),
'browser_agent' => $this->_backendsession->getBrowserAgent(),
'url' => $this->_backendsession->getUrl(),
'referer' => $this->_redirect->getRefererUrl(),
'status_code' => 'LOGGED_OUT_MANUALLY'
];
$this->_loginLogFactory->create()->addData($loginLog)->save();
}
}
AuthSessionCheck.phpnamespace Ap\LogAction\Plugin;
use Magento\Backend\Model\Auth\Session;
use Magento\Framework\App\Response\RedirectInterface;
use Ap\LogAction\Model\LoginLogFactory;
use Magento\Framework\HTTP\PhpEnvironment\Request;
use Magento\Backend\Model\Session as BackendSession;
use Magento\Security\Model\AdminSessionsManager;
class AuthSessionCheck
{
protected $authSession;
protected $_redirect;
protected $_request;
protected $_backendsession;
protected $_loginLogFactory;
private $sessionsManager;
public function __construct(
Session $session,
BackendSession $backendsession,
Request $request,
RedirectInterface $redirect,
LoginLogFactory $loginLogFactory,
AdminSessionsManager $sessionsManager
)
{
$this->authSession = $session;
$this->_redirect = $redirect;
$this->_request = $request;
$this->_backendsession = $backendsession;
$this->_loginLogFactory = $loginLogFactory;
$this->sessionsManager = $sessionsManager;
}
public function beforeGetLogoutReasonMessageByStatus()
{
$clientIp = $this->_request->getClientIp();
$msg = "LOGGED_OUT";
if($this->sessionsManager->getCurrentSession()->getStatus() == 2)
{
$msg = "LOGGED_OUT_BY_LOGIN";
}
$loginLog = [
'time' => time(),
'user_id' => $this->sessionsManager->getCurrentSession()->getUserId(),
'ip_address' => $this->_request->getClientIp(),
'browser_agent' => $this->_backendsession->getBrowserAgent(),
'url' => $this->_backendsession->getUrl(),
'referer' => $this->_redirect->getRefererUrl(),
'status_code' => $msg
];
$this->_loginLogFactory->create()->addData($loginLog)->save();
}
}
Step 5:
Create File inside etc=>events.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="backend_auth_user_login_success">
<observer name="ap_login_success" instance="Ap\LogAction\Observer\LoginSuccess"/>
</event>
<event name="backend_auth_user_login_failed">
<observer name="ap_login_failed" instance="Ap\LogAction\Observer\LoginFailed"/>
</event>
</config>
Step 6 :
Create file inside Ap=>LogAction=>Observer
LoginFailed.php
namespace Ap\LogAction\Observer;
use Magento\Backend\Model\Session;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\HTTP\PhpEnvironment\Request;
use Ap\LogAction\Model\LoginLogFactory;
class LoginFailed implements ObserverInterface
{
protected $_request;
protected $_backendSession;
protected $_loginLogFactory;
public function __construct(
Request $request,
Session $session,
LoginLogFactory $loginLogFactory
)
{
$this->_request = $request;
$this->_backendSession = $session;
$this->_loginLogFactory = $loginLogFactory;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$clientIp = $this->_request->getClientIp();
$userName = $observer->getUserName();
$loginLog = [
'time' => time(),
'user_name' => $userName,
'ip_address' => $clientIp,
'browser_agent' => $this->_backendSession->getBrowserAgent(),
'url' => $this->_backendSession->getUrl(),
'referer' => $this->_backendSession->getRefererUrl(),
'status_code' => 'LOGIN FAIL'
];
$this->_loginLogFactory->create()->addData($loginLog)->save();
}
}
LoginSuccess.phpnamespace Ap\LogAction\Observer;
use Magento\Backend\Model\Session;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\HTTP\PhpEnvironment\Request;
use Ap\LogAction\Model\LoginLogFactory;
class LoginSuccess implements ObserverInterface
{
protected $_request;
protected $_backendSession;
protected $_loginLogFactory;
protected $_helperData;
public function __construct(
Request $request,
Session $session,
LoginLogFactory $loginLogFactory
)
{
$this->_request = $request;
$this->_backendSession = $session;
$this->_loginLogFactory = $loginLogFactory;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$loginLog = [
'time' => time(),
'user_name' => $observer->getUser()->getUserName(),
'user_id' => $observer->getUser()->getUserId(),
'ip_address' => $this->_request->getClientIp(),
'browser_agent' => $this->_backendSession->getBrowserAgent(),
'url' => $this->_backendSession->getUrl(),
'referer' => $this->_backendSession->getRefererUrl(),
'status_code' => 'LOGIN SUCCESS'
];
$this->_loginLogFactory->create()->addData($loginLog)->save();
}
}
Comments
Post a Comment