mirror of
https://github.com/flarum/core.git
synced 2025-10-13 07:54:25 +02:00
Move flood control from core to API layer
This means that flood control can be disabled depending on the nature of the request (i.e. when authenticated using a master API key). The particular use case for this is to allow using the API to migrate data from an old forum.
This commit is contained in:
@@ -95,7 +95,6 @@ class CoreServiceProvider extends AbstractServiceProvider
|
||||
$events->subscribe('Flarum\Core\Listener\UserMetadataUpdater');
|
||||
$events->subscribe('Flarum\Core\Listener\EmailConfirmationMailer');
|
||||
$events->subscribe('Flarum\Core\Listener\DiscussionRenamedNotifier');
|
||||
$events->subscribe('Flarum\Core\Listener\FloodController');
|
||||
|
||||
$events->subscribe('Flarum\Core\Access\DiscussionPolicy');
|
||||
$events->subscribe('Flarum\Core\Access\GroupPolicy');
|
||||
|
@@ -1,75 +0,0 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Core\Listener;
|
||||
|
||||
use DateTime;
|
||||
use Flarum\Core\Exception\FloodingException;
|
||||
use Flarum\Core\Post;
|
||||
use Flarum\Core\User;
|
||||
use Flarum\Event\DiscussionWillBeSaved;
|
||||
use Flarum\Event\PostWillBeSaved;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
||||
class FloodController
|
||||
{
|
||||
/**
|
||||
* @param Dispatcher $events
|
||||
*/
|
||||
public function subscribe(Dispatcher $events)
|
||||
{
|
||||
$events->listen(DiscussionWillBeSaved::class, [$this, 'whenDiscussionWillBeSaved']);
|
||||
$events->listen(PostWillBeSaved::class, [$this, 'whenPostWillBeSaved']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param DiscussionWillBeSaved $event
|
||||
*/
|
||||
public function whenDiscussionWillBeSaved(DiscussionWillBeSaved $event)
|
||||
{
|
||||
if ($event->discussion->exists) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->assertNotFlooding($event->actor);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PostWillBeSaved $event
|
||||
*/
|
||||
public function whenPostWillBeSaved(PostWillBeSaved $event)
|
||||
{
|
||||
if ($event->post->exists) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->assertNotFlooding($event->actor);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $actor
|
||||
* @throws FloodingException
|
||||
*/
|
||||
protected function assertNotFlooding(User $actor)
|
||||
{
|
||||
if ($this->isFlooding($actor)) {
|
||||
throw new FloodingException;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $actor
|
||||
* @return bool
|
||||
*/
|
||||
protected function isFlooding(User $actor)
|
||||
{
|
||||
return Post::where('user_id', $actor->id)->where('time', '>=', new DateTime('-10 seconds'))->exists();
|
||||
}
|
||||
}
|
39
src/Core/Post/Floodgate.php
Normal file
39
src/Core/Post/Floodgate.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Core\Post;
|
||||
|
||||
use DateTime;
|
||||
use Flarum\Core\Exception\FloodingException;
|
||||
use Flarum\Core\Post;
|
||||
use Flarum\Core\User;
|
||||
|
||||
class Floodgate
|
||||
{
|
||||
/**
|
||||
* @param User $actor
|
||||
* @throws FloodingException
|
||||
*/
|
||||
public function assertNotFlooding(User $actor)
|
||||
{
|
||||
if ($this->isFlooding($actor)) {
|
||||
throw new FloodingException;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $actor
|
||||
* @return bool
|
||||
*/
|
||||
public function isFlooding(User $actor)
|
||||
{
|
||||
return Post::where('user_id', $actor->id)->where('time', '>=', new DateTime('-10 seconds'))->exists();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user