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