mirror of
https://github.com/flarum/core.git
synced 2025-10-27 05:31:29 +01:00
Search Filter Split, Use Same Controller (#2454)
This commit is contained in:
committed by
GitHub
parent
1c578a83e4
commit
023871ef86
@@ -10,7 +10,7 @@
|
||||
namespace Flarum\User\Event;
|
||||
|
||||
use Flarum\Search\SearchCriteria;
|
||||
use Flarum\User\Search\UserSearch;
|
||||
use Flarum\Search\SearchState;
|
||||
|
||||
/**
|
||||
* @deprecated beta 16, remove beta 17
|
||||
@@ -18,7 +18,7 @@ use Flarum\User\Search\UserSearch;
|
||||
class Searching
|
||||
{
|
||||
/**
|
||||
* @var \Flarum\User\Search\UserSearch
|
||||
* @var \Flarum\User\Search\SearchState
|
||||
*/
|
||||
public $search;
|
||||
|
||||
@@ -28,10 +28,10 @@ class Searching
|
||||
public $criteria;
|
||||
|
||||
/**
|
||||
* @param UserSearch $search
|
||||
* @param SearchState $search
|
||||
* @param SearchCriteria $criteria
|
||||
*/
|
||||
public function __construct(UserSearch $search, SearchCriteria $criteria)
|
||||
public function __construct(SearchState $search, SearchCriteria $criteria)
|
||||
{
|
||||
$this->search = $search;
|
||||
$this->criteria = $criteria;
|
||||
|
||||
68
src/User/Filter/EmailFilterGambit.php
Normal file
68
src/User/Filter/EmailFilterGambit.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\User\Filter;
|
||||
|
||||
use Flarum\Filter\FilterInterface;
|
||||
use Flarum\Filter\FilterState;
|
||||
use Flarum\Search\AbstractRegexGambit;
|
||||
use Flarum\Search\SearchState;
|
||||
use Illuminate\Database\Query\Builder;
|
||||
|
||||
class EmailFilterGambit extends AbstractRegexGambit implements FilterInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function apply(SearchState $search, $bit)
|
||||
{
|
||||
if (! $search->getActor()->hasPermission('user.edit')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return parent::apply($search, $bit);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getGambitPattern()
|
||||
{
|
||||
return 'email:(.+)';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function conditions(SearchState $search, array $matches, $negate)
|
||||
{
|
||||
$this->constrain($search->getQuery(), $matches[1], $negate);
|
||||
}
|
||||
|
||||
public function getFilterKey(): string
|
||||
{
|
||||
return 'email';
|
||||
}
|
||||
|
||||
public function filter(FilterState $filterState, string $filterValue, bool $negate)
|
||||
{
|
||||
if (! $filterState->getActor()->hasPermission('user.edit')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->constrain($filterState->getQuery(), $filterValue, $negate);
|
||||
}
|
||||
|
||||
protected function constrain(Builder $query, $rawEmail, bool $negate)
|
||||
{
|
||||
$email = trim($rawEmail, '"');
|
||||
|
||||
$query->where('email', $negate ? '!=' : '=', $email);
|
||||
}
|
||||
}
|
||||
68
src/User/Filter/GroupFilterGambit.php
Normal file
68
src/User/Filter/GroupFilterGambit.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\User\Filter;
|
||||
|
||||
use Flarum\Filter\FilterInterface;
|
||||
use Flarum\Filter\FilterState;
|
||||
use Flarum\Group\Group;
|
||||
use Flarum\Search\AbstractRegexGambit;
|
||||
use Flarum\Search\SearchState;
|
||||
use Flarum\User\User;
|
||||
use Illuminate\Database\Query\Builder;
|
||||
|
||||
class GroupFilterGambit extends AbstractRegexGambit implements FilterInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getGambitPattern()
|
||||
{
|
||||
return 'group:(.+)';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function conditions(SearchState $search, array $matches, $negate)
|
||||
{
|
||||
$this->constrain($search->getQuery(), $search->getActor(), $matches[1], $negate);
|
||||
}
|
||||
|
||||
public function getFilterKey(): string
|
||||
{
|
||||
return 'group';
|
||||
}
|
||||
|
||||
public function filter(FilterState $filterState, string $filterValue, bool $negate)
|
||||
{
|
||||
$this->constrain($filterState->getQuery(), $filterState->getActor(), $filterValue, $negate);
|
||||
}
|
||||
|
||||
protected function constrain(Builder $query, User $actor, string $rawQuery, bool $negate)
|
||||
{
|
||||
$groupIdentifiers = explode(',', trim($rawQuery, '"'));
|
||||
|
||||
$groupQuery = Group::whereVisibleTo($actor);
|
||||
|
||||
foreach ($groupIdentifiers as $identifier) {
|
||||
if (is_numeric($identifier)) {
|
||||
$groupQuery->orWhere('id', $identifier);
|
||||
} else {
|
||||
$groupQuery->orWhere('name_singular', $identifier)->orWhere('name_plural', $identifier);
|
||||
}
|
||||
}
|
||||
|
||||
$userIds = $groupQuery->join('group_user', 'groups.id', 'group_user.group_id')
|
||||
->pluck('group_user.user_id')
|
||||
->all();
|
||||
|
||||
$query->whereIn('id', $userIds, 'and', $negate);
|
||||
}
|
||||
}
|
||||
40
src/User/Filter/UserFilterer.php
Normal file
40
src/User/Filter/UserFilterer.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?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\User\Filter;
|
||||
|
||||
use Flarum\Filter\AbstractFilterer;
|
||||
use Flarum\User\User;
|
||||
use Flarum\User\UserRepository;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class UserFilterer extends AbstractFilterer
|
||||
{
|
||||
/**
|
||||
* @var UserRepository
|
||||
*/
|
||||
protected $users;
|
||||
|
||||
/**
|
||||
* @param UserRepository $users
|
||||
* @param array $filters
|
||||
* @param array $filterMutators
|
||||
*/
|
||||
public function __construct(UserRepository $users, array $filters, array $filterMutators)
|
||||
{
|
||||
parent::__construct($filters, $filterMutators);
|
||||
|
||||
$this->users = $users;
|
||||
}
|
||||
|
||||
protected function getQuery(User $actor): Builder
|
||||
{
|
||||
return $this->users->query()->whereVisibleTo($actor);
|
||||
}
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
<?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\User\Search\Gambit;
|
||||
|
||||
use Flarum\Search\AbstractRegexGambit;
|
||||
use Flarum\Search\AbstractSearch;
|
||||
use Flarum\User\Search\UserSearch;
|
||||
use Flarum\User\UserRepository;
|
||||
use LogicException;
|
||||
|
||||
class EmailGambit extends AbstractRegexGambit
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $pattern = 'email:(.+)';
|
||||
|
||||
/**
|
||||
* @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()->hasPermission('user.edit')) {
|
||||
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], '"');
|
||||
|
||||
$search->getQuery()->where('email', $negate ? '!=' : '=', $email);
|
||||
}
|
||||
}
|
||||
@@ -9,8 +9,8 @@
|
||||
|
||||
namespace Flarum\User\Search\Gambit;
|
||||
|
||||
use Flarum\Search\AbstractSearch;
|
||||
use Flarum\Search\GambitInterface;
|
||||
use Flarum\Search\SearchState;
|
||||
use Flarum\User\UserRepository;
|
||||
|
||||
class FulltextGambit implements GambitInterface
|
||||
@@ -43,7 +43,7 @@ class FulltextGambit implements GambitInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function apply(AbstractSearch $search, $searchValue)
|
||||
public function apply(SearchState $search, $searchValue)
|
||||
{
|
||||
$search->getQuery()
|
||||
->whereIn(
|
||||
|
||||
@@ -1,77 +0,0 @@
|
||||
<?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\User\Search\Gambit;
|
||||
|
||||
use Flarum\Group\Group;
|
||||
use Flarum\Group\GroupRepository;
|
||||
use Flarum\Search\AbstractRegexGambit;
|
||||
use Flarum\Search\AbstractSearch;
|
||||
use Flarum\User\Search\UserSearch;
|
||||
use LogicException;
|
||||
|
||||
class GroupGambit extends AbstractRegexGambit
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $pattern = 'group:(.+)';
|
||||
|
||||
/**
|
||||
* @var GroupRepository
|
||||
*/
|
||||
protected $groups;
|
||||
|
||||
/**
|
||||
* @param \Flarum\Group\GroupRepository $groups
|
||||
*/
|
||||
public function __construct(GroupRepository $groups)
|
||||
{
|
||||
$this->groups = $groups;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@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');
|
||||
}
|
||||
|
||||
$groupIdentifiers = $this->extractGroupIdentifiers($matches);
|
||||
|
||||
$groupQuery = Group::whereVisibleTo($search->getActor());
|
||||
|
||||
foreach ($groupIdentifiers as $identifier) {
|
||||
if (is_numeric($identifier)) {
|
||||
$groupQuery->orWhere('id', $identifier);
|
||||
} else {
|
||||
$groupQuery->orWhere('name_singular', $identifier)->orWhere('name_plural', $identifier);
|
||||
}
|
||||
}
|
||||
|
||||
$userIds = $groupQuery->join('group_user', 'groups.id', 'group_user.group_id')
|
||||
->pluck('group_user.user_id')
|
||||
->all();
|
||||
|
||||
$search->getQuery()->whereIn('id', $userIds, 'and', $negate);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract the group names from the pattern match.
|
||||
*
|
||||
* @param array $matches
|
||||
* @return array
|
||||
*/
|
||||
protected function extractGroupIdentifiers(array $matches)
|
||||
{
|
||||
return explode(',', trim($matches[1], '"'));
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
<?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\User\Search;
|
||||
|
||||
use Flarum\Search\AbstractSearch;
|
||||
|
||||
class UserSearch extends AbstractSearch
|
||||
{
|
||||
}
|
||||
@@ -9,10 +9,10 @@
|
||||
|
||||
namespace Flarum\User\Search;
|
||||
|
||||
use Flarum\Search\AbstractSearch;
|
||||
use Flarum\Search\AbstractSearcher;
|
||||
use Flarum\Search\GambitManager;
|
||||
use Flarum\Search\SearchCriteria;
|
||||
use Flarum\Search\SearchState;
|
||||
use Flarum\User\Event\Searching;
|
||||
use Flarum\User\User;
|
||||
use Flarum\User\UserRepository;
|
||||
@@ -54,15 +54,10 @@ class UserSearcher extends AbstractSearcher
|
||||
return $this->users->query()->whereVisibleTo($actor);
|
||||
}
|
||||
|
||||
protected function getSearch(Builder $query, User $actor): AbstractSearch
|
||||
{
|
||||
return new UserSearch($query->getQuery(), $actor);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated along with the Searching event, remove in Beta 17.
|
||||
*/
|
||||
protected function mutateSearch(AbstractSearch $search, SearchCriteria $criteria)
|
||||
protected function mutateSearch(SearchState $search, SearchCriteria $criteria)
|
||||
{
|
||||
parent::mutateSearch($search, $criteria);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user