Activity.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Security\Block\Adminhtml\Session;
  7. use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
  8. use Magento\Security\Model\ConfigInterface;
  9. /**
  10. * Block Session Activity
  11. *
  12. * @api
  13. * @since 100.1.0
  14. */
  15. class Activity extends \Magento\Backend\Block\Template
  16. {
  17. /**
  18. * @var ConfigInterface
  19. * @since 100.1.0
  20. */
  21. protected $securityConfig;
  22. /**
  23. * @var \Magento\Security\Model\AdminSessionsManager
  24. * @since 100.1.0
  25. */
  26. protected $sessionsManager;
  27. /**
  28. * @var \Magento\Security\Model\ResourceModel\AdminSessionInfo\CollectionFactory
  29. * @since 100.1.0
  30. */
  31. protected $sessionsInfoCollection;
  32. /**
  33. * @var RemoteAddress
  34. */
  35. private $remoteAddress;
  36. /**
  37. * @param \Magento\Backend\Block\Template\Context $context
  38. * @param ConfigInterface $securityConfig
  39. * @param \Magento\Security\Model\AdminSessionsManager $sessionsManager
  40. * @param RemoteAddress $remoteAddress
  41. */
  42. public function __construct(
  43. \Magento\Backend\Block\Template\Context $context,
  44. ConfigInterface $securityConfig,
  45. \Magento\Security\Model\AdminSessionsManager $sessionsManager,
  46. RemoteAddress $remoteAddress
  47. ) {
  48. parent::__construct($context);
  49. $this->securityConfig = $securityConfig;
  50. $this->sessionsManager = $sessionsManager;
  51. $this->remoteAddress = $remoteAddress;
  52. }
  53. /**
  54. * @return \Magento\Security\Model\ResourceModel\AdminSessionInfo\Collection
  55. * @since 100.1.0
  56. */
  57. public function getSessionInfoCollection()
  58. {
  59. if (null === $this->sessionsInfoCollection) {
  60. $this->sessionsInfoCollection = $this->sessionsManager->getSessionsForCurrentUser();
  61. }
  62. return $this->sessionsInfoCollection;
  63. }
  64. /**
  65. * @return bool
  66. * @since 100.1.0
  67. */
  68. public function areMultipleSessionsActive()
  69. {
  70. return count($this->getSessionInfoCollection()) > 1;
  71. }
  72. /**
  73. * @return string
  74. * @since 100.1.0
  75. */
  76. public function getRemoteIp()
  77. {
  78. return $this->remoteAddress->getRemoteAddress(false);
  79. }
  80. /**
  81. * Retrieve formatting datetime
  82. *
  83. * @param string $time
  84. * @return string
  85. * @since 100.1.0
  86. */
  87. public function formatDateTime($time)
  88. {
  89. $time = new \DateTime($time);
  90. return $this->_localeDate->formatDateTime(
  91. $time,
  92. \IntlDateFormatter::MEDIUM,
  93. \IntlDateFormatter::MEDIUM
  94. );
  95. }
  96. }