0

How Can I perform this event: if some one from Magento admin delete any customer then write a log which admin user delete that customer and date and time of the action.

Any help would be appreciated.

Thanks in advance..!

1 Answer 1

1

You can make a plugin for Magento\User\Model\ResourceModel\User class

di.xml

app/code/Vendor/Module/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\User\Model\ResourceModel\User">
             <plugin name="delete_user_action" type="Vendor\Module\Plugin\User\Delete" sortOrder="1"/>
        </type>
    </config>

Delete.php

app/code/Vendor/Module/Plugin/User/Delete.php

<?php
namespace Vendor\Module\Plugin\User;

use Psr\Log\LoggerInterface;

class Delete
{

    /**
     * @var Logger
     */
    protected $logger;

    /**
     * @var Session
     */
    protected $authSession;

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

    public function aroundDelete(\Magento\User\Model\ResourceModel\User $user, callable $proceed, $object)
    {
        $object->load($object->getId());

        $result = $proceed($object);
        $object->afterDelete();

        $this->logger->debug('START :-'.__METHOD__ . ' - ' . __LINE__);

        if ($this->authSession->isLoggedIn()) {
            $adminUserName = $this->authSession->getUser()->getUserName();
        }

        $date = date('Y/m/d H:i:s');
        $message = sprintf(' %s has Deleted Customer', $adminUserName);
        $this->logger->debug($date. ' => '.$message);       

        $this->logger->debug('END :-'.__METHOD__ . ' - ' . __LINE__);

        return $result;
    }
}
27
  • Ok thank you let me implement the code and if I face any issue I will let you know.
    – Naiwrita09
    Commented Jul 13, 2019 at 7:16
  • One small query do I have to create the virtual type within di.xml I am learning this for the first time and not so clear about the concepts.
    – Naiwrita09
    Commented Jul 13, 2019 at 7:22
  • no just <type> i have updated my answer please check.
    – Mohit Rane
    Commented Jul 13, 2019 at 7:26
  • Hi I implemented the code but if I run s:d:c its giving an error that the delete file does not exist
    – Naiwrita09
    Commented Jul 13, 2019 at 7:43
  • can you share whole error?
    – Mohit Rane
    Commented Jul 13, 2019 at 7:50

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.