3 namespace BookStack\Http\Controllers\Api;
5 use BookStack\Auth\User;
6 use BookStack\Auth\UserRepo;
7 use BookStack\Exceptions\UserUpdateException;
9 use Illuminate\Http\Request;
10 use Illuminate\Support\Facades\DB;
11 use Illuminate\Validation\Rules\Password;
12 use Illuminate\Validation\Rules\Unique;
14 class UserApiController extends ApiController
18 protected $fieldsToExpose = [
19 'email', 'created_at', 'updated_at', 'last_activity_at', 'external_auth_id',
22 public function __construct(UserRepo $userRepo)
24 $this->userRepo = $userRepo;
26 // Checks for all endpoints in this controller
27 $this->middleware(function ($request, $next) {
28 $this->checkPermission('users-manage');
29 $this->preventAccessInDemoMode();
31 return $next($request);
35 protected function rules(int $userId = null): array
39 'name' => ['required', 'min:2'],
41 'required', 'min:2', 'email', new Unique('users', 'email'),
43 'external_auth_id' => ['string'],
44 'language' => ['string'],
45 'password' => [Password::default()],
47 'roles.*' => ['integer'],
48 'send_invite' => ['boolean'],
55 (new Unique('users', 'email'))->ignore($userId ?? null),
57 'external_auth_id' => ['string'],
58 'language' => ['string'],
59 'password' => [Password::default()],
61 'roles.*' => ['integer'],
64 'migrate_ownership_id' => ['integer', 'exists:users,id'],
70 * Get a listing of users in the system.
71 * Requires permission to manage users.
73 public function list()
75 $users = $this->userRepo->getApiUsersBuilder();
77 return $this->apiListingResponse($users, [
78 'id', 'name', 'slug', 'email', 'external_auth_id',
79 'created_at', 'updated_at', 'last_activity_at',
80 ], [Closure::fromCallable([$this, 'listFormatter'])]);
84 * Create a new user in the system.
85 * Requires permission to manage users.
87 public function create(Request $request)
89 $data = $this->validate($request, $this->rules()['create']);
90 $sendInvite = ($data['send_invite'] ?? false) === true;
93 DB::transaction(function () use ($data, $sendInvite, &$user) {
94 $user = $this->userRepo->create($data, $sendInvite);
97 $this->singleFormatter($user);
99 return response()->json($user);
103 * View the details of a single user.
104 * Requires permission to manage users.
106 public function read(string $id)
108 $user = $this->userRepo->getById($id);
109 $this->singleFormatter($user);
111 return response()->json($user);
115 * Update an existing user in the system.
116 * Requires permission to manage users.
118 * @throws UserUpdateException
120 public function update(Request $request, string $id)
122 $data = $this->validate($request, $this->rules($id)['update']);
123 $user = $this->userRepo->getById($id);
124 $this->userRepo->update($user, $data, userCan('users-manage'));
125 $this->singleFormatter($user);
127 return response()->json($user);
131 * Delete a user from the system.
132 * Can optionally accept a user id via `migrate_ownership_id` to indicate
133 * who should be the new owner of their related content.
134 * Requires permission to manage users.
136 public function delete(Request $request, string $id)
138 $user = $this->userRepo->getById($id);
139 $newOwnerId = $request->get('migrate_ownership_id', null);
141 $this->userRepo->destroy($user, $newOwnerId);
143 return response('', 204);
147 * Format the given user model for single-result display.
149 protected function singleFormatter(User $user)
151 $this->listFormatter($user);
152 $user->load('roles:id,display_name');
153 $user->makeVisible(['roles']);
157 * Format the given user model for a listing multi-result display.
159 protected function listFormatter(User $user)
161 $user->makeVisible($this->fieldsToExpose);
162 $user->setAttribute('profile_url', $user->getProfileUrl());
163 $user->setAttribute('edit_url', $user->getEditUrl());
164 $user->setAttribute('avatar_url', $user->getAvatar());