]> BookStack Code Mirror - bookstack/blob - app/Users/Controllers/UserAccountController.php
User: Started cleanup of user self-management
[bookstack] / app / Users / Controllers / UserAccountController.php
1 <?php
2
3 namespace BookStack\Users\Controllers;
4
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;
11
12 class UserAccountController extends Controller
13 {
14     public function __construct(
15         protected UserRepo $userRepo
16     ) {
17     }
18
19     /**
20      * Show the overview for user preferences.
21      */
22     public function index()
23     {
24         $guest = user()->isGuest();
25         $mfaMethods = $guest ? [] : user()->mfaValues->groupBy('method');
26
27         return view('users.account.index', [
28             'mfaMethods' => $mfaMethods,
29         ]);
30     }
31
32     /**
33      * Show the user-specific interface shortcuts.
34      */
35     public function showShortcuts()
36     {
37         $shortcuts = UserShortcutMap::fromUserPreferences();
38         $enabled = setting()->getForCurrentUser('ui-shortcuts-enabled', false);
39
40         $this->setPageTitle(trans('preferences.shortcuts_interface'));
41
42         return view('users.account.shortcuts', [
43             'shortcuts' => $shortcuts,
44             'enabled' => $enabled,
45         ]);
46     }
47
48     /**
49      * Update the user-specific interface shortcuts.
50      */
51     public function updateShortcuts(Request $request)
52     {
53         $enabled = $request->get('enabled') === 'true';
54         $providedShortcuts = $request->get('shortcut', []);
55         $shortcuts = new UserShortcutMap($providedShortcuts);
56
57         setting()->putForCurrentUser('ui-shortcuts', $shortcuts->toJson());
58         setting()->putForCurrentUser('ui-shortcuts-enabled', $enabled);
59
60         $this->showSuccessNotification(trans('preferences.shortcuts_update_success'));
61
62         return redirect('/my-account/shortcuts');
63     }
64
65     /**
66      * Show the notification preferences for the current user.
67      */
68     public function showNotifications(PermissionApplicator $permissions)
69     {
70         $this->checkPermission('receive-notifications');
71         $this->preventGuestAccess();
72
73         $preferences = (new UserNotificationPreferences(user()));
74
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);
79
80         $this->setPageTitle(trans('preferences.notifications'));
81         return view('users.account.notifications', [
82             'preferences' => $preferences,
83             'watches' => $watches,
84         ]);
85     }
86
87     /**
88      * Update the notification preferences for the current user.
89      */
90     public function updateNotifications(Request $request)
91     {
92         $this->checkPermission('receive-notifications');
93         $this->preventGuestAccess();
94         $data = $this->validate($request, [
95            'preferences' => ['required', 'array'],
96            'preferences.*' => ['required', 'string'],
97         ]);
98
99         $preferences = (new UserNotificationPreferences(user()));
100         $preferences->updateFromSettingsArray($data['preferences']);
101         $this->showSuccessNotification(trans('preferences.notifications_update_success'));
102
103         return redirect('/my-account/notifications');
104     }
105 }