]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/UserSearchController.php
Guest create page: name field autofocus
[bookstack] / app / Http / Controllers / UserSearchController.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
5 use BookStack\Auth\User;
6 use Illuminate\Http\Request;
7
8 class UserSearchController extends Controller
9 {
10     /**
11      * Search users in the system, with the response formatted
12      * for use in a select-style list.
13      */
14     public function forSelect(Request $request)
15     {
16         $hasPermission = signedInUser() && (
17             userCan('users-manage')
18                 || userCan('restrictions-manage-own')
19                 || userCan('restrictions-manage-all')
20         );
21
22         if (!$hasPermission) {
23             $this->showPermissionError();
24         }
25
26         $search = $request->get('search', '');
27         $query = User::query()
28             ->orderBy('name', 'asc')
29             ->take(20);
30
31         if (!empty($search)) {
32             $query->where('name', 'like', '%' . $search . '%');
33         }
34
35         return view('form.user-select-list', [
36             'users' => $query->get(),
37         ]);
38     }
39 }