3 namespace BookStack\Http\Controllers;
5 use BookStack\Auth\Access\SocialAuthService;
6 use BookStack\Auth\Queries\UsersAllPaginatedAndSorted;
7 use BookStack\Auth\Role;
8 use BookStack\Auth\UserRepo;
9 use BookStack\Exceptions\ImageUploadException;
10 use BookStack\Exceptions\UserUpdateException;
11 use BookStack\Uploads\ImageRepo;
12 use BookStack\Util\SimpleListOptions;
14 use Illuminate\Http\Request;
15 use Illuminate\Support\Facades\DB;
16 use Illuminate\Validation\Rules\Password;
17 use Illuminate\Validation\ValidationException;
19 class UserController extends Controller
21 protected UserRepo $userRepo;
22 protected ImageRepo $imageRepo;
24 public function __construct(UserRepo $userRepo, ImageRepo $imageRepo)
26 $this->userRepo = $userRepo;
27 $this->imageRepo = $imageRepo;
31 * Display a listing of the users.
33 public function index(Request $request)
35 $this->checkPermission('users-manage');
37 $listOptions = SimpleListOptions::fromRequest($request, 'users')->withSortOptions([
38 'name' => trans('common.sort_name'),
39 'email' => trans('auth.email'),
40 'created_at' => trans('common.sort_created_at'),
41 'updated_at' => trans('common.sort_updated_at'),
42 'last_activity_at' => trans('settings.users_latest_activity'),
45 $users = (new UsersAllPaginatedAndSorted())->run(20, $listOptions);
47 $this->setPageTitle(trans('settings.users'));
48 $users->appends($listOptions->getPaginationAppends());
50 return view('users.index', [
52 'listOptions' => $listOptions,
57 * Show the form for creating a new user.
59 public function create()
61 $this->checkPermission('users-manage');
62 $authMethod = config('auth.method');
63 $roles = Role::query()->orderBy('display_name', 'asc')->get();
64 $this->setPageTitle(trans('settings.users_add_new'));
66 return view('users.create', ['authMethod' => $authMethod, 'roles' => $roles]);
70 * Store a new user in storage.
72 * @throws ValidationException
74 public function store(Request $request)
76 $this->checkPermission('users-manage');
78 $authMethod = config('auth.method');
79 $sendInvite = ($request->get('send_invite', 'false') === 'true');
80 $externalAuth = $authMethod === 'ldap' || $authMethod === 'saml2' || $authMethod === 'oidc';
81 $passwordRequired = ($authMethod === 'standard' && !$sendInvite);
84 'name' => ['required', 'max:100'],
85 'email' => ['required', 'email', 'unique:users,email'],
86 'language' => ['string', 'max:15', 'alpha_dash'],
88 'roles.*' => ['integer'],
89 'password' => $passwordRequired ? ['required', Password::default()] : null,
90 'password-confirm' => $passwordRequired ? ['required', 'same:password'] : null,
91 'external_auth_id' => $externalAuth ? ['required'] : null,
94 $validated = $this->validate($request, array_filter($validationRules));
96 DB::transaction(function () use ($validated, $sendInvite) {
97 $this->userRepo->create($validated, $sendInvite);
100 return redirect('/settings/users');
104 * Show the form for editing the specified user.
106 public function edit(int $id, SocialAuthService $socialAuthService)
108 $this->checkPermissionOrCurrentUser('users-manage', $id);
110 $user = $this->userRepo->getById($id);
111 $user->load(['apiTokens', 'mfaValues']);
112 $authMethod = ($user->system_name) ? 'system' : config('auth.method');
114 $activeSocialDrivers = $socialAuthService->getActiveDrivers();
115 $mfaMethods = $user->mfaValues->groupBy('method');
116 $this->setPageTitle(trans('settings.user_profile'));
117 $roles = Role::query()->orderBy('display_name', 'asc')->get();
119 return view('users.edit', [
121 'activeSocialDrivers' => $activeSocialDrivers,
122 'mfaMethods' => $mfaMethods,
123 'authMethod' => $authMethod,
129 * Update the specified user in storage.
131 * @throws UserUpdateException
132 * @throws ImageUploadException
133 * @throws ValidationException
135 public function update(Request $request, int $id)
137 $this->preventAccessInDemoMode();
138 $this->checkPermissionOrCurrentUser('users-manage', $id);
140 $validated = $this->validate($request, [
141 'name' => ['min:2', 'max:100'],
142 'email' => ['min:2', 'email', 'unique:users,email,' . $id],
143 'password' => ['required_with:password_confirm', Password::default()],
144 'password-confirm' => ['same:password', 'required_with:password'],
145 'language' => ['string', 'max:15', 'alpha_dash'],
146 'roles' => ['array'],
147 'roles.*' => ['integer'],
148 'external_auth_id' => ['string'],
149 'profile_image' => array_merge(['nullable'], $this->getImageValidationRules()),
152 $user = $this->userRepo->getById($id);
153 $this->userRepo->update($user, $validated, userCan('users-manage'));
155 // Save profile image if in request
156 if ($request->hasFile('profile_image')) {
157 $imageUpload = $request->file('profile_image');
158 $this->imageRepo->destroyImage($user->avatar);
159 $image = $this->imageRepo->saveNew($imageUpload, 'user', $user->id);
160 $user->image_id = $image->id;
164 // Delete the profile image if reset option is in request
165 if ($request->has('profile_image_reset')) {
166 $this->imageRepo->destroyImage($user->avatar);
171 $redirectUrl = userCan('users-manage') ? '/settings/users' : "/settings/users/{$user->id}";
173 return redirect($redirectUrl);
177 * Show the user delete page.
179 public function delete(int $id)
181 $this->checkPermissionOrCurrentUser('users-manage', $id);
183 $user = $this->userRepo->getById($id);
184 $this->setPageTitle(trans('settings.users_delete_named', ['userName' => $user->name]));
186 return view('users.delete', ['user' => $user]);
190 * Remove the specified user from storage.
194 public function destroy(Request $request, int $id)
196 $this->preventAccessInDemoMode();
197 $this->checkPermissionOrCurrentUser('users-manage', $id);
199 $user = $this->userRepo->getById($id);
200 $newOwnerId = $request->get('new_owner_id', null);
202 $this->userRepo->destroy($user, $newOwnerId);
204 return redirect('/settings/users');