Commit fa887a26 by lmf

后台增加日志模块

parent f249153b
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Api\Activity;
/**
* Interface ModelInterface
* @package KiwiCommerce\AdminActivity\Api\Activity
*/
interface ModelInterface
{
/**
* Get old data
* @param $model
* @return mixed
*/
public function getOldData($model);
/**
* Get edit data
* @param $model
* @return mixed
*/
public function getEditData($model, $fieldArray);
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Api;
/**
* Interface ActivityRepositoryInterface
* @package KiwiCommerce\AdminActivity\Api
*/
interface ActivityRepositoryInterface
{
/**
* Array of protected fields
* @return mixed
*/
public function protectedFields();
/**
* Get collection of admin activity
* @return mixed
*/
public function getList();
/**
* Get all admin activity data before date
* @param $endDate
* @return mixed
*/
public function getListBeforeDate($endDate);
/**
* Remove activity log entry
* @param $activityId
* @return mixed
*/
public function deleteActivityById($activityId);
/**
* Get all admin activity detail by activity id
* @param $activityId
* @return mixed
*/
public function getActivityDetail($activityId);
/**
* Get all admin activity log by activity id
* @param $activityId
* @return mixed
*/
public function getActivityLog($activityId);
/**
* Revert last changes made in module
* @param $activity
* @return mixed
*/
public function revertActivity($activity);
/**
* Get old data for system config module
* @param $model
* @return mixed
*/
public function getOldData($model);
/**
* Get admin activity by id
* @param $activityId
* @return mixed
*/
public function getActivityById($activityId);
/**
* Check field is protected or not
* @param $fieldName
* @return mixed
*/
public function isFieldProtected($fieldName);
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Api\Data;
/**
* Interface ActivityInterface
* @package KiwiCommerce\AdminActivity\Api\Data
*/
interface ActivityInterface
{
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Api\Data;
/**
* Interface LogSearchResultsInterface
* @package KiwiCommerce\EnhancedSMTP\Api\Data
*/
interface ActivitySearchResultsInterface extends \Magento\Framework\Api\SearchResultsInterface
{
/**
* Get admin activity list.
* @api
* @return \KiwiCommerce\AdminActivity\Model\Activity[]
*/
public function getItems();
/**
* Set admin activity list.
* @api
* @param \KiwiCommerce\AdminActivity\Model\Activity[] $items
* @return $this
*/
public function setItems(array $items);
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Api;
/**
* Interface LoginRepositoryInterface
* @package KiwiCommerce\AdminActivity\Api
*/
interface LoginRepositoryInterface
{
/**
* Set login data
* @param $status
* @param $type
* @return mixed
*/
public function addLog($status, $type);
/**
* Get all admin activity data before date
* @param $endDate
* @return mixed
*/
public function getListBeforeDate($endDate);
/**
* Set login user
* @param $user
* @return mixed
*/
public function setUser($user);
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Block\Adminhtml;
use Magento\Backend\Block\Template;
use KiwiCommerce\AdminActivity\Api\ActivityRepositoryInterface;
use KiwiCommerce\AdminActivity\Helper\Browser;
/**
* Class ActivityLogListing
* @package KiwiCommerce\AdminActivity\Block\Adminhtml
*/
class ActivityLogListing extends Template
{
/**
* @var ActivityRepositoryInterface
*/
public $activityRepository;
/**
* @var Browser
*/
public $browser;
/**
* Path to template file in theme.
* @var string
*/
public $_template = 'KiwiCommerce_AdminActivity::log_listing.phtml';
/**
* ActivityLogListing constructor.
* @param Template\Context $context
* @param ActivityRepositoryInterface $activityRepository
* @param Browser $browser
*/
public function __construct(
\Magento\Backend\Block\Template\Context $context,
ActivityRepositoryInterface $activityRepository,
Browser $browser
) {
$this->activityRepository = $activityRepository;
$this->browser = $browser;
parent::__construct($context);
}
/**
* Get admin activity log listing
* @return array
*/
public function getLogListing()
{
$id = $this->getRequest()->getParam('id');
$data = $this->activityRepository->getActivityLog($id);
return $data->getData();
}
/**
* Get admin activity details
* @return array
*/
public function getAdminDetails()
{
$id = $this->getRequest()->getParam('id');
$activity = $this->activityRepository->getActivityById($id);
$this->browser->reset();
$this->browser->setUserAgent($activity->getUserAgent());
$browser = $this->browser->__toString();
$logData = [];
$logData['username'] = $activity->getUsername();
$logData['module'] = $activity->getModule();
$logData['name'] = $activity->getName();
$logData['fullaction'] = $activity->getFullaction();
$logData['browser'] = $browser;
$logData['date'] = $activity->getUpdatedAt();
return $logData;
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Block\Adminhtml;
/**
* Class Selector
* @package KiwiCommerce\AdminActivity\Block\Adminhtml
*/
class Selector extends \Magento\Backend\Block\Template
{
/**
* Revert Activity Log action URL
* @return string
*/
public function getRevertUrl()
{
return $this->getUrl('adminactivity/activity/revert');
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customise this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Block\Adminhtml\System\Config\Fieldset;
use \Magento\Backend\Block\Template;
use \Magento\Framework\Data\Form\Element\Renderer\RendererInterface;
/**
* Class Hint
* @package KiwiCommerce\AdminActivity\Block\Adminhtml\System\Config\Fieldset
*/
class Hint extends Template implements RendererInterface
{
/**
* @var \Magento\Framework\Module\ModuleList
*/
private $moduleList;
/**
* Class constructor.
* @param Template\Context $context
* @param \Magento\Framework\Module\ModuleList $moduleList
* @param array $data
*/
public function __construct(
Template\Context $context,
\Magento\Framework\Module\ModuleList $moduleList,
array $data = []
) {
$this->_template = 'KiwiCommerce_AdminActivity::system/config/fieldset/hint.phtml';
parent::__construct($context, $data);
$this->moduleList = $moduleList;
}
/**
* @param \Magento\Framework\Data\Form\Element\AbstractElement $element
* @return string
*/
public function render(\Magento\Framework\Data\Form\Element\AbstractElement $element)
{
$_element = $element;
return $this->toHtml();
}
/**
* @return mixed
*/
public function getModuleVersion()
{
return $this->moduleList->getOne('KiwiCommerce_AdminActivity')['setup_version'];
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Controller\Adminhtml\Activity;
use Magento\Backend\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Backend\App\Action;
/**
* Class Index
* @package KiwiCommerce\AdminActivity\Controller\Adminhtml\Activity
*/
class Index extends Action
{
/**
* @var string
*/
const ADMIN_RESOURCE = 'KiwiCommerce_AdminActivity::activity';
/**
* @var PageFactory
*/
public $resultPageFactory;
/**
* Index constructor.
* @param Context $context
* @param PageFactory $resultPageFactory
*/
public function __construct(
Context $context,
PageFactory $resultPageFactory
) {
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
}
/**
* Index action
* @return \Magento\Backend\Model\View\Result\Page
*/
public function execute()
{
/** @var \Magento\Backend\Model\View\Result\Page $resultPage */
$resultPage = $this->resultPageFactory->create();
$resultPage->setActiveMenu('KiwiCommerce_AdminActivity::activity');
$resultPage->addBreadcrumb(__('KiwiCommerce'), __('Admin Activity'));
$resultPage->getConfig()->getTitle()->prepend(__('Admin Activity'));
return $resultPage;
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Controller\Adminhtml\Activity;
use Magento\Backend\App\Action\Context;
use Magento\Backend\App\Action;
/**
* Class Log
* @package KiwiCommerce\AdminActivity\Controller\Adminhtml\Activity
*/
class Log extends Action
{
/**
* @var \Magento\Framework\Controller\Result\RawFactory
*/
public $resultRawFactory;
/**
* @var \Magento\Framework\View\LayoutFactory
*/
public $layoutFactory;
/**
* Log constructor.
* @param Context $context
* @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
* @param \Magento\Framework\View\LayoutFactory $layoutFactory
*/
public function __construct(
Context $context,
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
\Magento\Framework\View\LayoutFactory $layoutFactory
) {
$this->resultRawFactory = $resultRawFactory;
$this->layoutFactory = $layoutFactory;
parent::__construct($context);
}
/**
* view action
* @return $this
*/
public function execute()
{
$content = $this->layoutFactory->create()
->createBlock(
\KiwiCommerce\AdminActivity\Block\Adminhtml\ActivityLogListing::class
);
/** @var \Magento\Framework\Controller\Result\Raw $resultRaw */
$resultRaw = $this->resultRawFactory->create();
return $resultRaw->setContents($content->toHtml());
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Controller\Adminhtml\Activity;
use Magento\Backend\App\Action\Context;
use Magento\Backend\App\Action;
/**
* Class Revert
* @package KiwiCommerce\AdminActivity\Controller\Adminhtml\Activity
*/
class Revert extends Action
{
/**
* @var \Magento\Framework\Controller\Result\JsonFactory
*/
public $resultJsonFactory;
/**
* @var \KiwiCommerce\AdminActivity\Model\Processor
*/
public $processor;
/**
* Revert constructor.
* @param Context $context
* @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
* @param \KiwiCommerce\AdminActivity\Model\Processor $processor
*/
public function __construct(
Context $context,
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
\KiwiCommerce\AdminActivity\Model\Processor $processor
) {
parent::__construct($context);
$this->resultJsonFactory = $resultJsonFactory;
$this->processor = $processor;
}
/**
* Revert action
* @return \Magento\Backend\Model\View\Result\Page
*/
public function execute()
{
$activityId = $this->getRequest()->getParam('id');
$result = $this->processor->revertActivity($activityId);
return $this->resultJsonFactory->create()->setData($result);
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Controller\Adminhtml\Login;
use Magento\Backend\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Backend\App\Action;
/**
* Class Index
* @package KiwiCommerce\AdminActivity\Controller\Adminhtml\Login
*/
class Index extends Action
{
/**
* @var string
*/
const ADMIN_RESOURCE = 'KiwiCommerce_AdminActivity::login_activity';
/**
* @var PageFactory
*/
public $resultPageFactory;
/**
* Index constructor.
* @param Context $context
* @param PageFactory $resultPageFactory
*/
public function __construct(
Context $context,
PageFactory $resultPageFactory
) {
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
}
/**
* Index action
* @return \Magento\Backend\Model\View\Result\Page
*/
public function execute()
{
/** @var \Magento\Backend\Model\View\Result\Page $resultPage */
$resultPage = $this->resultPageFactory->create();
$resultPage->setActiveMenu('KiwiCommerce_AdminActivity::login_activity');
$resultPage->addBreadcrumb(__('KiwiCommerce'), __('Login Activity'));
$resultPage->getConfig()->getTitle()->prepend(__('Login Activity'));
return $resultPage;
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Cron;
use Psr\Log\LoggerInterface;
use KiwiCommerce\AdminActivity\Helper\Data as Helper;
use KiwiCommerce\AdminActivity\Api\ActivityRepositoryInterface;
/**
* Class ClearLog
* @package KiwiCommerce\AdminActivity\Cron
*/
class ClearLog
{
/**
* Default date format
* @var string
*/
const DATE_FORMAT = 'Y-m-d H:i:s';
/**
* @var LoggerInterface
*/
public $logger;
/**
* @var \Magento\Framework\Stdlib\DateTime\DateTime
*/
public $dateTime;
/**
* @var \KiwiCommerce\AdminActivity\Helper\Data
*/
public $helper;
/**
* @var ActivityRepositoryInterface
*/
public $activityRepository;
/**
* @var \KiwiCommerce\AdminActivity\Api\LoginRepositoryInterface
*/
public $loginRepository;
/**
* ClearLog constructor.
* @param LoggerInterface $logger
* @param \Magento\Framework\Stdlib\DateTime\DateTime $dateTime
* @param Helper $helper
* @param ActivityRepositoryInterface $activityRepository
* @param \KiwiCommerce\AdminActivity\Api\LoginRepositoryInterface $loginRepository
*/
public function __construct(
LoggerInterface $logger,
\Magento\Framework\Stdlib\DateTime\DateTime $dateTime,
Helper $helper,
ActivityRepositoryInterface $activityRepository,
\KiwiCommerce\AdminActivity\Api\LoginRepositoryInterface $loginRepository
) {
$this->logger = $logger;
$this->dateTime = $dateTime;
$this->helper = $helper;
$this->activityRepository = $activityRepository;
$this->loginRepository = $loginRepository;
}
/**
* Return cron cleanup date
* @return null|string
*/
public function __getDate()
{
$timestamp = $this->dateTime->gmtTimestamp();
$day = $this->helper->getConfigValue('CLEAR_LOG_DAYS');
if ($day) {
$timestamp -= $day * 24 * 60 * 60;
return $this->dateTime->gmtDate(self::DATE_FORMAT, $timestamp);
}
return null;
}
/**
* Delete record which date is less than the current date
* @return $this|null
*/
public function execute()
{
try {
if (!$this->helper->isEnable()) {
return $this;
}
if ($date = $this->__getDate()) {
$activities = $this->activityRepository->getListBeforeDate($date);
if (!empty($activities)) {
foreach ($activities as $activity) {
//TODO: Remove activity detail
$activity->delete();
}
}
//TODO: Remove login activity detail
if ($this->helper->isLoginEnable()) {
$activities = $this->loginRepository->getListBeforeDate($date);
if (!empty($activities)) {
foreach ($activities as $activity) {
$activity->delete();
}
}
}
}
} catch (\Exception $e) {
$this->logger->debug($e->getMessage());
}
return null;
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customise this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
/**
* Class Benchmark
* @package KiwiCommerce\AdminActivity\Helper
*/
class Benchmark extends AbstractHelper
{
/**
* Get Benchmark is enable or not
*/
const BENCHMARK_ENABLE = 1;
/**
* @var \KiwiCommerce\AdminActivity\Logger\Logger
*/
public $logger;
/**
* @var String[] Start time of execution
*/
public $startTime;
/**
* @var String[] End time of execution
*/
public $endTime;
/**
* Benchmark constructor.
* @param \Magento\Framework\App\Helper\Context $context
* @param \KiwiCommerce\AdminActivity\Logger\Logger $logger
*/
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\KiwiCommerce\AdminActivity\Logger\Logger $logger
) {
$this->logger = $logger;
parent::__construct($context);
}
/**
* log info about start time in millisecond
* @param $method
* @return void
*/
public function start($method)
{
$this->reset($method);
if (self::BENCHMARK_ENABLE) {
$this->startTime[$method] = round(microtime(true) * 1000);
$this->logger->info("Method: ". $method);
$this->logger->info("Start time: ". $this->startTime[$method]);
\Magento\Framework\Profiler::start($method);
}
}
/**
* log info about end time and time diiference in millisecond
* @param $method
* @return void
*/
public function end($method)
{
if (self::BENCHMARK_ENABLE) {
$this->endTime[$method] = round(microtime(true) * 1000);
$difference = $this->endTime[$method] - $this->startTime[$method];
if ($difference) {
$this->logger->info("Method: ". $method);
$this->logger->info("Ends time: ". $this->endTime[$method]);
$this->logger->info("Time difference in millisecond: ". $difference);
}
\Magento\Framework\Profiler::stop($method);
}
}
/**
* Reset start time and end time
* @param $method
* @return void
*/
public function reset($method)
{
$this->startTime[$method] = 0;
$this->endTime[$method] = 0;
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Helper;
use Magento\Framework\App\Config\ScopeConfigInterface;
/**
* Class Data
* @package KiwiCommerce\AdminActivity\Helper
*/
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
/**
* @var string
*/
const ACTIVITY_ENABLE = 'admin_activity/general/enable';
/**
* @var string
*/
const LOGIN_ACTIVITY_ENABLE = 'admin_activity/general/login_activity';
/**
* @var string
*/
const PAGE_VISIT_ENABLE = 'admin_activity/general/page_visit';
/**
* @var string
*/
const CLEAR_LOG_DAYS = 'admin_activity/general/clearlog';
/**
* @var string
*/
const MODULE_ORDER = 'admin_activity/module/order';
/**
* @var string
*/
const MODULE_PRODUCT = 'admin_activity/module/product';
/**
* @var string
*/
const MODULE_CATEGORY = 'admin_activity/module/category';
/**
* @var string
*/
const MODULE_CUSTOMER = 'admin_activity/module/customer';
/**
* @var string
*/
const MODULE_PROMOTION = 'admin_activity/module/promotion';
/**
* @var string
*/
const MODULE_EMAIL = 'admin_activity/module/email';
/**
* @var string
*/
const MODULE_PAGE = 'admin_activity/module/page';
/**
* @var string
*/
const MODULE_BLOCK = 'admin_activity/module/block';
/**
* @var string
*/
const MODULE_WIDGET = 'admin_activity/module/widget';
/**
* @var string
*/
const MODULE_THEME = 'admin_activity/module/theme';
/**
* @var string
*/
const MODULE_SYSTEM_CONFIG = 'admin_activity/module/system_config';
/**
* @var string
*/
const MODULE_ATTRIBUTE = 'admin_activity/module/attibute';
/**
* @var string
*/
const MODULE_ADMIN_USER = 'admin_activity/module/admin_user';
/**
* @var string
*/
const MODULE_SEO = 'admin_activity/module/seo';
/**
* @var \KiwiCommerce\AdminActivity\Model\Config
*/
public $config;
/**
* @var array
*/
public static $wildcardModels = [
\Magento\Framework\App\Config\Value\Interceptor::class
];
/**
* Data constructor.
* @param \Magento\Framework\App\Helper\Context $context
* @param \KiwiCommerce\AdminActivity\Model\Config $config
*/
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\KiwiCommerce\AdminActivity\Model\Config $config
) {
$this->config = $config;
parent::__construct($context);
}
/**
* Check and return status of module
* @return bool
*/
public function isEnable()
{
$status = $this->scopeConfig->isSetFlag(self::ACTIVITY_ENABLE, ScopeConfigInterface::SCOPE_TYPE_DEFAULT);
if ($status == '1') {
return true;
}
return false;
}
/**
* Check and return status for login activity
* @return bool
*/
public function isLoginEnable()
{
$status = $this->scopeConfig->isSetFlag(self::ACTIVITY_ENABLE, ScopeConfigInterface::SCOPE_TYPE_DEFAULT);
$loginStatus = $this->scopeConfig
->isSetFlag(self::LOGIN_ACTIVITY_ENABLE, ScopeConfigInterface::SCOPE_TYPE_DEFAULT);
if ($status == '1' && $loginStatus == '1') {
return true;
}
return false;
}
/**
* Check and return status for page visit history
* @return bool
*/
public function isPageVisitEnable()
{
$status = $this->scopeConfig->isSetFlag(self::ACTIVITY_ENABLE, ScopeConfigInterface::SCOPE_TYPE_DEFAULT);
$pageVisitStatus = $this->scopeConfig
->isSetFlag(self::PAGE_VISIT_ENABLE, ScopeConfigInterface::SCOPE_TYPE_DEFAULT);
if ($status == '1' && $pageVisitStatus == '1') {
return true;
}
return false;
}
/**
* Get value of system config from path
* @param $path
* @return bool
*/
public function getConfigValue($path)
{
$moduleValue = $this->scopeConfig->getValue(
constant(
'self::'
. $path
),
ScopeConfigInterface::SCOPE_TYPE_DEFAULT
);
if ($moduleValue) {
return $moduleValue;
}
return false;
}
/**
* Get translated label by action name
* @param string $action
* @return string
*/
public function getActionTranslatedLabel($action)
{
return $this->config->getActionLabel($action);
}
/**
* Get all actions
* @return array
*/
public function getAllActions()
{
return $this->config->getActions();
}
/**
* Get activity module name
* @return bool
*/
public function getActivityModuleName($module)
{
return $this->config->getActivityModuleName($module);
}
/**
* Get module name is valid or not
* @param $model
* @return bool
*/
public static function isWildCardModel($model)
{
$model = is_string($model)?$model:get_class($model);
if (in_array($model, self::$wildcardModels)) {
return true;
}
return false;
}
}
<!---
To maintain an effective issue workflow and make sure issues will be solved in a timely manner we kindly ask reporters to follow some guidelines.
Please have a look at our guidelines article before adding a new issue https://kiwicommerce.co.uk/docs/issue_reporting_guidelines/
-->
### Title
<!--
A title should contain a clear, brief explanation of the issue, making emphasis on the most important points.
-->
### Preconditions
<!---
Add everything that would help developer set up the same environment as you have.
For example Magento version, tag, HEAD, PHP & MySQL version, etc.
-->
1.
2.
### Steps to reproduce
<!---
It is important to provide a set of clear steps to reproduce this bug.
If relevant please include code samples
-->
1.
2.
### Expected result
<!--- Tell us what should happen -->
1. [Screenshots, logs or description]
### Actual result
<!--- Tell us what happens instead -->
1. [Screenshots, logs or description]
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customise this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Logger;
/**
* Class Handler
* @package KiwiCommerce\AdminActivity\Logger
*/
class Handler extends \Magento\Framework\Logger\Handler\Base
{
/**
* Logging level
* @var int
*/
public $loggerType = \KiwiCommerce\AdminActivity\Logger\Logger::INFO;
/**
* File name
* @var string
*/
public $fileName = '/var/log/adminactivity.log';
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customise this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Logger;
/**
* Class Logger
* @package KiwiCommerce\AdminActivity\Logger
*/
class Logger extends \Monolog\Logger
{
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Model;
use \Magento\Framework\Model\AbstractModel;
/**
* Class Activity
* @package KiwiCommerce\Activity\Model
*/
class Activity extends AbstractModel
{
/**
* @var string
*/
const ACTIVITY_ID = 'entity_id'; // We define the id field name
/**
* Initialize resource model
* @return void
*/
public function _construct()
{
$this->_init('KiwiCommerce\AdminActivity\Model\ResourceModel\Activity');
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Model\Activity;
use \Magento\Framework\Model\AbstractModel;
/**
* Class Status
* @package KiwiCommerce\AdminActivity\Model\Activity
*/
class Status extends AbstractModel
{
/**
* @var Int
*/
const ACTIVITY_NONE = 0;
/**
* @var Int
*/
const ACTIVITY_REVERTABLE = 1;
/**
* @var Int
*/
const ACTIVITY_REVERT_SUCCESS = 2;
/**
* @var Int
*/
const ACTIVITY_FAIL = 3;
/**
* @var \KiwiCommerce\AdminActivity\Model\ActivityFactory
*/
public $activityFactory;
/**
* Status constructor.
* @param \KiwiCommerce\AdminActivity\Model\ActivityFactory $activityFactory
*/
public function __construct(
\KiwiCommerce\AdminActivity\Model\ActivityFactory $activityFactory
) {
$this->activityFactory = $activityFactory;
}
/**
* Set success revert status
* @param $activityId
* @return void
*/
public function markSuccess($activityId)
{
$activityModel = $this->activityFactory->create()->load($activityId);
$activityModel->setIsRevertable(self::ACTIVITY_REVERT_SUCCESS);
$activityModel->save();
}
/**
* Set fail revert status
* @param $activityId
* @return void
*/
public function markFail($activityId)
{
$activityModel = $this->activityFactory->create()->load($activityId);
$activityModel->setIsRevertable(self::ACTIVITY_FAIL);
$activityModel->save();
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Model\Activity;
/**
* Class SystemConfig
* @package KiwiCommerce\AdminActivity\Model\Activity
*/
class SystemConfig implements \KiwiCommerce\AdminActivity\Api\Activity\ModelInterface
{
/**
* @var \Magento\Framework\DataObject
*/
public $dataObject;
/**
* @var \Magento\Framework\App\Config\ValueFactory
*/
public $valueFactory;
/**
* @var \Magento\Framework\App\Config\Storage\WriterInterface
*/
public $configWriter;
/**
* SystemConfig constructor.
* @param \Magento\Framework\DataObject $dataObject
* @param \Magento\Framework\App\Config\ValueFactory $valueFactory
* @param \Magento\Framework\App\Config\Storage\WriterInterface $configWriter
*/
public function __construct(
\Magento\Framework\DataObject $dataObject,
\Magento\Framework\App\Config\ValueFactory $valueFactory,
\Magento\Framework\App\Config\Storage\WriterInterface $configWriter
) {
$this->dataObject = $dataObject;
$this->valueFactory = $valueFactory;
$this->configWriter = $configWriter;
}
/**
* Get config path
* @param $model
* @return string
*/
public function getPath($model)
{
if ($model->getData('path')) {
return current(
explode(
'/',
$model->getData('path')
)
);
}
return '';
}
/**
* Get old activity data of system config module
* @param $model
* @return mixed
*/
public function getOldData($model)
{
$path = $this->getPath($model);
$systemData = $this->valueFactory->create()->getCollection()->addFieldToFilter('path', ['like'=> $path.'/%']);
$data = [];
foreach ($systemData->getData() as $config) {
$splittedPath = explode('/', $config['path']);
if (count($splittedPath) === 2) {
[$group, $field] = explode('/', $config['path']);
} else {
[$path, $group, $field] = explode('/', $config['path']);
}
$data[$group]['fields'][$field]['value'] = $config['value'];
}
return $this->dataObject->setData($data);
}
/**
* Get edit activity data of system config module
* @param $model
* @return mixed
*/
public function getEditData($model, $fieldArray)
{
$logData = [];
$path = $this->getPath($model);
$result = $this->collectAdditionalData($model->getOrigData(), $model->getGroups());
$model->setConfig('System Configuration');
$model->setId($path);
if (!empty($result) && is_array($result)) {
foreach ($result as $group => $fields) {
if (!empty($fields['fields'])) {
foreach ($fields['fields'] as $field => $value) {
if (empty($value)) {
continue;
}
$fieldPath = implode('/', [
$path, $group, $field
]);
$logData[$fieldPath] = [
'old_value' => $value['old_value'],
'new_value' => $value['new_value']
];
}
}
}
}
return $logData;
}
/**
* Get revert activity data of system config module
* @param $logData
* @param $scopeId
* @return bool
*/
public function revertData($logData, $scopeId)
{
if (!empty($logData)) {
foreach ($logData as $log) {
$this->configWriter->save(
$log->getFieldName(),
$log->getOldValue(),
\Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
$scopeId
);
}
}
return true;
}
/**
* Set additional data
* @param $oldData
* @param $newData
* @return array
*/
public function collectAdditionalData($oldData, $newData)
{
$result = [];
if (!empty($oldData) && is_array($oldData)) {
foreach ($oldData as $key => $value) {
if (isset($newData[$key])) {
if (is_array($value)) {
$result[$key] = $this->collectAdditionalData($value, $newData[$key]);
} elseif (!is_array($newData[$key]) && (string)$value != (string)$newData[$key]) {
$result['new_value'] = (string)$newData[$key];
$result['old_value'] = $value;
}
}
}
}
return $result;
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Model\Activity;
/**
* Class ThemeConfig
* @package KiwiCommerce\AdminActivity\Model\Activity
*/
class ThemeConfig implements \KiwiCommerce\AdminActivity\Api\Activity\ModelInterface
{
/**
* @var \Magento\Framework\DataObject
*/
public $dataObject;
/**
* @var \Magento\Framework\App\Config\ValueFactory
*/
public $valueFactory;
/**
* @var \Magento\Framework\App\Config\Storage\WriterInterface
*/
public $configWriter;
/**
* Request
* @var \Magento\Framework\App\RequestInterface
*/
public $request;
/**
* ThemeConfig constructor.
* @param \Magento\Framework\DataObject $dataObject
* @param \Magento\Framework\App\Config\ValueFactory $valueFactory
* @param \Magento\Framework\App\RequestInterface $request
* @param \Magento\Framework\App\Config\Storage\WriterInterface $configWriter
*/
public function __construct(
\Magento\Framework\DataObject $dataObject,
\Magento\Framework\App\Config\ValueFactory $valueFactory,
\Magento\Framework\App\RequestInterface $request,
\Magento\Framework\App\Config\Storage\WriterInterface $configWriter
) {
$this->dataObject = $dataObject;
$this->valueFactory = $valueFactory;
$this->request = $request;
$this->configWriter = $configWriter;
}
/**
* Get config path of theme configuration
* @param $model
* @return string
*/
public function getPath($model)
{
if ($model->getData('path')) {
return current(
explode(
'/',
$model->getData('path')
)
);
}
return '';
}
/**
* Get old activity data of theme configuration
* @param $model
* @return mixed
*/
public function getOldData($model)
{
$path = $this->getPath($model);
$systemData = $this->valueFactory->create()->getCollection()
->addFieldToFilter('path', ['like'=> $path.'/%']);
$data = [];
foreach ($systemData->getData() as $config) {
$path = str_replace('design_', '', str_replace('/', '_', $config['path']));
$data[$path] = $config['value'];
}
return $data;
}
/**
* Get edit activity data of theme configuration
* @param $model
* @return mixed
*/
public function getEditData($model, $fieldArray)
{
$path = 'stores/scope_id/'.$model->getScopeId();
$oldData = $this->getOldData($model);
$newData = $this->request->getPostValue();
$result = $this->collectAdditionalData($oldData, $newData, $fieldArray);
$model->setConfig('Theme Configuration');
$model->setId($path);
return $result;
}
/**
* Get revert activity data of theme configuration
* @param $logData
* @param $scopeId
* @return bool
*/
public function revertData($logData, $scopeId, $scope)
{
if (!empty($logData)) {
foreach ($logData as $log) {
$this->configWriter->save(
$log->getFieldName(),
$log->getOldValue(),
$scope,
$scopeId
);
}
}
return true;
}
/**
* Set additional data
* @param $oldData
* @param $newData
* @return array
*/
public function collectAdditionalData($oldData, $newData, $fieldArray)
{
$logData = [];
foreach ($newData as $key => $value) {
if (in_array($key, $fieldArray)) {
continue;
}
$newValue = !empty($value) ? $value : '';
$oldValue = !empty($oldData[$key]) ? $oldData[$key] : '';
if ($newValue != $oldValue) {
$key = 'design/'.preg_replace('/_/', '/', $key, 1);
$logData[$key] = [
'old_value' => $oldValue,
'new_value'=> $newValue
];
}
}
return $logData;
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Model;
use \Magento\Framework\Model\AbstractModel;
/**
* Class Activity
* @package KiwiCommerce\Activity\Model
*/
class ActivityLog extends AbstractModel
{
/**
* @var string
*/
const ACTIVITYLOG_ID = 'entity_id'; // We define the id fieldname
/**
* Initialize resource model
* @return void
*/
public function _construct()
{
$this->_init('KiwiCommerce\AdminActivity\Model\ResourceModel\ActivityLog');
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Model;
use \Magento\Framework\Model\AbstractModel;
/**
* Class ActivityLogDetail
* @package KiwiCommerce\AdminActivity\Model
*/
class ActivityLogDetail extends AbstractModel
{
/**
* @var string
*/
const ACTIVITYLOGDETAIL_ID = 'entity_id'; // We define the id fieldname
/**
* Initialize resource model
* @return void
*/
public function _construct()
{
$this->_init('KiwiCommerce\AdminActivity\Model\ResourceModel\ActivityLogDetail');
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Model;
use KiwiCommerce\AdminActivity\Model\Activity\SystemConfig;
/**
* Class ActivityRepository
* @package KiwiCommerce\AdminActivity\Model
*/
class ActivityRepository implements \KiwiCommerce\AdminActivity\Api\ActivityRepositoryInterface
{
/**
* @var string
*/
const THEME_MODULE = 'Themes';
/**
* @var string
*/
const QTY_FIELD = 'qty';
/**
* @var ActivityFactory
*/
public $activityFactory;
/**
* @var ResourceModel\Activity\CollectionFactory
*/
public $collectionFactory;
/**
* @var ActivityLogDetailFactory
*/
public $activityLogDetailFactory;
/**
* @var ActivityLogFactory
*/
public $activityLogFactory;
/**
* @var ResourceModel\ActivityLog\CollectionFactory
*/
public $LogCollectionFactory;
/**
* @var SystemConfig
*/
public $systemConfig;
/**
* @var Activity\ThemeConfig
*/
public $themeConfig;
/**
* Object Manager instance
* @var \Magento\Framework\ObjectManagerInterface
*/
public $objectManager;
/**
* ActivityRepository constructor.
* @param ActivityFactory $activityFactory
* @param ResourceModel\Activity\CollectionFactory $collectionFactory
* @param ActivityLogDetailFactory $activityLogDetailFactory
* @param ActivityLogFactory $activityLogFactory
* @param ResourceModel\ActivityLog\CollectionFactory $LogCollectionFactory
* @param SystemConfig $systemConfig
* @param Activity\ThemeConfig $themeConfig
* @param \Magento\Framework\ObjectManagerInterface $objectManager
*/
public function __construct(
\KiwiCommerce\AdminActivity\Model\ActivityFactory $activityFactory,
\KiwiCommerce\AdminActivity\Model\ResourceModel\Activity\CollectionFactory $collectionFactory,
\KiwiCommerce\AdminActivity\Model\ActivityLogDetailFactory $activityLogDetailFactory,
\KiwiCommerce\AdminActivity\Model\ActivityLogFactory $activityLogFactory,
\KiwiCommerce\AdminActivity\Model\ResourceModel\ActivityLog\CollectionFactory $LogCollectionFactory,
\KiwiCommerce\AdminActivity\Model\Activity\SystemConfig $systemConfig,
\KiwiCommerce\AdminActivity\Model\Activity\ThemeConfig $themeConfig,
\Magento\Framework\ObjectManagerInterface $objectManager
) {
$this->activityFactory = $activityFactory;
$this->collectionFactory = $collectionFactory;
$this->activityLogDetailFactory = $activityLogDetailFactory;
$this->activityLogFactory = $activityLogFactory;
$this->LogCollectionFactory = $LogCollectionFactory;
$this->systemConfig = $systemConfig;
$this->themeConfig = $themeConfig;
$this->objectManager = $objectManager;
}
/**
* Array of protected fields
* @return array
*/
public function protectedFields()
{
return [
'password'
];
}
/**
* Get all admin activity data
* @return ResourceModel\Activity\Collection
*/
public function getList()
{
$collection = $this->collectionFactory->create();
return $collection;
}
/**
* Get all admin activity data before date
* @param $endDate
* @return $this
*/
public function getListBeforeDate($endDate)
{
$collection = $this->collectionFactory->create()
->addFieldToSelect('entity_id')
->addFieldToFilter('created_at', ["lteq" => $endDate]);
return $collection;
}
/**
* Remove activity log entry
* @param $activityId
* @return void
*/
public function deleteActivityById($activityId)
{
$model = $this->activityFactory->create();
$model->load($activityId);
$model->delete();
}
/**
* Get all admin activity detail by activity id
* @param $activityId
* @return $this
*/
public function getActivityDetail($activityId)
{
$data = $this->activityLogDetailFactory->create()
->load($activityId, 'activity_id');
return $data;
}
/**
* Get all admin activity log by activity id
* @param $activityId
* @return $this
*/
public function getActivityLog($activityId)
{
$collection = $this->LogCollectionFactory->create()
->addFieldToFilter('activity_id', ["eq" => $activityId]);
return $collection;
}
/**
* Get method name
* @param $field
* @return string
*/
public function getMethodName($field)
{
return implode(
'',
array_map(
"ucfirst",
array_map(
"strtolower",
explode('_', $field)
)
)
);
}
/**
* Revert last changes made in module
* @param $activity
* @return bool
*/
public function revertActivity($activity)
{
$logData = $this->getActivityLog($activity->getId());
$detailModel = $this->getActivityDetail($activity->getId());
if (\KiwiCommerce\AdminActivity\Helper\Data::isWildCardModel($detailModel->getModelClass())) {
if ($activity->getModule()==self::THEME_MODULE) {
return $this->themeConfig->revertData($logData, $activity->getStoreId(), $activity->getScope());
}
return $this->systemConfig->revertData($logData, $activity->getStoreId());
}
$model = $this->objectManager->get($detailModel->getModelClass())
->setStoreId($activity->getStoreId())
->setScope($activity->getScope())
->load($detailModel->getItemId());
if ($model->getId()) {
foreach ($logData as $log) {
if ($this->isFieldProtected($log->getFieldName())) {
continue;
}
if ($log->getFieldName()==self::QTY_FIELD) {
$model-> setStockData(['qty' => $log->getOldValue()]);
}
$method = 'set'.$this->getMethodName($log->getFieldName());
$model->{$method}($log->getOldValue());
}
$model->setStoreId($activity->getStoreId());
$model->save();
return true;
}
return false;
}
/**
* Get old data for system config module
* @param $model
* @return bool
*/
public function getOldData($model)
{
if (\KiwiCommerce\AdminActivity\Helper\Data::isWildCardModel($model)) {
return $this->systemConfig->getOldData($model);
}
$data = $this->objectManager->get(get_class($model))->load($model->getId());
if ($data) {
return $data;
}
return false;
}
/**
* Get admin activity by id
* @param $activityId
* @return $this
*/
public function getActivityById($activityId)
{
return $this->activityFactory->create()->load($activityId);
}
/**
* Check field is protected or not
* @param $fieldName
* @return bool
*/
public function isFieldProtected($fieldName)
{
$fieldArray = $this->protectedFields();
if (in_array($fieldName, $fieldArray)) {
return true;
}
return false;
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Model;
/**
* Class Config
* @package KiwiCommerce\AdminActivity\Model
*/
class Config
{
/**
* Merged adminactivity.xml config
* @var array
*/
public $_xmlConfig;
/**
* Translated and sorted labels
* @var array
*/
public $_labels = [];
/**
* Config constructor.
* @param Config\Data $dataStorage
*/
public function __construct(
\KiwiCommerce\AdminActivity\Model\Config\Data $dataStorage
) {
$this->_xmlConfig = $dataStorage->get('config');
}
/**
* Get all action labels translated and sorted ASC
* @return array
*/
public function getActions()
{
if (!$this->_labels && isset($this->_xmlConfig['actions'])) {
foreach ($this->_xmlConfig['actions'] as $id => $label) {
$this->_labels[$id] = __($label);
}
asort($this->_labels);
}
return $this->_labels;
}
/**
* List of all full actions
* @return array
*/
public function getControllerActions()
{
$actions = [];
foreach ($this->_xmlConfig as $module => $config) {
if(isset($config['actions'])) {
$actions = array_merge($actions, array_keys($config['actions']));
}
}
return $actions;
}
/**
* Get logging action translated label
* @param string $action
* @return \Magento\Framework\Phrase|string
*/
public function getActionLabel($action)
{
if (isset($this->_xmlConfig['actions'])
&& array_key_exists(
$action,
$this->_xmlConfig['actions']
)
) {
return __($this->_xmlConfig['actions'][$action]);
}
return $action;
}
/**
* Get event by action
* @param $action
* @return bool
*/
public function getEventByAction($action)
{
foreach ($this->_xmlConfig as $module => $config) {
if (isset($config['actions']) && array_key_exists($action, $config['actions'])) {
return $config['actions'][$action];
}
}
return false;
}
/**
* Return Model class name
* @param $module
* @return string
*/
public function getEventModel($module)
{
if (!array_key_exists($module, $this->_xmlConfig)) {
return false;
}
return $this->_xmlConfig[$module]['model'];
}
/**
* Return model label name
* @param $module
* @return bool
*/
public function getActivityModuleName($module)
{
if (!array_key_exists($module, $this->_xmlConfig)) {
return false;
}
return $this->_xmlConfig[$module]['label'];
}
/**
* Return model class name
* @param $module
* @return string
*/
public function getTrackFieldModel($module)
{
if (!array_key_exists($module, $this->_xmlConfig)) {
return false;
}
return $this->_xmlConfig[$module]['config']['trackfield'];
}
/**
* Return module constant
* @param $module
* @return bool
*/
public function getActivityModuleConstant($module)
{
if (!array_key_exists($module, $this->_xmlConfig)) {
return false;
}
return $this->_xmlConfig[$module]['config']['configpath'];
}
/**
* Return module edit url
* @param $module
* @return bool
*/
public function getActivityModuleEditUrl($module)
{
if (!array_key_exists($module, $this->_xmlConfig)) {
return false;
}
return $this->_xmlConfig[$module]['config']['editurl'];
}
/**
* Return module item name
* @param $module
* @return bool
*/
public function getActivityModuleItemField($module)
{
if (!array_key_exists($module, $this->_xmlConfig)) {
return false;
}
return $this->_xmlConfig[$module]['config']['itemfield'];
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Model\Config;
/**
* Class Converter
* @package KiwiCommerce\AdminActivity\Model\Config
*/
class Converter implements \Magento\Framework\Config\ConverterInterface
{
/**
* Convert actions in array from Xpath object
* @param \DOMDocument $source
* @return array
*/
public function convert($source)
{
$result = ['config' => []];
$xpath = new \DOMXPath($source);
$result['config']['actions'] = $this->_getActions($xpath);
$modules = $xpath->query('/config/modules/module');
foreach ($modules as $module) {
$moduleId = $module->attributes->getNamedItem('name')->nodeValue;
$result['config'][$moduleId] = $this->_processModule($module, $moduleId);
}
return $result;
}
/**
* Retrieves actions array from Xpath object
* @param \DOMXPath $xpath
* @return array
*/
public function _getActions($xpath)
{
$result = [];
$actions = $xpath->query('/config/actions/action');
/** @var \DOMNode $action */
foreach ($actions as $action) {
$actionId = $action->attributes->getNamedItem('id')->nodeValue;
foreach ($action->childNodes as $label) {
if ($label->nodeName == 'label') {
$result[$actionId] = $label->nodeValue;
}
}
}
return $result;
}
/**
* Convert module node to array
*
* @param $module
* @param $moduleId
* @return array
*/
public function _processModule($module, $moduleId)
{
$result = [];
foreach ($module->childNodes as $params) {
switch ($params->nodeName) {
case 'label':
$result['label'] = $params->nodeValue;
break;
case 'models':
$result['model'] = $this->_processModels($params);
break;
case 'events':
$result['actions'] = $this->_processEvents($params, $moduleId);
break;
case 'config':
$result['config'] = $this->_processConfig($params);
break;
}
}
return $result;
}
/**
* @param $events
* @return array
*/
public function _processModels($events)
{
$result = [];
foreach ($events->childNodes as $event) {
if ($event->nodeName == 'class') {
$result[] = $event->attributes->getNamedItem('name')->nodeValue;
}
}
return $result;
}
/**
* Convert events node to array
*
* @param $events
* @param $moduleId
* @return array
*/
public function _processEvents($events, $moduleId)
{
$result = [];
foreach ($events->childNodes as $event) {
if ($event->nodeName == 'event') {
$result[$event->attributes->getNamedItem('controller_action')->nodeValue] = [
'action' => $event->attributes->getNamedItem('action_alias')->nodeValue,
'module' => $moduleId
];
$postDispatch = $event->attributes->getNamedItem('post_dispatch');
if ($postDispatch !== null) {
$result[$event->attributes->getNamedItem('controller_action')->nodeValue]['post_dispatch'] = $postDispatch->nodeValue;
}
}
}
return $result;
}
/**
* Converts config to array
*
* @param $configs
* @return array
*/
public function _processConfig($configs)
{
$result = [];
foreach ($configs->childNodes as $config) {
switch ($config->nodeName) {
case 'trackfield':
$result['trackfield'] = $config->attributes->getNamedItem('method')->nodeValue;
break;
case 'configpath':
$result['configpath'] = $config->attributes->getNamedItem('constant')->nodeValue;
break;
case 'editurl':
$result['editurl'] = $config->attributes->getNamedItem('url')->nodeValue;
break;
case 'itemfield':
$result['itemfield'] = $config->attributes->getNamedItem('field')->nodeValue;
break;
}
}
return $result;
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Model\Config;
/**
* Class Data
* @package KiwiCommerce\AdminActivity\Model\Config
*/
class Data extends \Magento\Framework\Config\Data
{
}
\ No newline at end of file
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Model\Config;
use Magento\Framework\Config\SchemaLocatorInterface;
use Magento\Framework\Module\Dir;
/**
* Class SchemaLocator
* @package KiwiCommerce\AdminActivity\Model\Config
*/
class SchemaLocator implements SchemaLocatorInterface
{
/**
* XML schema for config file.
*/
const CONFIG_FILE_SCHEMA = 'adminactivity.xsd';
/**
* Path to corresponding XSD file with validation rules for merged config
* @var string
*/
public $schema = null;
/**
* Path to corresponding XSD file with validation rules for separate config files
* @var string
*/
public $perFileSchema = null;
/**
* SchemaLocator constructor.
* @param Dir\Reader $moduleReader
*/
public function __construct(\Magento\Framework\Module\Dir\Reader $moduleReader)
{
$configDir = $moduleReader->getModuleDir(Dir::MODULE_ETC_DIR, 'KiwiCommerce_AdminActivity');
$this->schema = $configDir . DIRECTORY_SEPARATOR . self::CONFIG_FILE_SCHEMA;
$this->perFileSchema = $configDir . DIRECTORY_SEPARATOR . self::CONFIG_FILE_SCHEMA;
}
/**
* Get schema
* @return string
*/
public function getSchema()
{
return $this->schema;
}
/**
* Get file schema
* @return string
*/
public function getPerFileSchema()
{
return $this->perFileSchema;
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Model;
use \KiwiCommerce\AdminActivity\Helper\TrackField as Helper;
use \Magento\Framework\App\Request\Http;
use \Magento\Framework\UrlInterface;
/**
* Class Handler
* @package KiwiCommerce\AdminActivity\Model
*/
class Handler
{
/**
* @var Helper
*/
public $helper;
/**
* @var \Magento\Framework\HTTP\Header
*/
public $header;
/**
* @var \Magento\Framework\App\Request\Http
*/
public $request;
/**
* @var UrlInterface
*/
public $urlInterface;
/**
* @var ActivityLogFactory
*/
public $activityLogFactory;
/**
* Handler constructor.
* @param Helper $helper
* @param \Magento\Framework\HTTP\Header $header
* @param Http $request
* @param UrlInterface $urlInterface
* @param ActivityLogFactory $activityLogFactory
*/
public function __construct(
Helper $helper,
\Magento\Framework\HTTP\Header $header,
Http $request,
UrlInterface $urlInterface,
\KiwiCommerce\AdminActivity\Model\ActivityLogFactory $activityLogFactory
) {
$this->helper = $helper;
$this->header = $header;
$this->request = $request;
$this->urlInterface = $urlInterface;
$this->activityLogFactory = $activityLogFactory;
}
/**
* Set log data
* @param $logs
* @return mixed
*/
public function __initLog($logs)
{
if (!empty($logs)) {
foreach ($logs as $field => $value) {
$log = $this->activityLogFactory->create()->setData($value);
$log->setFieldName($field);
$logs[$field] = $log;
}
}
return $logs;
}
/**
* Get add activity log data
* @param $model
* @param $method
* @return mixed
*/
public function modelAdd($model, $method)
{
return $this->__initLog(
$this->helper->getAddData($model, $method)
);
}
/**
* Get edit activity log data
* @param $model
* @param $method
* @return mixed
*/
public function modelEdit($model, $method)
{
return $this->__initLog(
$this->helper->getEditData($model, $method)
);
}
/**
* Get delete activity log data
* @param $model
* @param $method
* @return mixed
*/
public function modelDelete($model, $method)
{
return $this->__initLog(
$this->helper->getDeleteData($model, $method)
);
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Model\Handler;
/**
* Class PostDispatch
* @package KiwiCommerce\AdminActivity\Model\Handler
*/
class PostDispatch
{
/**
* Request
*
* @var \Magento\Framework\App\RequestInterface
*/
public $request;
/**
* Response
*
* @var \Magento\Framework\App\ResponseInterface
*/
public $response;
/**
* @var \Magento\Catalog\Api\ProductRepositoryInterface
*/
public $productRepository;
/**
* @var \Magento\Backend\Model\Session
*/
public $session;
/**
* PostDispatch constructor.
*
* @param \Magento\Framework\App\RequestInterface $request
* @param \Magento\Framework\App\ResponseInterface $response
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
* @param \Magento\Backend\Model\Session $session
*/
public function __construct(
\Magento\Framework\App\RequestInterface $request,
\Magento\Framework\App\ResponseInterface $response,
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
\Magento\Backend\Model\Session $session
) {
$this->request = $request;
$this->response = $response;
$this->productRepository = $productRepository;
$this->session = $session;
}
/**
* @param $model
* @return array
*/
public function getProductAttributes($model)
{
$logData = [];
$status = $this->request->getParam('status', '');
if($status != '') {
$logData['status'] = [
'old_value' => $model->getStatus(),
'new_value' => $status
];
}
$attributes = $this->request->getParam('attributes', []);
if(!empty($attributes)) {
foreach ($attributes as $attribute => $value) {
$logData[$attribute] = [
'old_value' => $model->getData($attribute),
'new_value' => $value
];
}
}
$inventories = $this->request->getParam('inventory', []);
if(!empty($inventories)) {
foreach ($inventories as $field => $value) {
$logData[$field] = [
'old_value' => $model->getData($field),
'new_value' => $value
];
}
}
$websiteIds = $this->request->getParam('remove_website', []);
if ($websiteIds) {
$logData['remove_website_ids'] = [
'old_value' => '[]',
'new_value' => implode(', ', $websiteIds)
];
}
$websiteIds = $this->request->getParam('add_website', []);
if ($websiteIds) {
$logData['add_website_ids'] = [
'old_value' => '[]',
'new_value' => implode(', ', $websiteIds)
];
}
return $logData;
}
/**
* Set product update activity log
* @param $config
* @param $processor
*/
public function productUpdate($config, $processor)
{
$activity = $processor->_initLog();
$activity->setIsRevertable(1);
$selected = $this->request->getParam('selected');
if(empty($selected)) {
$selected = $this->session->getProductIds();
}
if(!empty($selected)) {
foreach ($selected as $id) {
$model = $this->productRepository->getById($id);
$log = clone $activity;
$log->setItemName($model->getData($processor->config->getActivityModuleItemField($config['module'])));
$log->setItemUrl($processor->getEditUrl($model));
$logData = $processor->handler->__initLog($this->getProductAttributes($model));
$logDetail = $processor->_initActivityDetail($model);
$processor->activityLogs[] = [
\KiwiCommerce\AdminActivity\Model\Activity::class => $log,
\KiwiCommerce\AdminActivity\Model\ActivityLog::class => $logData,
\KiwiCommerce\AdminActivity\Model\ActivityLogDetail::class => $logDetail
];
}
}
}
}
\ No newline at end of file
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Model;
use \Magento\Framework\Model\AbstractModel;
/**
* Class Login
* @package KiwiCommerce\Activity\Model
*/
class Login extends AbstractModel
{
/**
* @var string
*/
const LOGIN_ACTIVITY_ID = 'entity_id'; // We define the id field name
/**
* Initialize resource model
* @return void
*/
public function _construct()
{
$this->_init('KiwiCommerce\AdminActivity\Model\ResourceModel\Login');
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Model;
/**
* Class LoginRepository
* @package KiwiCommerce\AdminActivity\Model
*/
class LoginRepository implements \KiwiCommerce\AdminActivity\Api\LoginRepositoryInterface
{
/**
* @var boolean
*/
const LOGIN_SUCCESS = 1;
/**
* @var boolean
*/
const LOGIN_FAILED = 0;
/**
* @var LoginFactory
*/
public $loginFactory;
/**
* @var Processor
*/
public $processor;
/**
* @var object Magento\User\Model\User
*/
public $user;
/**
* @var ResourceModel\Activity\CollectionFactory
*/
public $collectionFactory;
/**
* LoginRepository constructor.
* @param LoginFactory $loginFactory
* @param ResourceModel\Login\CollectionFactory $collectionFactory
* @param Processor $processor
*/
public function __construct(
\KiwiCommerce\AdminActivity\Model\LoginFactory $loginFactory,
\KiwiCommerce\AdminActivity\Model\ResourceModel\Login\CollectionFactory $collectionFactory,
\KiwiCommerce\AdminActivity\Model\Processor $processor
) {
$this->loginFactory = $loginFactory;
$this->collectionFactory = $collectionFactory;
$this->processor = $processor;
}
/**
* Get login user
* @return string
*/
public function getUser()
{
return $this->user;
}
/**
* Set login user
* @param $user
* @return $this
*/
public function setUser($user)
{
$this->user = $user;
return $this;
}
/**
* Set login activity data
* @return mixed
*/
public function _initLoginActivity()
{
$login = $this->loginFactory->create();
$user = $this->getUser();
if($user != null && $user instanceof \Magento\User\Model\User) {
$login->setUsername($user->getUsername());
$login->setName(ucwords($user->getName()));
}
$login->setRemoteIp($this->processor->remoteAddress->getRemoteAddress());
$login->setForwardedIp($this->processor->request->getServer('HTTP_X_FORWARDED_FOR'));
$login->setUserAgent($this->processor->handler->header->getHttpUserAgent());
return $login;
}
/**
* Set login data
* @param $status
* @param $type
* @param string $remark
* @return boolean
*/
public function addLog($status, $type, $remark = '')
{
$login = $this->_initLoginActivity();
$login->setStatus($status);
$login->setType($type);
$login->setRemarks($remark);
$login->save();
return true;
}
/**
* Track login success log
* @return void
*/
public function addSuccessLog()
{
$this->addLog(self::LOGIN_SUCCESS, 'Login');
}
/**
* Track login fail log
* @param string $remark
*/
public function addFailedLog($remark = '')
{
$this->addLog(self::LOGIN_FAILED, 'Login', $remark);
}
/**
* Track logout success log
* @return void
*/
public function addLogoutLog()
{
$this->addLog(self::LOGIN_SUCCESS, 'Logout');
}
/**
* Get all admin activity data before date
* @param $endDate
* @return $this
*/
public function getListBeforeDate($endDate)
{
$collection = $this->collectionFactory->create()
->addFieldToSelect('entity_id')
->addFieldToFilter('created_at', ["lteq" => $endDate]);
return $collection;
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Model\ResourceModel;
use \Magento\Framework\Model\ResourceModel\Db\AbstractDb;
/**
* Class Activity
* @package KiwiCommerce\AdminActivity\Model\ResourceModel
*/
class Activity extends AbstractDb
{
/**
* Initialize resource model
* @return void
*/
public function _construct()
{
// Table Name and Primary Key column
$this->_init('kiwicommerce_activity', 'entity_id');
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Model\ResourceModel\Activity;
use \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
/**
* Class Collection
* @package KiwiCommerce\AdminActivity\Model\ResourceModel\Activity
*/
class Collection extends AbstractCollection
{
/**
* Define resource model
* @return void
*/
public function _construct()
{
$this->_init(
'KiwiCommerce\AdminActivity\Model\Activity',
'KiwiCommerce\AdminActivity\Model\ResourceModel\Activity'
);
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Model\ResourceModel;
use \Magento\Framework\Model\ResourceModel\Db\AbstractDb;
/**
* Class ActivityLog
* @package KiwiCommerce\AdminActivity\Model\ResourceModel
*/
class ActivityLog extends AbstractDb
{
/**
* Initialize resource model
* @return void
*/
public function _construct()
{
// Table Name and Primary Key column
$this->_init('kiwicommerce_activity_log', 'entity_id');
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Model\ResourceModel\ActivityLog;
use \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
/**
* Class Collection
* @package KiwiCommerce\AdminActivity\Model\ResourceModel\ActivityLog
*/
class Collection extends AbstractCollection
{
/**
* Define resource model
* @return void
*/
public function _construct()
{
$this->_init(
'KiwiCommerce\AdminActivity\Model\ActivityLog',
'KiwiCommerce\AdminActivity\Model\ResourceModel\ActivityLog'
);
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Model\ResourceModel;
use \Magento\Framework\Model\ResourceModel\Db\AbstractDb;
/**
* Class ActivityLogDetail
* @package KiwiCommerce\AdminActivity\Model\ResourceModel
*/
class ActivityLogDetail extends AbstractDb
{
/**
* Initialize resource model
* @return void
*/
public function _construct()
{
// Table Name and Primary Key column
$this->_init('kiwicommerce_activity_detail', 'entity_id');
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Model\ResourceModel\ActivityLogDetail;
use \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
/**
* Class Collection
* @package KiwiCommerce\AdminActivity\Model\ResourceModel\ActivityLogDetail
*/
class Collection extends AbstractCollection
{
/**
* Define resource model
* @return void
*/
public function _construct()
{
$this->_init(
'KiwiCommerce\AdminActivity\Model\ActivityLogDetail',
'KiwiCommerce\AdminActivity\Model\ResourceModel\ActivityLogDetail'
);
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Model\ResourceModel;
use \Magento\Framework\Model\ResourceModel\Db\AbstractDb;
/**
* Class Login
* @package KiwiCommerce\AdminActivity\Model\ResourceModel
*/
class Login extends AbstractDb
{
/**
* Initialize resource model
* @return void
*/
public function _construct()
{
// Table Name and Primary Key column
$this->_init('kiwicommerce_login_activity', 'entity_id');
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Model\ResourceModel\Login;
use \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
/**
* Class Collection
* @package KiwiCommerce\AdminActivity\Model\ResourceModel\Login
*/
class Collection extends AbstractCollection
{
/**
* Define resource model
* @return void
*/
public function _construct()
{
$this->_init(
'KiwiCommerce\AdminActivity\Model\Login',
'KiwiCommerce\AdminActivity\Model\ResourceModel\Login'
);
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Observer;
use Magento\Framework\Event\ObserverInterface;
use \KiwiCommerce\AdminActivity\Helper\Data as Helper;
use \KiwiCommerce\AdminActivity\Api\ActivityRepositoryInterface;
/**
* Class DeleteAfter
* @package KiwiCommerce\AdminActivity\Observer
*/
class DeleteAfter implements ObserverInterface
{
/**
* @var string
*/
const SYSTEM_CONFIG = 'adminhtml_system_config_save';
/**
* @var \KiwiCommerce\AdminActivity\Model\Processor
*/
private $processor;
/**
* @var \KiwiCommerce\AdminActivity\Helper\Data
*/
public $helper;
/**
* @var \KiwiCommerce\AdminActivity\Helper\Benchmark
*/
public $benchmark;
/**
* DeleteAfter constructor.
* @param \KiwiCommerce\AdminActivity\Model\Processor $processor
* @param Helper $helper
* @param \KiwiCommerce\AdminActivity\Helper\Benchmark $benchmark
*/
public function __construct(
\KiwiCommerce\AdminActivity\Model\Processor $processor,
\KiwiCommerce\AdminActivity\Helper\Data $helper,
\KiwiCommerce\AdminActivity\Helper\Benchmark $benchmark
) {
$this->processor = $processor;
$this->helper = $helper;
$this->benchmark = $benchmark;
}
/**
* Delete after
* @param \Magento\Framework\Event\Observer $observer
* @return \Magento\Framework\Event\Observer
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$this->benchmark->start(__METHOD__);
if (!$this->helper->isEnable()) {
return $observer;
}
$object = $observer->getEvent()->getObject();
if ($this->processor->validate($object) && ($this->processor->initAction==self::SYSTEM_CONFIG)) {
$this->processor->modelEditAfter($object);
}
$this->processor->modelDeleteAfter($object);
$this->benchmark->end(__METHOD__);
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Observer;
use Magento\Framework\Event\ObserverInterface;
/**
* Class LoadAfter
* @package KiwiCommerce\AdminActivity\Observer
*/
class LoadAfter implements ObserverInterface
{
/**
* @var \KiwiCommerce\AdminActivity\Model\Processor
*/
private $processor;
/**
* @var \KiwiCommerce\AdminActivity\Helper\Data
*/
public $helper;
/**
* @var \KiwiCommerce\AdminActivity\Helper\Benchmark
*/
public $benchmark;
/**
* LoadAfter constructor.
* @param \KiwiCommerce\AdminActivity\Model\Processor $processor
* @param \KiwiCommerce\AdminActivity\Helper\Data $helper
* @param \KiwiCommerce\AdminActivity\Helper\Benchmark $benchmark
*/
public function __construct(
\KiwiCommerce\AdminActivity\Model\Processor $processor,
\KiwiCommerce\AdminActivity\Helper\Data $helper,
\KiwiCommerce\AdminActivity\Helper\Benchmark $benchmark
) {
$this->processor = $processor;
$this->helper = $helper;
$this->benchmark = $benchmark;
}
/**
* Delete after
* @param \Magento\Framework\Event\Observer $observer
* @return \Magento\Framework\Event\Observer
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$this->benchmark->start(__METHOD__);
if (!$this->helper->isEnable()) {
return $observer;
}
$object = $observer->getEvent()->getObject();
$this->processor->modelLoadAfter($object);
$this->benchmark->end(__METHOD__);
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Observer;
use Magento\Framework\Event\ObserverInterface;
use \KiwiCommerce\AdminActivity\Helper\Data as Helper;
/**
* Class LoginFailed
* @package KiwiCommerce\AdminActivity\Observer
*/
class LoginFailed implements ObserverInterface
{
/**
* @var Helper
*/
public $helper;
/**
* @var \Magento\User\Model\User
*/
public $user;
/**
* @var \KiwiCommerce\AdminActivity\Api\LoginRepositoryInterface
*/
public $loginRepository;
/**
* @var \KiwiCommerce\AdminActivity\Helper\Benchmark
*/
public $benchmark;
/**
* LoginFailed constructor.
* @param Helper $helper
* @param \Magento\User\Model\User $user
* @param \KiwiCommerce\AdminActivity\Api\LoginRepositoryInterface $loginRepository
* @param \KiwiCommerce\AdminActivity\Helper\Benchmark $benchmark
*/
public function __construct(
Helper $helper,
\Magento\User\Model\User $user,
\KiwiCommerce\AdminActivity\Api\LoginRepositoryInterface $loginRepository,
\KiwiCommerce\AdminActivity\Helper\Benchmark $benchmark
) {
$this->helper = $helper;
$this->user = $user;
$this->loginRepository = $loginRepository;
$this->benchmark = $benchmark;
}
/**
* Login failed
* @param \Magento\Framework\Event\Observer $observer
* @return void
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$this->benchmark->start(__METHOD__);
if (!$this->helper->isLoginEnable()) {
return $observer;
}
$user = null;
if ($observer->getUserName()) {
$user = $this->user->loadByUsername($observer->getUserName());
}
$this->loginRepository
->setUser($user)
->addFailedLog($observer->getException()->getMessage());
$this->benchmark->end(__METHOD__);
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Observer;
use Magento\Framework\Event\ObserverInterface;
use \KiwiCommerce\AdminActivity\Helper\Data as Helper;
/**
* Class LoginSuccess
* @package KiwiCommerce\AdminActivity\Observer
*/
class LoginSuccess implements ObserverInterface
{
/**
* @var Helper
*/
public $helper;
/**
* @var \KiwiCommerce\AdminActivity\Api\LoginRepositoryInterface
*/
public $loginRepository;
/**
* @var \KiwiCommerce\AdminActivity\Helper\Benchmark
*/
public $benchmark;
/**
* LoginSuccess constructor.
* @param Helper $helper
* @param \KiwiCommerce\AdminActivity\Api\LoginRepositoryInterface $loginRepository
* @param \KiwiCommerce\AdminActivity\Helper\Benchmark $benchmark
*/
public function __construct(
Helper $helper,
\KiwiCommerce\AdminActivity\Api\LoginRepositoryInterface $loginRepository,
\KiwiCommerce\AdminActivity\Helper\Benchmark $benchmark
) {
$this->helper = $helper;
$this->loginRepository = $loginRepository;
$this->benchmark = $benchmark;
}
/**
* Login success
* @param \Magento\Framework\Event\Observer $observer
* @return void
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$this->benchmark->start(__METHOD__);
if (!$this->helper->isLoginEnable()) {
return $observer;
}
$this->loginRepository
->setUser($observer->getUser())
->addSuccessLog();
$this->benchmark->end(__METHOD__);
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Observer;
use Magento\Framework\Event\ObserverInterface;
use \KiwiCommerce\AdminActivity\Helper\Data as Helper;
/**
* Class PostDispatch
* @package KiwiCommerce\AdminActivity\Observer
*/
class PostDispatch implements ObserverInterface
{
/**
* @var \KiwiCommerce\AdminActivity\Model\Processor
*/
private $processor;
/**
* @var Helper
*/
public $helper;
/**
* @var \KiwiCommerce\AdminActivity\Helper\Benchmark
*/
public $benchmark;
/**
* PostDispatch constructor.
* @param \KiwiCommerce\AdminActivity\Model\Processor $processor
* @param Helper $helper
* @param \KiwiCommerce\AdminActivity\Helper\Benchmark $benchmark
*/
public function __construct(
\KiwiCommerce\AdminActivity\Model\Processor $processor,
Helper $helper,
\KiwiCommerce\AdminActivity\Helper\Benchmark $benchmark
) {
$this->processor = $processor;
$this->helper = $helper;
$this->benchmark = $benchmark;
}
/**
* Post dispatch
* @param \Magento\Framework\Event\Observer $observer
* @return \Magento\Framework\Event\Observer
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$this->benchmark->start(__METHOD__);
if (!$this->helper->isEnable()) {
return $observer;
}
$this->processor->saveLogs();
$this->benchmark->end(__METHOD__);
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Observer;
use Magento\Framework\Event\ObserverInterface;
use \KiwiCommerce\AdminActivity\Helper\Data as Helper;
/**
* Class SaveAfter
* @package KiwiCommerce\AdminActivity\Observer
*/
class SaveAfter implements ObserverInterface
{
/**
* @var string
*/
const ACTION_MASSCANCEL = 'massCancel';
/**
* @var string
*/
const SYSTEM_CONFIG = 'adminhtml_system_config_save';
/**
* @var \KiwiCommerce\AdminActivity\Model\Processor
*/
private $processor;
/**
* @var Helper
*/
public $helper;
/**
* @var \KiwiCommerce\AdminActivity\Helper\Benchmark
*/
public $benchmark;
/**
* SaveAfter constructor.
* @param \KiwiCommerce\AdminActivity\Model\Processor $processor
* @param Helper $helper
* @param \KiwiCommerce\AdminActivity\Helper\Benchmark $benchmark
*/
public function __construct(
\KiwiCommerce\AdminActivity\Model\Processor $processor,
Helper $helper,
\KiwiCommerce\AdminActivity\Helper\Benchmark $benchmark
) {
$this->processor = $processor;
$this->helper = $helper;
$this->benchmark = $benchmark;
}
/**
* Save after
* @param \Magento\Framework\Event\Observer $observer
* @return \Magento\Framework\Event\Observer|boolean
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$this->benchmark->start(__METHOD__);
if (!$this->helper->isEnable()) {
return $observer;
}
$object = $observer->getEvent()->getObject();
if ($object->getCheckIfIsNew()) {
if ($this->processor->initAction==self::SYSTEM_CONFIG) {
$this->processor->modelEditAfter($object);
}
$this->processor->modelAddAfter($object);
} else {
if ($this->processor->validate($object)) {
if ($this->processor->eventConfig['action']==self::ACTION_MASSCANCEL) {
$this->processor->modelDeleteAfter($object);
}
$this->processor->modelEditAfter($object);
}
}
$this->benchmark->end(__METHOD__);
return true;
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Observer;
use Magento\Framework\Event\ObserverInterface;
use \KiwiCommerce\AdminActivity\Helper\Data as Helper;
use KiwiCommerce\AdminActivity\Api\ActivityRepositoryInterface;
/**
* Class SaveBefore
* @package KiwiCommerce\AdminActivity\Observer
*/
class SaveBefore implements ObserverInterface
{
/**
* @var Helper
*/
public $helper;
/**
* @var \KiwiCommerce\AdminActivity\Model\Processor
*/
public $processor;
/**
* @var ActivityRepositoryInterface
*/
public $activityRepository;
/**
* @var \KiwiCommerce\AdminActivity\Helper\Benchmark
*/
public $benchmark;
/**
* SaveBefore constructor.
* @param Helper $helper
* @param \KiwiCommerce\AdminActivity\Model\Processor $processor
* @param ActivityRepositoryInterface $activityRepository
* @param \KiwiCommerce\AdminActivity\Helper\Benchmark $banchmark
*/
public function __construct(
Helper $helper,
\KiwiCommerce\AdminActivity\Model\Processor $processor,
ActivityRepositoryInterface $activityRepository,
\KiwiCommerce\AdminActivity\Helper\Benchmark $benchmark
) {
$this->helper = $helper;
$this->processor = $processor;
$this->activityRepository = $activityRepository;
$this->benchmark = $benchmark;
}
/**
* Save before
* @param \Magento\Framework\Event\Observer $observer
* @return \Magento\Framework\Event\Observer
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$this->benchmark->start(__METHOD__);
if (!$this->helper->isEnable()) {
return $observer;
}
$object = $observer->getEvent()->getObject();
if ($object->getId() == 0) {
$object->setCheckIfIsNew(true);
} else {
$object->setCheckIfIsNew(false);
if ($this->processor->validate($object)) {
$origData = $object->getOrigData();
if (!empty($origData)) {
return $observer;
}
$data = $this->activityRepository->getOldData($object);
foreach ($data->getData() as $key => $value) {
$object->setOrigData($key, $value);
}
}
}
$this->benchmark->end(__METHOD__);
return $observer;
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Plugin\App;
/**
* Class Action
* @package KiwiCommerce\AdminActivity\Plugin\App
*/
class Action
{
/**
* @var \KiwiCommerce\AdminActivity\Model\Processor
*/
public $processor;
/**
* @var \KiwiCommerce\AdminActivity\Helper\Benchmark
*/
public $benchmark;
/**
* Action constructor.
* @param \KiwiCommerce\AdminActivity\Model\Processor $processor
* @param \KiwiCommerce\AdminActivity\Helper\Benchmark $benchmark
*/
public function __construct(
\KiwiCommerce\AdminActivity\Model\Processor $processor,
\KiwiCommerce\AdminActivity\Helper\Benchmark $benchmark
) {
$this->processor = $processor;
$this->benchmark = $benchmark;
}
/**
* Get before dispatch data
* @param \Magento\Framework\Interception\InterceptorInterface $controller
* @return void
*/
public function beforeDispatch(\Magento\Framework\Interception\InterceptorInterface $controller)
{
$this->benchmark->start(__METHOD__);
$actionName = $controller->getRequest()->getActionName();
$fullActionName = $controller->getRequest()->getFullActionName();
$this->processor->init($fullActionName, $actionName);
$this->processor->addPageVisitLog($controller->getRequest()->getModuleName());
$this->benchmark->end(__METHOD__);
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Plugin;
use \KiwiCommerce\AdminActivity\Helper\Data as Helper;
/**
* Class Auth
* @package KiwiCommerce\AdminActivity\Plugin
*/
class Auth
{
/**
* @var Helper
*/
public $helper;
/**
* @var \KiwiCommerce\AdminActivity\Api\LoginRepositoryInterface
*/
public $loginRepository;
/**
* @var \KiwiCommerce\AdminActivity\Helper\Benchmark
*/
public $benchmark;
/**
* Auth constructor.
* @param Helper $helper
* @param \KiwiCommerce\AdminActivity\Api\LoginRepositoryInterface $loginRepository
* @param \KiwiCommerce\AdminActivity\Helper\Benchmark $benchmark
*/
public function __construct(
Helper $helper,
\KiwiCommerce\AdminActivity\Api\LoginRepositoryInterface $loginRepository,
\KiwiCommerce\AdminActivity\Helper\Benchmark $benchmark
) {
$this->helper = $helper;
$this->loginRepository = $loginRepository;
$this->benchmark = $benchmark;
}
/**
* Track admin logout activity
* @param \Magento\Backend\Model\Auth $auth
* @param callable $proceed
* @return mixed
*/
public function aroundLogout(\Magento\Backend\Model\Auth $auth, callable $proceed)
{
$this->benchmark->start(__METHOD__);
if ($this->helper->isLoginEnable()) {
$user = $auth->getAuthStorage()->getUser();
$this->loginRepository->setUser($user)->addLogoutLog();
}
$result = $proceed();
$this->benchmark->end(__METHOD__);
return $result;
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Plugin\User;
/**
* Class Delete
* @package KiwiCommerce\AdminActivity\Plugin\User
*/
class Delete
{
/**
* @var \KiwiCommerce\AdminActivity\Helper\Benchmark
*/
public $benchmark;
/**
* Delete constructor.
* @param \KiwiCommerce\AdminActivity\Helper\Benchmark $benchmark
*/
public function __construct(
\KiwiCommerce\AdminActivity\Helper\Benchmark $benchmark
) {
$this->benchmark = $benchmark;
}
/**
* @param \Magento\User\Model\ResourceModel\User $user
* @param callable $proceed
* @param $object
* @return mixed
*/
public function aroundDelete(\Magento\User\Model\ResourceModel\User $user, callable $proceed, $object)
{
$this->benchmark->start(__METHOD__);
$object->load($object->getId());
$result = $proceed($object);
$object->afterDelete();
$this->benchmark->end(__METHOD__);
return $result;
}
}
# Magento 2 - Admin Activity by [KiwiCommerce](https://kiwicommerce.co.uk/)
- Easily track every admin activity like add, edit, delete, print, view, mass update etc.
- Failed attempts of admin login are recorded as well. You get access to the user’s login information and IP address.
- Track page visit history of admin.
- Track fields that have been changed from the backend.
- Allow administrator to revert the modification.
## **Installation**
1. Composer Installation
- Navigate to your Magento root folder<br />
`cd path_to_the_magento_root_directory`
- Then run the following command<br />
`composer require kiwicommerce/module-admin-activity`
- Make sure that composer finished the installation without errors.
2. Command Line Installation
- Backup your web directory and database.
- Download the latest Cron Scheduler installation package kiwicommerce-admin-activity-vvvv.zip [here](https://github.com/kiwicommerce/magento2-admin-activity/releases)
- Navigate to your Magento root folder<br />
`cd path_to_the_magento_root_directory`<br />
- Upload contents of the Admin Activity Log installation package to your Magento root directory
- Then run the following command<br />
`php bin/magento module:enable KiwiCommerce_AdminActivity`<br />
- After installing the extension, run the following command
```
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy
php bin/magento cache:flush
```
- Log out from the backend and log in again.
Find More details on [KiwiCommerce](https://kiwicommerce.co.uk/extensions/magento2-admin-activity)
## Where will it appear in the Admin Panel
### Admin Activity Log
Go to **System > Admin Activity by KiwiCommerce > Admin Activity**. Here you can See the list of admin activity logs and page visit history.
<img src="https://kiwicommerce.co.uk/wp-content/uploads/2018/06/admin-activity-history.png"/><br/>
- Page Visit History
<img src="https://kiwicommerce.co.uk/wp-content/uploads/2018/06/page-visit-history.png"/><br/>
By clicking View in each admin activity log, you can see the slider with admin activity log details.
<img src="https://kiwicommerce.co.uk/wp-content/uploads/2018/05/activity-log-slider.png"/> <br/>
### Login Activity
Go to **System > Admin Activity by KiwiCommerce > Login Activity**. Here you can See the list of login activity logs.
<img src="https://kiwicommerce.co.uk/wp-content/uploads/2018/06/admin-activity-history.png"/><br/>
## Configuration
You need to follow this path. **System > Admin Activity by KiwiCommerce > Configuration**
- General configuration
<img src="https://kiwicommerce.co.uk/wp-content/uploads/2018/05/configuration-general-section.png" /> <br/>
- Allow Module Section
<img src="https://kiwicommerce.co.uk/wp-content/uploads/2018/05/configuration-allow-module-section.png" /> <br/>
## Need Additional Features?
Feel free to get in touch with us at https://kiwicommerce.co.uk/get-in-touch/
## Other KiwiCommerce Extensions
* [Magento 2 Cron Scheduler](https://kiwicommerce.co.uk/extensions/magento2-cron-scheduler/)
* [Magento 2 Login As Customer](https://kiwicommerce.co.uk/extensions/magento2-login-as-customer/)
* [Magento 2 Inventory Log](https://kiwicommerce.co.uk/extensions/magento2-inventory-log/)
* [Magento 2 Enhanced SMTP](https://kiwicommerce.co.uk/extensions/magento2-enhanced-smtp/)
* [Magento 2 Customer Password](https://github.com/kiwicommerce/magento2-customer-password/)
## Contribution
Well unfortunately there is no formal way to contribute, we would encourage you to feel free and contribute by:
- Creating bug reports, issues or feature requests on [Github](https://github.com/kiwicommerce/magento2-admin-activity/issues)
- Submitting pull requests for improvements.
We love answering questions or doubts simply ask us in issue section. We're looking forward to hearing from you!
- Follow us [@KiwiCommerce](https://twitter.com/KiwiCommerce)
- <a href="mailto:support@kiwicommerce.co.uk">Email Us</a>
- Have a look at our [documentation](https://kiwicommerce.co.uk/docs/admin-activity/)
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Setup;
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
/**
* Class InstallSchema
* @package KiwiCommerce\AdminActivity\Setup
*/
class InstallSchema implements InstallSchemaInterface
{
/**
* Installs DB schema for a module
* @param SchemaSetupInterface $setup
* @param ModuleContextInterface $context
* @return void
*/
public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$installer = $setup;
$installer->startSetup();
/**
* Create table 'kiwicommerce_activity'
*/
$table = $installer->getConnection()->newTable(
$installer->getTable('kiwicommerce_activity')
)->addColumn(
'entity_id',
\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
10,
['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
'Primary key for the Table'
)->addColumn(
'username',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
40,
['nullable' => false, 'default' => ''],
'Store admin username'
)->addColumn(
'name',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
64,
['nullable' => false, 'default' => ''],
'Full name of admin'
)->addColumn(
'admin_id',
\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
10,
['nullable' => false, 'unsigned' => true, 'default' => 1],
'Store id of admin user'
)->addColumn(
'store_id',
\Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
5,
['nullable' => false, 'unsigned' => true, 'default' => 0],
'Insert store id'
)->addColumn(
'action_type',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
32,
['nullable' => false, 'default' => ''],
'Action type like New, Edit, View, Delete, Mass Delete, etc'
)->addColumn(
'remote_ip',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
32,
['nullable' => false, 'default' => ''],
'IP address of logged in admin user'
)->addColumn(
'user_agent',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'',
['nullable' => false, 'default' => ''],
'Store browser’s user agent'
)->addColumn(
'module',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
50,
['nullable' => false, 'default' => ''],
'Name of module in which action performed'
)->addColumn(
'item_name',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
255,
['nullable' => false, 'default' => ''],
'Name of item which is effected like product, user, order, etc'
)->addColumn(
'item_url',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
255,
['nullable' => false, 'default' => ''],
'Url of item if possible'
)->addColumn(
'is_revertable',
\Magento\Framework\DB\Ddl\Table::TYPE_BOOLEAN,
'',
['nullable' => true, 'default' => 0],
'0 = Not able revert activity, 1= Able to revert activity'
)->addColumn(
'revert_by',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
64,
['nullable' => false, 'default' => ''],
'Name of admin user who reverted the activity'
)->addColumn(
'created_at',
\Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
null,
['nullable' => false, 'default' => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT],
'The date when the activity was created'
)->addColumn(
'updated_at',
\Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
null,
['nullable' => false, 'default' => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT_UPDATE],
'The date when the activity was modified or reverted'
)->addIndex(
$installer->getIdxName('admin_user', ['user_id']),
['admin_id']
)->addIndex(
$installer->getIdxName('store', ['store_id']),
['store_id']
)->setComment(
'This is master table of admin activity extension'
);
$installer->getConnection()->createTable($table);
/**
* Create table 'kiwicommerce_activity_log'
*/
$table = $installer->getConnection()->newTable(
$installer->getTable('kiwicommerce_activity_log')
)->addColumn(
'entity_id',
\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
10,
['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
'Primary key for the Table'
)->addColumn(
'activity_id',
\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
10,
['nullable' => true, 'unsigned' => true, 'nullable' => false],
'Entity id of kiwicommerce_activity table'
)->addColumn(
'field_name',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
32,
['nullable' => false, 'default' => ''],
'Name of field which is effected'
)->addColumn(
'old_value',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'',
['nullable' => false, 'default' => ''],
'Old value of field'
)->addColumn(
'new_value',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'',
['nullable' => false, 'default' => ''],
'New value of field'
)->addIndex(
$installer->getIdxName('kiwicommerce_activity', ['entity_id']),
['activity_id']
)->addForeignKey(
$installer->getFkName(
'kiwicommerce_activity_log',
'activity_id',
'kiwicommerce_activity',
'entity_id'
),
'activity_id',
$installer->getTable('kiwicommerce_activity'),
'entity_id',
\Magento\Framework\DB\Ddl\Table::ACTION_CASCADE
)->setComment(
'This is activity log table of admin activity extension'
);
$installer->getConnection()->createTable($table);
/**
* Create table 'kiwicommerce_activity_detail'
*/
$table = $installer->getConnection()->newTable(
$installer->getTable('kiwicommerce_activity_detail')
)->addColumn(
'entity_id',
\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
10,
['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
'Primary key for the Table'
)->addColumn(
'activity_id',
\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
10,
['unsigned' => true, 'nullable' => false],
'Entity id of kiwicommerce_activity table'
)->addColumn(
'model_class',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
255,
['nullable' => false, 'default' => ''],
'Name of field which is effected'
)->addColumn(
'item_id',
\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
10,
['nullable' => true],
'Old value of field'
)->addColumn(
'status',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
16,
['nullable' => false, 'default' => ''],
'Status of revert process'
)->addColumn(
'response',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'',
['nullable' => false, 'default' => ''],
'Error message faced during revert process'
)->addIndex(
$installer->getIdxName('kiwicommerce_activity', ['entity_id']),
['activity_id']
)->addForeignKey(
$installer->getFkName(
'kiwicommerce_activity_detail',
'activity_id',
'kiwicommerce_activity',
'entity_id'
),
'activity_id',
$installer->getTable('kiwicommerce_activity'),
'entity_id',
\Magento\Framework\DB\Ddl\Table::ACTION_CASCADE
)->setComment(
'This is activity log details table of admin activity extension'
);
$installer->getConnection()->createTable($table);
// Table creation
$installer->endSetup();
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Setup;
use Magento\Framework\Setup\UninstallInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\App\Config\Storage\Writer;
use KiwiCommerce\AdminActivity\Helper\Data as ActivityHelper;
/**
* Class Uninstall
* @package KiwiCommerce\AdminActivity\Setup
*/
class Uninstall implements UninstallInterface
{
/**
* @var Writer
*/
public $scope;
/**
* Uninstall constructor.
* @param Writer $scopeWriter
*/
public function __construct(Writer $scopeWriter)
{
$this->scope = $scopeWriter;
}
/**
* Module uninstall code
* @param SchemaSetupInterface $setup
* @param ModuleContextInterface $context
* @return void
*/
public function uninstall(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
$connection = $setup->getConnection();
$connection->dropTable($connection->getTableName('kiwicommerce_activity_log'));
$connection->dropTable($connection->getTableName('kiwicommerce_activity_detail'));
$connection->dropTable($connection->getTableName('kiwicommerce_login_activity'));
$connection->dropTable($connection->getTableName('kiwicommerce_activity'));
$setup->endSetup();
$scope = $this->scope;
$scope->delete(ActivityHelper::ACTIVITY_ENABLE);
$scope->delete(ActivityHelper::LOGIN_ACTIVITY_ENABLE);
$scope->delete(ActivityHelper::PAGE_VISIT_ENABLE);
$scope->delete(ActivityHelper::CLEAR_LOG_DAYS);
$scope->delete(ActivityHelper::MODULE_ORDER);
$scope->delete(ActivityHelper::MODULE_PRODUCT);
$scope->delete(ActivityHelper::MODULE_CATEGORY);
$scope->delete(ActivityHelper::MODULE_CUSTOMER);
$scope->delete(ActivityHelper::MODULE_PROMOTION);
$scope->delete(ActivityHelper::MODULE_EMAIL);
$scope->delete(ActivityHelper::MODULE_PAGE);
$scope->delete(ActivityHelper::MODULE_BLOCK);
$scope->delete(ActivityHelper::MODULE_WIDGET);
$scope->delete(ActivityHelper::MODULE_THEME);
$scope->delete(ActivityHelper::MODULE_SYSTEM_CONFIG);
$scope->delete(ActivityHelper::MODULE_ATTRIBUTE);
$scope->delete(ActivityHelper::MODULE_ADMIN_USER);
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Setup;
use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
/**
* Class UpgradeSchema
* @package KiwiCommerce\AdminActivity\Setup
*/
class UpgradeSchema implements UpgradeSchemaInterface
{
/**
* Upgrade DB schema for a module
* @param SchemaSetupInterface $setup
* @param ModuleContextInterface $context
* @return void
*/
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
if (version_compare($context->getVersion(), '0.0.2') < 0) {
$tableName = $setup->getTable('kiwicommerce_activity');
//TODO: Check if the table already exists
if ($setup->getConnection()->isTableExists($tableName) == true) {
$columns = [
'forwarded_ip' => [
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'length' => 32,
'nullable' => true,
'comment' => __('Real ip address if visitor used proxy'),
'after' => 'remote_ip'
]
];
$connection = $setup->getConnection();
foreach ($columns as $name => $definition) {
$connection->addColumn($tableName, $name, $definition);
}
}
}
if (version_compare($context->getVersion(), '0.1.2') < 0) {
$tableName = $setup->getTable('kiwicommerce_activity');
//TODO: Check if the table already exists
if ($setup->getConnection()->isTableExists($tableName) == true) {
$columns = [
'scope' => [
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'length' => 15,
'nullable' => true,
'comment' => __('Scope of activity'),
'after' => 'store_id'
]
];
$connection = $setup->getConnection();
foreach ($columns as $name => $definition) {
$connection->addColumn($tableName, $name, $definition);
}
}
}
if (version_compare($context->getVersion(), '0.1.3') < 0) {
/**
* Create table 'kiwicommerce_login_activity'
*/
$table = $setup->getConnection()->newTable(
$setup->getTable('kiwicommerce_login_activity')
)->addColumn(
'entity_id',
\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
10,
['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
'Primary key for the table'
)->addColumn(
'username',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
40,
['nullable' => false, 'default' => ''],
'Admin username'
)->addColumn(
'name',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
64,
['nullable' => false, 'default' => ''],
'Full name of admin'
)->addColumn(
'remote_ip',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
32,
['nullable' => false, 'default' => ''],
'IP address of logged in admin user'
)->addColumn(
'forwarded_ip',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
32,
['nullable' => false, 'default' => ''],
'Real ip address if visitor used proxy'
)->addColumn(
'user_agent',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'',
['nullable' => false, 'default' => ''],
'Store browser’s user agent'
)->addColumn(
'location',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
255,
['nullable' => false, 'default' => ''],
'Location of visitor'
)->addColumn(
'type',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
8,
['nullable' => false, 'default' => ''],
'Is it Login/Logout?'
)->addColumn(
'status',
\Magento\Framework\DB\Ddl\Table::TYPE_BOOLEAN,
'',
['nullable' => true],
'0 = Faild, 1= Success'
)->addColumn(
'created_at',
\Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
null,
['nullable' => false, 'default' => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT],
'The date when the activity was created'
)->setComment(
'Log all login/logout activity of admin user'
);
$setup->getConnection()->createTable($table);
}
if (version_compare($context->getVersion(), '0.1.4') < 0) {
$tableName = $setup->getTable('kiwicommerce_login_activity');
$connection = $setup->getConnection();
//TODO: Check if the table already exists
if ($connection->isTableExists($tableName) == true) {
$connection->addColumn($tableName, 'remarks', [
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'length' => '',
'nullable' => false,
'default' => '',
'comment' => __('Reason for failed'),
'after' => 'status'
]);
$connection->dropColumn($tableName, 'location', $schemaName = null);
}
}
if (version_compare($context->getVersion(), '0.1.5') < 0) {
$tableName = $setup->getTable('kiwicommerce_activity_log');
//TODO: Check if the table already exists
if ($setup->getConnection()->isTableExists($tableName) == true) {
$definition = [
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'length' => 255
];
$setup->getConnection()->addColumn($tableName, 'field_name', $definition);
}
}
if (version_compare($context->getVersion(), '0.1.6') < 0) {
$tableName = $setup->getTable('kiwicommerce_activity');
$connection = $setup->getConnection();
//TODO: Check if the table already exists
if ($connection->isTableExists($tableName) == true) {
$connection->addColumn($tableName, 'fullaction', [
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'length' => 200,
'nullable' => false,
'default' => '',
'comment' => __('Full Action Name'),
'after' => 'module'
]);
}
}
$setup->endSetup();
}
}
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Test\Unit\Block\Adminhtml;
/**
* Class ActivityLogListingTest
* @package KiwiCommerce\AdminActivity\Test\Unit\Block\Adminhtml
*/
class ActivityLogListingTest extends \PHPUnit\Framework\TestCase
{
public $activityRepositoryMock;
public $browserMock;
public $contextMock;
public $request;
public $activityLogListing;
public $activityModel;
public $userAgent = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:60.0) G...';
public $logData = [
'username' => 'admin',
'module' => 'Admin',
'name' => 'admin admin',
'fullaction' => 'Adminhtml/Index/Index',
'browser' => '',
'date' => '2018-05-18 14:51:23'
];
/**
* @requires PHP 7.0
*/
public function setUp()
{
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$this->activityRepositoryMock = $this->getMockForAbstractClass(
\KiwiCommerce\AdminActivity\Api\ActivityRepositoryInterface::class,
[],
'',
false
);
$this->browserMock = $this->createMock(\KiwiCommerce\AdminActivity\Helper\Browser::class);
$this->request = $this->getMockForAbstractClass(
\Magento\Framework\App\RequestInterface::class,
[],
'',
false
);
$this->contextMock = $objectManager->getObject(
\Magento\Backend\Block\Template\Context::class,
[
'request' => $this->request
]
);
$this->activityModel = $this
->getMockBuilder(\KiwiCommerce\AdminActivity\Model\Activity::class)
->setMethods(
[
'getUserAgent',
'getUsername',
'getModule',
'getName',
'getFullaction',
'getUpdatedAt'
]
)
->disableOriginalConstructor()
->getMock();
$this->activityLogListing = $objectManager->getObject(
\KiwiCommerce\AdminActivity\Block\Adminhtml\ActivityLogListing::class,
[
'activityRepository' => $this->activityRepositoryMock,
'browser' => $this->browserMock,
'context' => $this->contextMock,
]
);
}
/**
* @requires PHP 7.0
*/
public function testGetLogListing()
{
$this->request->expects($this->once())
->method('getParam')
->with('id')
->willReturn(1);
$this->activityRepositoryMock->expects($this->once())
->method('getActivityLog')
->with(1)
->willReturn($this->activityModel);
$this->activityLogListing->getLogListing();
}
/**
* @requires PHP 7.0
*/
public function testGetAdminDetails()
{
$this->request->expects($this->once())
->method('getParam')
->with('id')
->willReturn(1);
$this->activityRepositoryMock->expects($this->once())
->method('getActivityById')
->with(1)
->willReturn($this->activityModel);
$this->browserMock->expects($this->once())
->method('reset')
->willReturnSelf();
$this->activityModel->expects($this->once())
->method('getUserAgent')
->willReturn($this->userAgent);
$this->browserMock->expects($this->once())
->method('setUserAgent')
->with($this->userAgent)
->willReturnSelf();
$this->activityModel->expects($this->once())
->method('getUsername')
->willReturn($this->logData['username']);
$this->activityModel->expects($this->once())
->method('getModule')
->willReturn($this->logData['module']);
$this->activityModel->expects($this->once())
->method('getName')
->willReturn($this->logData['name']);
$this->activityModel->expects($this->once())
->method('getFullaction')
->willReturn($this->logData['fullaction']);
$this->activityModel->expects($this->once())
->method('getUpdatedAt')
->willReturn($this->logData['date']);
$this->assertEquals($this->logData, $this->activityLogListing->getAdminDetails());
}
}
\ No newline at end of file
<?php
/**
* KiwiCommerce
*
* Do not edit or add to this file if you wish to upgrade to newer versions in the future.
* If you wish to customize this module for your needs.
* Please contact us https://kiwicommerce.co.uk/contacts.
*
* @category KiwiCommerce
* @package KiwiCommerce_AdminActivity
* @copyright Copyright (C) 2018 Kiwi Commerce Ltd (https://kiwicommerce.co.uk/)
* @license https://kiwicommerce.co.uk/magento2-extension-license/
*/
namespace KiwiCommerce\AdminActivity\Test\Unit\Block\Adminhtml;
/**
* Class SelectorTest
* @package KiwiCommerce\AdminActivity\Test\Unit\Block\Adminhtml
*/
class SelectorTest extends \PHPUnit\Framework\TestCase
{
public $urlBuiler;
public $revertUrl = 'http://magento.com/adminactivity/activity/revert';
public $selector;
/**
* @requires PHP 7.0
*/
public function setUp()
{
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$this->urlBuiler = $this->createMock(\Magento\Framework\UrlInterface::class);
$this->selector = $objectManager->getObject(
\KiwiCommerce\AdminActivity\Block\Adminhtml\Selector::class,
[
'_urlBuilder' => $this->urlBuiler,
]
);
}
/**
* @requires PHP 7.0
*/
public function testGetRevertUrl()
{
$this->urlBuiler->expects($this->once())
->method('getUrl')
->with('adminactivity/activity/revert')
->willReturn($this->revertUrl);
$this->assertEquals($this->revertUrl, $this->selector->getRevertUrl());
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment