2023-03-24 13:54:17 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Actions\Fortify;
|
|
|
|
|
|
|
|
use App\Models\User;
|
|
|
|
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
|
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
use Illuminate\Validation\Rule;
|
|
|
|
use Laravel\Fortify\Contracts\UpdatesUserProfileInformation;
|
|
|
|
|
|
|
|
class UpdateUserProfileInformation implements UpdatesUserProfileInformation
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Validate and update the given user's profile information.
|
|
|
|
*
|
2023-08-08 09:51:36 +00:00
|
|
|
* @param array<string, string> $input
|
2023-03-24 13:54:17 +00:00
|
|
|
*/
|
|
|
|
public function update(User $user, array $input): void
|
|
|
|
{
|
|
|
|
Validator::make($input, [
|
|
|
|
'name' => ['required', 'string', 'max:255'],
|
|
|
|
|
|
|
|
'email' => [
|
|
|
|
'required',
|
|
|
|
'string',
|
|
|
|
'email',
|
|
|
|
'max:255',
|
|
|
|
Rule::unique('users')->ignore($user->id),
|
|
|
|
],
|
|
|
|
])->validateWithBag('updateProfileInformation');
|
|
|
|
|
2023-08-11 18:48:52 +00:00
|
|
|
if (
|
|
|
|
$input['email'] !== $user->email &&
|
|
|
|
$user instanceof MustVerifyEmail
|
|
|
|
) {
|
2023-03-24 13:54:17 +00:00
|
|
|
$this->updateVerifiedUser($user, $input);
|
|
|
|
} else {
|
|
|
|
$user->forceFill([
|
|
|
|
'name' => $input['name'],
|
|
|
|
'email' => $input['email'],
|
|
|
|
])->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the given verified user's profile information.
|
|
|
|
*
|
2023-08-08 09:51:36 +00:00
|
|
|
* @param array<string, string> $input
|
2023-03-24 13:54:17 +00:00
|
|
|
*/
|
|
|
|
protected function updateVerifiedUser(User $user, array $input): void
|
|
|
|
{
|
|
|
|
$user->forceFill([
|
|
|
|
'name' => $input['name'],
|
|
|
|
'email' => $input['email'],
|
|
|
|
'email_verified_at' => null,
|
|
|
|
])->save();
|
|
|
|
|
|
|
|
$user->sendEmailVerificationNotification();
|
|
|
|
}
|
|
|
|
}
|