BookStack/app/Users/Controllers/UserSearchController.php

41 lines
997 B
PHP
Raw Normal View History

2020-12-31 18:25:20 +01:00
<?php
2023-05-17 18:56:55 +02:00
namespace BookStack\Users\Controllers;
2020-12-31 18:25:20 +01:00
use BookStack\Http\Controller;
2023-05-17 18:56:55 +02:00
use BookStack\Users\Models\User;
2020-12-31 18:25:20 +01:00
use Illuminate\Http\Request;
class UserSearchController extends Controller
{
/**
* Search users in the system, with the response formatted
* for use in a select-style list.
*/
public function forSelect(Request $request)
{
$hasPermission = signedInUser() && (
2021-12-15 14:49:20 +01:00
userCan('users-manage')
|| userCan('restrictions-manage-own')
|| userCan('restrictions-manage-all')
);
if (!$hasPermission) {
$this->showPermissionError();
}
2020-12-31 18:25:20 +01:00
$search = $request->get('search', '');
$query = User::query()
->orderBy('name', 'asc')
2020-12-31 18:25:20 +01:00
->take(20);
if (!empty($search)) {
$query->where('name', 'like', '%' . $search . '%');
2020-12-31 18:25:20 +01:00
}
return view('form.user-select-list', [
'users' => $query->get(),
]);
2020-12-31 18:25:20 +01:00
}
}