feat: Phase 2 - page CRUD, subscriber management, user management
This commit is contained in:
54
app/Http/Requests/Admin/UpdateUserRequest.php
Normal file
54
app/Http/Requests/Admin/UpdateUserRequest.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests\Admin;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Illuminate\Validation\Rules\Password;
|
||||
|
||||
class UpdateUserRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
$user = $this->route('user');
|
||||
if (! $user instanceof User) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->user()?->can('update', $user) ?? false;
|
||||
}
|
||||
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
if ($this->input('password') === '' || $this->input('password') === null) {
|
||||
$this->merge(['password' => null]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, array<int, ValidationRule|string>>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
/** @var User $target */
|
||||
$target = $this->route('user');
|
||||
|
||||
return [
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'email' => [
|
||||
'required',
|
||||
'string',
|
||||
'lowercase',
|
||||
'email',
|
||||
'max:255',
|
||||
Rule::unique(User::class)->ignore($target->id),
|
||||
],
|
||||
'password' => ['nullable', 'confirmed', Password::defaults()],
|
||||
'role' => ['required', Rule::in(['superadmin', 'user'])],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user