Token.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Integration\Model\ResourceModel\Oauth;
  7. use Magento\Authorization\Model\UserContextInterface;
  8. /**
  9. * OAuth token resource model
  10. */
  11. class Token extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  12. {
  13. /**
  14. * @var \Magento\Framework\Stdlib\DateTime
  15. */
  16. protected $_dateTime;
  17. /**
  18. * Date
  19. *
  20. * @var \Magento\Framework\Stdlib\DateTime\DateTime
  21. */
  22. protected $date;
  23. /**
  24. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  25. * @param \Magento\Framework\Stdlib\DateTime $dateTime
  26. * @param \Magento\Framework\Stdlib\DateTime\DateTime $date
  27. * @param string $connectionName
  28. */
  29. public function __construct(
  30. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  31. \Magento\Framework\Stdlib\DateTime $dateTime,
  32. \Magento\Framework\Stdlib\DateTime\DateTime $date,
  33. $connectionName = null
  34. ) {
  35. $this->_dateTime = $dateTime;
  36. $this->date = $date;
  37. parent::__construct($context, $connectionName);
  38. }
  39. /**
  40. * Initialize resource model
  41. *
  42. * @return void
  43. */
  44. protected function _construct()
  45. {
  46. $this->_init('oauth_token', 'entity_id');
  47. }
  48. /**
  49. * Clean up old authorized tokens for specified consumer-user pairs
  50. *
  51. * @param \Magento\Integration\Model\Oauth\Token $exceptToken Token just created to exclude from delete
  52. * @throws \Magento\Framework\Exception\LocalizedException
  53. * @return int The number of affected rows
  54. */
  55. public function cleanOldAuthorizedTokensExcept(\Magento\Integration\Model\Oauth\Token $exceptToken)
  56. {
  57. if (!$exceptToken->getId() || !$exceptToken->getAuthorized()) {
  58. throw new \Magento\Framework\Exception\LocalizedException(__('Invalid token to except'));
  59. }
  60. $connection = $this->getConnection();
  61. $where = $connection->quoteInto(
  62. 'authorized = 1 AND consumer_id = ?',
  63. $exceptToken->getConsumerId(),
  64. \Zend_Db::INT_TYPE
  65. );
  66. $where .= $connection->quoteInto(' AND entity_id <> ?', $exceptToken->getId(), \Zend_Db::INT_TYPE);
  67. if ($exceptToken->getCustomerId()) {
  68. $where .= $connection->quoteInto(' AND customer_id = ?', $exceptToken->getCustomerId(), \Zend_Db::INT_TYPE);
  69. } elseif ($exceptToken->getAdminId()) {
  70. $where .= $connection->quoteInto(' AND admin_id = ?', $exceptToken->getAdminId(), \Zend_Db::INT_TYPE);
  71. } else {
  72. throw new \Magento\Framework\Exception\LocalizedException(__('Invalid token to except'));
  73. }
  74. return $connection->delete($this->getMainTable(), $where);
  75. }
  76. /**
  77. * Delete old entries
  78. *
  79. * @param int $minutes
  80. * @return int
  81. */
  82. public function deleteOldEntries($minutes)
  83. {
  84. if ($minutes > 0) {
  85. $connection = $this->getConnection();
  86. return $connection->delete(
  87. $this->getMainTable(),
  88. $connection->quoteInto(
  89. 'type = "' . \Magento\Integration\Model\Oauth\Token::TYPE_REQUEST . '" AND created_at <= ?',
  90. $this->_dateTime->formatDate($this->date->gmtTimestamp() - $minutes * 60)
  91. )
  92. );
  93. } else {
  94. return 0;
  95. }
  96. }
  97. /**
  98. * Delete expired tokens for the specified user types
  99. *
  100. * @param int $hours token lifetime
  101. * @param int[] $userTypes @see \Magento\Authorization\Model\UserContextInterface
  102. * @return int number of deleted tokens
  103. */
  104. public function deleteExpiredTokens($hours, $userTypes)
  105. {
  106. if ($hours > 0) {
  107. $connection = $this->getConnection();
  108. $userTypeCondition = $connection->quoteInto('user_type IN (?)', $userTypes);
  109. $createdAtCondition = $connection->quoteInto(
  110. 'created_at <= ?',
  111. $this->_dateTime->formatDate($this->date->gmtTimestamp() - $hours * 60 * 60)
  112. );
  113. return $connection->delete(
  114. $this->getMainTable(),
  115. $userTypeCondition . ' AND ' . $createdAtCondition
  116. );
  117. } else {
  118. return 0;
  119. }
  120. }
  121. /**
  122. * Select a single token of the specified type for the specified consumer.
  123. *
  124. * @param int $consumerId - The consumer id
  125. * @param string $type - The token type (e.g. 'verifier')
  126. * @return array|boolean - Row data (array) or false if there is no corresponding row
  127. */
  128. public function selectTokenByType($consumerId, $type)
  129. {
  130. $connection = $this->getConnection();
  131. $select = $connection->select()
  132. ->from($this->getMainTable())
  133. ->where('consumer_id = ?', $consumerId)
  134. ->where('type = ?', $type);
  135. return $connection->fetchRow($select);
  136. }
  137. /**
  138. * Select token for a given consumer and user type.
  139. *
  140. * @param int $consumerId
  141. * @param int $userType
  142. * @return array|boolean - Row data (array) or false if there is no corresponding row
  143. */
  144. public function selectTokenByConsumerIdAndUserType($consumerId, $userType)
  145. {
  146. $connection = $this->getConnection();
  147. $select = $connection->select()
  148. ->from($this->getMainTable())
  149. ->where('consumer_id = ?', (int)$consumerId)
  150. ->where('user_type = ?', (int)$userType);
  151. return $connection->fetchRow($select);
  152. }
  153. /**
  154. * Select token for a given admin id.
  155. *
  156. * @param int $adminId
  157. * @return array|boolean - Row data (array) or false if there is no corresponding row
  158. */
  159. public function selectTokenByAdminId($adminId)
  160. {
  161. $connection = $this->getConnection();
  162. $select = $connection->select()
  163. ->from($this->getMainTable())
  164. ->where('admin_id = ?', $adminId)
  165. ->where('user_type = ?', UserContextInterface::USER_TYPE_ADMIN);
  166. return $connection->fetchRow($select);
  167. }
  168. /**
  169. * Select token for a given customer.
  170. *
  171. * @param int $customerId
  172. * @return array|boolean - Row data (array) or false if there is no corresponding row
  173. */
  174. public function selectTokenByCustomerId($customerId)
  175. {
  176. $connection = $this->getConnection();
  177. $select = $connection->select()
  178. ->from($this->getMainTable())
  179. ->where('customer_id = ?', $customerId)
  180. ->where('user_type = ?', UserContextInterface::USER_TYPE_CUSTOMER);
  181. return $connection->fetchRow($select);
  182. }
  183. }