3 namespace BookStack\Users\Controllers;
5 use BookStack\Exceptions\UserUpdateException;
6 use BookStack\Http\Controllers\ApiController;
7 use BookStack\Users\Models\User;
8 use BookStack\Users\UserRepo;
10 use Illuminate\Http\Request;
11 use Illuminate\Support\Facades\DB;
12 use Illuminate\Validation\Rules\Password;
13 use Illuminate\Validation\Rules\Unique;
15 class UserApiController extends ApiController
17 protected UserRepo $userRepo;
19 protected array $fieldsToExpose = [
20 'email', 'created_at', 'updated_at', 'last_activity_at', 'external_auth_id',
23 public function __construct(UserRepo $userRepo)
25 $this->userRepo = $userRepo;
27 // Checks for all endpoints in this controller
28 $this->middleware(function ($request, $next) {
29 $this->checkPermission('users-manage');
30 $this->preventAccessInDemoMode();
32 return $next($request);
36 protected function rules(int $userId = null): array
40 'name' => ['required', 'min:2', 'max:100'],
42 'required', 'min:2', 'email', new Unique('users', 'email'),
44 'external_auth_id' => ['string'],
45 'language' => ['string', 'max:15', 'alpha_dash'],
46 'password' => [Password::default()],
48 'roles.*' => ['integer'],
49 'send_invite' => ['boolean'],
52 'name' => ['min:2', 'max:100'],
56 (new Unique('users', 'email'))->ignore($userId ?? null),
58 'external_auth_id' => ['string'],
59 'language' => ['string', 'max:15', 'alpha_dash'],
60 'password' => [Password::default()],
62 'roles.*' => ['integer'],
65 'migrate_ownership_id' => ['integer', 'exists:users,id'],
71 * Get a listing of users in the system.
72 * Requires permission to manage users.
74 public function list()
76 $users = User::query()->select(['*'])
77 ->scopes('withLastActivityAt')
80 return $this->apiListingResponse($users, [
81 'id', 'name', 'slug', 'email', 'external_auth_id',
82 'created_at', 'updated_at', 'last_activity_at',
83 ], [Closure::fromCallable([$this, 'listFormatter'])]);
87 * Create a new user in the system.
88 * Requires permission to manage users.
90 public function create(Request $request)
92 $data = $this->validate($request, $this->rules()['create']);
93 $sendInvite = ($data['send_invite'] ?? false) === true;
96 DB::transaction(function () use ($data, $sendInvite, &$user) {
97 $user = $this->userRepo->create($data, $sendInvite);
100 $this->singleFormatter($user);
102 return response()->json($user);
106 * View the details of a single user.
107 * Requires permission to manage users.
109 public function read(string $id)
111 $user = $this->userRepo->getById($id);
112 $this->singleFormatter($user);
114 return response()->json($user);
118 * Update an existing user in the system.
119 * Requires permission to manage users.
121 * @throws UserUpdateException
123 public function update(Request $request, string $id)
125 $data = $this->validate($request, $this->rules($id)['update']);
126 $user = $this->userRepo->getById($id);
127 $this->userRepo->update($user, $data, userCan('users-manage'));
128 $this->singleFormatter($user);
130 return response()->json($user);
134 * Delete a user from the system.
135 * Can optionally accept a user id via `migrate_ownership_id` to indicate
136 * who should be the new owner of their related content.
137 * Requires permission to manage users.
139 public function delete(Request $request, string $id)
141 $user = $this->userRepo->getById($id);
142 $newOwnerId = $request->get('migrate_ownership_id', null);
144 $this->userRepo->destroy($user, $newOwnerId);
146 return response('', 204);
150 * Format the given user model for single-result display.
152 protected function singleFormatter(User $user)
154 $this->listFormatter($user);
155 $user->load('roles:id,display_name');
156 $user->makeVisible(['roles']);
160 * Format the given user model for a listing multi-result display.
162 protected function listFormatter(User $user)
164 $user->makeVisible($this->fieldsToExpose);
165 $user->setAttribute('profile_url', $user->getProfileUrl());
166 $user->setAttribute('edit_url', $user->getEditUrl());
167 $user->setAttribute('avatar_url', $user->getAvatar());