+ /**
+ * Update an existing user in the system.
+ * Requires permission to manage users.
+ *
+ * @throws UserUpdateException
+ */
+ public function update(Request $request, string $id)
+ {
+ $data = $this->validate($request, $this->rules($id)['update']);
+ $user = $this->userRepo->getById($id);
+ $this->userRepo->update($user, $data, userCan('users-manage'));
+ $this->singleFormatter($user);
+
+ return response()->json($user);
+ }
+
+ /**
+ * Delete a user from the system.
+ * Can optionally accept a user id via `migrate_ownership_id` to indicate
+ * who should be the new owner of their related content.
+ * Requires permission to manage users.
+ */
+ public function delete(Request $request, string $id)
+ {
+ $user = $this->userRepo->getById($id);
+ $newOwnerId = $request->get('migrate_ownership_id', null);
+
+ $this->userRepo->destroy($user, $newOwnerId);
+
+ return response('', 204);
+ }
+
+ /**
+ * Format the given user model for single-result display.
+ */
+ protected function singleFormatter(User $user)
+ {
+ $this->listFormatter($user);
+ $user->load('roles:id,display_name');
+ $user->makeVisible(['roles']);
+ }
+
+ /**
+ * Format the given user model for a listing multi-result display.
+ */
+ protected function listFormatter(User $user)
+ {
+ $user->makeVisible($this->fieldsToExpose);
+ $user->setAttribute('profile_url', $user->getProfileUrl());
+ $user->setAttribute('edit_url', $user->getEditUrl());
+ $user->setAttribute('avatar_url', $user->getAvatar());