Log.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Captcha\Model\ResourceModel;
  7. /**
  8. * Log Attempts resource
  9. *
  10. * @author Magento Core Team <core@magentocommerce.com>
  11. */
  12. class Log extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  13. {
  14. /**
  15. * Type Remote Address
  16. */
  17. const TYPE_REMOTE_ADDRESS = 1;
  18. /**
  19. * Type User Login Name
  20. */
  21. const TYPE_LOGIN = 2;
  22. /**
  23. * Core Date
  24. *
  25. * @var \Magento\Framework\Stdlib\DateTime\DateTime
  26. */
  27. protected $_coreDate;
  28. /**
  29. * @var \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress
  30. */
  31. protected $_remoteAddress;
  32. /**
  33. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  34. * @param \Magento\Framework\Stdlib\DateTime\DateTime $coreDate
  35. * @param \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress
  36. * @param string $connectionName
  37. */
  38. public function __construct(
  39. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  40. \Magento\Framework\Stdlib\DateTime\DateTime $coreDate,
  41. \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress,
  42. $connectionName = null
  43. ) {
  44. $this->_coreDate = $coreDate;
  45. $this->_remoteAddress = $remoteAddress;
  46. parent::__construct($context, $connectionName);
  47. }
  48. /**
  49. * Define main table
  50. *
  51. * @return void
  52. */
  53. protected function _construct()
  54. {
  55. $this->_setMainTable('captcha_log');
  56. }
  57. /**
  58. * Save or Update count Attempts
  59. *
  60. * @param string|null $login
  61. * @return $this
  62. * @throws \Magento\Framework\Exception\LocalizedException
  63. */
  64. public function logAttempt($login)
  65. {
  66. if ($login != null) {
  67. $this->getConnection()->insertOnDuplicate(
  68. $this->getMainTable(),
  69. [
  70. 'type' => self::TYPE_LOGIN,
  71. 'value' => $login,
  72. 'count' => 1,
  73. 'updated_at' => $this->_coreDate->gmtDate()
  74. ],
  75. ['count' => new \Zend\Db\Sql\Expression('count+1'), 'updated_at']
  76. );
  77. }
  78. $ip = $this->_remoteAddress->getRemoteAddress();
  79. if ($ip != null) {
  80. $this->getConnection()->insertOnDuplicate(
  81. $this->getMainTable(),
  82. [
  83. 'type' => self::TYPE_REMOTE_ADDRESS,
  84. 'value' => $ip,
  85. 'count' => 1,
  86. 'updated_at' => $this->_coreDate->gmtDate()
  87. ],
  88. ['count' => new \Zend\Db\Sql\Expression('count+1'), 'updated_at']
  89. );
  90. }
  91. return $this;
  92. }
  93. /**
  94. * Delete User attempts by login
  95. *
  96. * @param string $login
  97. * @return $this
  98. * @throws \Magento\Framework\Exception\LocalizedException
  99. */
  100. public function deleteUserAttempts($login)
  101. {
  102. if ($login != null) {
  103. $this->getConnection()->delete(
  104. $this->getMainTable(),
  105. ['type = ?' => self::TYPE_LOGIN, 'value = ?' => $login]
  106. );
  107. }
  108. $ip = $this->_remoteAddress->getRemoteAddress();
  109. if ($ip != null) {
  110. $this->getConnection()->delete(
  111. $this->getMainTable(),
  112. ['type = ?' => self::TYPE_REMOTE_ADDRESS, 'value = ?' => $ip]
  113. );
  114. }
  115. return $this;
  116. }
  117. /**
  118. * Get count attempts by ip
  119. *
  120. * @return null|int
  121. * @throws \Magento\Framework\Exception\LocalizedException
  122. */
  123. public function countAttemptsByRemoteAddress()
  124. {
  125. $ip = $this->_remoteAddress->getRemoteAddress();
  126. if (!$ip) {
  127. return 0;
  128. }
  129. $connection = $this->getConnection();
  130. $select = $connection->select()->from(
  131. $this->getMainTable(),
  132. 'count'
  133. )->where(
  134. 'type = ?',
  135. self::TYPE_REMOTE_ADDRESS
  136. )->where(
  137. 'value = ?',
  138. $ip
  139. );
  140. return $connection->fetchOne($select);
  141. }
  142. /**
  143. * Get count attempts by user login
  144. *
  145. * @param string $login
  146. * @return null|int
  147. * @throws \Magento\Framework\Exception\LocalizedException
  148. */
  149. public function countAttemptsByUserLogin($login)
  150. {
  151. if (!$login) {
  152. return 0;
  153. }
  154. $connection = $this->getConnection();
  155. $select = $connection->select()->from(
  156. $this->getMainTable(),
  157. 'count'
  158. )->where(
  159. 'type = ?',
  160. self::TYPE_LOGIN
  161. )->where(
  162. 'value = ?',
  163. $login
  164. );
  165. return $connection->fetchOne($select);
  166. }
  167. /**
  168. * Delete attempts with expired in update_at time
  169. *
  170. * @return void
  171. * @throws \Magento\Framework\Exception\LocalizedException
  172. */
  173. public function deleteOldAttempts()
  174. {
  175. $this->getConnection()->delete(
  176. $this->getMainTable(),
  177. ['updated_at < ?' => $this->_coreDate->gmtDate(null, time() - 60 * 30)]
  178. );
  179. }
  180. }