107 lines
3.5 KiB
PHP
107 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Auth;
|
|
|
|
use App\Enums\Role;
|
|
use App\Models\Setting;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Validation\Rules\Password;
|
|
use Livewire\Component;
|
|
|
|
class SignupForm extends Component
|
|
{
|
|
public string $name = '';
|
|
public string $email = '';
|
|
public string $password = '';
|
|
public string $password_confirmation = '';
|
|
public bool $accept = false;
|
|
public string $successMessage = '';
|
|
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'name' => ['required','string','max:120'],
|
|
'email' => ['required','string','lowercase','email','max:190','unique:users,email'],
|
|
'password' => ['required', 'confirmed', Password::min(4)],
|
|
'accept' => ['accepted'],
|
|
];
|
|
}
|
|
|
|
public function register()
|
|
{
|
|
$this->validate();
|
|
|
|
// Registrierung global gesperrt?
|
|
if (!Setting::signupAllowed()) {
|
|
$this->addError('email', 'Registrierung ist derzeit deaktiviert.');
|
|
return;
|
|
}
|
|
|
|
// Redis-Lock verhindert Doppel-Admin bei parallelem Signup
|
|
Cache::lock('signup:first-user', 10)->block(5, function () {
|
|
DB::transaction(function () {
|
|
|
|
// innerh. Lock & TX nochmal prüfen
|
|
$isFirstUser = !User::query()->exists();
|
|
|
|
User::create([
|
|
'name' => $this->name,
|
|
'email' => $this->email,
|
|
'password' => Hash::make($this->password),
|
|
'role' => $isFirstUser ? Role::Admin : Role::Member,
|
|
]);
|
|
|
|
// nach erstem User: Signup deaktivieren
|
|
if ($isFirstUser) {
|
|
Setting::set('system.signup_enabled', 0); // Redis + DB
|
|
}
|
|
});
|
|
});
|
|
|
|
// Reset + UI
|
|
$this->reset(['name','email','password','password_confirmation','accept']);
|
|
$this->dispatch('toast',
|
|
state: 'done',
|
|
badge: 'Signup',
|
|
domain: 'Registrierung erfolgreich',
|
|
message: 'Dein Konto wurde angelegt. Bitte melde dich jetzt mit deinen Zugangsdaten an. <i class="ph ph-arrow-right"></i> <a href="'.route('login').'" class="underline">Zum Login</a>',
|
|
duration: -1,
|
|
);
|
|
}
|
|
|
|
// public function register()
|
|
// {
|
|
// $this->validate();
|
|
// $isFirstUser = User::count() === 0;
|
|
//
|
|
// User::create([
|
|
// 'name' => $this->name,
|
|
// 'email' => $this->email,
|
|
// 'password' => Hash::make($this->password),
|
|
// 'role' => Setting::signupAllowed() ? Role::Admin : Role::Member,
|
|
// ]);
|
|
//
|
|
// if ($isFirstUser) {
|
|
// Setting::updateOrCreate(['key' => 'signup_enabled'], ['value' => '0']);
|
|
// }
|
|
//
|
|
// $this->reset(['name','email','password','password_confirmation', 'accept']);
|
|
// $this->dispatch('toast',
|
|
// state: 'done',
|
|
// badge: 'Signup',
|
|
// domain: 'Registrierung erfolgreich',
|
|
// message: 'Dein Konto wurde angelegt. Bitte melde dich jetzt mit deinen Zugangsdaten an. <i class="ph ph-arrow-right" ></i> <a href="'.route('login').'" class="underline">Zum Login</a>',
|
|
// duration: -1,
|
|
// );
|
|
// }
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.auth.signup-form');
|
|
}
|
|
}
|