Add New registered user
This commit is contained in:
@@ -2,16 +2,24 @@
|
||||
|
||||
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.
|
||||
*
|
||||
@@ -19,15 +27,43 @@ class CreateNewUser implements CreatesNewUsers
|
||||
*/
|
||||
public function create(array $input): User
|
||||
{
|
||||
Validator::make($input, [
|
||||
$validated = Validator::make($input, [
|
||||
...$this->profileRules(),
|
||||
'password' => $this->passwordRules(),
|
||||
'invite_token' => ['nullable', 'string', 'size:128'],
|
||||
])->validate();
|
||||
|
||||
return User::create([
|
||||
'name' => $input['name'],
|
||||
'email' => $input['email'],
|
||||
'password' => $input['password'],
|
||||
]);
|
||||
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;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user