|
13 | 13 | use Analog\Analog; |
14 | 14 | use DI\Attribute\Inject; |
15 | 15 | use Galette\Controllers\AbstractPluginController; |
| 16 | +use Galette\Core\AuthThrottle; |
16 | 17 | use Galette\Core\History; |
| 18 | +use Galette\Core\Login; |
17 | 19 | use Galette\Entity\Adherent; |
18 | 20 | use Galette\Entity\Contribution; |
19 | 21 | use Galette\Entity\ContributionsTypes; |
@@ -45,14 +47,22 @@ class HelloassoController extends AbstractPluginController |
45 | 47 | #[Inject] |
46 | 48 | protected Helloasso $helloasso; |
47 | 49 |
|
| 50 | + private const string THROTTLE_SCOPE = 'helloasso-payment'; |
| 51 | + |
48 | 52 | /** |
49 | 53 | * Main form |
50 | 54 | */ |
51 | | - public function form(Response $response): Response |
| 55 | + public function form(Response $response, AuthThrottle $throttle, Login $login): Response |
52 | 56 | { |
53 | 57 | $helloasso = $this->helloasso; |
54 | 58 |
|
55 | 59 | $current_url = $this->preferences->getURL(); |
| 60 | + $address = $helloasso->getUserIPAddress(); |
| 61 | + |
| 62 | + // When a member is logged there is nothing left to hold against its address |
| 63 | + if ($login->isLogged() && $address !== '') { |
| 64 | + $throttle->clearEvent(self::THROTTLE_SCOPE, $address); |
| 65 | + } |
56 | 66 |
|
57 | 67 | $params = [ |
58 | 68 | 'helloasso' => $helloasso, |
@@ -90,12 +100,38 @@ public function form(Response $response): Response |
90 | 100 | /** |
91 | 101 | * Checkout form |
92 | 102 | */ |
93 | | - public function formCheckout(Request $request, Response $response): Response |
| 103 | + public function formCheckout(Request $request, Response $response, AuthThrottle $throttle, Login $login): Response |
94 | 104 | { |
95 | 105 | $helloasso_request = $request->getParsedBody(); |
96 | 106 | $helloasso = $this->helloasso; |
97 | 107 | $adherent = new Adherent($this->zdb); |
98 | 108 |
|
| 109 | + $address = $helloasso->getUserIPAddress(); |
| 110 | + |
| 111 | + // Throttle public form submissions |
| 112 | + if (!$login->isLogged() && $address !== '') { |
| 113 | + // Asked before anything is done, so that a caller being refused costs |
| 114 | + // nothing but a lookup |
| 115 | + $delay = $throttle->getDelayForEvent(self::THROTTLE_SCOPE, $address); |
| 116 | + if ($delay > 0) { |
| 117 | + $this->flash->addMessage( |
| 118 | + 'error_detected', |
| 119 | + str_replace( |
| 120 | + '%seconds', |
| 121 | + (string)$delay, |
| 122 | + _T("Too many requests. Please try again in %seconds seconds.", "helloasso") |
| 123 | + ) |
| 124 | + ); |
| 125 | + return $response |
| 126 | + ->withStatus(301) |
| 127 | + ->withHeader('Location', $this->routeparser->urlFor('helloasso_form')); |
| 128 | + } |
| 129 | + |
| 130 | + // The attempt is counted, not its outcome: what is limited is how many |
| 131 | + // times the form can be submitted, whatever it answers |
| 132 | + $throttle->recordEvent(self::THROTTLE_SCOPE, $address, 120, 3600); |
| 133 | + } |
| 134 | + |
99 | 135 | // Only reasons proposed to the current user can be paid |
100 | 136 | $item_id = (int)($helloasso_request['item_id'] ?? 0); |
101 | 137 | $helloasso_amounts = $helloasso->getAmounts($this->login); |
|
0 commit comments