From ff2ef836c61cd19bb52577932ef2c7cc54721a4b Mon Sep 17 00:00:00 2001 From: Mark Gregory Date: Mon, 16 Nov 2026 09:45:47 +0000 Subject: [PATCH] enable us to disable registration --- .env.example | 3 + config/fortify.php | 2 +- .../PublicRegistrationDisabledTest.php | 68 +++++++++++++++++++ 3 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 tests/Feature/PublicRegistrationDisabledTest.php diff --git a/.env.example b/.env.example index 40bb13b..0e0dba3 100644 --- a/.env.example +++ b/.env.example @@ -4,6 +4,9 @@ APP_KEY= APP_DEBUG=true APP_URL=http://localhost:8000 +# Set to false to disable public account registration. +PUBLIC_REGISTRATION_ENABLED=true + APP_LOCALE=en APP_FALLBACK_LOCALE=en APP_FAKER_LOCALE=en_US diff --git a/config/fortify.php b/config/fortify.php index cedebce..533adfc 100644 --- a/config/fortify.php +++ b/config/fortify.php @@ -161,7 +161,7 @@ return [ */ 'features' => [ - Features::registration(), + ...(env('PUBLIC_REGISTRATION_ENABLED', true) ? [Features::registration()] : []), Features::resetPasswords(), Features::emailVerification(), Features::twoFactorAuthentication([ diff --git a/tests/Feature/PublicRegistrationDisabledTest.php b/tests/Feature/PublicRegistrationDisabledTest.php new file mode 100644 index 0000000..5133682 --- /dev/null +++ b/tests/Feature/PublicRegistrationDisabledTest.php @@ -0,0 +1,68 @@ +get('/register')->assertNotFound(); +}); + +test('public registration rejects form and JSON submissions without creating a user', function (bool $json) { + $credentials = [ + 'name' => 'Public Signup', + 'email' => 'signup@example.com', + 'password' => 'password', + 'password_confirmation' => 'password', + ]; + + $response = $json + ? $this->postJson('/register', $credentials) + : $this->post('/register', $credentials); + + $response->assertNotFound(); + $this->assertDatabaseMissing('users', ['email' => $credentials['email']]); + $this->assertGuest(); +})->with(['form' => [false], 'JSON' => [true]]); + +test('login and welcome views hide their registration links', function () { + $registrationLink = 'href="'.url('/register').'"'; + + $this->get('/login')->assertOk()->assertDontSee($registrationLink, false); + $this->view('stylist-theme-frontend::welcome')->assertDontSee($registrationLink, false); +}); + +test('administrators can create users while public registration is disabled', function () { + $admin = User::factory()->create(['helpdesk_admin' => true]); + + $this->actingAs($admin)->post(route('user-management.store'), [ + 'name' => 'Provisioned User', + 'email' => 'provisioned@example.com', + 'password' => 'password', + 'password_confirmation' => 'password', + ])->assertSessionHasNoErrors()->assertRedirect(); + + $user = User::query()->where('email', 'provisioned@example.com')->sole(); + expect($user->hasVerifiedEmail())->toBeTrue(); + $this->assertAuthenticatedAs($admin); +});