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