3 namespace BookStack\Users\Controllers;
5 use BookStack\Access\SocialAuthService;
6 use BookStack\Http\Controller;
7 use BookStack\Permissions\PermissionApplicator;
8 use BookStack\Settings\UserNotificationPreferences;
9 use BookStack\Settings\UserShortcutMap;
10 use BookStack\Uploads\ImageRepo;
11 use BookStack\Users\UserRepo;
13 use Illuminate\Http\Request;
14 use Illuminate\Validation\Rules\Password;
16 class UserAccountController extends Controller
18 public function __construct(
19 protected UserRepo $userRepo,
21 $this->middleware(function (Request $request, Closure $next) {
22 $this->preventGuestAccess();
23 return $next($request);
28 * Redirect the root my-account path to the main/first category.
29 * Required as a controller method, instead of the Route::redirect helper,
30 * to ensure the URL is generated correctly.
32 public function redirect()
34 return redirect('/my-account/profile');
38 * Show the profile form interface.
40 public function showProfile()
42 $this->setPageTitle(trans('preferences.profile'));
44 return view('users.account.profile', [
46 'category' => 'profile',
51 * Handle the submission of the user profile form.
53 public function updateProfile(Request $request, ImageRepo $imageRepo)
55 $this->preventAccessInDemoMode();
58 $validated = $this->validate($request, [
59 'name' => ['min:2', 'max:100'],
60 'email' => ['min:2', 'email', 'unique:users,email,' . $user->id],
61 'language' => ['string', 'max:15', 'alpha_dash'],
62 'profile_image' => array_merge(['nullable'], $this->getImageValidationRules()),
65 $this->userRepo->update($user, $validated, userCan('users-manage'));
67 // Save profile image if in request
68 if ($request->hasFile('profile_image')) {
69 $imageUpload = $request->file('profile_image');
70 $imageRepo->destroyImage($user->avatar);
71 $image = $imageRepo->saveNew($imageUpload, 'user', $user->id);
72 $user->image_id = $image->id;
76 // Delete the profile image if reset option is in request
77 if ($request->has('profile_image_reset')) {
78 $imageRepo->destroyImage($user->avatar);
83 return redirect('/my-account/profile');
87 * Show the user-specific interface shortcuts.
89 public function showShortcuts()
91 $shortcuts = UserShortcutMap::fromUserPreferences();
92 $enabled = setting()->getForCurrentUser('ui-shortcuts-enabled', false);
94 $this->setPageTitle(trans('preferences.shortcuts_interface'));
96 return view('users.account.shortcuts', [
97 'category' => 'shortcuts',
98 'shortcuts' => $shortcuts,
99 'enabled' => $enabled,
104 * Update the user-specific interface shortcuts.
106 public function updateShortcuts(Request $request)
108 $enabled = $request->get('enabled') === 'true';
109 $providedShortcuts = $request->get('shortcut', []);
110 $shortcuts = new UserShortcutMap($providedShortcuts);
112 setting()->putForCurrentUser('ui-shortcuts', $shortcuts->toJson());
113 setting()->putForCurrentUser('ui-shortcuts-enabled', $enabled);
115 $this->showSuccessNotification(trans('preferences.shortcuts_update_success'));
117 return redirect('/my-account/shortcuts');
121 * Show the notification preferences for the current user.
123 public function showNotifications(PermissionApplicator $permissions)
125 $this->checkPermission('receive-notifications');
127 $preferences = (new UserNotificationPreferences(user()));
129 $query = user()->watches()->getQuery();
130 $query = $permissions->restrictEntityRelationQuery($query, 'watches', 'watchable_id', 'watchable_type');
131 $query = $permissions->filterDeletedFromEntityRelationQuery($query, 'watches', 'watchable_id', 'watchable_type');
132 $watches = $query->with('watchable')->paginate(20);
134 $this->setPageTitle(trans('preferences.notifications'));
135 return view('users.account.notifications', [
136 'category' => 'notifications',
137 'preferences' => $preferences,
138 'watches' => $watches,
143 * Update the notification preferences for the current user.
145 public function updateNotifications(Request $request)
147 $this->preventAccessInDemoMode();
148 $this->checkPermission('receive-notifications');
149 $data = $this->validate($request, [
150 'preferences' => ['required', 'array'],
151 'preferences.*' => ['required', 'string'],
154 $preferences = (new UserNotificationPreferences(user()));
155 $preferences->updateFromSettingsArray($data['preferences']);
156 $this->showSuccessNotification(trans('preferences.notifications_update_success'));
158 return redirect('/my-account/notifications');
162 * Show the view for the "Access & Security" account options.
164 public function showAuth(SocialAuthService $socialAuthService)
166 $mfaMethods = user()->mfaValues()->get()->groupBy('method');
168 $this->setPageTitle(trans('preferences.auth'));
170 return view('users.account.auth', [
171 'category' => 'auth',
172 'mfaMethods' => $mfaMethods,
173 'authMethod' => config('auth.method'),
174 'activeSocialDrivers' => $socialAuthService->getActiveDrivers(),
179 * Handle the submission for the auth change password form.
181 public function updatePassword(Request $request)
183 $this->preventAccessInDemoMode();
185 if (config('auth.method') !== 'standard') {
186 $this->showPermissionError();
189 $validated = $this->validate($request, [
190 'password' => ['required_with:password_confirm', Password::default()],
191 'password-confirm' => ['same:password', 'required_with:password'],
194 $this->userRepo->update(user(), $validated, false);
196 $this->showSuccessNotification(trans('preferences.auth_change_password_success'));
198 return redirect('/my-account/auth');
202 * Show the user self-delete page.
204 public function delete()
206 $this->setPageTitle(trans('preferences.delete_my_account'));
208 return view('users.account.delete', [
209 'category' => 'profile',
214 * Remove the current user from the system.
216 public function destroy(Request $request)
218 $this->preventAccessInDemoMode();
220 $requestNewOwnerId = intval($request->get('new_owner_id')) ?: null;
221 $newOwnerId = userCan('users-manage') ? $requestNewOwnerId : null;
223 $this->userRepo->destroy(user(), $newOwnerId);
225 return redirect('/');