sintonia_webapp/app/Actions/Fortify/UpdateUserProfileInformation.php

77 lines
2.3 KiB
PHP

<?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.
*
* @param array<string, string> $input
*/
public function update(User $user, array $input): void
{
$rules = [
'login' => ['required', 'string', 'max:255', Rule::unique('cc_subjs')->ignore($user->id)],
'email' => [
'required',
'string',
'email',
'max:255',
Rule::unique('cc_subjs')->ignore($user->id),
],
];
// Only add the 'type' validation rule if the user has the permission to change roles
if (auth()->user()->hasPermissionTo('user.changeRole')) {
$rules['type'] = ['required', 'string', 'max:6', Rule::in(['admin', 'editor', 'dj'])];
}
Validator::make($input, $rules)->validateWithBag('updateProfileInformation');
if ($input['email'] !== $user->email && $user instanceof MustVerifyEmail) {
$this->updateVerifiedUser($user, $input);
} else {
$data = [
'login' => $input['login'],
'email' => $input['email'],
];
// Only update 'type' if the user has the permission
if (auth()->user()->hasPermissionTo('user.changeRole')) {
$data['type'] = $input['type'];
}
$user->forceFill($data)->save();
}
}
/**
* Update the given verified user's profile information.
*
* @param array<string, string> $input
*/
protected function updateVerifiedUser(User $user, array $input): void
{
$data = [
'login' => $input['login'],
'email' => $input['email'],
'email_verified_at' => null,
];
// Only update 'type' if the user has the permission
if (auth()->user()->hasPermissionTo('user.changeRole')) {
$data['type'] = $input['type'];
}
$user->forceFill($data)->save();
$user->sendEmailVerificationNotification();
}
}