Skip to content

Commit a572caf

Browse files
add Symfony EventSubscriber example for request blocking (#381)
1 parent 2b46f3f commit a572caf

File tree

1 file changed

+91
-1
lines changed

1 file changed

+91
-1
lines changed

docs/should_block_request.md

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,94 @@ return Application::configure(basePath: dirname(__DIR__))
183183
})
184184

185185
// ...
186-
```
186+
```
187+
188+
## Symfony
189+
190+
1. Create an EventSubscriber in `src/EventSubscriber/AikidoEventSubscriber.php`:
191+
192+
```php
193+
<?php
194+
195+
namespace App\EventSubscriber;
196+
197+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
198+
use Symfony\Component\HttpKernel\Event\RequestEvent;
199+
use Symfony\Component\HttpKernel\KernelEvents;
200+
use Symfony\Component\HttpFoundation\JsonResponse;
201+
use Symfony\Bundle\SecurityBundle\Security;
202+
203+
class AikidoEventSubscriber implements EventSubscriberInterface
204+
{
205+
public function __construct(
206+
private readonly Security $security
207+
) {
208+
}
209+
210+
public static function getSubscribedEvents(): array
211+
{
212+
return [
213+
KernelEvents::REQUEST => ['onKernelRequest', 10],
214+
];
215+
}
216+
217+
public function onKernelRequest(RequestEvent $event): void
218+
{
219+
// Only handle the main request
220+
if (!$event->isMainRequest()) {
221+
return;
222+
}
223+
224+
// Check if Aikido extension is loaded
225+
if (!extension_loaded('aikido')) {
226+
return;
227+
}
228+
229+
// You can pass in the Aikido token here
230+
// \aikido\set_token("your token here");
231+
232+
// Get the authenticated user from Symfony's Security component
233+
$user = $this->security->getUser();
234+
235+
// If a user is authenticated, set the user in Aikido Zen context
236+
if ($user) {
237+
$userId = $user->getUserIdentifier();
238+
\aikido\set_user($userId);
239+
// If you want to set the user's name in Aikido Zen context, you can change the above to:
240+
// \aikido\set_user($userId, $user->getUsername());
241+
}
242+
243+
// Check blocking decision from Aikido
244+
$decision = \aikido\should_block_request();
245+
246+
if ($decision->block) {
247+
if ($decision->type == "blocked") {
248+
if ($decision->trigger == "user") {
249+
$event->setResponse(new JsonResponse(
250+
['message' => 'Your user is blocked!'],
251+
403
252+
));
253+
return;
254+
}
255+
}
256+
else if ($decision->type == "ratelimited") {
257+
$message = '';
258+
if ($decision->trigger == "user") {
259+
$message = 'Your user exceeded the rate limit for this endpoint!';
260+
}
261+
else if ($decision->trigger == "ip") {
262+
$message = "Your IP ({$decision->ip}) exceeded the rate limit for this endpoint!";
263+
}
264+
else if ($decision->trigger == "group") {
265+
$message = "Your group exceeded the rate limit for this endpoint!";
266+
}
267+
268+
$event->setResponse(new JsonResponse(
269+
['message' => $message],
270+
429
271+
));
272+
return;
273+
}
274+
}
275+
}
276+
}

0 commit comments

Comments
 (0)