From fdfa4bc697a64e6a725cfbec3997ca39367da4d7 Mon Sep 17 00:00:00 2001 From: Augus Date: Tue, 9 Jan 2024 19:42:40 +0100 Subject: [PATCH 1/4] Add disable registration setting. --- app/Filament/Pages/Settings.php | 10 +++++++--- app/Settings/GeneralSettings.php | 1 + ...24_01_09_181742_create_disable_user_setting.php | 10 ++++++++++ resources/views/livewire/header.blade.php | 14 ++++++++------ resources/views/partials/header.blade.php | 14 ++++++++------ routes/web.php | 8 ++++++-- tests/Feature/Auth/RegistrationTest.php | 12 ++++++++++++ 7 files changed, 52 insertions(+), 17 deletions(-) create mode 100644 database/settings/2024_01_09_181742_create_disable_user_setting.php diff --git a/app/Filament/Pages/Settings.php b/app/Filament/Pages/Settings.php index fcd8323e..73c8db09 100644 --- a/app/Filament/Pages/Settings.php +++ b/app/Filament/Pages/Settings.php @@ -2,9 +2,6 @@ namespace App\Filament\Pages; -use Filament\Actions\Action; -use Filament\Notifications\Notification; -use Filament\Support\Enums\Alignment; use Storage; use App\Models\Board; use App\Enums\UserRole; @@ -12,6 +9,7 @@ use Filament\Forms\Form; use Illuminate\Support\Str; use App\Enums\InboxWorkflow; +use Filament\Actions\Action; use App\Services\GitHubService; use Filament\Pages\SettingsPage; use App\Settings\GeneralSettings; @@ -21,11 +19,13 @@ use Filament\Forms\Components\Group; use Filament\Forms\Components\Select; use Filament\Forms\Components\Toggle; +use Filament\Support\Enums\Alignment; use Filament\Forms\Components\Section; use Filament\Forms\Components\Repeater; use Filament\Forms\Components\Textarea; use Filament\Forms\Components\TagsInput; use Filament\Forms\Components\TextInput; +use Filament\Notifications\Notification; use Filament\Forms\Components\RichEditor; class Settings extends SettingsPage @@ -113,6 +113,10 @@ public function form(Form $form): Form ->label('Disallow users to upload files or images via the markdown editors.') ->columnSpan(1), + Toggle::make('disable_user_registration') + ->label('Disable user registration') + ->columnSpan(1), + Toggle::make('show_github_link') ->label('Show a link to the linked GitHub issue on the item page') ->columnSpan(1) diff --git a/app/Settings/GeneralSettings.php b/app/Settings/GeneralSettings.php index 4d99bf1d..71a4188c 100644 --- a/app/Settings/GeneralSettings.php +++ b/app/Settings/GeneralSettings.php @@ -30,6 +30,7 @@ class GeneralSettings extends Settings public bool $show_changelog_author; public bool $show_changelog_related_items; public bool $disable_file_uploads; + public bool $disable_user_registration; public array $excluded_matching_search_words; public array $profanity_words; public bool $show_github_link; diff --git a/database/settings/2024_01_09_181742_create_disable_user_setting.php b/database/settings/2024_01_09_181742_create_disable_user_setting.php new file mode 100644 index 00000000..ae91dd92 --- /dev/null +++ b/database/settings/2024_01_09_181742_create_disable_user_setting.php @@ -0,0 +1,10 @@ +migrator->add('general.disable_user_registration', false); + } +}; diff --git a/resources/views/livewire/header.blade.php b/resources/views/livewire/header.blade.php index 3810e677..cfd2b8b5 100644 --- a/resources/views/livewire/header.blade.php +++ b/resources/views/livewire/header.blade.php @@ -22,12 +22,14 @@ {{ trans('auth.login') }} -
  • - - {{ trans('auth.register') }} - -
  • + @if(! app(App\Settings\GeneralSettings::class)->disable_user_registration) +
  • + + {{ trans('auth.register') }} + +
  • + @endif @endguest @auth diff --git a/resources/views/partials/header.blade.php b/resources/views/partials/header.blade.php index 5443a5f1..60173673 100644 --- a/resources/views/partials/header.blade.php +++ b/resources/views/partials/header.blade.php @@ -22,12 +22,14 @@ {{ trans('auth.login') }} -
  • - - {{ trans('auth.register') }} - -
  • + @if(! app(App\Settings\GeneralSettings::class)->disable_user_registration) +
  • + + {{ trans('auth.register') }} + +
  • + @endif @endguest @auth diff --git a/routes/web.php b/routes/web.php index a88ae649..c2cbcf23 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,17 +1,21 @@ !app(GeneralSettings::class)->disable_user_registration +]); Route::get('oauth/login', [\App\Http\Controllers\Auth\LoginController::class, 'redirectToProvider']) ->middleware('guest') diff --git a/tests/Feature/Auth/RegistrationTest.php b/tests/Feature/Auth/RegistrationTest.php index 7c4e73fa..ceab713e 100644 --- a/tests/Feature/Auth/RegistrationTest.php +++ b/tests/Feature/Auth/RegistrationTest.php @@ -2,6 +2,8 @@ namespace App\Tests\Feature\Auth; +use App\Settings\GeneralSettings; + test('registration screen can be rendered', function () { $response = $this->get(route('register')); @@ -43,3 +45,13 @@ $this->assertAuthenticatedAs($user); }); + +test('guests cannot register an account when this feature is disabled', function () { + GeneralSettings::fake([ + 'disable_user_registration' => true + ]); + + $response = $this->get(route('register')); + + $response->assertStatus(404); +}); From 20a1381fee066f778ae3a346598a4151d02361aa Mon Sep 17 00:00:00 2001 From: Augus Date: Tue, 9 Jan 2024 20:03:53 +0100 Subject: [PATCH 2/4] Avoid database usage on routes --- app/Http/Controllers/Auth/RegisterController.php | 9 +++++++++ routes/web.php | 4 +--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/Auth/RegisterController.php b/app/Http/Controllers/Auth/RegisterController.php index 187972d7..e4d8670a 100644 --- a/app/Http/Controllers/Auth/RegisterController.php +++ b/app/Http/Controllers/Auth/RegisterController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers\Auth; use App\Models\User; +use App\Settings\GeneralSettings; use App\Http\Controllers\Controller; use App\SocialProviders\SsoProvider; use Illuminate\Support\Facades\Hash; @@ -36,6 +37,10 @@ protected function validator(array $data) protected function create(array $data) { + if (app(GeneralSettings::class)->disable_user_registration) { + abort(301, 'User registration is disabled.'); + } + return User::create([ 'name' => $data['name'], 'email' => $data['email'], @@ -45,6 +50,10 @@ protected function create(array $data) public function showRegistrationForm() { + if (app(GeneralSettings::class)->disable_user_registration) { + return redirect()->route('home'); + } + if (SsoProvider::isForced()) { return to_route('oauth.login'); } diff --git a/routes/web.php b/routes/web.php index c2cbcf23..d5e9e61c 100644 --- a/routes/web.php +++ b/routes/web.php @@ -13,9 +13,7 @@ use App\Http\Controllers\ItemEmailUnsubscribeController; use App\Http\Controllers\Auth\PasswordProtectionController; -Auth::routes([ - 'register' => !app(GeneralSettings::class)->disable_user_registration -]); +Auth::routes(); Route::get('oauth/login', [\App\Http\Controllers\Auth\LoginController::class, 'redirectToProvider']) ->middleware('guest') From 881390c46669fc8261ca01f7746e617986151f18 Mon Sep 17 00:00:00 2001 From: Augus Date: Tue, 9 Jan 2024 20:10:46 +0100 Subject: [PATCH 3/4] Proper redirect test for home --- tests/Feature/Auth/RegistrationTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Feature/Auth/RegistrationTest.php b/tests/Feature/Auth/RegistrationTest.php index ceab713e..e3489b6d 100644 --- a/tests/Feature/Auth/RegistrationTest.php +++ b/tests/Feature/Auth/RegistrationTest.php @@ -53,5 +53,6 @@ $response = $this->get(route('register')); - $response->assertStatus(404); + $response->assertRedirect(route('home')); + $response->assertStatus(302); }); From 1135b30083091bdeeef2508ddb5526628b2f0282 Mon Sep 17 00:00:00 2001 From: Augus Date: Tue, 9 Jan 2024 20:13:43 +0100 Subject: [PATCH 4/4] Better naming --- tests/Feature/Auth/RegistrationTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Feature/Auth/RegistrationTest.php b/tests/Feature/Auth/RegistrationTest.php index e3489b6d..ca4d412d 100644 --- a/tests/Feature/Auth/RegistrationTest.php +++ b/tests/Feature/Auth/RegistrationTest.php @@ -46,7 +46,7 @@ $this->assertAuthenticatedAs($user); }); -test('guests cannot register an account when this feature is disabled', function () { +test('guests cannot access /register when this feature is disabled', function () { GeneralSettings::fake([ 'disable_user_registration' => true ]); @@ -56,3 +56,4 @@ $response->assertRedirect(route('home')); $response->assertStatus(302); }); +