3 namespace BookStack\Users\Controllers;
5 use BookStack\Http\Controller;
6 use BookStack\Permissions\PermissionApplicator;
7 use BookStack\Settings\UserNotificationPreferences;
8 use BookStack\Settings\UserShortcutMap;
9 use BookStack\Users\UserRepo;
10 use Illuminate\Http\Request;
12 class UserAccountController extends Controller
14 public function __construct(
15 protected UserRepo $userRepo
20 * Show the overview for user preferences.
22 public function index()
24 $guest = user()->isGuest();
25 $mfaMethods = $guest ? [] : user()->mfaValues->groupBy('method');
27 return view('users.account.index', [
28 'mfaMethods' => $mfaMethods,
33 * Show the user-specific interface shortcuts.
35 public function showShortcuts()
37 $shortcuts = UserShortcutMap::fromUserPreferences();
38 $enabled = setting()->getForCurrentUser('ui-shortcuts-enabled', false);
40 $this->setPageTitle(trans('preferences.shortcuts_interface'));
42 return view('users.account.shortcuts', [
43 'shortcuts' => $shortcuts,
44 'enabled' => $enabled,
49 * Update the user-specific interface shortcuts.
51 public function updateShortcuts(Request $request)
53 $enabled = $request->get('enabled') === 'true';
54 $providedShortcuts = $request->get('shortcut', []);
55 $shortcuts = new UserShortcutMap($providedShortcuts);
57 setting()->putForCurrentUser('ui-shortcuts', $shortcuts->toJson());
58 setting()->putForCurrentUser('ui-shortcuts-enabled', $enabled);
60 $this->showSuccessNotification(trans('preferences.shortcuts_update_success'));
62 return redirect('/my-account/shortcuts');
66 * Show the notification preferences for the current user.
68 public function showNotifications(PermissionApplicator $permissions)
70 $this->checkPermission('receive-notifications');
71 $this->preventGuestAccess();
73 $preferences = (new UserNotificationPreferences(user()));
75 $query = user()->watches()->getQuery();
76 $query = $permissions->restrictEntityRelationQuery($query, 'watches', 'watchable_id', 'watchable_type');
77 $query = $permissions->filterDeletedFromEntityRelationQuery($query, 'watches', 'watchable_id', 'watchable_type');
78 $watches = $query->with('watchable')->paginate(20);
80 $this->setPageTitle(trans('preferences.notifications'));
81 return view('users.account.notifications', [
82 'preferences' => $preferences,
83 'watches' => $watches,
88 * Update the notification preferences for the current user.
90 public function updateNotifications(Request $request)
92 $this->checkPermission('receive-notifications');
93 $this->preventGuestAccess();
94 $data = $this->validate($request, [
95 'preferences' => ['required', 'array'],
96 'preferences.*' => ['required', 'string'],
99 $preferences = (new UserNotificationPreferences(user()));
100 $preferences->updateFromSettingsArray($data['preferences']);
101 $this->showSuccessNotification(trans('preferences.notifications_update_success'));
103 return redirect('/my-account/notifications');