]> BookStack Code Mirror - bookstack/blob - app/Auth/Queries/AllUsersPaginatedAndSorted.php
664db1925a1a6a977d5656037544bd593388af60
[bookstack] / app / Auth / Queries / AllUsersPaginatedAndSorted.php
1 <?php
2
3 namespace BookStack\Auth\Queries;
4
5
6 use BookStack\Auth\User;
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 AllUsersPaginatedAndSorted
16 {
17
18     /**
19      * @param array{sort: string, order: string, search: string} $sortData
20      */
21     public function run(int $count, array $sortData): LengthAwarePaginator
22     {
23         $sort = $sortData['sort'];
24
25         $query = User::query()->select(['*'])
26             ->scopes(['withLastActivityAt'])
27             ->with(['roles', 'avatar'])
28             ->withCount('mfaValues')
29             ->orderBy($sort, $sortData['order']);
30
31         if ($sortData['search']) {
32             $term = '%' . $sortData['search'] . '%';
33             $query->where(function ($query) use ($term) {
34                 $query->where('name', 'like', $term)
35                     ->orWhere('email', 'like', $term);
36             });
37         }
38
39         return $query->paginate($count);
40     }
41
42 }