Add admin-only email: gambit to look up users by email

This commit is contained in:
Toby Zerner 2016-01-02 15:09:56 +10:30
parent 7d55b81d8e
commit cc08e135e6
3 changed files with 70 additions and 2 deletions

View File

@ -40,7 +40,9 @@ class SearchServiceProvider extends AbstractServiceProvider
->needs('Flarum\Core\Search\GambitManager')
->give(function (Container $app) {
$gambits = new GambitManager($app);
$gambits->setFulltextGambit('Flarum\Core\Search\User\Gambits\FulltextGambit');
$gambits->setFulltextGambit('Flarum\Core\Search\User\Gambit\FulltextGambit');
$gambits->add('Flarum\Core\Search\User\Gambit\EmailGambit');
$app->make('events')->fire(
new ConfigureUserGambits($gambits)

View File

@ -0,0 +1,66 @@
<?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\Search\User\Gambit;
use Flarum\Core\Search\User\UserSearch;
use Flarum\Core\Repository\UserRepository;
use Flarum\Core\Search\AbstractRegexGambit;
use Flarum\Core\Search\AbstractSearch;
use LogicException;
class EmailGambit extends AbstractRegexGambit
{
/**
* {@inheritdoc}
*/
protected $pattern = 'email:(.+)';
/**
* @var UserRepository
*/
protected $users;
/**
* @param \Flarum\Core\Repository\UserRepository $users
*/
public function __construct(UserRepository $users)
{
$this->users = $users;
}
/**
* {@inheritdoc}
*/
public function apply(AbstractSearch $search, $bit)
{
if (! $search->getActor()->isAdmin()) {
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 UserSearch');
}
$email = trim($matches[1], '"');
$user = $this->users->findByEmail($email);
$search->getQuery()->where('id', $negate ? '!=' : '=', $user->id);
}
}

View File

@ -8,7 +8,7 @@
* file that was distributed with this source code.
*/
namespace Flarum\Core\Search\User\Gambits;
namespace Flarum\Core\Search\User\Gambit;
use Flarum\Core\Repository\UserRepository;
use Flarum\Core\Search\AbstractSearch;