Skip to content

Use constructor property promotion in module Security #37218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 2.4-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 19 additions & 32 deletions app/code/Magento/Security/Block/Adminhtml/Session/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,47 @@
*/
namespace Magento\Security\Block\Adminhtml\Session;

use DateTime;
use IntlDateFormatter;
use Magento\Backend\Block\Template;
use Magento\Backend\Block\Template\Context as TemplateContext;
use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
use Magento\Security\Model\AdminSessionsManager;
use Magento\Security\Model\ConfigInterface;
use Magento\Security\Model\ResourceModel\AdminSessionInfo\Collection as AdminSessionInfoCollection;
use Magento\Security\Model\ResourceModel\AdminSessionInfo\CollectionFactory;

/**
* Block Session Activity
*
* @api
* @since 100.1.0
*/
class Activity extends \Magento\Backend\Block\Template
class Activity extends Template
{
/**
* @var ConfigInterface
* @since 100.1.0
*/
protected $securityConfig;

/**
* @var \Magento\Security\Model\AdminSessionsManager
* @since 100.1.0
*/
protected $sessionsManager;

/**
* @var \Magento\Security\Model\ResourceModel\AdminSessionInfo\CollectionFactory
* @var CollectionFactory
* @since 100.1.0
*/
protected $sessionsInfoCollection;

/**
* @var RemoteAddress
*/
private $remoteAddress;

/**
* @param \Magento\Backend\Block\Template\Context $context
* @param TemplateContext $context
* @param ConfigInterface $securityConfig
* @param \Magento\Security\Model\AdminSessionsManager $sessionsManager
* @param AdminSessionsManager $sessionsManager
* @param RemoteAddress $remoteAddress
*/
public function __construct(
\Magento\Backend\Block\Template\Context $context,
ConfigInterface $securityConfig,
\Magento\Security\Model\AdminSessionsManager $sessionsManager,
RemoteAddress $remoteAddress
TemplateContext $context,
protected readonly ConfigInterface $securityConfig,
protected readonly AdminSessionsManager $sessionsManager,
private readonly RemoteAddress $remoteAddress
) {
parent::__construct($context);
$this->securityConfig = $securityConfig;
$this->sessionsManager = $sessionsManager;
$this->remoteAddress = $remoteAddress;
}

/**
* @return \Magento\Security\Model\ResourceModel\AdminSessionInfo\Collection
* @return AdminSessionInfoCollection
* @since 100.1.0
*/
public function getSessionInfoCollection()
Expand Down Expand Up @@ -96,11 +83,11 @@ public function getRemoteIp()
*/
public function formatDateTime($time)
{
$time = new \DateTime($time);
$time = new DateTime($time);
return $this->_localeDate->formatDateTime(
$time,
\IntlDateFormatter::MEDIUM,
\IntlDateFormatter::MEDIUM
IntlDateFormatter::MEDIUM,
IntlDateFormatter::MEDIUM
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,17 @@
*/
class SessionSize extends Field
{
/**
* @var Json
*/
private $json;

/**
* @param Context $context
* @param Json $json
* @param array $data
*/
public function __construct(
Context $context,
Json $json,
private readonly Json $json,
array $data = []
) {
parent::__construct($context, $data);
$this->json = $json;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
*/
namespace Magento\Security\Controller\Adminhtml\Session;

use Magento\Backend\App\Action;

/**
* Admin session activity
*/
class Activity extends \Magento\Backend\App\Action
class Activity extends Action
{
/**
* @return void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,27 @@
*/
namespace Magento\Security\Controller\Adminhtml\Session;

use Exception;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Exception\LocalizedException;
use Magento\Security\Model\AdminSessionsManager;

/**
* Admin session logout all
*/
class LogoutAll extends \Magento\Backend\App\Action
class LogoutAll extends Action
{
/**
* @var AdminSessionsManager
*/
protected $sessionsManager;

/**
* Check constructor.
* @param Context $context
* @param AdminSessionsManager $sessionsManager
*/
public function __construct(
Context $context,
AdminSessionsManager $sessionsManager
protected readonly AdminSessionsManager $sessionsManager
) {
parent::__construct($context);
$this->sessionsManager = $sessionsManager;
}

/**
Expand All @@ -39,9 +36,9 @@ public function execute()
try {
$this->sessionsManager->logoutOtherUserSessions();
$this->messageManager->addSuccessMessage(__('All other open sessions for this account were terminated.'));
} catch (\Magento\Framework\Exception\LocalizedException $e) {
} catch (LocalizedException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
} catch (\Exception $e) {
} catch (Exception $e) {
$this->messageManager->addExceptionMessage($e, __("We couldn't logout because of an error."));
}
$this->_redirect('*/*/activity');
Expand Down
47 changes: 21 additions & 26 deletions app/code/Magento/Security/Model/AdminSessionInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@

namespace Magento\Security\Model;

use Magento\Framework\Data\Collection\AbstractDb;
use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Model\Context as ModelContext;
use Magento\Framework\Model\ResourceModel\AbstractResource;
use Magento\Framework\Registry;
use Magento\Framework\Stdlib\DateTime\DateTime as DateTimeModel;
use Magento\Security\Model\ResourceModel\AdminSessionInfo as ResourceAdminSessionInfo;

/**
* Admin Session Info Model
*
Expand All @@ -18,7 +26,7 @@
* @api
* @since 100.1.0
*/
class AdminSessionInfo extends \Magento\Framework\Model\AbstractModel
class AdminSessionInfo extends AbstractModel
{
/**
* Admin session status definition
Expand Down Expand Up @@ -51,40 +59,27 @@ class AdminSessionInfo extends \Magento\Framework\Model\AbstractModel
*/
protected $isOtherSessionsTerminated = false;

/**
* @var ConfigInterface
* @since 100.1.0
*/
protected $securityConfig;

/**
* @var \Magento\Framework\Stdlib\DateTime\DateTime
*/
private $dateTime;

/**
* AdminSessionInfo constructor
*
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
* @param ModelContext $context
* @param Registry $registry
* @param ConfigInterface $securityConfig
* @param \Magento\Framework\Stdlib\DateTime\DateTime $dateTime
* @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
* @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
* @param DateTimeModel $dateTime
* @param AbstractResource|null $resource
* @param AbstractDb|null $resourceCollection
* @param array $data
*/
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
ConfigInterface $securityConfig,
\Magento\Framework\Stdlib\DateTime\DateTime $dateTime,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
ModelContext $context,
Registry $registry,
protected readonly ConfigInterface $securityConfig,
private readonly DateTimeModel $dateTime,
AbstractResource $resource = null,
AbstractDb $resourceCollection = null,
array $data = []
) {
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
$this->securityConfig = $securityConfig;
$this->dateTime = $dateTime;
}

/**
Expand All @@ -95,7 +90,7 @@ public function __construct(
*/
protected function _construct()
{
$this->_init(\Magento\Security\Model\ResourceModel\AdminSessionInfo::class);
$this->_init(ResourceAdminSessionInfo::class);
}

/**
Expand Down
64 changes: 13 additions & 51 deletions app/code/Magento/Security/Model/AdminSessionsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
use Magento\Backend\Model\Auth\Session;
use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
use Magento\Framework\Stdlib\DateTime;
use Magento\Framework\Stdlib\DateTime\DateTime as DateTimeModel;
use Magento\Security\Model\ResourceModel\AdminSessionInfo\Collection;
use Magento\Security\Model\ResourceModel\AdminSessionInfo\CollectionFactory;
use Magento\Security\Model\ResourceModel\AdminSessionInfo\CollectionFactory as AdminSessionInfoCollectionFactory;

/**
* Admin Sessions Manager Model
Expand All @@ -33,45 +35,11 @@ class AdminSessionsManager
public const LOGOUT_REASON_USER_LOCKED = 10;

/**
* @var ConfigInterface
* @since 100.1.0
*/
protected $securityConfig;

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

/**
* @var AdminSessionInfoFactory
* @since 100.1.0
*/
protected $adminSessionInfoFactory;

/**
* @var \Magento\Security\Model\ResourceModel\AdminSessionInfo\CollectionFactory
* @since 100.1.0
*/
protected $adminSessionInfoCollectionFactory;

/**
* @var \Magento\Security\Model\AdminSessionInfo
* @var AdminSessionInfo
* @since 100.1.0
*/
protected $currentSession;

/**
* @var \Magento\Framework\Stdlib\DateTime\DateTime
*/
private $dateTime;

/**
* @var RemoteAddress
*/
private $remoteAddress;

/**
* Max lifetime for session prolong to be valid (sec)
*
Expand All @@ -87,23 +55,17 @@ class AdminSessionsManager
* @param Session $authSession
* @param AdminSessionInfoFactory $adminSessionInfoFactory
* @param CollectionFactory $adminSessionInfoCollectionFactory
* @param \Magento\Framework\Stdlib\DateTime\DateTime $dateTime
* @param DateTimeModel $dateTime
* @param RemoteAddress $remoteAddress
*/
public function __construct(
ConfigInterface $securityConfig,
Session $authSession,
\Magento\Security\Model\AdminSessionInfoFactory $adminSessionInfoFactory,
\Magento\Security\Model\ResourceModel\AdminSessionInfo\CollectionFactory $adminSessionInfoCollectionFactory,
\Magento\Framework\Stdlib\DateTime\DateTime $dateTime,
RemoteAddress $remoteAddress
protected readonly ConfigInterface $securityConfig,
protected readonly Session $authSession,
protected readonly AdminSessionInfoFactory $adminSessionInfoFactory,
protected readonly AdminSessionInfoCollectionFactory $adminSessionInfoCollectionFactory,
private readonly DateTimeModel $dateTime,
private readonly RemoteAddress $remoteAddress
) {
$this->securityConfig = $securityConfig;
$this->authSession = $authSession;
$this->adminSessionInfoFactory = $adminSessionInfoFactory;
$this->adminSessionInfoCollectionFactory = $adminSessionInfoCollectionFactory;
$this->dateTime = $dateTime;
$this->remoteAddress = $remoteAddress;
}

/**
Expand Down Expand Up @@ -253,7 +215,7 @@ public function getLogoutReasonMessage()
public function getSessionsForCurrentUser()
{
return $this->createAdminSessionInfoCollection()
->filterByUser($this->authSession->getUser()->getId(), \Magento\Security\Model\AdminSessionInfo::LOGGED_IN)
->filterByUser($this->authSession->getUser()->getId(), AdminSessionInfo::LOGGED_IN)
->filterExpiredSessions($this->securityConfig->getAdminSessionLifetime())
->loadData();
}
Expand All @@ -271,13 +233,13 @@ public function logoutOtherUserSessions()
$collection = $this->createAdminSessionInfoCollection()
->filterByUser(
$user->getId(),
\Magento\Security\Model\AdminSessionInfo::LOGGED_IN,
AdminSessionInfo::LOGGED_IN,
$this->authSession->getAdminSessionInfoId()
)
->filterExpiredSessions($this->securityConfig->getAdminSessionLifetime())
->loadData();

$collection->setDataToAll('status', \Magento\Security\Model\AdminSessionInfo::LOGGED_OUT_MANUALLY)
$collection->setDataToAll('status', AdminSessionInfo::LOGGED_OUT_MANUALLY)
->save();
}

Expand Down
Loading