2022-02-13 20:56:26 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Auth\Queries;
|
|
|
|
|
|
|
|
use BookStack\Auth\User;
|
2022-10-30 23:16:06 +08:00
|
|
|
use BookStack\Util\SimpleListOptions;
|
2022-02-13 20:56:26 +08: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.
|
|
|
|
*/
|
2022-10-30 20:02:06 +08:00
|
|
|
class UsersAllPaginatedAndSorted
|
2022-02-13 20:56:26 +08:00
|
|
|
{
|
2022-10-30 23:16:06 +08:00
|
|
|
public function run(int $count, SimpleListOptions $listOptions): LengthAwarePaginator
|
2022-02-13 20:56:26 +08:00
|
|
|
{
|
2022-10-30 23:16:06 +08:00
|
|
|
$sort = $listOptions->getSort();
|
2022-10-29 22:23:21 +08:00
|
|
|
if ($sort === 'created_at') {
|
|
|
|
$sort = 'users.created_at';
|
|
|
|
}
|
2022-02-13 20:56:26 +08:00
|
|
|
|
|
|
|
$query = User::query()->select(['*'])
|
|
|
|
->scopes(['withLastActivityAt'])
|
|
|
|
->with(['roles', 'avatar'])
|
|
|
|
->withCount('mfaValues')
|
2022-10-30 23:16:06 +08:00
|
|
|
->orderBy($sort, $listOptions->getOrder());
|
2022-02-13 20:56:26 +08:00
|
|
|
|
2022-10-30 23:16:06 +08:00
|
|
|
if ($listOptions->getSearch()) {
|
|
|
|
$term = '%' . $listOptions->getSearch() . '%';
|
2022-02-13 20:56:26 +08:00
|
|
|
$query->where(function ($query) use ($term) {
|
|
|
|
$query->where('name', 'like', $term)
|
|
|
|
->orWhere('email', 'like', $term);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return $query->paginate($count);
|
|
|
|
}
|
2022-02-13 21:16:43 +08:00
|
|
|
}
|