]> BookStack Code Mirror - bookstack/blob - app/Users/Controllers/UserAccountController.php
3dd13b85141c0ed697f84ec29e2a3eb6aeda36fe
[bookstack] / app / Users / Controllers / UserAccountController.php
1 <?php
2
3 namespace BookStack\Users\Controllers;
4
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;
11 use Closure;
12 use Illuminate\Http\Request;
13 use Illuminate\Validation\Rules\Password;
14
15 class UserAccountController extends Controller
16 {
17     public function __construct(
18         protected UserRepo $userRepo,
19     ) {
20         $this->middleware(function (Request $request, Closure $next) {
21             $this->preventGuestAccess();
22             return $next($request);
23         });
24     }
25
26     /**
27      * Show the overview for user preferences.
28      */
29     public function index()
30     {
31         $mfaMethods = user()->mfaValues->groupBy('method');
32
33         return view('users.account.index', [
34             'mfaMethods' => $mfaMethods,
35         ]);
36     }
37
38     /**
39      * Show the user-specific interface shortcuts.
40      */
41     public function showShortcuts()
42     {
43         $shortcuts = UserShortcutMap::fromUserPreferences();
44         $enabled = setting()->getForCurrentUser('ui-shortcuts-enabled', false);
45
46         $this->setPageTitle(trans('preferences.shortcuts_interface'));
47
48         return view('users.account.shortcuts', [
49             'category' => 'shortcuts',
50             'shortcuts' => $shortcuts,
51             'enabled' => $enabled,
52         ]);
53     }
54
55     /**
56      * Update the user-specific interface shortcuts.
57      */
58     public function updateShortcuts(Request $request)
59     {
60         $enabled = $request->get('enabled') === 'true';
61         $providedShortcuts = $request->get('shortcut', []);
62         $shortcuts = new UserShortcutMap($providedShortcuts);
63
64         setting()->putForCurrentUser('ui-shortcuts', $shortcuts->toJson());
65         setting()->putForCurrentUser('ui-shortcuts-enabled', $enabled);
66
67         $this->showSuccessNotification(trans('preferences.shortcuts_update_success'));
68
69         return redirect('/my-account/shortcuts');
70     }
71
72     /**
73      * Show the notification preferences for the current user.
74      */
75     public function showNotifications(PermissionApplicator $permissions)
76     {
77         $this->checkPermission('receive-notifications');
78
79         $preferences = (new UserNotificationPreferences(user()));
80
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);
85
86         $this->setPageTitle(trans('preferences.notifications'));
87         return view('users.account.notifications', [
88             'category' => 'notifications',
89             'preferences' => $preferences,
90             'watches' => $watches,
91         ]);
92     }
93
94     /**
95      * Update the notification preferences for the current user.
96      */
97     public function updateNotifications(Request $request)
98     {
99         $this->checkPermission('receive-notifications');
100         $data = $this->validate($request, [
101            'preferences' => ['required', 'array'],
102            'preferences.*' => ['required', 'string'],
103         ]);
104
105         $preferences = (new UserNotificationPreferences(user()));
106         $preferences->updateFromSettingsArray($data['preferences']);
107         $this->showSuccessNotification(trans('preferences.notifications_update_success'));
108
109         return redirect('/my-account/notifications');
110     }
111
112     /**
113      * Show the view for the "Access & Security" account options.
114      */
115     public function showAuth(SocialAuthService $socialAuthService)
116     {
117         $mfaMethods = user()->mfaValues->groupBy('method');
118
119         $this->setPageTitle(trans('preferences.auth'));
120
121         return view('users.account.auth', [
122             'category' => 'auth',
123             'mfaMethods' => $mfaMethods,
124             'authMethod' => config('auth.method'),
125             'activeSocialDrivers' => $socialAuthService->getActiveDrivers(),
126         ]);
127     }
128
129     /**
130      * Handle the submission for the auth change password form.
131      */
132     public function updatePassword(Request $request)
133     {
134         if (config('auth.method') !== 'standard') {
135             $this->showPermissionError();
136         }
137
138         $validated = $this->validate($request, [
139             'password'         => ['required_with:password_confirm', Password::default()],
140             'password-confirm' => ['same:password', 'required_with:password'],
141         ]);
142
143         $this->userRepo->update(user(), $validated, false);
144
145         $this->showSuccessNotification(trans('preferences.auth_change_password_success'));
146
147         return redirect('/my-account/auth');
148     }
149 }