2024-06-05 05:28:37 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
|
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
|
|
use Illuminate\Validation\Rule;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use App\Models\User;
|
2024-06-22 10:49:49 -07:00
|
|
|
use Illuminate\Support\Facades\Gate;
|
2024-06-05 05:28:37 -07:00
|
|
|
|
|
|
|
|
|
|
|
class DeleteUserRequest extends FormRequest
|
|
|
|
{
|
2024-06-05 06:50:45 -07:00
|
|
|
|
|
|
|
protected $redirectRoute = 'users.index';
|
|
|
|
|
2024-06-05 05:28:37 -07:00
|
|
|
/**
|
|
|
|
* Determine if the user is authorized to make this request.
|
|
|
|
*/
|
|
|
|
public function authorize(): bool
|
|
|
|
{
|
2024-06-22 11:55:58 -07:00
|
|
|
return Gate::allows('delete', User::class);
|
2024-06-05 05:28:37 -07:00
|
|
|
}
|
|
|
|
|
2024-06-22 10:49:49 -07:00
|
|
|
public function prepareForValidation(): void
|
2024-06-05 05:28:37 -07:00
|
|
|
{
|
2024-06-22 10:49:49 -07:00
|
|
|
$user_to_delete = User::withTrashed()->find(request()->route('user'));
|
2024-06-05 05:28:37 -07:00
|
|
|
|
|
|
|
if ($user_to_delete) {
|
|
|
|
$this->merge([
|
|
|
|
'user' => request()->route('user'),
|
2024-07-04 12:49:22 -07:00
|
|
|
'admin_id' => auth()->id(),
|
2024-06-05 05:28:37 -07:00
|
|
|
'managed_users' => $user_to_delete->managesUsers()->count(),
|
|
|
|
'managed_locations' => $user_to_delete->managedLocations()->count(),
|
|
|
|
'assigned_assets' => $user_to_delete->assets()->count(),
|
|
|
|
'assigned_licenses' => $user_to_delete->licenses()->count(),
|
|
|
|
'assigned_accessories' => $user_to_delete->accessories()->count(),
|
2024-06-22 10:49:49 -07:00
|
|
|
'deleted_at' => $user_to_delete->deleted_at,
|
2024-06-05 05:28:37 -07:00
|
|
|
]);
|
|
|
|
}
|
2024-06-22 10:49:49 -07:00
|
|
|
}
|
2024-06-05 05:28:37 -07:00
|
|
|
|
2024-06-22 10:49:49 -07:00
|
|
|
/**
|
|
|
|
* Get the validation rules that apply to the request.
|
|
|
|
*
|
|
|
|
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
|
|
|
*/
|
|
|
|
public function rules(): array
|
|
|
|
{
|
2024-06-05 05:28:37 -07:00
|
|
|
return [
|
2024-07-04 12:49:22 -07:00
|
|
|
'user' => Rule::notIn([auth()->id()]),
|
2024-06-05 05:28:37 -07:00
|
|
|
'managed_users' => Rule::in([0]),
|
|
|
|
'managed_locations' => Rule::in([0]),
|
|
|
|
'assigned_assets' => Rule::in([0]),
|
|
|
|
'assigned_licenses' => Rule::in([0]),
|
|
|
|
'assigned_accessories' => Rule::in([0]),
|
2024-06-22 10:49:49 -07:00
|
|
|
'deleted_at' => Rule::in([null]),
|
2024-06-05 05:28:37 -07:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function messages(): array
|
|
|
|
{
|
|
|
|
|
2024-06-22 10:49:49 -07:00
|
|
|
$user_to_delete = User::withTrashed()->find(request()->route('user'));
|
|
|
|
$messages = [];
|
2024-06-05 05:28:37 -07:00
|
|
|
|
|
|
|
if ($user_to_delete) {
|
|
|
|
|
|
|
|
$messages = array_merge([
|
|
|
|
|
2024-06-22 11:42:34 -07:00
|
|
|
'user.exists' => trans('admin/users/message.user_not_found'),
|
2024-06-22 10:49:49 -07:00
|
|
|
|
2024-06-05 05:28:37 -07:00
|
|
|
// Cannot delete yourself
|
|
|
|
'user.not_in' => trans('admin/users/message.error.cannot_delete_yourself'),
|
|
|
|
|
|
|
|
// managed users is not 0
|
|
|
|
'managed_users.in' => trans_choice('admin/users/message.error.delete_has_users_var', $user_to_delete->managesUsers()->count(), ['count' => $user_to_delete->managesUsers()->count()]),
|
|
|
|
|
|
|
|
// managed locations is not 0
|
|
|
|
'managed_locations.in' => trans_choice('admin/users/message.error.delete_has_locations_var', $user_to_delete->managedLocations()->count(), ['count' => $user_to_delete->managedLocations()->count()]),
|
|
|
|
|
|
|
|
|
|
|
|
// assigned_assets is not 0
|
|
|
|
'assigned_assets.in' => trans_choice('admin/users/message.error.delete_has_assets_var', $user_to_delete->assets()->count(), ['count' => $user_to_delete->assets()->count()]),
|
|
|
|
|
|
|
|
// assigned licenses is not 0
|
|
|
|
'assigned_licenses.in' => trans_choice('admin/users/message.error.delete_has_licenses_var', $user_to_delete->licenses()->count(), ['count' => $user_to_delete->licenses()->count()]),
|
|
|
|
|
|
|
|
// assigned accessories is not 0
|
|
|
|
'assigned_accessories.in' => trans_choice('admin/users/message.error.delete_has_accessories_var', $user_to_delete->accessories()->count(), ['count' => $user_to_delete->accessories()->count()]),
|
|
|
|
|
2024-06-22 10:49:49 -07:00
|
|
|
'deleted_at.in' => trans('admin/users/message.user_deleted_warning'),
|
|
|
|
|
2024-06-05 05:28:37 -07:00
|
|
|
], $messages);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $messages;
|
|
|
|
}
|
|
|
|
}
|