mirror of
https://github.com/flarum/framework.git
synced 2025-03-22 04:55:16 +08:00
Add suspended gambit (#23)
* Add is:suspended gambit * Wrap where clauses in function * Added permission check for suspended gambit * Apply fixes from StyleCI Co-authored-by: luceos <daniel+github@klabbers.email>
This commit is contained in:
parent
c925f92f79
commit
061a8585c2
@ -9,6 +9,7 @@
|
||||
|
||||
use Flarum\Api\Serializer\BasicUserSerializer;
|
||||
use Flarum\Event\ConfigureNotificationTypes;
|
||||
use Flarum\Event\ConfigureUserGambits;
|
||||
use Flarum\Extend;
|
||||
use Flarum\Suspend\Access;
|
||||
use Flarum\Suspend\Event\Suspended;
|
||||
@ -16,6 +17,7 @@ use Flarum\Suspend\Event\Unsuspended;
|
||||
use Flarum\Suspend\Listener;
|
||||
use Flarum\Suspend\Notification\UserSuspendedBlueprint;
|
||||
use Flarum\Suspend\Notification\UserUnsuspendedBlueprint;
|
||||
use Flarum\Suspend\Search\Gambit\SuspendedGambit;
|
||||
use Flarum\User\Event\Saving;
|
||||
use Flarum\User\User;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
@ -48,5 +50,9 @@ return [
|
||||
$events->listen(Unsuspended::class, Listener\SendNotificationWhenUserIsUnsuspended::class);
|
||||
|
||||
$events->subscribe(Access\UserPolicy::class);
|
||||
|
||||
$events->listen(ConfigureUserGambits::class, function (ConfigureUserGambits $event) {
|
||||
$event->gambits->add(SuspendedGambit::class);
|
||||
});
|
||||
}
|
||||
];
|
||||
|
68
extensions/suspend/src/Search/Gambit/SuspendedGambit.php
Normal file
68
extensions/suspend/src/Search/Gambit/SuspendedGambit.php
Normal file
@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* For detailed copyright and license information, please view the
|
||||
* LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Suspend\Search\Gambit;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Flarum\Search\AbstractRegexGambit;
|
||||
use Flarum\Search\AbstractSearch;
|
||||
use Flarum\User\Search\UserSearch;
|
||||
use Flarum\User\UserRepository;
|
||||
use LogicException;
|
||||
|
||||
class SuspendedGambit extends AbstractRegexGambit
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $pattern = 'is:suspended';
|
||||
|
||||
/**
|
||||
* @var \Flarum\User\UserRepository
|
||||
*/
|
||||
protected $users;
|
||||
|
||||
/**
|
||||
* @param \Flarum\User\UserRepository $users
|
||||
*/
|
||||
public function __construct(UserRepository $users)
|
||||
{
|
||||
$this->users = $users;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function apply(AbstractSearch $search, $bit)
|
||||
{
|
||||
if (! $search->getActor()->can('suspend')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return parent::apply($search, $bit);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function conditions(AbstractSearch $search, array $matches, $negate)
|
||||
{
|
||||
if (! $search instanceof UserSearch) {
|
||||
throw new LogicException('This gambit can only be applied on a DiscussionSearch');
|
||||
}
|
||||
|
||||
$search->getQuery()->where(function ($query) use ($negate) {
|
||||
if ($negate) {
|
||||
$query->where('suspended_until', null)->orWhere('suspended_until', '<', Carbon::now());
|
||||
} else {
|
||||
$query->where('suspended_until', '>', Carbon::now());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user