The following is a guide on creating a local extender to assign a permission group to users that have a comment count of 10 or more. This code has been untested, feel free to report back with any issues. Make sure to enable debug when trying this code out.
Replace slug of your permission group that can post
below with the slug of the permission group that you wish to assign.
In your composer.json, add on next to the level of require
:
"autoload": {
"psr-4": {
"App\\": "app/"
}
}
Make sure to add a comma ,
to the line above "autoload" for it to be valid json!
Run composer dumpautoload
for the new app
path to be accepted by composer.
Create a directory app
and app/Extend
in the root Flarum directory (next to the composer.json), create a file in app/Extend
called GrantAccess.php
that contains this code:
<?php
namespace App\Extend;
use Flarum\Extend\ExtenderInterface;
use Flarum\Extension\Extension;
use Flarum\Group\Group;
use Flarum\Post\Event\Posted;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Collection;
class GrantAccess implements ExtenderInterface
{
protected $grants = [];
public function byPostCount(int $postCount, string $groupSlug)
{
$this->grants[] = [
'type' => 'postCount',
'count' => $postCount,
'group' => $groupSlug
];
return $this;
}
public function extend(Container $container, Extension $extension = null)
{
/** @var Dispatcher $events */
$events = $container->make(Dispatcher::class);
$events->listen(Posted::class, function (Posted $event) {
$actor = $event->actor;
$this->byType('postCount')->each(function ($grant) use ($actor) {
$group = Group::where('slug', $grant['group'])->firstOrFail();
$hasGroup = $actor->groups->contains($group);
$hasCount = $actor->comment_count >= $grant['count'];
if (! $hasGroup && $hasCount) {
$actor->groups()->attach($group);
}
});
});
}
protected function byType(string $type): Collection
{
return collect($this->grants)->where('type', $type);
}
}
In your extend.php
next to the composer.json`, change to add this new local extender:
<?php
use App\Extend\GrantAccess;
return [
(new GrantAccess)->byPostCount(10, 'slug of your permission group that can post')
];