From 371779205a29816b90fcf31bb6dba609aeb192fc Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Mon, 14 Aug 2023 17:29:12 +0100 Subject: [PATCH] Notifications: Added new preferences view and access control - Added general user preferences view and updated link in profile menu to suit. - Made notification permission required for notification preferences view, added test to cover. --- .../Controllers/UserPreferencesController.php | 11 +++++ lang/en/common.php | 1 + lang/en/preferences.php | 5 ++ resources/icons/user-preferences.svg | 1 + resources/views/common/header.blade.php | 6 +-- .../views/users/preferences/index.blade.php | 41 ++++++++++++++++ .../users/preferences/notifications.blade.php | 49 ++++++++++--------- routes/web.php | 2 +- tests/User/UserPreferencesTest.php | 36 +++++++++++++- 9 files changed, 123 insertions(+), 29 deletions(-) create mode 100644 resources/icons/user-preferences.svg create mode 100644 resources/views/users/preferences/index.blade.php diff --git a/app/Users/Controllers/UserPreferencesController.php b/app/Users/Controllers/UserPreferencesController.php index 999115e7b..d9ee50ca7 100644 --- a/app/Users/Controllers/UserPreferencesController.php +++ b/app/Users/Controllers/UserPreferencesController.php @@ -17,6 +17,14 @@ class UserPreferencesController extends Controller ) { } + /** + * Show the overview for user preferences. + */ + public function index() + { + return view('users.preferences.index'); + } + /** * Show the user-specific interface shortcuts. */ @@ -53,6 +61,8 @@ class UserPreferencesController extends Controller */ public function showNotifications(PermissionApplicator $permissions) { + $this->checkPermission('receive-notifications'); + $preferences = (new UserNotificationPreferences(user())); $query = Watch::query()->where('user_id', '=', user()->id); @@ -70,6 +80,7 @@ class UserPreferencesController extends Controller */ public function updateNotifications(Request $request) { + $this->checkPermission('receive-notifications'); $data = $this->validate($request, [ 'preferences' => ['required', 'array'], 'preferences.*' => ['required', 'string'], diff --git a/lang/en/common.php b/lang/en/common.php index de7937b2b..47b74d5b6 100644 --- a/lang/en/common.php +++ b/lang/en/common.php @@ -42,6 +42,7 @@ return [ 'remove' => 'Remove', 'add' => 'Add', 'configure' => 'Configure', + 'manage' => 'Manage', 'fullscreen' => 'Fullscreen', 'favourite' => 'Favourite', 'unfavourite' => 'Unfavourite', diff --git a/lang/en/preferences.php b/lang/en/preferences.php index 97968f8b1..118e8ba82 100644 --- a/lang/en/preferences.php +++ b/lang/en/preferences.php @@ -5,6 +5,8 @@ */ return [ + 'preferences' => 'Preferences', + 'shortcuts' => 'Shortcuts', 'shortcuts_interface' => 'Interface Keyboard Shortcuts', 'shortcuts_toggle_desc' => 'Here you can enable or disable keyboard system interface shortcuts, used for navigation and actions.', @@ -15,6 +17,7 @@ return [ 'shortcuts_save' => 'Save Shortcuts', 'shortcuts_overlay_desc' => 'Note: When shortcuts are enabled a helper overlay is available via pressing "?" which will highlight the available shortcuts for actions currently visible on the screen.', 'shortcuts_update_success' => 'Shortcut preferences have been updated!', + 'shortcuts_overview_desc' => 'Manage keyboard shortcuts you can use to navigate the system user interface.', 'notifications' => 'Notification Preferences', 'notifications_desc' => 'Control the email notifications you receive when certain activity is performed within the system.', @@ -25,4 +28,6 @@ return [ 'notifications_update_success' => 'Notification preferences have been updated!', 'notifications_watched' => 'Watched & Ignored Items', 'notifications_watched_desc' => ' Below are the items that have custom watch preferences applied. To update your preferences for these, view the item then find the watch options in the sidebar.', + + 'profile_overview_desc' => ' Manage your user profile details including preferred language and authentication options.', ]; diff --git a/resources/icons/user-preferences.svg b/resources/icons/user-preferences.svg new file mode 100644 index 000000000..5ae1773ca --- /dev/null +++ b/resources/icons/user-preferences.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/resources/views/common/header.blade.php b/resources/views/common/header.blade.php index a8b711595..97a411d84 100644 --- a/resources/views/common/header.blade.php +++ b/resources/views/common/header.blade.php @@ -104,9 +104,9 @@

  • - - @icon('shortcuts') -
    {{ trans('preferences.shortcuts') }}
    +
    + @icon('user-preferences') +
    {{ trans('preferences.preferences') }}
  • diff --git a/resources/views/users/preferences/index.blade.php b/resources/views/users/preferences/index.blade.php new file mode 100644 index 000000000..a79245acb --- /dev/null +++ b/resources/views/users/preferences/index.blade.php @@ -0,0 +1,41 @@ +@extends('layouts.simple') + +@section('body') +
    + +
    +
    +

    {{ trans('preferences.shortcuts_interface') }}

    +

    {{ trans('preferences.shortcuts_overview_desc') }}

    +
    + +
    + + @if(userCan('receive-notifications')) +
    +
    +

    {{ trans('preferences.notifications') }}

    +

    {{ trans('preferences.notifications_desc') }}

    +
    + +
    + @endif + + @if(signedInUser()) +
    +
    +

    {{ trans('settings.users_edit_profile') }}

    +

    {{ trans('preferences.profile_overview_desc') }}

    +
    + +
    + @endif + +
    +@stop diff --git a/resources/views/users/preferences/notifications.blade.php b/resources/views/users/preferences/notifications.blade.php index 3bbf78280..ae89c087e 100644 --- a/resources/views/users/preferences/notifications.blade.php +++ b/resources/views/users/preferences/notifications.blade.php @@ -11,33 +11,36 @@

    {{ trans('preferences.notifications') }}

    {{ trans('preferences.notifications_desc') }}

    -
    -
    - @include('form.toggle-switch', [ - 'name' => 'preferences[own-page-changes]', - 'value' => $preferences->notifyOnOwnPageChanges(), - 'label' => trans('preferences.notifications_opt_own_page_changes'), - ]) +
    +
    +
    + @include('form.toggle-switch', [ + 'name' => 'preferences[own-page-changes]', + 'value' => $preferences->notifyOnOwnPageChanges(), + 'label' => trans('preferences.notifications_opt_own_page_changes'), + ]) +
    +
    + @include('form.toggle-switch', [ + 'name' => 'preferences[own-page-comments]', + 'value' => $preferences->notifyOnOwnPageComments(), + 'label' => trans('preferences.notifications_opt_own_page_comments'), + ]) +
    +
    + @include('form.toggle-switch', [ + 'name' => 'preferences[comment-replies]', + 'value' => $preferences->notifyOnCommentReplies(), + 'label' => trans('preferences.notifications_opt_comment_replies'), + ]) +
    -
    - @include('form.toggle-switch', [ - 'name' => 'preferences[own-page-comments]', - 'value' => $preferences->notifyOnOwnPageComments(), - 'label' => trans('preferences.notifications_opt_own_page_comments'), - ]) -
    -
    - @include('form.toggle-switch', [ - 'name' => 'preferences[comment-replies]', - 'value' => $preferences->notifyOnCommentReplies(), - 'label' => trans('preferences.notifications_opt_comment_replies'), - ]) + +
    +
    -
    - -
    diff --git a/routes/web.php b/routes/web.php index 27a54f8b4..c7fc92fc7 100644 --- a/routes/web.php +++ b/routes/web.php @@ -231,7 +231,7 @@ Route::middleware('auth')->group(function () { Route::delete('/settings/users/{id}', [UserControllers\UserController::class, 'destroy']); // User Preferences - Route::redirect('/preferences', '/'); + Route::get('/preferences', [UserControllers\UserPreferencesController::class, 'index']); Route::get('/preferences/shortcuts', [UserControllers\UserPreferencesController::class, 'showShortcuts']); Route::put('/preferences/shortcuts', [UserControllers\UserPreferencesController::class, 'updateShortcuts']); Route::get('/preferences/notifications', [UserControllers\UserPreferencesController::class, 'showNotifications']); diff --git a/tests/User/UserPreferencesTest.php b/tests/User/UserPreferencesTest.php index e83df5731..a30484bd2 100644 --- a/tests/User/UserPreferencesTest.php +++ b/tests/User/UserPreferencesTest.php @@ -6,6 +6,22 @@ use Tests\TestCase; class UserPreferencesTest extends TestCase { + public function test_index_view() + { + $resp = $this->asEditor()->get('/preferences'); + $resp->assertOk(); + $resp->assertSee('Interface Keyboard Shortcuts'); + $resp->assertSee('Edit Profile'); + } + + public function test_index_view_accessible_but_without_profile_for_guest_user() + { + $this->setSettings(['app-public' => 'true']); + $resp = $this->get('/preferences'); + $resp->assertOk(); + $resp->assertSee('Interface Keyboard Shortcuts'); + $resp->assertDontSee('Edit Profile'); + } public function test_interface_shortcuts_updating() { $this->asEditor(); @@ -45,12 +61,28 @@ class UserPreferencesTest extends TestCase $this->withHtml($this->get('/'))->assertElementExists('body[component="shortcuts"]'); } + public function test_notification_routes_requires_notification_permission() + { + $viewer = $this->users->viewer(); + $resp = $this->actingAs($viewer)->get('/preferences/notifications'); + $this->assertPermissionError($resp); + + $resp = $this->put('/preferences/notifications'); + $this->assertPermissionError($resp); + + $this->permissions->grantUserRolePermissions($viewer, ['receive-notifications']); + $resp = $this->get('/preferences/notifications'); + $resp->assertOk(); + $resp->assertSee('Notification Preferences'); + } + public function test_notification_preferences_updating() { - $this->asEditor(); + $editor = $this->users->editor(); + $this->permissions->grantUserRolePermissions($editor, ['receive-notifications']); // View preferences with defaults - $resp = $this->get('/preferences/notifications'); + $resp = $this->actingAs($editor)->get('/preferences/notifications'); $resp->assertSee('Notification Preferences'); $html = $this->withHtml($resp);