Collection.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Security\Model\ResourceModel\AdminSessionInfo;
  7. /**
  8. * Admin Session Info collection
  9. *
  10. * @api
  11. * @since 100.1.0
  12. */
  13. class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  14. {
  15. /**
  16. * @var string
  17. * @since 100.1.0
  18. */
  19. protected $_idFieldName = 'id';
  20. /**
  21. * @var \Magento\Framework\Stdlib\DateTime\DateTime
  22. * @since 100.1.0
  23. */
  24. protected $dateTime;
  25. /**
  26. * @param \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory
  27. * @param \Psr\Log\LoggerInterface $logger
  28. * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
  29. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  30. * @param \Magento\Framework\Stdlib\DateTime\DateTime $dateTime
  31. * @param \Magento\Framework\DB\Adapter\AdapterInterface|null $connection
  32. * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb|null $resource
  33. */
  34. public function __construct(
  35. \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
  36. \Psr\Log\LoggerInterface $logger,
  37. \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
  38. \Magento\Framework\Event\ManagerInterface $eventManager,
  39. \Magento\Framework\Stdlib\DateTime\DateTime $dateTime,
  40. \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
  41. \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
  42. ) {
  43. parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
  44. $this->dateTime = $dateTime;
  45. }
  46. /**
  47. * Define resource model
  48. *
  49. * @return void
  50. * @since 100.1.0
  51. */
  52. protected function _construct()
  53. {
  54. $this->_init(
  55. \Magento\Security\Model\AdminSessionInfo::class,
  56. \Magento\Security\Model\ResourceModel\AdminSessionInfo::class
  57. );
  58. }
  59. /**
  60. * Update active sessions status except a specific one
  61. *
  62. * @param int $status
  63. * @param int $userId
  64. * @param string $sessionIdToExclude
  65. * @param int $updateOlderThen
  66. * @return int The number of affected rows.
  67. * @since 100.1.0
  68. */
  69. public function updateActiveSessionsStatus(
  70. $status,
  71. $userId,
  72. $sessionIdToExclude,
  73. $updateOlderThen = null
  74. ) {
  75. return $this->getResource()->updateStatusByUserId(
  76. $status,
  77. $userId,
  78. [\Magento\Security\Model\AdminSessionInfo::LOGGED_IN],
  79. [$sessionIdToExclude],
  80. $updateOlderThen
  81. );
  82. }
  83. /**
  84. * Filter by user
  85. *
  86. * @param int $userId
  87. * @param int $status
  88. * @param null|string $sessionIdToExclude
  89. * @return $this
  90. * @since 100.1.0
  91. */
  92. public function filterByUser(
  93. $userId,
  94. $status = \Magento\Security\Model\AdminSessionInfo::LOGGED_IN,
  95. $sessionIdToExclude = null
  96. ) {
  97. $this->addFieldToFilter('user_id', $userId);
  98. $this->addFieldToFilter('status', $status);
  99. if (null !== $sessionIdToExclude) {
  100. $this->addFieldToFilter('session_id', ['neq' => $sessionIdToExclude]);
  101. }
  102. return $this;
  103. }
  104. /**
  105. * Filter expired sessions
  106. *
  107. * @param int $sessionLifeTime
  108. * @return $this
  109. * @since 100.1.0
  110. */
  111. public function filterExpiredSessions($sessionLifeTime)
  112. {
  113. $connection = $this->getConnection();
  114. $gmtTimestamp = $this->dateTime->gmtTimestamp();
  115. $this->addFieldToFilter(
  116. 'updated_at',
  117. ['gt' => $connection->formatDate($gmtTimestamp - $sessionLifeTime)]
  118. );
  119. return $this;
  120. }
  121. /**
  122. * Delete sessions older than some value
  123. *
  124. * @param int $timestamp
  125. * @return $this
  126. * @since 100.1.0
  127. */
  128. public function deleteSessionsOlderThen($timestamp)
  129. {
  130. $this->getResource()->deleteSessionsOlderThen((int) $timestamp);
  131. return $this;
  132. }
  133. }