65 lines
2.0 KiB
PHP
65 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Actions\Fortify;
|
|
|
|
use App\Models\Customer;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Validation\Rule;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Laravel\Fortify\Contracts\CreatesNewUsers;
|
|
|
|
class CreateNewUser implements CreatesNewUsers
|
|
{
|
|
use PasswordValidationRules;
|
|
|
|
/**
|
|
* Validate and create a newly registered user.
|
|
*
|
|
* @param array<string, string> $input
|
|
*
|
|
* @throws ValidationException
|
|
*/
|
|
public function create(array $input): User
|
|
{
|
|
Validator::make($input, [
|
|
'name' => ['required', 'string', 'max:255'],
|
|
'email' => [
|
|
'required',
|
|
'string',
|
|
'email',
|
|
'max:255',
|
|
Rule::unique(User::class),
|
|
// Never let public signup claim an existing (possibly not-yet-
|
|
// provisioned) customer's email — ensureUser() would later link
|
|
// that account and hand over the customer's portal.
|
|
Rule::unique(Customer::class, 'email'),
|
|
],
|
|
'password' => $this->passwordRules(),
|
|
])->validate();
|
|
|
|
// Create the user and its linked customer atomically — a public signup is
|
|
// a customer, and the portal (Billing::purchase, Settings, …) needs one.
|
|
// Either both exist or neither, so a failure never orphans a user.
|
|
return DB::transaction(function () use ($input) {
|
|
$user = User::create([
|
|
'name' => $input['name'],
|
|
'email' => $input['email'],
|
|
'password' => Hash::make($input['password']),
|
|
]);
|
|
|
|
Customer::query()->create([
|
|
'user_id' => $user->id,
|
|
'name' => $input['name'],
|
|
'email' => $input['email'],
|
|
'locale' => app()->getLocale(),
|
|
'status' => 'active',
|
|
]);
|
|
|
|
return $user;
|
|
});
|
|
}
|
|
}
|