]> BookStack Code Mirror - bookstack/blob - app/Auth/Queries/UsersAllPaginatedAndSorted.php
Refactored common list handling operations to new class
[bookstack] / app / Auth / Queries / UsersAllPaginatedAndSorted.php
1 <?php
2
3 namespace BookStack\Auth\Queries;
4
5 use BookStack\Auth\User;
6 use BookStack\Util\SimpleListOptions;
7 use Illuminate\Pagination\LengthAwarePaginator;
8
9 /**
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.
14  */
15 class UsersAllPaginatedAndSorted
16 {
17     public function run(int $count, SimpleListOptions $listOptions): LengthAwarePaginator
18     {
19         $sort = $listOptions->getSort();
20         if ($sort === 'created_at') {
21             $sort = 'users.created_at';
22         }
23
24         $query = User::query()->select(['*'])
25             ->scopes(['withLastActivityAt'])
26             ->with(['roles', 'avatar'])
27             ->withCount('mfaValues')
28             ->orderBy($sort, $listOptions->getOrder());
29
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);
35             });
36         }
37
38         return $query->paginate($count);
39     }
40 }