ltsecurityinc安防产品在线商城代码
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
<?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\ActiveSession;
|
||||
|
||||
use Amasty\AdminActionsLog\Api\Data\ActiveSessionInterface;
|
||||
use Magento\Framework\Model\AbstractModel;
|
||||
|
||||
class ActiveSession extends AbstractModel implements ActiveSessionInterface
|
||||
{
|
||||
/**
|
||||
* Constants defined for keys of data array
|
||||
*/
|
||||
public const ID = 'id';
|
||||
public const SESSION_ID = 'session_id';
|
||||
public const ADMIN_SESSION_INFO_ID = 'admin_session_info_id';
|
||||
public const USER_ID = 'user_id';
|
||||
public const USERNAME = 'username';
|
||||
public const FULL_NAME = 'full_name';
|
||||
public const IP = 'ip';
|
||||
public const SESSION_START = 'session_start';
|
||||
public const RECENT_ACTIVITY = 'recent_activity';
|
||||
public const LOCATION = 'location';
|
||||
public const COUNTRY_ID = 'country_id';
|
||||
|
||||
public function _construct()
|
||||
{
|
||||
parent::_construct();
|
||||
$this->_init(ResourceModel\ActiveSession::class);
|
||||
$this->setIdFieldName(self::ID);
|
||||
}
|
||||
|
||||
public function getSessionId(): ?string
|
||||
{
|
||||
return $this->_getData(self::SESSION_ID);
|
||||
}
|
||||
|
||||
public function setSessionId(string $sessionId): ActiveSessionInterface
|
||||
{
|
||||
return $this->setData(self::SESSION_ID, $sessionId);
|
||||
}
|
||||
|
||||
public function getAdminSessionInfoId(): ?int
|
||||
{
|
||||
return $this->hasData(self::ADMIN_SESSION_INFO_ID)
|
||||
? (int)$this->_getData(self::ADMIN_SESSION_INFO_ID)
|
||||
: null;
|
||||
}
|
||||
|
||||
public function setAdminSessionInfoId(?int $adminSessionInfoId): ActiveSessionInterface
|
||||
{
|
||||
return $this->setData(self::ADMIN_SESSION_INFO_ID, $adminSessionInfoId);
|
||||
}
|
||||
|
||||
public function getUserId(): ?int
|
||||
{
|
||||
return $this->hasData(self::USER_ID)
|
||||
? (int)$this->_getData(self::USER_ID)
|
||||
: null;
|
||||
}
|
||||
|
||||
public function setUserId(?int $userId): ActiveSessionInterface
|
||||
{
|
||||
return $this->setData(self::USER_ID, $userId);
|
||||
}
|
||||
|
||||
public function getUsername(): ?string
|
||||
{
|
||||
return $this->_getData(self::USERNAME);
|
||||
}
|
||||
|
||||
public function setUsername(string $username): ActiveSessionInterface
|
||||
{
|
||||
return $this->setData(self::USERNAME, $username);
|
||||
}
|
||||
|
||||
public function getFullName(): ?string
|
||||
{
|
||||
return $this->_getData(self::FULL_NAME);
|
||||
}
|
||||
|
||||
public function setFullName(string $fullName): ActiveSessionInterface
|
||||
{
|
||||
return $this->setData(self::FULL_NAME, $fullName);
|
||||
}
|
||||
|
||||
public function getIp(): ?string
|
||||
{
|
||||
return $this->_getData(self::IP);
|
||||
}
|
||||
|
||||
public function setIp(string $ip): ActiveSessionInterface
|
||||
{
|
||||
return $this->setData(self::IP, $ip);
|
||||
}
|
||||
|
||||
public function getSessionStart(): ?string
|
||||
{
|
||||
return $this->_getData(self::SESSION_START);
|
||||
}
|
||||
|
||||
public function setSessionStart(string $startTime): ActiveSessionInterface
|
||||
{
|
||||
return $this->setData(self::SESSION_START, $startTime);
|
||||
}
|
||||
|
||||
public function getRecentActivity(): ?string
|
||||
{
|
||||
return $this->_getData(self::RECENT_ACTIVITY);
|
||||
}
|
||||
|
||||
public function setRecentActivity(string $activityTime): ActiveSessionInterface
|
||||
{
|
||||
return $this->setData(self::RECENT_ACTIVITY, $activityTime);
|
||||
}
|
||||
|
||||
public function getLocation(): ?string
|
||||
{
|
||||
return $this->_getData(self::LOCATION);
|
||||
}
|
||||
|
||||
public function setLocation(string $location): ActiveSessionInterface
|
||||
{
|
||||
return $this->setData(self::LOCATION, $location);
|
||||
}
|
||||
|
||||
public function getCountryId(): ?string
|
||||
{
|
||||
return $this->_getData(self::COUNTRY_ID);
|
||||
}
|
||||
|
||||
public function setCountryId(string $countryId): ActiveSessionInterface
|
||||
{
|
||||
return $this->setData(self::COUNTRY_ID, $countryId);
|
||||
}
|
||||
}
|
||||
+199
@@ -0,0 +1,199 @@
|
||||
<?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\ActiveSession;
|
||||
|
||||
use Amasty\AdminActionsLog\Api\ActiveSessionManagerInterface;
|
||||
use Amasty\AdminActionsLog\Api\ActiveSessionRepositoryInterface;
|
||||
use Amasty\AdminActionsLog\Api\Data\ActiveSessionInterfaceFactory;
|
||||
use Amasty\AdminActionsLog\Model\ActiveSession\ResourceModel\ActiveSession as ActiveSessionResource;
|
||||
use Amasty\AdminActionsLog\Model\ActiveSession\ResourceModel\CollectionFactory as ActiveSessionCollectionFactory;
|
||||
use Amasty\AdminActionsLog\Model\Admin\SessionUserDataProvider;
|
||||
use Magento\Framework\App\Config\ScopeConfigInterface;
|
||||
use Magento\Framework\App\ObjectManager;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
use Magento\Framework\Stdlib\DateTime\DateTime;
|
||||
use Magento\Security\Model\AdminSessionInfo;
|
||||
use Magento\Security\Model\ResourceModel\AdminSessionInfo as AdminSessionInfoResource;
|
||||
use Magento\Security\Model\ResourceModel\AdminSessionInfo\CollectionFactory;
|
||||
|
||||
class ActiveSessionManager implements ActiveSessionManagerInterface
|
||||
{
|
||||
public const SESSION_LIFETIME_CONFIG_PATH = 'admin/security/session_lifetime';
|
||||
|
||||
/**
|
||||
* @var SessionUserDataProvider
|
||||
*/
|
||||
private $sessionUserDataProvider;
|
||||
|
||||
/**
|
||||
* @var ActiveSessionRepositoryInterface
|
||||
*/
|
||||
private $activeSessionRepository;
|
||||
|
||||
/**
|
||||
* @var ActiveSessionInterfaceFactory
|
||||
*/
|
||||
private $activeSessionFactory;
|
||||
|
||||
/**
|
||||
* @var DateTime
|
||||
*/
|
||||
private $dateTime;
|
||||
|
||||
/**
|
||||
* @var CollectionFactory
|
||||
*/
|
||||
private $adminSessionInfoCollectionFactory;
|
||||
|
||||
/**
|
||||
* @var AdminSessionInfoResource
|
||||
*/
|
||||
private $adminSessionInfoResource;
|
||||
|
||||
/**
|
||||
* @var ScopeConfigInterface
|
||||
*/
|
||||
private $scopeConfig;
|
||||
|
||||
/**
|
||||
* @var ActiveSessionCollectionFactory
|
||||
*/
|
||||
private $activeSessionCollectionFactory;
|
||||
|
||||
/**
|
||||
* @var ActiveSessionResource
|
||||
*/
|
||||
private $activeSession;
|
||||
|
||||
public function __construct(
|
||||
SessionUserDataProvider $sessionUserDataProvider,
|
||||
ActiveSessionRepositoryInterface $activeSessionRepository,
|
||||
ActiveSessionInterfaceFactory $activeSessionFactory,
|
||||
DateTime $dateTime,
|
||||
CollectionFactory $adminSessionInfoCollectionFactory,
|
||||
AdminSessionInfoResource $adminSessionInfoResource,
|
||||
ScopeConfigInterface $scopeConfig,
|
||||
ActiveSessionCollectionFactory $activeSessionCollectionFactory,
|
||||
ActiveSessionResource $activeSession = null
|
||||
) {
|
||||
$this->sessionUserDataProvider = $sessionUserDataProvider;
|
||||
$this->activeSessionRepository = $activeSessionRepository;
|
||||
$this->activeSessionFactory = $activeSessionFactory;
|
||||
$this->dateTime = $dateTime;
|
||||
$this->adminSessionInfoCollectionFactory = $adminSessionInfoCollectionFactory;
|
||||
$this->adminSessionInfoResource = $adminSessionInfoResource;
|
||||
$this->scopeConfig = $scopeConfig;
|
||||
$this->activeSessionCollectionFactory = $activeSessionCollectionFactory;
|
||||
// OM for backward compatibility
|
||||
$this->activeSession = $activeSession ?? ObjectManager::getInstance()->get(ActiveSessionResource::class);
|
||||
}
|
||||
|
||||
public function initNew(): void
|
||||
{
|
||||
$userData = $this->sessionUserDataProvider->getUserPreparedData();
|
||||
$activeSessionModel = $this->activeSessionFactory->create()->setData($userData);
|
||||
|
||||
$this->activeSessionRepository->save($activeSessionModel);
|
||||
}
|
||||
|
||||
public function update(): void
|
||||
{
|
||||
$sessionId = $this->sessionUserDataProvider->getSessionId();
|
||||
try {
|
||||
$activeSessionModel = $this->activeSessionRepository->getBySessionId($sessionId);
|
||||
$activeSessionModel->setRecentActivity($this->dateTime->date());
|
||||
|
||||
$this->activeSessionRepository->save($activeSessionModel);
|
||||
} catch (NoSuchEntityException $e) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public function terminate(string $sessionId = null): void
|
||||
{
|
||||
if ($sessionId === null) {
|
||||
$sessionId = $this->sessionUserDataProvider->getSessionId();
|
||||
}
|
||||
|
||||
try {
|
||||
$activeSessionModel = $this->activeSessionRepository->getBySessionId($sessionId);
|
||||
$this->activeSessionRepository->delete($activeSessionModel);
|
||||
} catch (NoSuchEntityException $e) {
|
||||
return;
|
||||
}
|
||||
|
||||
$adminSessionInfoId = (int)$activeSessionModel->getAdminSessionInfoId();
|
||||
if ($adminSessionInfoId) {
|
||||
// Since Magento 2.4.3-p1, 2.3.7-p2 session_id is no longer used in admin_user_session
|
||||
$this->destroySessionByAdminSessionInfoId($adminSessionInfoId);
|
||||
} else {
|
||||
$this->destroySessionBySessionId($sessionId);
|
||||
}
|
||||
}
|
||||
|
||||
public function getInactiveSessions(): array
|
||||
{
|
||||
$sessionLifeTime = $this->scopeConfig->getValue(self::SESSION_LIFETIME_CONFIG_PATH);
|
||||
if (empty($sessionLifeTime)) {
|
||||
$sessionLifeTime = 900;
|
||||
}
|
||||
$activeSessionCollection = $this->activeSessionCollectionFactory->create();
|
||||
$activeSessionCollection->addFieldToFilter(
|
||||
ActiveSession::RECENT_ACTIVITY,
|
||||
['lteq' => $this->dateTime->gmtDate('Y-m-d H:i:s', "- $sessionLifeTime seconds") ]
|
||||
);
|
||||
|
||||
return $activeSessionCollection->getColumnValues(ActiveSession::SESSION_ID);
|
||||
}
|
||||
|
||||
public function terminateAll(): void
|
||||
{
|
||||
$sessionIds = $this->activeSession->getAllSessionIds();
|
||||
foreach ($sessionIds as $sessionId) {
|
||||
$this->terminate($sessionId);
|
||||
}
|
||||
}
|
||||
|
||||
private function destroySessionBySessionId(string $sessionId): void
|
||||
{
|
||||
/** @var AdminSessionInfo $adminSessionInfo */
|
||||
$adminSessionInfo = $this->adminSessionInfoCollectionFactory->create()
|
||||
->addFieldToFilter('session_id', $sessionId)
|
||||
->getFirstItem();
|
||||
if (!$adminSessionInfo->getId()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->updateAdminSessionInfoStatus($adminSessionInfo);
|
||||
}
|
||||
|
||||
private function destroySessionByAdminSessionInfoId(int $adminSessionInfoId): void
|
||||
{
|
||||
/** @var AdminSessionInfo $adminSessionInfo */
|
||||
$adminSessionInfo = $this->adminSessionInfoCollectionFactory->create()
|
||||
->addFieldToFilter('id', $adminSessionInfoId)
|
||||
->getFirstItem();
|
||||
if (!$adminSessionInfo->getId()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->updateAdminSessionInfoStatus($adminSessionInfo);
|
||||
}
|
||||
|
||||
private function updateAdminSessionInfoStatus(AdminSessionInfo $adminSessionInfo): void
|
||||
{
|
||||
$this->adminSessionInfoResource->updateStatusByUserId(
|
||||
AdminSessionInfo::LOGGED_OUT_MANUALLY,
|
||||
$adminSessionInfo->getUserId(),
|
||||
[AdminSessionInfo::LOGGED_IN]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
<?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\ActiveSession;
|
||||
|
||||
use Amasty\AdminActionsLog\Api\ActiveSessionRepositoryInterface;
|
||||
use Amasty\AdminActionsLog\Api\Data\ActiveSessionInterface;
|
||||
use Amasty\AdminActionsLog\Api\Data\ActiveSessionInterfaceFactory;
|
||||
use Magento\Framework\Exception\CouldNotDeleteException;
|
||||
use Magento\Framework\Exception\CouldNotSaveException;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
|
||||
class Repository implements ActiveSessionRepositoryInterface
|
||||
{
|
||||
/**
|
||||
* @var ActiveSessionInterfaceFactory
|
||||
*/
|
||||
private $activeSessionFactory;
|
||||
|
||||
/**
|
||||
* @var ResourceModel\ActiveSession
|
||||
*/
|
||||
private $resource;
|
||||
|
||||
/**
|
||||
* @var ResourceModel\CollectionFactory
|
||||
*/
|
||||
private $collectionFactory;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $activeSessions = [];
|
||||
|
||||
public function __construct(
|
||||
ActiveSessionInterfaceFactory $activeSessionFactory,
|
||||
ResourceModel\ActiveSession $resource,
|
||||
ResourceModel\CollectionFactory $collectionFactory
|
||||
) {
|
||||
$this->activeSessionFactory = $activeSessionFactory;
|
||||
$this->resource = $resource;
|
||||
$this->collectionFactory = $collectionFactory;
|
||||
}
|
||||
|
||||
public function getById(int $id): ActiveSessionInterface
|
||||
{
|
||||
if (!isset($this->activeSessions[$id])) {
|
||||
/** @var ActiveSessionInterface $activeSession */
|
||||
$activeSession = $this->activeSessionFactory->create();
|
||||
$this->resource->load($activeSession, $id);
|
||||
if (!$activeSession->getId()) {
|
||||
throw new NoSuchEntityException(__('Active Session with specified ID "%1" not found.', $id));
|
||||
}
|
||||
|
||||
$this->activeSessions[$id] = $activeSession;
|
||||
}
|
||||
|
||||
return $this->activeSessions[$id];
|
||||
}
|
||||
|
||||
public function getBySessionId(string $sessionId): ActiveSessionInterface
|
||||
{
|
||||
/** @var ActiveSessionInterface $searchedRecord */
|
||||
$searchedRecord = $this->collectionFactory->create()
|
||||
->addFieldToFilter(ActiveSession::SESSION_ID, $sessionId)
|
||||
->getFirstItem();
|
||||
|
||||
return $this->getById((int)$searchedRecord->getId());
|
||||
}
|
||||
|
||||
public function save(ActiveSessionInterface $activeSession): ActiveSessionInterface
|
||||
{
|
||||
try {
|
||||
if ($activeSession->getId()) {
|
||||
$activeSession = $this->getById((int)$activeSession->getId())->addData($activeSession->getData());
|
||||
}
|
||||
$this->resource->save($activeSession);
|
||||
|
||||
unset($this->activeSessions[$activeSession->getId()]);
|
||||
} catch (\Exception $e) {
|
||||
throw new CouldNotSaveException(__('Unable to save the Active Session. Error: %1', $e->getMessage()));
|
||||
}
|
||||
|
||||
return $activeSession;
|
||||
}
|
||||
|
||||
public function delete(ActiveSessionInterface $activeSession): bool
|
||||
{
|
||||
try {
|
||||
$this->resource->delete($activeSession);
|
||||
unset($this->activeSessions[$activeSession->getId()]);
|
||||
} catch (\Exception $e) {
|
||||
throw new CouldNotDeleteException(__('Unable to delete the Active Session. Error: %1', $e->getMessage()));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function deleteById(int $id): bool
|
||||
{
|
||||
return $this->delete($this->getById($id));
|
||||
}
|
||||
}
|
||||
+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\ActiveSession\ResourceModel;
|
||||
|
||||
use Amasty\AdminActionsLog\Model\ActiveSession\ActiveSession as ActiveSessionModel;
|
||||
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
|
||||
|
||||
class ActiveSession extends AbstractDb
|
||||
{
|
||||
public const TABLE_NAME = 'amasty_audit_active_sessions';
|
||||
|
||||
protected function _construct()
|
||||
{
|
||||
$this->_init(self::TABLE_NAME, ActiveSessionModel::ID);
|
||||
}
|
||||
|
||||
public function updateIfSessionInvalid(int $userId, int $adminSessionInfoId): void
|
||||
{
|
||||
if (!$this->isSessionValid($userId, $adminSessionInfoId)) {
|
||||
$connection = $this->getConnection();
|
||||
$connection->update(
|
||||
$this->getMainTable(),
|
||||
[ActiveSessionModel::ADMIN_SESSION_INFO_ID => $adminSessionInfoId],
|
||||
$connection->quoteInto(ActiveSessionModel::USER_ID . ' = ?', $userId)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function getAllSessionIds(): array
|
||||
{
|
||||
$connection = $this->getConnection();
|
||||
$tableName = $this->getMainTable();
|
||||
$select = $connection->select()
|
||||
->from(
|
||||
$tableName,
|
||||
[ActiveSessionModel::SESSION_ID]
|
||||
);
|
||||
|
||||
return $connection->fetchCol($select);
|
||||
}
|
||||
|
||||
private function isSessionValid(int $userId, int $adminSessionInfoId): bool
|
||||
{
|
||||
$connection = $this->getConnection();
|
||||
$select = $connection->select()->from(
|
||||
$this->getMainTable(),
|
||||
[ActiveSessionModel::ADMIN_SESSION_INFO_ID]
|
||||
)->where(
|
||||
ActiveSessionModel::USER_ID . ' = ?',
|
||||
$userId
|
||||
)->limit(1);
|
||||
$result = (int)$connection->fetchOne($select);
|
||||
|
||||
return $adminSessionInfoId == $result;
|
||||
}
|
||||
}
|
||||
+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\ActiveSession\ResourceModel;
|
||||
|
||||
use Amasty\AdminActionsLog\Model\ActiveSession\ActiveSession as ActiveSessionModel;
|
||||
use Amasty\AdminActionsLog\Model\ActiveSession\ResourceModel\ActiveSession as ActiveSessionResource;
|
||||
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
|
||||
|
||||
class Collection extends AbstractCollection
|
||||
{
|
||||
public function _construct()
|
||||
{
|
||||
$this->_init(ActiveSessionModel::class, ActiveSessionResource::class);
|
||||
$this->_setIdFieldName($this->getResource()->getIdFieldName());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user