+1
-1
@@ -4,7 +4,7 @@ APP_KEY=
|
|||||||
APP_DEBUG=true
|
APP_DEBUG=true
|
||||||
APP_URL=http://localhost:8000
|
APP_URL=http://localhost:8000
|
||||||
|
|
||||||
# Set to false to disable public account registration.
|
# Default for public registration until an administrator saves a choice in Users.
|
||||||
PUBLIC_REGISTRATION_ENABLED=true
|
PUBLIC_REGISTRATION_ENABLED=true
|
||||||
|
|
||||||
APP_LOCALE=en
|
APP_LOCALE=en
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
|||||||
namespace Modules\UserManagement\Http\Controllers;
|
namespace Modules\UserManagement\Http\Controllers;
|
||||||
|
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
|
use App\Support\PublicRegistration;
|
||||||
use Illuminate\Contracts\View\View;
|
use Illuminate\Contracts\View\View;
|
||||||
use Illuminate\Http\RedirectResponse;
|
use Illuminate\Http\RedirectResponse;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
@@ -32,7 +33,20 @@ final class UserController extends Controller
|
|||||||
->paginate(5)
|
->paginate(5)
|
||||||
->withQueryString();
|
->withQueryString();
|
||||||
|
|
||||||
return view('modules.user-management.index', compact('users', 'search'));
|
return view('modules.user-management.index', [
|
||||||
|
'users' => $users,
|
||||||
|
'search' => $search,
|
||||||
|
'publicRegistrationEnabled' => PublicRegistration::enabled(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function updatePublicRegistration(Request $request): RedirectResponse
|
||||||
|
{
|
||||||
|
$request->validate(['enabled' => ['required', 'boolean']]);
|
||||||
|
|
||||||
|
PublicRegistration::setEnabled($request->boolean('enabled'));
|
||||||
|
|
||||||
|
return redirect()->route('user-management.index')->with('status', 'Public registration setting saved.');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function create(): View
|
public function create(): View
|
||||||
|
|||||||
@@ -23,5 +23,7 @@ Route::put('/permissions/{permission}', [UserController::class, 'updatePermissio
|
|||||||
Route::delete('/permissions/{permission}', [UserController::class, 'destroyPermission'])->name('permissions.destroy');
|
Route::delete('/permissions/{permission}', [UserController::class, 'destroyPermission'])->name('permissions.destroy');
|
||||||
|
|
||||||
Route::get('/{user}/edit', [UserController::class, 'edit'])->name('edit');
|
Route::get('/{user}/edit', [UserController::class, 'edit'])->name('edit');
|
||||||
|
Route::put('/public-registration', [UserController::class, 'updatePublicRegistration'])->name('public-registration.update');
|
||||||
|
|
||||||
Route::put('/{user}', [UserController::class, 'update'])->name('update');
|
Route::put('/{user}', [UserController::class, 'update'])->name('update');
|
||||||
Route::delete('/{user}', [UserController::class, 'destroy'])->name('destroy');
|
Route::delete('/{user}', [UserController::class, 'destroy'])->name('destroy');
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Middleware;
|
||||||
|
|
||||||
|
use App\Support\PublicRegistration;
|
||||||
|
use Closure;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
|
||||||
|
final class EnsurePublicRegistrationEnabled
|
||||||
|
{
|
||||||
|
public function handle(Request $request, Closure $next): Response
|
||||||
|
{
|
||||||
|
if ($request->routeIs('register', 'register.store')) {
|
||||||
|
abort_unless(PublicRegistration::enabled(), 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $next($request);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Support;
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
|
final class PublicRegistration
|
||||||
|
{
|
||||||
|
public static function enabled(): bool
|
||||||
|
{
|
||||||
|
$enabled = DB::table('public_registration_settings')->where('id', 1)->value('enabled');
|
||||||
|
|
||||||
|
return (bool) ($enabled ?? config('fortify.public_registration_default'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function setEnabled(bool $enabled): void
|
||||||
|
{
|
||||||
|
DB::table('public_registration_settings')->upsert(
|
||||||
|
['id' => 1, 'enabled' => $enabled],
|
||||||
|
['id'],
|
||||||
|
['enabled'],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
+5
-2
@@ -1,5 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use App\Http\Middleware\EnsurePublicRegistrationEnabled;
|
||||||
use Laravel\Fortify\Features;
|
use Laravel\Fortify\Features;
|
||||||
|
|
||||||
return [
|
return [
|
||||||
@@ -101,7 +102,9 @@ return [
|
|||||||
|
|
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
'middleware' => ['web'],
|
'middleware' => ['web', EnsurePublicRegistrationEnabled::class],
|
||||||
|
|
||||||
|
'public_registration_default' => env('PUBLIC_REGISTRATION_ENABLED', true),
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
@@ -161,7 +164,7 @@ return [
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
'features' => [
|
'features' => [
|
||||||
...(env('PUBLIC_REGISTRATION_ENABLED', true) ? [Features::registration()] : []),
|
Features::registration(),
|
||||||
Features::resetPasswords(),
|
Features::resetPasswords(),
|
||||||
Features::emailVerification(),
|
Features::emailVerification(),
|
||||||
Features::twoFactorAuthentication([
|
Features::twoFactorAuthentication([
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('public_registration_settings', function (Blueprint $table): void {
|
||||||
|
$table->unsignedTinyInteger('id')->primary();
|
||||||
|
$table->boolean('enabled');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('public_registration_settings');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -58,7 +58,7 @@
|
|||||||
<button class="btn btn-primary" type="submit" data-test="login-button">{{ __('Log in') }}</button>
|
<button class="btn btn-primary" type="submit" data-test="login-button">{{ __('Log in') }}</button>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
@if (Route::has('register'))
|
@if (Route::has('register') && \App\Support\PublicRegistration::enabled())
|
||||||
<p class="signup">{{ __('Don\'t have an account?') }} <a class="link" href="{{ route('register') }}">{{ __('Sign up') }}</a></p>
|
<p class="signup">{{ __('Don\'t have an account?') }} <a class="link" href="{{ route('register') }}">{{ __('Sign up') }}</a></p>
|
||||||
@endif
|
@endif
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,17 @@
|
|||||||
<div class="hd-alert hd-alert-success">{{ session('status') }}</div>
|
<div class="hd-alert hd-alert-success">{{ session('status') }}</div>
|
||||||
@endif
|
@endif
|
||||||
|
|
||||||
|
<form method="POST" action="{{ route('user-management.public-registration.update') }}" class="hd-panel hd-toolbar">
|
||||||
|
@csrf
|
||||||
|
@method('PUT')
|
||||||
|
<input type="hidden" name="enabled" value="0">
|
||||||
|
<label class="flex items-center gap-2">
|
||||||
|
<input type="checkbox" name="enabled" value="1" @checked($publicRegistrationEnabled)>
|
||||||
|
Allow public registration
|
||||||
|
</label>
|
||||||
|
<button class="hd-action hd-action-blue" type="submit">Save registration setting</button>
|
||||||
|
</form>
|
||||||
|
|
||||||
<section class="hd-panel">
|
<section class="hd-panel">
|
||||||
<form method="GET" class="hd-toolbar">
|
<form method="GET" class="hd-toolbar">
|
||||||
<div class="hd-search-field">
|
<div class="hd-search-field">
|
||||||
|
|||||||
@@ -36,7 +36,7 @@
|
|||||||
Log in
|
Log in
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
@if (Route::has('register'))
|
@if (Route::has('register') && \App\Support\PublicRegistration::enabled())
|
||||||
<a
|
<a
|
||||||
href="{{ route('register') }}"
|
href="{{ route('register') }}"
|
||||||
class="inline-block px-5 py-1.5 dark:text-[#EDEDEC] border-[#19140035] hover:border-[#1915014a] border text-[#1b1b18] dark:border-[#3E3E3A] dark:hover:border-[#62605b] rounded-sm text-sm leading-normal">
|
class="inline-block px-5 py-1.5 dark:text-[#EDEDEC] border-[#19140035] hover:border-[#1915014a] border text-[#1b1b18] dark:border-[#3E3E3A] dark:hover:border-[#62605b] rounded-sm text-sm leading-normal">
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
use Laravel\Fortify\Features;
|
use Laravel\Fortify\Features;
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
$this->skipUnlessFortifyHas(Features::registration());
|
$this->skipUnlessFortifyHas(Features::registration());
|
||||||
|
config(['fortify.public_registration_default' => true]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('registration screen can be rendered', function () {
|
test('registration screen can be rendered', function () {
|
||||||
@@ -25,3 +27,44 @@ test('new users can register', function () {
|
|||||||
|
|
||||||
$this->assertAuthenticated();
|
$this->assertAuthenticated();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('administrators can disable and re-enable public registration from Users', function () {
|
||||||
|
$admin = User::factory()->create(['helpdesk_admin' => true]);
|
||||||
|
$settingsUrl = '/admin/users/public-registration';
|
||||||
|
|
||||||
|
$this->actingAs($admin)->get('/admin/users')
|
||||||
|
->assertOk()
|
||||||
|
->assertSee('Allow public registration');
|
||||||
|
|
||||||
|
$this->put($settingsUrl, ['enabled' => '0'])->assertRedirect('/admin/users');
|
||||||
|
|
||||||
|
auth()->logout();
|
||||||
|
$this->get('/register')->assertNotFound();
|
||||||
|
$this->postJson('/register', [
|
||||||
|
'name' => 'New User',
|
||||||
|
'email' => 'public@example.com',
|
||||||
|
'password' => 'password',
|
||||||
|
'password_confirmation' => 'password',
|
||||||
|
])->assertNotFound();
|
||||||
|
$this->assertDatabaseMissing('users', ['email' => 'public@example.com']);
|
||||||
|
$this->get('/login')->assertOk()->assertDontSee('href="'.url('/register').'"', false);
|
||||||
|
$this->view('stylist-theme-frontend::welcome')->assertDontSee('href="'.url('/register').'"', false);
|
||||||
|
|
||||||
|
config(['fortify.public_registration_default' => false]);
|
||||||
|
$this->actingAs($admin)->put($settingsUrl, ['enabled' => '1'])->assertRedirect('/admin/users');
|
||||||
|
|
||||||
|
auth()->logout();
|
||||||
|
$this->get('/register')->assertOk();
|
||||||
|
$this->get('/login')->assertOk()->assertSee('href="'.url('/register').'"', false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('only administrators can change public registration', function () {
|
||||||
|
$settingsUrl = '/admin/users/public-registration';
|
||||||
|
$this->put($settingsUrl, ['enabled' => '0'])->assertRedirect(route('login'));
|
||||||
|
|
||||||
|
$this->actingAs(User::factory()->create())
|
||||||
|
->put($settingsUrl, ['enabled' => '0'])->assertForbidden();
|
||||||
|
|
||||||
|
auth()->logout();
|
||||||
|
$this->get('/register')->assertOk();
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user