ltsecurityinc安防产品在线商城代码
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
<?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;
|
||||
|
||||
use Amasty\AdminActionsLog\Api\Data\LoginAttemptInterface;
|
||||
use Magento\Framework\Model\AbstractModel;
|
||||
|
||||
class LoginAttempt extends AbstractModel implements LoginAttemptInterface
|
||||
{
|
||||
/**#@+
|
||||
* Constants defined for keys of data array
|
||||
*/
|
||||
public const ID = 'id';
|
||||
public const DATE = 'date';
|
||||
public const USERNAME = 'username';
|
||||
public const FULL_NAME = 'full_name';
|
||||
public const IP = 'ip';
|
||||
public const STATUS = 'status';
|
||||
public const LOCATION = 'location';
|
||||
public const COUNTRY_ID = 'country_id';
|
||||
public const USER_AGENT = 'user_agent';
|
||||
/**#@-*/
|
||||
|
||||
public function _construct()
|
||||
{
|
||||
parent::_construct();
|
||||
$this->_init(ResourceModel\LoginAttempt::class);
|
||||
$this->setIdFieldName(self::ID);
|
||||
}
|
||||
|
||||
public function getDate(): ?string
|
||||
{
|
||||
return $this->_getData(self::DATE);
|
||||
}
|
||||
|
||||
public function setDate(string $date): LoginAttemptInterface
|
||||
{
|
||||
return $this->setData(self::DATE, $date);
|
||||
}
|
||||
|
||||
public function getUsername(): ?string
|
||||
{
|
||||
return $this->_getData(self::USERNAME);
|
||||
}
|
||||
|
||||
public function setUsername(string $username): LoginAttemptInterface
|
||||
{
|
||||
return $this->setData(self::USERNAME, $username);
|
||||
}
|
||||
|
||||
public function getFullName(): ?string
|
||||
{
|
||||
return $this->_getData(self::FULL_NAME);
|
||||
}
|
||||
|
||||
public function setFullName(string $fullName): LoginAttemptInterface
|
||||
{
|
||||
return $this->setData(self::FULL_NAME, $fullName);
|
||||
}
|
||||
|
||||
public function getIp(): ?string
|
||||
{
|
||||
return $this->_getData(self::IP);
|
||||
}
|
||||
|
||||
public function setIp(string $ip): LoginAttemptInterface
|
||||
{
|
||||
return $this->setData(self::IP, $ip);
|
||||
}
|
||||
|
||||
public function getStatus(): ?int
|
||||
{
|
||||
return $this->hasData(self::STATUS) ? (int)$this->_getData(self::STATUS) : null;
|
||||
}
|
||||
|
||||
public function setStatus(int $status): LoginAttemptInterface
|
||||
{
|
||||
return $this->setData(self::STATUS, $status);
|
||||
}
|
||||
|
||||
public function getLocation(): ?string
|
||||
{
|
||||
return $this->_getData(self::LOCATION);
|
||||
}
|
||||
|
||||
public function setLocation(string $location): LoginAttemptInterface
|
||||
{
|
||||
return $this->setData(self::LOCATION, $location);
|
||||
}
|
||||
|
||||
public function getCountryId(): ?string
|
||||
{
|
||||
return $this->_getData(self::COUNTRY_ID);
|
||||
}
|
||||
|
||||
public function setCountryId(string $countryId): LoginAttemptInterface
|
||||
{
|
||||
return $this->setData(self::COUNTRY_ID, $countryId);
|
||||
}
|
||||
|
||||
public function getUserAgent(): ?string
|
||||
{
|
||||
return $this->_getData(self::USER_AGENT);
|
||||
}
|
||||
|
||||
public function setUserAgent(string $userAgent): LoginAttemptInterface
|
||||
{
|
||||
return $this->setData(self::USER_AGENT, $userAgent);
|
||||
}
|
||||
}
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
<?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;
|
||||
|
||||
use Amasty\AdminActionsLog\Api\Data\LoginAttemptInterfaceFactory;
|
||||
use Amasty\AdminActionsLog\Api\LoginAttemptManagerInterface;
|
||||
use Amasty\AdminActionsLog\Api\LoginAttemptRepositoryInterface;
|
||||
use Amasty\AdminActionsLog\Model\Admin\SessionUserDataProvider;
|
||||
use Amasty\AdminActionsLog\Model\ConfigProvider;
|
||||
use Amasty\AdminActionsLog\Model\LoginAttempt\Notification\Processor;
|
||||
use Magento\Framework\Stdlib\DateTime\DateTime;
|
||||
use Magento\User\Api\Data\UserInterfaceFactory;
|
||||
|
||||
class LoginAttemptManager implements LoginAttemptManagerInterface
|
||||
{
|
||||
/**
|
||||
* @var SessionUserDataProvider
|
||||
*/
|
||||
private $sessionUserDataProvider;
|
||||
|
||||
/**
|
||||
* @var LoginAttemptInterfaceFactory
|
||||
*/
|
||||
private $loginAttemptFactory;
|
||||
|
||||
/**
|
||||
* @var LoginAttemptRepositoryInterface
|
||||
*/
|
||||
private $loginAttemptRepository;
|
||||
|
||||
/**
|
||||
* @var UserInterfaceFactory
|
||||
*/
|
||||
private $userFactory;
|
||||
|
||||
/**
|
||||
* @var Processor
|
||||
*/
|
||||
private $notificationProcessor;
|
||||
|
||||
/**
|
||||
* @var ConfigProvider
|
||||
*/
|
||||
private $configProvider;
|
||||
|
||||
/**
|
||||
* @var DateTime
|
||||
*/
|
||||
private $dateTime;
|
||||
|
||||
public function __construct(
|
||||
SessionUserDataProvider $sessionUserDataProvider,
|
||||
LoginAttemptInterfaceFactory $loginAttemptFactory,
|
||||
LoginAttemptRepositoryInterface $loginAttemptRepository,
|
||||
UserInterfaceFactory $userFactory,
|
||||
Processor $notificationProcessor,
|
||||
ConfigProvider $configProvider,
|
||||
DateTime $dateTime
|
||||
) {
|
||||
$this->sessionUserDataProvider = $sessionUserDataProvider;
|
||||
$this->loginAttemptFactory = $loginAttemptFactory;
|
||||
$this->loginAttemptRepository = $loginAttemptRepository;
|
||||
$this->userFactory = $userFactory;
|
||||
$this->notificationProcessor = $notificationProcessor;
|
||||
$this->configProvider = $configProvider;
|
||||
$this->dateTime = $dateTime;
|
||||
}
|
||||
|
||||
public function saveAttempt(?string $username, int $status): void
|
||||
{
|
||||
$userData = $this->sessionUserDataProvider->getUserPreparedData();
|
||||
$loginAttempt = $this->loginAttemptFactory->create(['data' => $userData]);
|
||||
$loginAttempt->setDate($this->dateTime->date());
|
||||
$loginAttempt->setStatus($status);
|
||||
$loginAttempt->setUserAgent($this->sessionUserDataProvider->getUserAgent());
|
||||
|
||||
if ($username && !$this->sessionUserDataProvider->getUserName()) {
|
||||
$loginAttempt->setUsername($username)->setFullName($username);
|
||||
$user = $this->userFactory->create()->loadByUsername($username);
|
||||
if ($user->getId()) {
|
||||
$loginAttempt->setUsername($user->getUserName());
|
||||
$loginAttempt->setFullName($user->getFirstName() . ' ' . $user->getLastName());
|
||||
}
|
||||
}
|
||||
|
||||
$this->loginAttemptRepository->save($loginAttempt);
|
||||
$this->notificationProcessor->execute($loginAttempt);
|
||||
}
|
||||
|
||||
public function clear(int $period = null): void
|
||||
{
|
||||
$this->loginAttemptRepository->clean($period);
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
<?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;
|
||||
|
||||
use Amasty\AdminActionsLog\Api\Data\LoginAttemptSearchResultsInterface;
|
||||
use Magento\Framework\Api\SearchResults;
|
||||
|
||||
/**
|
||||
* Service Data Object with Log Entry search results.
|
||||
*/
|
||||
class LoginAttemptSearchResults extends SearchResults implements LoginAttemptSearchResultsInterface
|
||||
{
|
||||
}
|
||||
+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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
<?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;
|
||||
|
||||
use Amasty\AdminActionsLog\Api\Data\LoginAttemptSearchResultsInterface;
|
||||
use Amasty\AdminActionsLog\Api\Data\LoginAttemptSearchResultsInterfaceFactory;
|
||||
use Amasty\AdminActionsLog\Api\LoginAttemptRepositoryInterface;
|
||||
use Amasty\AdminActionsLog\Api\Data\LoginAttemptInterface;
|
||||
use Amasty\AdminActionsLog\Api\Data\LoginAttemptInterfaceFactory;
|
||||
use Magento\Framework\Api\Search\FilterGroup;
|
||||
use Magento\Framework\Api\SearchCriteriaInterface;
|
||||
use Magento\Framework\Api\SortOrder;
|
||||
use Magento\Framework\Exception\CouldNotDeleteException;
|
||||
use Magento\Framework\Exception\CouldNotSaveException;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
use Magento\Framework\Stdlib\DateTime\DateTime;
|
||||
|
||||
class Repository implements LoginAttemptRepositoryInterface
|
||||
{
|
||||
/**
|
||||
* @var LoginAttemptSearchResultsInterfaceFactory
|
||||
*/
|
||||
private $searchResultsFactory;
|
||||
|
||||
/**
|
||||
* @var LoginAttemptInterfaceFactory
|
||||
*/
|
||||
private $loginAttemptFactory;
|
||||
|
||||
/**
|
||||
* @var ResourceModel\LoginAttempt
|
||||
*/
|
||||
private $resource;
|
||||
|
||||
/**
|
||||
* @var ResourceModel\CollectionFactory
|
||||
*/
|
||||
private $collectionFactory;
|
||||
|
||||
/**
|
||||
* @var DateTime
|
||||
*/
|
||||
private $dateTime;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $loginAttempts = [];
|
||||
|
||||
public function __construct(
|
||||
LoginAttemptSearchResultsInterfaceFactory $searchResultsFactory,
|
||||
LoginAttemptInterfaceFactory $loginAttemptFactory,
|
||||
ResourceModel\LoginAttempt $resource,
|
||||
ResourceModel\CollectionFactory $collectionFactory,
|
||||
DateTime $dateTime
|
||||
) {
|
||||
$this->loginAttemptFactory = $loginAttemptFactory;
|
||||
$this->resource = $resource;
|
||||
$this->collectionFactory = $collectionFactory;
|
||||
$this->searchResultsFactory = $searchResultsFactory;
|
||||
$this->dateTime = $dateTime;
|
||||
}
|
||||
|
||||
public function getById(int $id): LoginAttemptInterface
|
||||
{
|
||||
if (!isset($this->loginAttempts[$id])) {
|
||||
/** @var LoginAttemptInterface $loginAttempt */
|
||||
$loginAttempt = $this->loginAttemptFactory->create();
|
||||
$this->resource->load($loginAttempt, $id);
|
||||
if (!$loginAttempt->getId()) {
|
||||
throw new NoSuchEntityException(__('Login Attempt with specified ID "%1" not found.', $id));
|
||||
}
|
||||
|
||||
$this->loginAttempts[$id] = $loginAttempt;
|
||||
}
|
||||
|
||||
return $this->loginAttempts[$id];
|
||||
}
|
||||
|
||||
public function save(LoginAttemptInterface $loginAttempt): LoginAttemptInterface
|
||||
{
|
||||
try {
|
||||
if ($loginAttempt->getId()) {
|
||||
$loginAttempt = $this->getById((int)$loginAttempt->getId())->addData($loginAttempt->getData());
|
||||
}
|
||||
$this->resource->save($loginAttempt);
|
||||
|
||||
unset($this->loginAttempts[$loginAttempt->getId()]);
|
||||
} catch (\Exception $e) {
|
||||
throw new CouldNotSaveException(__('Unable to save the Login Attempt. Error: %1', $e->getMessage()));
|
||||
}
|
||||
|
||||
return $loginAttempt;
|
||||
}
|
||||
|
||||
public function delete(LoginAttemptInterface $loginAttempt): bool
|
||||
{
|
||||
try {
|
||||
$this->resource->delete($loginAttempt);
|
||||
unset($this->loginAttempts[$loginAttempt->getId()]);
|
||||
} catch (\Exception $e) {
|
||||
throw new CouldNotDeleteException(__('Unable to delete the Login Attempt. Error: %1', $e->getMessage()));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function deleteById(int $id): bool
|
||||
{
|
||||
return $this->delete($this->getById($id));
|
||||
}
|
||||
|
||||
public function getList(SearchCriteriaInterface $searchCriteria): LoginAttemptSearchResultsInterface
|
||||
{
|
||||
$searchResults = $this->searchResultsFactory->create();
|
||||
$searchResults->setSearchCriteria($searchCriteria);
|
||||
|
||||
/** @var ResourceModel\Collection $loginAttemptCollection */
|
||||
$loginAttemptCollection = $this->collectionFactory->create();
|
||||
// Add filters from root filter group to the collection
|
||||
foreach ($searchCriteria->getFilterGroups() as $group) {
|
||||
$this->addFilterGroupToCollection($group, $loginAttemptCollection);
|
||||
}
|
||||
$searchResults->setTotalCount($loginAttemptCollection->getSize());
|
||||
$sortOrders = $searchCriteria->getSortOrders();
|
||||
if ($sortOrders) {
|
||||
$this->addOrderToCollection($sortOrders, $loginAttemptCollection);
|
||||
}
|
||||
$loginAttemptCollection->setCurPage($searchCriteria->getCurrentPage());
|
||||
$loginAttemptCollection->setPageSize($searchCriteria->getPageSize());
|
||||
$loginAttempts = [];
|
||||
/** @var LoginAttemptInterface $loginAttempt */
|
||||
foreach ($loginAttemptCollection->getItems() as $loginAttempt) {
|
||||
$loginAttempts[] = $this->getById((int)$loginAttempt->getId());
|
||||
}
|
||||
$searchResults->setItems($loginAttempts);
|
||||
|
||||
return $searchResults;
|
||||
}
|
||||
|
||||
public function clean(?int $period = null): void
|
||||
{
|
||||
$connection = $this->resource->getConnection();
|
||||
$tableName = $this->resource->getMainTable();
|
||||
|
||||
if ($period === null) {
|
||||
$connection->truncateTable($tableName);
|
||||
} else {
|
||||
$time = '-' . $period . ' days';
|
||||
$connection->delete(
|
||||
$tableName,
|
||||
[LoginAttempt::DATE . ' < ?' => $this->dateTime->gmtDate('Y-m-d H:i:s', $time)]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function that adds a FilterGroup to the collection.
|
||||
*
|
||||
* @param FilterGroup $filterGroup
|
||||
* @param ResourceModel\Collection $logEntryCollection
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function addFilterGroupToCollection(
|
||||
FilterGroup $filterGroup,
|
||||
ResourceModel\Collection $logEntryCollection
|
||||
): void {
|
||||
foreach ($filterGroup->getFilters() as $filter) {
|
||||
$condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
|
||||
$logEntryCollection->addFieldToFilter($filter->getField(), [$condition => $filter->getValue()]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function that adds a SortOrder to the collection.
|
||||
*
|
||||
* @param SortOrder[] $sortOrders
|
||||
* @param ResourceModel\Collection $logEntryCollection
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function addOrderToCollection(
|
||||
array $sortOrders,
|
||||
ResourceModel\Collection $logEntryCollection
|
||||
): void {
|
||||
foreach ($sortOrders as $sortOrder) {
|
||||
$field = $sortOrder->getField();
|
||||
$logEntryCollection->addOrder(
|
||||
$field,
|
||||
($sortOrder->getDirection() == SortOrder::SORT_DESC) ? 'DESC' : 'ASC'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
<?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\ResourceModel;
|
||||
|
||||
use Amasty\AdminActionsLog\Model\LoginAttempt\LoginAttempt as LoginAttemptModel;
|
||||
use Amasty\AdminActionsLog\Model\LoginAttempt\ResourceModel\LoginAttempt as LoginAttemptResource;
|
||||
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
|
||||
|
||||
class Collection extends AbstractCollection
|
||||
{
|
||||
public function _construct()
|
||||
{
|
||||
$this->_init(LoginAttemptModel::class, LoginAttemptResource::class);
|
||||
$this->_setIdFieldName($this->getResource()->getIdFieldName());
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
<?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\ResourceModel\Grid;
|
||||
|
||||
use Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult;
|
||||
|
||||
class Collection extends SearchResult
|
||||
{
|
||||
/**
|
||||
* @return \Magento\Framework\Api\Search\DocumentInterface[]
|
||||
*/
|
||||
public function getItems()
|
||||
{
|
||||
$this->_setIsLoaded(false);
|
||||
$this->_items = [];
|
||||
$searchCriteria = $this->getSearchCriteria();
|
||||
$this->setPageSize($searchCriteria->getPageSize());
|
||||
$this->setCurPage($searchCriteria->getCurrentPage());
|
||||
|
||||
return parent::getItems();
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
<?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\ResourceModel;
|
||||
|
||||
use \Amasty\AdminActionsLog\Model\LoginAttempt\LoginAttempt as LoginAttemptModel;
|
||||
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
|
||||
|
||||
class LoginAttempt extends AbstractDb
|
||||
{
|
||||
public const TABLE_NAME = 'amasty_audit_login_attempts';
|
||||
|
||||
protected function _construct()
|
||||
{
|
||||
$this->_init(self::TABLE_NAME, LoginAttemptModel::ID);
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
<?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\SuspiciousDetection;
|
||||
|
||||
use Amasty\AdminActionsLog\Api\Data\LoginAttemptInterface;
|
||||
|
||||
class Detector
|
||||
{
|
||||
/**
|
||||
* @var Type\DetectorInterface[]
|
||||
*/
|
||||
private $detectionTypes;
|
||||
|
||||
public function __construct(array $detectionTypes = [])
|
||||
{
|
||||
$this->detectionTypes = $detectionTypes;
|
||||
}
|
||||
|
||||
public function isSuspicious(string $type, LoginAttemptInterface $loginAttempt): bool
|
||||
{
|
||||
return isset($this->detectionTypes[$type])
|
||||
? $this->detectionTypes[$type]->isSuspiciousAttempt($loginAttempt)
|
||||
: false;
|
||||
}
|
||||
}
|
||||
Executable
+64
@@ -0,0 +1,64 @@
|
||||
<?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\SuspiciousDetection\Type;
|
||||
|
||||
use Amasty\AdminActionsLog\Api\Data\LoginAttemptInterface;
|
||||
use Amasty\AdminActionsLog\Api\Logging\ObjectDataStorageInterface;
|
||||
use Amasty\AdminActionsLog\Model\LoginAttempt\LoginAttempt;
|
||||
use Amasty\AdminActionsLog\Model\LoginAttempt\ResourceModel\CollectionFactory;
|
||||
use Amasty\AdminActionsLog\Model\OptionSource\LoginAttemptStatus;
|
||||
|
||||
abstract class AbstractDetection implements DetectorInterface
|
||||
{
|
||||
/**
|
||||
* @var CollectionFactory
|
||||
*/
|
||||
private $loginAttemptCollectionFactory;
|
||||
|
||||
/**
|
||||
* @var ObjectDataStorageInterface
|
||||
*/
|
||||
private $dataStorage;
|
||||
|
||||
public function __construct(
|
||||
CollectionFactory $loginAttemptCollectionFactory,
|
||||
ObjectDataStorageInterface $dataStorage
|
||||
) {
|
||||
$this->loginAttemptCollectionFactory = $loginAttemptCollectionFactory;
|
||||
$this->dataStorage = $dataStorage;
|
||||
}
|
||||
|
||||
abstract public function isSuspiciousAttempt(LoginAttemptInterface $loginAttempt): bool;
|
||||
|
||||
public function getLastSucceedAttempt(LoginAttemptInterface $loginAttempt): LoginAttemptInterface
|
||||
{
|
||||
$storageKey = spl_object_id($loginAttempt) . '.none';
|
||||
if ($this->dataStorage->isExists($storageKey)) {
|
||||
$lastLoginAttempt = $this->dataStorage->get($storageKey);
|
||||
|
||||
return array_shift($lastLoginAttempt);
|
||||
}
|
||||
|
||||
$loginAttemptCollection = $this->loginAttemptCollectionFactory->create();
|
||||
|
||||
$lastLoginAttempt = $loginAttemptCollection->addFieldToFilter(
|
||||
LoginAttempt::USERNAME,
|
||||
$loginAttempt->getUsername()
|
||||
)->addFieldToFilter(LoginAttempt::STATUS, LoginAttemptStatus::SUCCESS)
|
||||
->addFieldToFilter($loginAttemptCollection->getIdFieldName(), ['neq' => $loginAttempt->getId()])
|
||||
->setOrder($loginAttemptCollection->getIdFieldName(), $loginAttemptCollection::SORT_ORDER_DESC)
|
||||
->setPageSize(1)
|
||||
->getFirstItem();
|
||||
$this->dataStorage->set($storageKey, [$lastLoginAttempt]);
|
||||
|
||||
return $lastLoginAttempt;
|
||||
}
|
||||
}
|
||||
Executable
+15
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Admin Actions Log for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\AdminActionsLog\Model\LoginAttempt\SuspiciousDetection\Type;
|
||||
|
||||
use Amasty\AdminActionsLog\Api\Data\LoginAttemptInterface;
|
||||
|
||||
interface DetectorInterface
|
||||
{
|
||||
public function isSuspiciousAttempt(LoginAttemptInterface $loginAttempt): bool;
|
||||
}
|
||||
Executable
+27
@@ -0,0 +1,27 @@
|
||||
<?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\SuspiciousDetection\Type;
|
||||
|
||||
use Amasty\AdminActionsLog\Api\Data\LoginAttemptInterface;
|
||||
|
||||
class NewDevice extends AbstractDetection
|
||||
{
|
||||
public function isSuspiciousAttempt(LoginAttemptInterface $loginAttempt): bool
|
||||
{
|
||||
$lastAttempt = $this->getLastSucceedAttempt($loginAttempt);
|
||||
|
||||
if ($lastAttempt->getId() && !empty($lastAttempt->getUserAgent())) {
|
||||
return $loginAttempt->getUserAgent() !== $lastAttempt->getUserAgent();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
<?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\SuspiciousDetection\Type;
|
||||
|
||||
use Amasty\AdminActionsLog\Api\Data\LoginAttemptInterface;
|
||||
|
||||
class NewIp extends AbstractDetection
|
||||
{
|
||||
public function isSuspiciousAttempt(LoginAttemptInterface $loginAttempt): bool
|
||||
{
|
||||
$lastAttempt = $this->getLastSucceedAttempt($loginAttempt);
|
||||
|
||||
if ($lastAttempt->getId() && !empty($lastAttempt->getIp())) {
|
||||
return $loginAttempt->getIp() !== $lastAttempt->getIp();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Executable
+27
@@ -0,0 +1,27 @@
|
||||
<?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\SuspiciousDetection\Type;
|
||||
|
||||
use Amasty\AdminActionsLog\Api\Data\LoginAttemptInterface;
|
||||
|
||||
class NewLocation extends AbstractDetection
|
||||
{
|
||||
public function isSuspiciousAttempt(LoginAttemptInterface $loginAttempt): bool
|
||||
{
|
||||
$lastAttempt = $this->getLastSucceedAttempt($loginAttempt);
|
||||
|
||||
if ($lastAttempt->getId() && !empty($lastAttempt->getLocation())) {
|
||||
return $loginAttempt->getLocation() !== $lastAttempt->getLocation();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user