]> BookStack Code Mirror - bookstack/blob - app/Auth/Queries/AllUsersPaginatedAndSorted.php
Applied StyleCI changes, updated readme badges & roadmap
[bookstack] / app / Auth / Queries / AllUsersPaginatedAndSorted.php
1 <?php
2
3 namespace BookStack\Auth\Queries;
4
5 use BookStack\Auth\User;
6 use Illuminate\Pagination\LengthAwarePaginator;
7
8 /**
9  * Get all the users with their permissions in a paginated format.
10  * Note: Due to the use of email search this should only be used when
11  * user is assumed to be trusted. (Admin users).
12  * Email search can be abused to extract email addresses.
13  */
14 class AllUsersPaginatedAndSorted
15 {
16     /**
17      * @param array{sort: string, order: string, search: string} $sortData
18      */
19     public function run(int $count, array $sortData): LengthAwarePaginator
20     {
21         $sort = $sortData['sort'];
22
23         $query = User::query()->select(['*'])
24             ->scopes(['withLastActivityAt'])
25             ->with(['roles', 'avatar'])
26             ->withCount('mfaValues')
27             ->orderBy($sort, $sortData['order']);
28
29         if ($sortData['search']) {
30             $term = '%' . $sortData['search'] . '%';
31             $query->where(function ($query) use ($term) {
32                 $query->where('name', 'like', $term)
33                     ->orWhere('email', 'like', $term);
34             });
35         }
36
37         return $query->paginate($count);
38     }
39 }