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\Users\UserRepo;
12 use Illuminate\Http\Request;
13 use Illuminate\Validation\Rules\Password;
15 class UserAccountController extends Controller
17 public function __construct(
18 protected UserRepo $userRepo,
20 $this->middleware(function (Request $request, Closure $next) {
21 $this->preventGuestAccess();
22 return $next($request);
27 * Show the overview for user preferences.
29 public function index()
31 $mfaMethods = user()->mfaValues->groupBy('method');
33 return view('users.account.index', [
34 'mfaMethods' => $mfaMethods,
39 * Show the user-specific interface shortcuts.
41 public function showShortcuts()
43 $shortcuts = UserShortcutMap::fromUserPreferences();
44 $enabled = setting()->getForCurrentUser('ui-shortcuts-enabled', false);
46 $this->setPageTitle(trans('preferences.shortcuts_interface'));
48 return view('users.account.shortcuts', [
49 'category' => 'shortcuts',
50 'shortcuts' => $shortcuts,
51 'enabled' => $enabled,
56 * Update the user-specific interface shortcuts.
58 public function updateShortcuts(Request $request)
60 $enabled = $request->get('enabled') === 'true';
61 $providedShortcuts = $request->get('shortcut', []);
62 $shortcuts = new UserShortcutMap($providedShortcuts);
64 setting()->putForCurrentUser('ui-shortcuts', $shortcuts->toJson());
65 setting()->putForCurrentUser('ui-shortcuts-enabled', $enabled);
67 $this->showSuccessNotification(trans('preferences.shortcuts_update_success'));
69 return redirect('/my-account/shortcuts');
73 * Show the notification preferences for the current user.
75 public function showNotifications(PermissionApplicator $permissions)
77 $this->checkPermission('receive-notifications');
79 $preferences = (new UserNotificationPreferences(user()));
81 $query = user()->watches()->getQuery();
82 $query = $permissions->restrictEntityRelationQuery($query, 'watches', 'watchable_id', 'watchable_type');
83 $query = $permissions->filterDeletedFromEntityRelationQuery($query, 'watches', 'watchable_id', 'watchable_type');
84 $watches = $query->with('watchable')->paginate(20);
86 $this->setPageTitle(trans('preferences.notifications'));
87 return view('users.account.notifications', [
88 'category' => 'notifications',
89 'preferences' => $preferences,
90 'watches' => $watches,
95 * Update the notification preferences for the current user.
97 public function updateNotifications(Request $request)
99 $this->checkPermission('receive-notifications');
100 $data = $this->validate($request, [
101 'preferences' => ['required', 'array'],
102 'preferences.*' => ['required', 'string'],
105 $preferences = (new UserNotificationPreferences(user()));
106 $preferences->updateFromSettingsArray($data['preferences']);
107 $this->showSuccessNotification(trans('preferences.notifications_update_success'));
109 return redirect('/my-account/notifications');
113 * Show the view for the "Access & Security" account options.
115 public function showAuth(SocialAuthService $socialAuthService)
117 $mfaMethods = user()->mfaValues->groupBy('method');
119 $this->setPageTitle(trans('preferences.auth'));
121 return view('users.account.auth', [
122 'category' => 'auth',
123 'mfaMethods' => $mfaMethods,
124 'authMethod' => config('auth.method'),
125 'activeSocialDrivers' => $socialAuthService->getActiveDrivers(),
130 * Handle the submission for the auth change password form.
132 public function updatePassword(Request $request)
134 if (config('auth.method') !== 'standard') {
135 $this->showPermissionError();
138 $validated = $this->validate($request, [
139 'password' => ['required_with:password_confirm', Password::default()],
140 'password-confirm' => ['same:password', 'required_with:password'],
143 $this->userRepo->update(user(), $validated, false);
145 $this->showSuccessNotification(trans('preferences.auth_change_password_success'));
147 return redirect('/my-account/auth');