I am running the latest Flarum forum, and I would like to restrict the quantity of how many Developer/Access token users can make.
I tried to open the extend.php which I have already used to set minWordLimit, for posts and it's working fabulously.
I wrote this code into the extend.php, but somehow it does not do what I expected.
use Flarum\Extend;
use Flarum\Post\Event\Saving;
use Flarum\Post\Exception\InvalidPostException;
use Illuminate\Events\Dispatcher;
// use Illuminate\Contracts\Events\Dispatcher;
use Flarum\User\User;
use Flarum\Post\Event\Posted;
use Flarum\Event\TokenCreated;
return [
-- Existing code which is working is here - I will skip that --
(new Extend\Event())
->listen(Saving::class, function (Saving $event) {
$user = $event->post->user;
$existingTokensCount = count($user->getDeveloperAccessTokens());
if ($existingTokensCount >= 1) {
$event->post->addError('token', 'You can only have one developer access token.');
throw new InvalidPostException;
}
})
];
Can someone assist me to succeed on my problem?