mirror of
https://github.com/flarum/framework.git
synced 2025-02-11 20:26:16 +08:00
Group Gambit Improvements (#2192)
* - Add ID to fields searched in group gambit - Use joins instead of looping in group gambit * Add visibility scoping to group gambit * call IDs userIds * If group identifier is numerical, treat it as an ID
This commit is contained in:
parent
07b9866cfb
commit
b3c2672634
|
@ -41,19 +41,6 @@ class GroupRepository
|
||||||
return $this->scopeVisibleTo($query, $actor)->firstOrFail();
|
return $this->scopeVisibleTo($query, $actor)->firstOrFail();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Find a group by name.
|
|
||||||
*
|
|
||||||
* @param string $name
|
|
||||||
* @return User|null
|
|
||||||
*/
|
|
||||||
public function findByName($name, User $actor = null)
|
|
||||||
{
|
|
||||||
$query = Group::where('name_singular', $name)->orWhere('name_plural', $name);
|
|
||||||
|
|
||||||
return $this->scopeVisibleTo($query, $actor)->first();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Scope a query to only include records that are visible to a user.
|
* Scope a query to only include records that are visible to a user.
|
||||||
*
|
*
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
|
|
||||||
namespace Flarum\User\Search\Gambit;
|
namespace Flarum\User\Search\Gambit;
|
||||||
|
|
||||||
|
use Flarum\Group\Group;
|
||||||
use Flarum\Group\GroupRepository;
|
use Flarum\Group\GroupRepository;
|
||||||
use Flarum\Search\AbstractRegexGambit;
|
use Flarum\Search\AbstractRegexGambit;
|
||||||
use Flarum\Search\AbstractSearch;
|
use Flarum\Search\AbstractSearch;
|
||||||
|
@ -44,18 +45,23 @@ class GroupGambit extends AbstractRegexGambit
|
||||||
throw new LogicException('This gambit can only be applied on a UserSearch');
|
throw new LogicException('This gambit can only be applied on a UserSearch');
|
||||||
}
|
}
|
||||||
|
|
||||||
$groupNames = $this->extractGroupNames($matches);
|
$groupIdentifiers = $this->extractGroupIdentifiers($matches);
|
||||||
|
|
||||||
// TODO: Use a JOIN instead (and don't forget to remove the findByName() method again)
|
$groupQuery = Group::whereVisibleTo($search->getActor());
|
||||||
$ids = [];
|
|
||||||
foreach ($groupNames as $name) {
|
foreach ($groupIdentifiers as $identifier) {
|
||||||
$group = $this->groups->findByName($name);
|
if (is_numeric($identifier)) {
|
||||||
if ($group && count($group->users)) {
|
$groupQuery->orWhere('id', $identifier);
|
||||||
$ids = array_merge($ids, $group->users->pluck('id')->all());
|
} else {
|
||||||
|
$groupQuery->orWhere('name_singular', $identifier)->orWhere('name_plural', $identifier);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$search->getQuery()->whereIn('id', $ids, 'and', $negate);
|
$userIds = $groupQuery->join('group_user', 'groups.id', 'group_user.group_id')
|
||||||
|
->pluck('group_user.user_id')
|
||||||
|
->all();
|
||||||
|
|
||||||
|
$search->getQuery()->whereIn('id', $userIds, 'and', $negate);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -64,7 +70,7 @@ class GroupGambit extends AbstractRegexGambit
|
||||||
* @param array $matches
|
* @param array $matches
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function extractGroupNames(array $matches)
|
protected function extractGroupIdentifiers(array $matches)
|
||||||
{
|
{
|
||||||
return explode(',', trim($matches[1], '"'));
|
return explode(',', trim($matches[1], '"'));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user