]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Api/UserApiController.php
Added user-update 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 use Illuminate\Validation\Rules\Password;
10 use Illuminate\Validation\Rules\Unique;
11
12 class UserApiController extends ApiController
13 {
14     protected $userRepo;
15
16     protected $fieldsToExpose = [
17         'email', 'created_at', 'updated_at', 'last_activity_at', 'external_auth_id'
18     ];
19
20     public function __construct(UserRepo $userRepo)
21     {
22         $this->userRepo = $userRepo;
23     }
24
25     protected function rules(int $userId = null): array
26     {
27         return [
28             'create' => [
29             ],
30             'update' => [
31                 'name' => ['min:2'],
32                 'email' => [
33                     'min:2',
34                     'email',
35                     (new Unique('users', 'email'))->ignore($userId ?? null)
36                 ],
37                 'external_auth_id' => ['string'],
38                 'language' => ['string'],
39                 'password' => [Password::default()],
40                 'roles' => ['array'],
41                 'roles.*' => ['integer'],
42             ],
43             'delete' => [
44                 'migrate_ownership_id' => ['integer', 'exists:users,id'],
45             ],
46         ];
47     }
48
49     /**
50      * Get a listing of users in the system.
51      * Requires permission to manage users.
52      */
53     public function list()
54     {
55         $this->checkPermission('users-manage');
56
57         $users = $this->userRepo->getApiUsersBuilder();
58
59         return $this->apiListingResponse($users, [
60             'id', 'name', 'slug', 'email', 'external_auth_id',
61             'created_at', 'updated_at', 'last_activity_at',
62         ], [Closure::fromCallable([$this, 'listFormatter'])]);
63     }
64
65     /**
66      * View the details of a single user.
67      * Requires permission to manage users.
68      */
69     public function read(string $id)
70     {
71         $this->checkPermission('users-manage');
72
73         $user = $this->userRepo->getById($id);
74         $this->singleFormatter($user);
75
76         return response()->json($user);
77     }
78
79     /**
80      * Update an existing user in the system.
81      * @throws \BookStack\Exceptions\UserUpdateException
82      */
83     public function update(Request $request, string $id)
84     {
85         $this->checkPermission('users-manage');
86
87         $data = $this->validate($request, $this->rules($id)['update']);
88         $user = $this->userRepo->getById($id);
89         $this->userRepo->update($user, $data, userCan('users-manage'));
90         $this->singleFormatter($user);
91
92         return response()->json($user);
93     }
94
95     /**
96      * Delete a user from the system.
97      * Can optionally accept a user id via `migrate_ownership_id` to indicate
98      * who should be the new owner of their related content.
99      * Requires permission to manage users.
100      */
101     public function delete(Request $request, string $id)
102     {
103         $this->checkPermission('users-manage');
104
105         $user = $this->userRepo->getById($id);
106         $newOwnerId = $request->get('migrate_ownership_id', null);
107
108         $this->userRepo->destroy($user, $newOwnerId);
109
110         return response('', 204);
111     }
112
113     /**
114      * Format the given user model for single-result display.
115      */
116     protected function singleFormatter(User $user)
117     {
118         $this->listFormatter($user);
119         $user->load('roles:id,display_name');
120         $user->makeVisible(['roles']);
121     }
122
123     /**
124      * Format the given user model for a listing multi-result display.
125      */
126     protected function listFormatter(User $user)
127     {
128         $user->makeVisible($this->fieldsToExpose);
129         $user->setAttribute('profile_url', $user->getProfileUrl());
130         $user->setAttribute('edit_url', $user->getEditUrl());
131         $user->setAttribute('avatar_url', $user->getAvatar());
132     }
133 }