mirror of
https://github.com/snipe/snipe-it.git
synced 2025-01-25 20:51:30 -08:00
Created DeleteUser request
Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
parent
de7b8940fe
commit
5bd9ecb8df
89
app/Http/Requests/DeleteUserRequest.php
Normal file
89
app/Http/Requests/DeleteUserRequest.php
Normal file
|
@ -0,0 +1,89 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
|
||||
class DeleteUserRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
|
||||
$user_to_delete = User::find(request()->route('user'));
|
||||
|
||||
if ($user_to_delete) {
|
||||
$this->merge([
|
||||
'user' => request()->route('user'),
|
||||
'admin_id' => Auth::user()->id,
|
||||
'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(),
|
||||
]);
|
||||
}
|
||||
|
||||
return [
|
||||
'id' => ['exists:users,id'],
|
||||
'user' => Rule::notIn([Auth::user()->id]),
|
||||
'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]),
|
||||
];
|
||||
}
|
||||
|
||||
public function messages(): array
|
||||
{
|
||||
|
||||
$user_to_delete = User::find(request()->route('user'));
|
||||
$messages = ['id.exists' => trans('admin/users/message.user_not_found')];
|
||||
|
||||
if ($user_to_delete) {
|
||||
|
||||
$messages = array_merge([
|
||||
|
||||
// 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()]),
|
||||
|
||||
], $messages);
|
||||
}
|
||||
|
||||
return $messages;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue