> */ protected function rules(): array { return [ 'name' => ['required', 'string', 'max:120'], 'email' => ['required', 'string', 'email', 'max:255', Rule::unique('users', 'email')], // Optional — when set it must meet the same strength as a self-chosen password. 'password' => ['nullable', 'string', Password::min(6)->mixedCase()->numbers()], ]; } /** * @return array */ protected function validationAttributes(): array { return [ 'name' => __('accounts.name_label'), 'email' => __('accounts.email_label'), 'password' => __('accounts.password_label'), ]; } public function save(): void { $data = $this->validate(); // Re-check uniqueness server-side regardless of the live validator state. $email = mb_strtolower(trim($data['email'])); if (User::where('email', $email)->exists()) { $this->addError('email', __('validation.unique', ['attribute' => __('accounts.email_label')])); return; } // Operator-set password = the user's own login (no forced rotation). Blank = generate a strong // one-time password that the user must rotate, and reveal it once below. $operatorSet = $this->password !== ''; $password = $operatorSet ? $this->password : Str::password(16); $user = User::create([ 'name' => trim($data['name']), 'email' => $email, 'password' => Hash::make($password), 'must_change_password' => ! $operatorSet, ]); AuditEvent::create([ 'user_id' => Auth::id(), 'actor' => Auth::user()?->name ?? 'system', 'action' => 'user.create', 'target' => $user->email, 'ip' => request()->ip(), ]); // Operator already knows the password they set → no reveal; just confirm and close. if ($operatorSet) { $this->dispatch('usersChanged'); $this->dispatch('notify', message: __('accounts.created_notify', ['name' => $user->name])); $this->closeModal(); return; } // Reveal the generated one-time password ONCE — swaps the form for the reveal panel. $this->createdName = $user->name; $this->tempPassword = $password; } /** "Fertig": reload the list and close. */ public function finish(): void { $this->dispatch('usersChanged'); $this->closeModal(); } public function render() { return view('livewire.modals.create-user'); } }