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();
30 return $next($request);
34 protected function rules(int $userId = null): array
38 'name' => ['required', 'min:2'],
40 'required', 'min:2', 'email', new Unique('users', 'email')
42 'external_auth_id' => ['string'],
43 'language' => ['string'],
44 'password' => [Password::default()],
46 'roles.*' => ['integer'],
47 'send_invite' => ['boolean'],
54 (new Unique('users', 'email'))->ignore($userId ?? null)
56 'external_auth_id' => ['string'],
57 'language' => ['string'],
58 'password' => [Password::default()],
60 'roles.*' => ['integer'],
63 'migrate_ownership_id' => ['integer', 'exists:users,id'],
69 * Get a listing of users in the system.
70 * Requires permission to manage users.
72 public function list()
74 $users = $this->userRepo->getApiUsersBuilder();
76 return $this->apiListingResponse($users, [
77 'id', 'name', 'slug', 'email', 'external_auth_id',
78 'created_at', 'updated_at', 'last_activity_at',
79 ], [Closure::fromCallable([$this, 'listFormatter'])]);
83 * Create a new user in the system.
84 * Requires permission to manage users.
86 public function create(Request $request)
88 $data = $this->validate($request, $this->rules()['create']);
89 $sendInvite = ($data['send_invite'] ?? false) === true;
92 DB::transaction(function () use ($data, $sendInvite, &$user) {
93 $user = $this->userRepo->create($data, $sendInvite);
96 $this->singleFormatter($user);
98 return response()->json($user);
102 * View the details of a single user.
103 * Requires permission to manage users.
105 public function read(string $id)
107 $user = $this->userRepo->getById($id);
108 $this->singleFormatter($user);
110 return response()->json($user);
114 * Update an existing user in the system.
115 * Requires permission to manage users.
116 * @throws UserUpdateException
118 public function update(Request $request, string $id)
120 $data = $this->validate($request, $this->rules($id)['update']);
121 $user = $this->userRepo->getById($id);
122 $this->userRepo->update($user, $data, userCan('users-manage'));
123 $this->singleFormatter($user);
125 return response()->json($user);
129 * Delete a user from the system.
130 * Can optionally accept a user id via `migrate_ownership_id` to indicate
131 * who should be the new owner of their related content.
132 * Requires permission to manage users.
134 public function delete(Request $request, string $id)
136 $user = $this->userRepo->getById($id);
137 $newOwnerId = $request->get('migrate_ownership_id', null);
139 $this->userRepo->destroy($user, $newOwnerId);
141 return response('', 204);
145 * Format the given user model for single-result display.
147 protected function singleFormatter(User $user)
149 $this->listFormatter($user);
150 $user->load('roles:id,display_name');
151 $user->makeVisible(['roles']);
155 * Format the given user model for a listing multi-result display.
157 protected function listFormatter(User $user)
159 $user->makeVisible($this->fieldsToExpose);
160 $user->setAttribute('profile_url', $user->getProfileUrl());
161 $user->setAttribute('edit_url', $user->getEditUrl());
162 $user->setAttribute('avatar_url', $user->getAvatar());