70 lines
2.1 KiB
PHP
70 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Actions\Fortify;
|
|
|
|
use App\Actions\Teams\AcceptTeamInvite;
|
|
use App\Concerns\PasswordValidationRules;
|
|
use App\Concerns\ProfileValidationRules;
|
|
use App\Models\TeamInvite;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Laravel\Fortify\Contracts\CreatesNewUsers;
|
|
|
|
class CreateNewUser implements CreatesNewUsers
|
|
{
|
|
use PasswordValidationRules, ProfileValidationRules;
|
|
|
|
public function __construct(
|
|
private AcceptTeamInvite $acceptTeamInvite,
|
|
) {}
|
|
|
|
/**
|
|
* Validate and create a newly registered user.
|
|
*
|
|
* @param array<string, string> $input
|
|
*/
|
|
public function create(array $input): User
|
|
{
|
|
$validated = Validator::make($input, [
|
|
...$this->profileRules(),
|
|
'password' => $this->passwordRules(),
|
|
'invite_token' => ['nullable', 'string', 'size:128'],
|
|
])->validate();
|
|
|
|
return DB::transaction(function () use ($validated): User {
|
|
$invite = null;
|
|
|
|
if (! empty($validated['invite_token'])) {
|
|
$invite = TeamInvite::findByPlainToken($validated['invite_token'])
|
|
->lockForUpdate()
|
|
->first();
|
|
|
|
if (
|
|
$invite === null
|
|
|| mb_strtolower($invite->invited_email)
|
|
!== mb_strtolower($validated['email'])
|
|
) {
|
|
throw ValidationException::withMessages([
|
|
'invite_token' => 'Die Einladung ist ungültig oder gehört zu einer anderen E-Mail-Adresse.',
|
|
]);
|
|
}
|
|
}
|
|
|
|
$user = User::create([
|
|
'name' => $validated['name'],
|
|
'email' => $validated['email'],
|
|
'password' => $validated['password'],
|
|
]);
|
|
|
|
if ($invite !== null) {
|
|
$invite->team->members()->attach($user);
|
|
$invite->delete();
|
|
}
|
|
|
|
return $user;
|
|
});
|
|
}
|
|
}
|