mirror of
https://github.com/flarum/framework.git
synced 2025-02-01 14:53:03 +08:00
Add admin-only email: gambit to look up users by email
This commit is contained in:
parent
f7709aff95
commit
c8027d344a
|
@ -40,7 +40,9 @@ class SearchServiceProvider extends AbstractServiceProvider
|
||||||
->needs('Flarum\Core\Search\GambitManager')
|
->needs('Flarum\Core\Search\GambitManager')
|
||||||
->give(function (Container $app) {
|
->give(function (Container $app) {
|
||||||
$gambits = new GambitManager($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(
|
$app->make('events')->fire(
|
||||||
new ConfigureUserGambits($gambits)
|
new ConfigureUserGambits($gambits)
|
||||||
|
|
66
src/Core/Search/User/Gambit/EmailGambit.php
Normal file
66
src/Core/Search/User/Gambit/EmailGambit.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,7 +8,7 @@
|
||||||
* file that was distributed with this source code.
|
* 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\Repository\UserRepository;
|
||||||
use Flarum\Core\Search\AbstractSearch;
|
use Flarum\Core\Search\AbstractSearch;
|
Loading…
Reference in New Issue
Block a user