AdminSessionInfo.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Security\Model\ResourceModel;
  7. /**
  8. * Admin Session Info mysql resource
  9. *
  10. * @api
  11. * @since 100.1.0
  12. */
  13. class AdminSessionInfo extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  14. {
  15. /**
  16. * @var \Magento\Framework\Stdlib\DateTime
  17. * @since 100.1.0
  18. */
  19. protected $dateTime;
  20. /**
  21. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  22. * @param \Magento\Framework\Stdlib\DateTime $dateTime
  23. * @param null $connectionName
  24. */
  25. public function __construct(
  26. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  27. \Magento\Framework\Stdlib\DateTime $dateTime,
  28. $connectionName = null
  29. ) {
  30. parent::__construct($context, $connectionName);
  31. $this->dateTime = $dateTime;
  32. }
  33. /**
  34. * Initialize resource model
  35. *
  36. * @return void
  37. * @since 100.1.0
  38. */
  39. protected function _construct()
  40. {
  41. $this->_init('admin_user_session', 'id');
  42. }
  43. /**
  44. * Delete records which updated earlier than specified timestamp
  45. *
  46. * @param int $timestamp
  47. * @return $this
  48. * @throws \Magento\Framework\Exception\LocalizedException
  49. * @since 100.1.0
  50. */
  51. public function deleteSessionsOlderThen($timestamp)
  52. {
  53. $this->getConnection()->delete(
  54. $this->getMainTable(),
  55. ['updated_at < ?' => $this->dateTime->formatDate($timestamp)]
  56. );
  57. return $this;
  58. }
  59. /**
  60. * Update status by user ID
  61. *
  62. * @param int $status
  63. * @param int $userId
  64. * @param array $withStatuses
  65. * @param array $excludedSessionIds
  66. * @param int|null $updateOlderThen
  67. * @return int The number of affected rows.
  68. * @throws \Magento\Framework\Exception\LocalizedException
  69. * @since 100.1.0
  70. */
  71. public function updateStatusByUserId(
  72. $status,
  73. $userId,
  74. array $withStatuses = [],
  75. array $excludedSessionIds = [],
  76. $updateOlderThen = null
  77. ) {
  78. $whereStatement = [
  79. 'updated_at > ?' => $this->dateTime->formatDate($updateOlderThen),
  80. 'user_id = ?' => (int) $userId,
  81. ];
  82. if (!empty($excludedSessionIds)) {
  83. $whereStatement['session_id NOT IN (?)'] = $excludedSessionIds;
  84. }
  85. if (!empty($withStatuses)) {
  86. $whereStatement['status IN (?)'] = $withStatuses;
  87. }
  88. return $this->getConnection()->update(
  89. $this->getMainTable(),
  90. ['status' => (int) $status],
  91. $whereStatement
  92. );
  93. }
  94. }