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 $this->preventAccessInDemoMode();
24 return $next($request);
29 * Redirect the root my-account path to the main/first category.
30 * Required as a controller method, instead of the Route::redirect helper,
31 * to ensure the URL is generated correctly.
33 public function redirect()
35 return redirect('/my-account/profile');
39 * Show the profile form interface.
41 public function showProfile()
43 return view('users.account.profile', [
45 'category' => 'profile',
50 * Handle the submission of the user profile form.
52 public function updateProfile(Request $request, ImageRepo $imageRepo)
55 $validated = $this->validate($request, [
56 'name' => ['min:2', 'max:100'],
57 'email' => ['min:2', 'email', 'unique:users,email,' . $user->id],
58 'language' => ['string', 'max:15', 'alpha_dash'],
59 'profile_image' => array_merge(['nullable'], $this->getImageValidationRules()),
62 $this->userRepo->update($user, $validated, userCan('users-manage'));
64 // Save profile image if in request
65 if ($request->hasFile('profile_image')) {
66 $imageUpload = $request->file('profile_image');
67 $imageRepo->destroyImage($user->avatar);
68 $image = $imageRepo->saveNew($imageUpload, 'user', $user->id);
69 $user->image_id = $image->id;
73 // Delete the profile image if reset option is in request
74 if ($request->has('profile_image_reset')) {
75 $imageRepo->destroyImage($user->avatar);
80 return redirect('/my-account/profile');
84 * Show the user-specific interface shortcuts.
86 public function showShortcuts()
88 $shortcuts = UserShortcutMap::fromUserPreferences();
89 $enabled = setting()->getForCurrentUser('ui-shortcuts-enabled', false);
91 $this->setPageTitle(trans('preferences.shortcuts_interface'));
93 return view('users.account.shortcuts', [
94 'category' => 'shortcuts',
95 'shortcuts' => $shortcuts,
96 'enabled' => $enabled,
101 * Update the user-specific interface shortcuts.
103 public function updateShortcuts(Request $request)
105 $enabled = $request->get('enabled') === 'true';
106 $providedShortcuts = $request->get('shortcut', []);
107 $shortcuts = new UserShortcutMap($providedShortcuts);
109 setting()->putForCurrentUser('ui-shortcuts', $shortcuts->toJson());
110 setting()->putForCurrentUser('ui-shortcuts-enabled', $enabled);
112 $this->showSuccessNotification(trans('preferences.shortcuts_update_success'));
114 return redirect('/my-account/shortcuts');
118 * Show the notification preferences for the current user.
120 public function showNotifications(PermissionApplicator $permissions)
122 $this->checkPermission('receive-notifications');
124 $preferences = (new UserNotificationPreferences(user()));
126 $query = user()->watches()->getQuery();
127 $query = $permissions->restrictEntityRelationQuery($query, 'watches', 'watchable_id', 'watchable_type');
128 $query = $permissions->filterDeletedFromEntityRelationQuery($query, 'watches', 'watchable_id', 'watchable_type');
129 $watches = $query->with('watchable')->paginate(20);
131 $this->setPageTitle(trans('preferences.notifications'));
132 return view('users.account.notifications', [
133 'category' => 'notifications',
134 'preferences' => $preferences,
135 'watches' => $watches,
140 * Update the notification preferences for the current user.
142 public function updateNotifications(Request $request)
144 $this->checkPermission('receive-notifications');
145 $data = $this->validate($request, [
146 'preferences' => ['required', 'array'],
147 'preferences.*' => ['required', 'string'],
150 $preferences = (new UserNotificationPreferences(user()));
151 $preferences->updateFromSettingsArray($data['preferences']);
152 $this->showSuccessNotification(trans('preferences.notifications_update_success'));
154 return redirect('/my-account/notifications');
158 * Show the view for the "Access & Security" account options.
160 public function showAuth(SocialAuthService $socialAuthService)
162 $mfaMethods = user()->mfaValues->groupBy('method');
164 $this->setPageTitle(trans('preferences.auth'));
166 return view('users.account.auth', [
167 'category' => 'auth',
168 'mfaMethods' => $mfaMethods,
169 'authMethod' => config('auth.method'),
170 'activeSocialDrivers' => $socialAuthService->getActiveDrivers(),
175 * Handle the submission for the auth change password form.
177 public function updatePassword(Request $request)
179 if (config('auth.method') !== 'standard') {
180 $this->showPermissionError();
183 $validated = $this->validate($request, [
184 'password' => ['required_with:password_confirm', Password::default()],
185 'password-confirm' => ['same:password', 'required_with:password'],
188 $this->userRepo->update(user(), $validated, false);
190 $this->showSuccessNotification(trans('preferences.auth_change_password_success'));
192 return redirect('/my-account/auth');