BookStack/app/Users/Queries/UsersAllPaginatedAndSorted.php

41 lines
1.2 KiB
PHP
Raw Normal View History

2022-02-13 07:56:26 -05:00
<?php
2023-05-17 12:56:55 -04:00
namespace BookStack\Users\Queries;
2022-02-13 07:56:26 -05:00
2023-05-17 12:56:55 -04:00
use BookStack\Users\Models\User;
use BookStack\Util\SimpleListOptions;
2022-02-13 07:56:26 -05:00
use Illuminate\Pagination\LengthAwarePaginator;
/**
* Get all the users with their permissions in a paginated format.
* Note: Due to the use of email search this should only be used when
* user is assumed to be trusted. (Admin users).
* Email search can be abused to extract email addresses.
*/
class UsersAllPaginatedAndSorted
2022-02-13 07:56:26 -05:00
{
public function run(int $count, SimpleListOptions $listOptions): LengthAwarePaginator
2022-02-13 07:56:26 -05:00
{
$sort = $listOptions->getSort();
if ($sort === 'created_at') {
$sort = 'users.created_at';
}
2022-02-13 07:56:26 -05:00
$query = User::query()->select(['*'])
->scopes(['withLastActivityAt'])
->with(['roles', 'avatar'])
->withCount('mfaValues')
->orderBy($sort, $listOptions->getOrder());
2022-02-13 07:56:26 -05:00
if ($listOptions->getSearch()) {
$term = '%' . $listOptions->getSearch() . '%';
2022-02-13 07:56:26 -05:00
$query->where(function ($query) use ($term) {
$query->where('name', 'like', $term)
->orWhere('email', 'like', $term);
});
}
return $query->paginate($count);
}
}