]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Api/UserApiController.php
Added users-delete API endpoint
[bookstack] / app / Http / Controllers / Api / UserApiController.php
1 <?php
2
3 namespace BookStack\Http\Controllers\Api;
4
5 use BookStack\Auth\User;
6 use BookStack\Auth\UserRepo;
7 use Closure;
8 use Illuminate\Http\Request;
9
10 class UserApiController extends ApiController
11 {
12     protected $userRepo;
13
14     protected $fieldsToExpose = [
15         'email', 'created_at', 'updated_at', 'last_activity_at', 'external_auth_id'
16     ];
17
18     protected $rules = [
19         'create' => [
20         ],
21         'update' => [
22         ],
23         'delete' => [
24             'migrate_ownership_id' => ['integer', 'exists:users,id'],
25         ],
26     ];
27
28     public function __construct(UserRepo $userRepo)
29     {
30         $this->userRepo = $userRepo;
31     }
32
33     /**
34      * Get a listing of users in the system.
35      * Requires permission to manage users.
36      */
37     public function list()
38     {
39         $this->checkPermission('users-manage');
40
41         $users = $this->userRepo->getApiUsersBuilder();
42
43         return $this->apiListingResponse($users, [
44             'id', 'name', 'slug', 'email', 'external_auth_id',
45             'created_at', 'updated_at', 'last_activity_at',
46         ], [Closure::fromCallable([$this, 'listFormatter'])]);
47     }
48
49     /**
50      * View the details of a single user.
51      * Requires permission to manage users.
52      */
53     public function read(string $id)
54     {
55         $this->checkPermission('users-manage');
56
57         $singleUser = $this->userRepo->getById($id);
58         $this->singleFormatter($singleUser);
59
60         return response()->json($singleUser);
61     }
62
63     /**
64      * Delete a user from the system.
65      * Can optionally accept a user id via `migrate_ownership_id` to indicate
66      * who should be the new owner of their related content.
67      * Requires permission to manage users.
68      */
69     public function delete(Request $request, string $id)
70     {
71         $this->checkPermission('users-manage');
72
73         $user = $this->userRepo->getById($id);
74         $newOwnerId = $request->get('migrate_ownership_id', null);
75
76         $this->userRepo->destroy($user, $newOwnerId);
77
78         return response('', 204);
79     }
80
81     /**
82      * Format the given user model for single-result display.
83      */
84     protected function singleFormatter(User $user)
85     {
86         $this->listFormatter($user);
87         $user->load('roles:id,display_name');
88         $user->makeVisible(['roles']);
89     }
90
91     /**
92      * Format the given user model for a listing multi-result display.
93      */
94     protected function listFormatter(User $user)
95     {
96         $user->makeVisible($this->fieldsToExpose);
97         $user->setAttribute('profile_url', $user->getProfileUrl());
98         $user->setAttribute('edit_url', $user->getEditUrl());
99         $user->setAttribute('avatar_url', $user->getAvatar());
100     }
101 }