Quantity.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Security\Model\SecurityChecker;
  7. use Magento\Framework\Exception\SecurityViolationException;
  8. use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
  9. use Magento\Security\Model\Config\Source\ResetMethod;
  10. use Magento\Security\Model\ConfigInterface;
  11. use Magento\Security\Model\ResourceModel\PasswordResetRequestEvent\CollectionFactory;
  12. /**
  13. * Check by requests number per fixed period of time
  14. */
  15. class Quantity implements SecurityCheckerInterface
  16. {
  17. /**
  18. * @var \Magento\Security\Model\ResourceModel\PasswordResetRequestEvent\CollectionFactory
  19. */
  20. protected $collectionFactory;
  21. /**
  22. * @var ConfigInterface
  23. */
  24. protected $securityConfig;
  25. /**
  26. * @var RemoteAddress
  27. */
  28. private $remoteAddress;
  29. /**
  30. * @param ConfigInterface $securityConfig
  31. * @param CollectionFactory $collectionFactory
  32. * @param RemoteAddress $remoteAddress
  33. */
  34. public function __construct(
  35. ConfigInterface $securityConfig,
  36. CollectionFactory $collectionFactory,
  37. RemoteAddress $remoteAddress
  38. ) {
  39. $this->securityConfig = $securityConfig;
  40. $this->collectionFactory = $collectionFactory;
  41. $this->remoteAddress = $remoteAddress;
  42. }
  43. /**
  44. * @inheritdoc
  45. */
  46. public function check($securityEventType, $accountReference = null, $longIp = null)
  47. {
  48. $isEnabled = $this->securityConfig->getPasswordResetProtectionType() != ResetMethod::OPTION_NONE;
  49. $allowedAttemptsNumber = $this->securityConfig->getMaxNumberPasswordResetRequests();
  50. if ($isEnabled && $allowedAttemptsNumber) {
  51. $collection = $this->prepareCollection($securityEventType, $accountReference, $longIp);
  52. if ($collection->count() >= $allowedAttemptsNumber) {
  53. throw new SecurityViolationException(
  54. __(
  55. 'We received too many requests for password resets. '
  56. . 'Please wait and try again later or contact %1.',
  57. $this->securityConfig->getCustomerServiceEmail()
  58. )
  59. );
  60. }
  61. }
  62. }
  63. /**
  64. * Prepare collection
  65. *
  66. * @param int $securityEventType
  67. * @param string $accountReference
  68. * @param int $longIp
  69. * @return \Magento\Security\Model\ResourceModel\PasswordResetRequestEvent\Collection
  70. */
  71. protected function prepareCollection($securityEventType, $accountReference, $longIp)
  72. {
  73. if (null === $longIp) {
  74. $longIp = $this->remoteAddress->getRemoteAddress();
  75. }
  76. $collection = $this->collectionFactory->create($securityEventType, $accountReference, $longIp);
  77. $periodToCheck = $this->securityConfig->getLimitationTimePeriod();
  78. $collection->filterByLifetime($periodToCheck);
  79. return $collection;
  80. }
  81. }