|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Actions\Fortify; |
| 4 | + |
| 5 | +use Illuminate\Contracts\Auth\MustVerifyEmail; |
| 6 | +use Illuminate\Support\Facades\Validator; |
| 7 | +use Illuminate\Validation\Rule; |
| 8 | +use Laravel\Fortify\Contracts\UpdatesUserProfileInformation; |
| 9 | + |
| 10 | +class UpdateUserProfileInformation implements UpdatesUserProfileInformation |
| 11 | +{ |
| 12 | + /** |
| 13 | + * Validate and update the given user's profile information. |
| 14 | + * |
| 15 | + * @param mixed $user |
| 16 | + * @param array $input |
| 17 | + * @return void |
| 18 | + */ |
| 19 | + public function update($user, array $input) |
| 20 | + { |
| 21 | + Validator::make($input, [ |
| 22 | + 'name' => ['required', 'string', 'max:255'], |
| 23 | + |
| 24 | + 'email' => [ |
| 25 | + 'required', |
| 26 | + 'string', |
| 27 | + 'email', |
| 28 | + 'max:255', |
| 29 | + Rule::unique('users')->ignore($user->id), |
| 30 | + ], |
| 31 | + ])->validateWithBag('updateProfileInformation'); |
| 32 | + |
| 33 | + if ($input['email'] !== $user->email && |
| 34 | + $user instanceof MustVerifyEmail) { |
| 35 | + $this->updateVerifiedUser($user, $input); |
| 36 | + } else { |
| 37 | + $user->forceFill([ |
| 38 | + 'name' => $input['name'], |
| 39 | + 'email' => $input['email'], |
| 40 | + ])->save(); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Update the given verified user's profile information. |
| 46 | + * |
| 47 | + * @param mixed $user |
| 48 | + * @param array $input |
| 49 | + * @return void |
| 50 | + */ |
| 51 | + protected function updateVerifiedUser($user, array $input) |
| 52 | + { |
| 53 | + $user->forceFill([ |
| 54 | + 'name' => $input['name'], |
| 55 | + 'email' => $input['email'], |
| 56 | + 'email_verified_at' => null, |
| 57 | + ])->save(); |
| 58 | + |
| 59 | + $user->sendEmailVerificationNotification(); |
| 60 | + } |
| 61 | +} |
0 commit comments