3 namespace BookStack\Users\Controllers;
5 use BookStack\Entities\EntityExistsRule;
6 use BookStack\Exceptions\UserUpdateException;
7 use BookStack\Http\ApiController;
8 use BookStack\Permissions\Permission;
9 use BookStack\Users\Models\User;
10 use BookStack\Users\UserRepo;
11 use Illuminate\Http\Request;
12 use Illuminate\Support\Facades\DB;
13 use Illuminate\Validation\Rules\Password;
14 use Illuminate\Validation\Rules\Unique;
16 class UserApiController extends ApiController
18 protected UserRepo $userRepo;
20 protected array $fieldsToExpose = [
21 'email', 'created_at', 'updated_at', 'last_activity_at', 'external_auth_id',
24 public function __construct(UserRepo $userRepo)
26 $this->userRepo = $userRepo;
28 // Checks for all endpoints in this controller
29 $this->middleware(function ($request, $next) {
30 $this->checkPermission(Permission::UsersManage);
31 $this->preventAccessInDemoMode();
33 return $next($request);
37 protected function rules(?int $userId = null): array
41 'name' => ['required', 'string', 'min:1', 'max:100'],
43 'required', 'string', 'email', 'min:2', new Unique('users', 'email'),
45 'external_auth_id' => ['string'],
46 'language' => ['string', 'max:15', 'alpha_dash'],
47 'password' => ['string', Password::default()],
49 'roles.*' => ['integer'],
50 'send_invite' => ['boolean'],
53 'name' => ['string', 'min:1', 'max:100'],
58 (new Unique('users', 'email'))->ignore($userId),
60 'external_auth_id' => ['string'],
61 'language' => ['string', 'max:15', 'alpha_dash'],
62 'password' => ['string', Password::default()],
64 'roles.*' => ['integer'],
67 'migrate_ownership_id' => ['integer', 'exists:users,id'],
73 * Get a listing of users in the system.
74 * Requires permission to manage users.
76 public function list()
78 $users = User::query()->select(['users.*'])
79 ->scopes('withLastActivityAt')
82 return $this->apiListingResponse($users, [
83 'id', 'name', 'slug', 'email', 'external_auth_id',
84 'created_at', 'updated_at', 'last_activity_at',
85 ], [$this->listFormatter(...)]);
89 * Create a new user in the system.
90 * Requires permission to manage users.
92 public function create(Request $request)
94 $data = $this->validate($request, $this->rules()['create']);
95 $sendInvite = boolval($data['send_invite'] ?? false) === true;
98 DB::transaction(function () use ($data, $sendInvite, &$user) {
99 $user = $this->userRepo->create($data, $sendInvite);
102 $this->singleFormatter($user);
104 return response()->json($user);
108 * View the details of a single user.
109 * Requires permission to manage users.
111 public function read(string $id)
113 $user = $this->userRepo->getById($id);
114 $this->singleFormatter($user);
116 return response()->json($user);
120 * Update an existing user in the system.
121 * Requires permission to manage users.
123 * @throws UserUpdateException
125 public function update(Request $request, string $id)
127 $data = $this->validate($request, $this->rules($id)['update']);
128 $user = $this->userRepo->getById($id);
129 $this->userRepo->update($user, $data, userCan(Permission::UsersManage));
130 $this->singleFormatter($user);
132 return response()->json($user);
136 * Delete a user from the system.
137 * Can optionally accept a user id via `migrate_ownership_id` to indicate
138 * who should be the new owner of their related content.
139 * Requires permission to manage users.
141 public function delete(Request $request, string $id)
143 $user = $this->userRepo->getById($id);
144 $newOwnerId = $request->get('migrate_ownership_id', null);
146 $this->userRepo->destroy($user, $newOwnerId);
148 return response('', 204);
152 * Format the given user model for single-result display.
154 protected function singleFormatter(User $user)
156 $this->listFormatter($user);
157 $user->load('roles:id,display_name');
158 $user->makeVisible(['roles']);
162 * Format the given user model for a listing multi-result display.
164 protected function listFormatter(User $user)
166 $user->makeVisible($this->fieldsToExpose);
167 $user->setAttribute('profile_url', $user->getProfileUrl());
168 $user->setAttribute('edit_url', $user->getEditUrl());
169 $user->setAttribute('avatar_url', $user->getAvatar());