3 namespace BookStack\Http\Controllers\Api;
5 use BookStack\Auth\User;
6 use BookStack\Auth\UserRepo;
8 use Illuminate\Http\Request;
10 class UserApiController extends ApiController
14 protected $fieldsToExpose = [
15 'email', 'created_at', 'updated_at', 'last_activity_at', 'external_auth_id'
24 'migrate_ownership_id' => ['integer', 'exists:users,id'],
28 public function __construct(UserRepo $userRepo)
30 $this->userRepo = $userRepo;
34 * Get a listing of users in the system.
35 * Requires permission to manage users.
37 public function list()
39 $this->checkPermission('users-manage');
41 $users = $this->userRepo->getApiUsersBuilder();
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'])]);
50 * View the details of a single user.
51 * Requires permission to manage users.
53 public function read(string $id)
55 $this->checkPermission('users-manage');
57 $singleUser = $this->userRepo->getById($id);
58 $this->singleFormatter($singleUser);
60 return response()->json($singleUser);
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.
69 public function delete(Request $request, string $id)
71 $this->checkPermission('users-manage');
73 $user = $this->userRepo->getById($id);
74 $newOwnerId = $request->get('migrate_ownership_id', null);
76 $this->userRepo->destroy($user, $newOwnerId);
78 return response('', 204);
82 * Format the given user model for single-result display.
84 protected function singleFormatter(User $user)
86 $this->listFormatter($user);
87 $user->load('roles:id,display_name');
88 $user->makeVisible(['roles']);
92 * Format the given user model for a listing multi-result display.
94 protected function listFormatter(User $user)
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());