AdminSessionInfo.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Security\Model;
  7. /**
  8. * Admin Session Info Model
  9. *
  10. * @method string getSessionId()
  11. * @method int getUserId() getUserId()
  12. * @method int getStatus()
  13. * @method string getUpdatedAt()
  14. * @method string getCreatedAt()
  15. *
  16. * @api
  17. * @since 100.1.0
  18. */
  19. class AdminSessionInfo extends \Magento\Framework\Model\AbstractModel
  20. {
  21. /**
  22. * Admin session status definition
  23. */
  24. /**
  25. * Admin logged in
  26. */
  27. const LOGGED_IN = 1;
  28. /**
  29. * Admin logged out
  30. */
  31. const LOGGED_OUT = 0;
  32. /**
  33. * User has been logged out by another login with the same credentials
  34. */
  35. const LOGGED_OUT_BY_LOGIN = 2;
  36. /**
  37. * User has been logged out manually from another session
  38. */
  39. const LOGGED_OUT_MANUALLY = 3;
  40. /**
  41. * All other open sessions were terminated
  42. * @since 100.1.0
  43. */
  44. protected $isOtherSessionsTerminated = false;
  45. /**
  46. * @var ConfigInterface
  47. * @since 100.1.0
  48. */
  49. protected $securityConfig;
  50. /**
  51. * @var \Magento\Framework\Stdlib\DateTime\DateTime
  52. */
  53. private $dateTime;
  54. /**
  55. * AdminSessionInfo constructor
  56. *
  57. * @param \Magento\Framework\Model\Context $context
  58. * @param \Magento\Framework\Registry $registry
  59. * @param ConfigInterface $securityConfig
  60. * @param \Magento\Framework\Stdlib\DateTime\DateTime $dateTime
  61. * @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
  62. * @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
  63. * @param array $data
  64. */
  65. public function __construct(
  66. \Magento\Framework\Model\Context $context,
  67. \Magento\Framework\Registry $registry,
  68. ConfigInterface $securityConfig,
  69. \Magento\Framework\Stdlib\DateTime\DateTime $dateTime,
  70. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  71. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  72. array $data = []
  73. ) {
  74. parent::__construct($context, $registry, $resource, $resourceCollection, $data);
  75. $this->securityConfig = $securityConfig;
  76. $this->dateTime = $dateTime;
  77. }
  78. /**
  79. * Initialize resource model
  80. *
  81. * @return void
  82. * @since 100.1.0
  83. */
  84. protected function _construct()
  85. {
  86. $this->_init(\Magento\Security\Model\ResourceModel\AdminSessionInfo::class);
  87. }
  88. /**
  89. * Check if a status is logged in
  90. *
  91. * @return bool
  92. * @since 100.1.0
  93. */
  94. public function isLoggedInStatus()
  95. {
  96. $this->checkActivity();
  97. return $this->getData('status') == self::LOGGED_IN;
  98. }
  99. /**
  100. * Check if session is timed out and set status accordingly
  101. *
  102. * @return void
  103. */
  104. private function checkActivity()
  105. {
  106. if ($this->isSessionExpired()) {
  107. $this->setData('status', self::LOGGED_OUT);
  108. }
  109. }
  110. /**
  111. * Check whether the session is expired
  112. *
  113. * @return bool
  114. * @since 100.1.0
  115. */
  116. public function isSessionExpired()
  117. {
  118. $lifetime = $this->securityConfig->getAdminSessionLifetime();
  119. $currentTime = $this->dateTime->gmtTimestamp();
  120. $lastUpdatedTime = $this->getUpdatedAt();
  121. if (!is_numeric($lastUpdatedTime)) {
  122. $lastUpdatedTime = strtotime($lastUpdatedTime);
  123. }
  124. return $lastUpdatedTime <= ($currentTime - $lifetime) ? true : false;
  125. }
  126. /**
  127. * Get formatted IP
  128. *
  129. * @return string
  130. * @since 100.1.0
  131. */
  132. public function getFormattedIp()
  133. {
  134. return $this->getIp();
  135. }
  136. /**
  137. * Check if other sessions terminated
  138. *
  139. * @return bool
  140. * @since 100.1.0
  141. */
  142. public function isOtherSessionsTerminated()
  143. {
  144. return $this->isOtherSessionsTerminated;
  145. }
  146. /**
  147. * Setter for isOtherSessionsTerminated
  148. *
  149. * @param bool $isOtherSessionsTerminated
  150. * @return $this
  151. * @since 100.1.0
  152. */
  153. public function setIsOtherSessionsTerminated($isOtherSessionsTerminated)
  154. {
  155. $this->isOtherSessionsTerminated = (bool) $isOtherSessionsTerminated;
  156. return $this;
  157. }
  158. }