ltsecurityinc安防产品在线商城代码
This commit is contained in:
+36
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Admin Actions Log for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\AdminActionsLog\Model\LoginAttempt\Notification;
|
||||
|
||||
use Amasty\AdminActionsLog\Api\Data\LoginAttemptInterface;
|
||||
use Amasty\AdminActionsLog\Model\LoginAttempt\Notification\Type\AttemptNotificatorInterface;
|
||||
|
||||
class Processor
|
||||
{
|
||||
/**
|
||||
* @var AttemptNotificatorInterface[]
|
||||
*/
|
||||
private $notifierTypes;
|
||||
|
||||
public function __construct(array $notifierTypes = [])
|
||||
{
|
||||
$this->notifierTypes = $notifierTypes;
|
||||
}
|
||||
|
||||
public function execute(LoginAttemptInterface $loginAttempt)
|
||||
{
|
||||
$attemptStatus = $loginAttempt->getStatus();
|
||||
|
||||
if ($attemptStatus !== null && isset($this->notifierTypes[$attemptStatus])) {
|
||||
$this->notifierTypes[$attemptStatus]->execute($loginAttempt);
|
||||
}
|
||||
}
|
||||
}
|
||||
app/code/Amasty/AdminActionsLog/Model/LoginAttempt/Notification/Type/AttemptNotificatorInterface.php
Executable
+20
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Admin Actions Log for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\AdminActionsLog\Model\LoginAttempt\Notification\Type;
|
||||
|
||||
use Amasty\AdminActionsLog\Api\Data\LoginAttemptInterface;
|
||||
|
||||
interface AttemptNotificatorInterface
|
||||
{
|
||||
/**
|
||||
* Performs Login Attempt notification.
|
||||
*
|
||||
* @param LoginAttemptInterface $loginAttempt
|
||||
*/
|
||||
public function execute(LoginAttemptInterface $loginAttempt): void;
|
||||
}
|
||||
+115
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Admin Actions Log for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\AdminActionsLog\Model\LoginAttempt\Notification\Type;
|
||||
|
||||
use Amasty\AdminActionsLog\Api\Data\LoginAttemptInterface;
|
||||
use Amasty\AdminActionsLog\Api\Logging\ObjectDataStorageInterface;
|
||||
use Amasty\AdminActionsLog\Model\ConfigProvider;
|
||||
use Amasty\AdminActionsLog\Model\LoginAttempt\LoginAttempt;
|
||||
use Amasty\AdminActionsLog\Model\LoginAttempt\ResourceModel\CollectionFactory as AttemptCollectionFactory;
|
||||
use Amasty\AdminActionsLog\Model\OptionSource\LoginAttemptStatus;
|
||||
use Amasty\AdminActionsLog\Utils\EmailSender;
|
||||
use Magento\Framework\App\Area;
|
||||
use Magento\Framework\Stdlib\DateTime\DateTime;
|
||||
|
||||
class Failed implements AttemptNotificatorInterface
|
||||
{
|
||||
public const STORAGE_KEY = 'failed_notified_at';
|
||||
|
||||
/**
|
||||
* @var AttemptCollectionFactory
|
||||
*/
|
||||
private $attemptCollectionFactory;
|
||||
|
||||
/**
|
||||
* @var ObjectDataStorageInterface
|
||||
*/
|
||||
private $dataStorage;
|
||||
|
||||
/**
|
||||
* @var ConfigProvider
|
||||
*/
|
||||
private $configProvider;
|
||||
|
||||
/**
|
||||
* @var EmailSender
|
||||
*/
|
||||
private $emailSender;
|
||||
|
||||
/**
|
||||
* @var DateTime
|
||||
*/
|
||||
private $dateTime;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $failedRangeDuration;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $minFailedCount;
|
||||
|
||||
public function __construct(
|
||||
AttemptCollectionFactory $attemptCollectionFactory,
|
||||
ObjectDataStorageInterface $dataStorage,
|
||||
ConfigProvider $configProvider,
|
||||
EmailSender $emailSender,
|
||||
DateTime $dateTime,
|
||||
int $failedRangeDuration = 3600,
|
||||
int $minFailedCount = 5
|
||||
) {
|
||||
$this->attemptCollectionFactory = $attemptCollectionFactory;
|
||||
$this->dataStorage = $dataStorage;
|
||||
$this->configProvider = $configProvider;
|
||||
$this->emailSender = $emailSender;
|
||||
$this->dateTime = $dateTime;
|
||||
$this->failedRangeDuration = $failedRangeDuration;
|
||||
$this->minFailedCount = $minFailedCount;
|
||||
}
|
||||
|
||||
public function execute(LoginAttemptInterface $loginAttempt): void
|
||||
{
|
||||
$isEnabled = $this->configProvider->isEnabledEmailUnsuccessfulLoginsToAdmin();
|
||||
$lastNotificationTimestamp = $this->dataStorage->get(self::STORAGE_KEY)['timestamp'] ?? 1;
|
||||
$failedAttemptsCount = $this->getFailedAttemptsCount($lastNotificationTimestamp);
|
||||
$minFailedAttemptsCount = $this->configProvider->getNumberUnsuccessfulLoginAttempts() ?: $this->minFailedCount;
|
||||
|
||||
if ($isEnabled && $failedAttemptsCount >= $minFailedAttemptsCount) {
|
||||
$this->emailSender->sendEmail(
|
||||
$this->configProvider->getSendToEmailsUnsuccessfulLogins(),
|
||||
$this->configProvider->getTemplateEmailUnsuccessfulLogins(),
|
||||
$this->configProvider->getSenderEmailUnsuccessfulLogins(),
|
||||
['unsuccessful_login_count' => $failedAttemptsCount],
|
||||
Area::AREA_ADMINHTML
|
||||
);
|
||||
$this->dataStorage->set(self::STORAGE_KEY, ['timestamp' => $this->dateTime->gmtTimestamp()]);
|
||||
}
|
||||
}
|
||||
|
||||
private function getFailedAttemptsCount(int $lastNotificationTimestamp): int
|
||||
{
|
||||
$currentTimestamp = $this->dateTime->gmtTimestamp();
|
||||
if ($this->failedRangeDuration > $currentTimestamp - $lastNotificationTimestamp) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$attemptCollection = $this->attemptCollectionFactory->create();
|
||||
$attemptCollection->addFieldToFilter(LoginAttempt::STATUS, LoginAttemptStatus::FAILED);
|
||||
$attemptCollection->addFieldToFilter(
|
||||
LoginAttempt::DATE,
|
||||
['gteq' => $this->dateTime->gmtDate(null, $lastNotificationTimestamp)]
|
||||
);
|
||||
|
||||
return $attemptCollection->count();
|
||||
}
|
||||
}
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Admin Actions Log for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\AdminActionsLog\Model\LoginAttempt\Notification\Type;
|
||||
|
||||
use Amasty\AdminActionsLog\Api\Data\LoginAttemptInterface;
|
||||
use Amasty\AdminActionsLog\Model\ConfigProvider;
|
||||
use Amasty\AdminActionsLog\Model\LoginAttempt\SuspiciousDetection\Detector;
|
||||
use Amasty\AdminActionsLog\Utils\EmailSender;
|
||||
use Magento\Framework\App\Area;
|
||||
|
||||
class Succeed implements AttemptNotificatorInterface
|
||||
{
|
||||
/**
|
||||
* @var Detector
|
||||
*/
|
||||
private $suspiciousDetector;
|
||||
|
||||
/**
|
||||
* @var EmailSender
|
||||
*/
|
||||
private $emailSender;
|
||||
|
||||
/**
|
||||
* @var ConfigProvider
|
||||
*/
|
||||
private $configProvider;
|
||||
|
||||
public function __construct(
|
||||
Detector $suspiciousDetector,
|
||||
EmailSender $emailSender,
|
||||
ConfigProvider $configProvider
|
||||
) {
|
||||
$this->suspiciousDetector = $suspiciousDetector;
|
||||
$this->emailSender = $emailSender;
|
||||
$this->configProvider = $configProvider;
|
||||
}
|
||||
|
||||
public function execute(LoginAttemptInterface $loginAttempt): void
|
||||
{
|
||||
if ($this->isSuspiciousLogin($loginAttempt)) {
|
||||
$isEnabled = $this->configProvider->isEnabledEmailSuspiciousLoginsToAdmin();
|
||||
$recipients = $this->configProvider->getSendToEmailsSuspiciousLogins();
|
||||
$template = $this->configProvider->getTemplateEmailSuspiciousLogins();
|
||||
$sender = $this->configProvider->getSenderEmailSuspiciousLogins();
|
||||
} else {
|
||||
$isEnabled = $this->configProvider->isEnabledEmailSuccessfulLoginsToAdmin();
|
||||
$recipients = $this->configProvider->getSendToEmailsSuccessfulLogins();
|
||||
$template = $this->configProvider->getTemplateEmailSuccessfulLogins();
|
||||
$sender = $this->configProvider->getSenderEmailSuccessfulLogins();
|
||||
}
|
||||
|
||||
if ($isEnabled) {
|
||||
$this->emailSender->sendEmail(
|
||||
$recipients,
|
||||
$template,
|
||||
$sender,
|
||||
[
|
||||
'username' => $loginAttempt->getUsername(),
|
||||
'full_name' => $loginAttempt->getFullName(),
|
||||
'ip' => $loginAttempt->getIp(),
|
||||
'country_id' => $loginAttempt->getCountryId()
|
||||
],
|
||||
Area::AREA_ADMINHTML
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private function isSuspiciousLogin(LoginAttemptInterface $loginAttempt): bool
|
||||
{
|
||||
foreach ($this->configProvider->getListSuspiciousLoggedInWith() as $detectionType) {
|
||||
if ($this->suspiciousDetector->isSuspicious($detectionType, $loginAttempt)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user