RequestLog.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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\Token;
  7. use Magento\Integration\Model\Oauth\Token\RequestLog\ReaderInterface;
  8. use Magento\Integration\Model\Oauth\Token\RequestLog\WriterInterface;
  9. use Magento\Integration\Model\Oauth\Token\RequestLog\Config as RequestLogConfig;
  10. /**
  11. * Resource model for failed authentication attempts to retrieve admin/customer token.
  12. */
  13. class RequestLog extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb implements
  14. ReaderInterface,
  15. WriterInterface
  16. {
  17. /**
  18. * @var \Magento\Framework\Stdlib\DateTime\DateTime
  19. */
  20. private $dateTime;
  21. /**
  22. * @var RequestLogConfig
  23. */
  24. private $requestLogConfig;
  25. /**
  26. * Initialize dependencies.
  27. *
  28. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  29. * @param \Magento\Framework\Stdlib\DateTime\DateTime $dateTime
  30. * @param RequestLogConfig $requestLogConfig
  31. * @param string|null $connectionName
  32. */
  33. public function __construct(
  34. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  35. \Magento\Framework\Stdlib\DateTime\DateTime $dateTime,
  36. RequestLogConfig $requestLogConfig,
  37. $connectionName = null
  38. ) {
  39. parent::__construct($context, $connectionName);
  40. $this->dateTime = $dateTime;
  41. $this->requestLogConfig = $requestLogConfig;
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. protected function _construct()
  47. {
  48. $this->_init('oauth_token_request_log', 'entity_id');
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function getFailuresCount($userName, $userType)
  54. {
  55. $select = $this->getConnection()->select();
  56. $select->from($this->getMainTable(), 'failures_count')
  57. ->where('user_name = :user_name AND user_type = :user_type');
  58. return (int)$this->getConnection()->fetchOne($select, ['user_name' => $userName, 'user_type' => $userType]);
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function resetFailuresCount($userName, $userType)
  64. {
  65. $this->getConnection()->delete(
  66. $this->getMainTable(),
  67. ['user_name = ?' => $userName, 'user_type = ?' => $userType]
  68. );
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function incrementFailuresCount($userName, $userType)
  74. {
  75. $date = (new \DateTime())->setTimestamp($this->dateTime->gmtTimestamp());
  76. $date->add(new \DateInterval('PT' . $this->requestLogConfig->getLockTimeout() . 'S'));
  77. $dateTime = $date->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT);
  78. $this->getConnection()->insertOnDuplicate(
  79. $this->getMainTable(),
  80. [
  81. 'user_name' => $userName,
  82. 'user_type' => $userType,
  83. 'failures_count' => 1,
  84. 'lock_expires_at' => $dateTime
  85. ],
  86. [
  87. 'failures_count' => new \Zend_Db_Expr('failures_count+1'),
  88. 'lock_expires_at' => new \Zend_Db_Expr("'" . $dateTime . "'")
  89. ]
  90. );
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function clearExpiredFailures()
  96. {
  97. $date = (new \DateTime())->setTimestamp($this->dateTime->gmtTimestamp());
  98. $dateTime = $date->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT);
  99. $this->getConnection()->delete($this->getMainTable(), ['lock_expires_at <= ?' => $dateTime]);
  100. }
  101. }