Session.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Persistent\Model\ResourceModel;
  7. /**
  8. * Persistent Session Resource Model
  9. */
  10. class Session extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  11. {
  12. /**
  13. * Use is object new method for object saving
  14. *
  15. * @var bool
  16. */
  17. protected $_useIsObjectNew = true;
  18. /**
  19. * Session factory
  20. *
  21. * @var \Magento\Persistent\Model\SessionFactory
  22. */
  23. protected $_sessionFactory;
  24. /**
  25. * Class constructor
  26. *
  27. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  28. * @param \Magento\Persistent\Model\SessionFactory $sessionFactory
  29. * @param string $connectionName
  30. */
  31. public function __construct(
  32. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  33. \Magento\Persistent\Model\SessionFactory $sessionFactory,
  34. $connectionName = null
  35. ) {
  36. $this->_sessionFactory = $sessionFactory;
  37. parent::__construct($context, $connectionName);
  38. }
  39. /**
  40. * Initialize connection and define main table and primary key
  41. *
  42. * @return void
  43. */
  44. protected function _construct()
  45. {
  46. $this->_init('persistent_session', 'persistent_id');
  47. }
  48. /**
  49. * Add expiration date filter to select
  50. *
  51. * @param string $field
  52. * @param mixed $value
  53. * @param \Magento\Persistent\Model\Session $object
  54. * @return \Magento\Framework\DB\Select
  55. */
  56. protected function _getLoadSelect($field, $value, $object)
  57. {
  58. $select = parent::_getLoadSelect($field, $value, $object);
  59. if (!$object->getLoadExpired()) {
  60. $tableName = $this->getMainTable();
  61. $select->join(
  62. ['customer' => $this->getTable('customer_entity')],
  63. 'customer.entity_id = ' . $tableName . '.customer_id'
  64. )->where(
  65. $tableName . '.updated_at >= ?',
  66. $object->getExpiredBefore()
  67. );
  68. }
  69. return $select;
  70. }
  71. /**
  72. * Delete customer persistent session by customer id
  73. *
  74. * @param int $customerId
  75. * @return $this
  76. */
  77. public function deleteByCustomerId($customerId)
  78. {
  79. $this->getConnection()->delete($this->getMainTable(), ['customer_id = ?' => $customerId]);
  80. return $this;
  81. }
  82. /**
  83. * Check if such session key allowed (not exists)
  84. *
  85. * @param string $key
  86. * @return bool
  87. */
  88. public function isKeyAllowed($key)
  89. {
  90. $sameSession = $this->_sessionFactory->create()->setLoadExpired();
  91. $sameSession->loadByCookieKey($key);
  92. return !$sameSession->getId();
  93. }
  94. /**
  95. * Delete expired persistent sessions
  96. *
  97. * @param int $websiteId
  98. * @param string $expiredBefore A formatted date string
  99. * @return $this
  100. */
  101. public function deleteExpired($websiteId, $expiredBefore)
  102. {
  103. $this->getConnection()->delete(
  104. $this->getMainTable(),
  105. ['website_id = ?' => $websiteId, 'updated_at < ?' => $expiredBefore]
  106. );
  107. return $this;
  108. }
  109. }