3 namespace BookStack\Auth\Queries;
5 use BookStack\Auth\User;
6 use BookStack\Util\SimpleListOptions;
7 use Illuminate\Pagination\LengthAwarePaginator;
10 * Get all the users with their permissions in a paginated format.
11 * Note: Due to the use of email search this should only be used when
12 * user is assumed to be trusted. (Admin users).
13 * Email search can be abused to extract email addresses.
15 class UsersAllPaginatedAndSorted
17 public function run(int $count, SimpleListOptions $listOptions): LengthAwarePaginator
19 $sort = $listOptions->getSort();
20 if ($sort === 'created_at') {
21 $sort = 'users.created_at';
24 $query = User::query()->select(['*'])
25 ->scopes(['withLastActivityAt'])
26 ->with(['roles', 'avatar'])
27 ->withCount('mfaValues')
28 ->orderBy($sort, $listOptions->getOrder());
30 if ($listOptions->getSearch()) {
31 $term = '%' . $listOptions->getSearch() . '%';
32 $query->where(function ($query) use ($term) {
33 $query->where('name', 'like', $term)
34 ->orWhere('email', 'like', $term);
38 return $query->paginate($count);