diff --git a/app/Http/Controllers/Api/UsersController.php b/app/Http/Controllers/Api/UsersController.php index 19cf1c3bdf..c41afe0a3c 100644 --- a/app/Http/Controllers/Api/UsersController.php +++ b/app/Http/Controllers/Api/UsersController.php @@ -13,6 +13,8 @@ use App\Http\Transformers\SelectlistTransformer; use App\Http\Transformers\UsersTransformer; use App\Models\Actionlog; use App\Models\Asset; +use App\Models\Accessory; +use App\Models\Consumable; use App\Models\License; use App\Models\User; use App\Notifications\CurrentInventory; @@ -31,7 +33,7 @@ class UsersController extends Controller * @author [A. Gianotto] [] * @since [v4.0] * - * @return \Illuminate\Http\Response + * @return array */ public function index(Request $request) { @@ -359,7 +361,7 @@ class UsersController extends Controller * @author [A. Gianotto] [] * @since [v4.0] * @param \Illuminate\Http\Request $request - * @return \Illuminate\Http\Response + * @return array | \Illuminate\Http\JsonResponse */ public function store(SaveUserRequest $request) { @@ -406,7 +408,7 @@ class UsersController extends Controller * * @author [A. Gianotto] [] * @param int $id - * @return \Illuminate\Http\Response + * @return array | \Illuminate\Http\JsonResponse */ public function show($id) { @@ -429,7 +431,7 @@ class UsersController extends Controller * @since [v4.0] * @param \Illuminate\Http\Request $request * @param int $id - * @return \Illuminate\Http\Response + * @return \Illuminate\Http\JsonResponse */ public function update(SaveUserRequest $request, $id) { @@ -514,7 +516,7 @@ class UsersController extends Controller * @author [A. Gianotto] [] * @since [v4.0] * @param int $id - * @return \Illuminate\Http\Response + * @return \Illuminate\Http\JsonResponse */ public function destroy(DeleteUserRequest $request, $id) { @@ -543,7 +545,7 @@ class UsersController extends Controller } - return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/users/message.user_not_found', compact('id')))); + return response()->json(Helper::formatStandardApiResponse('error', null, 'Whoops.'.trans('admin/users/message.user_not_found'))); } @@ -553,7 +555,7 @@ class UsersController extends Controller * @author [A. Gianotto] [] * @since [v3.0] * @param $userId - * @return string JSON + * @return array | \Illuminate\Http\JsonResponse */ public function assets(Request $request, $id) { @@ -626,7 +628,7 @@ class UsersController extends Controller * @author [A. Gianotto] [] * @since [v3.0] * @param $userId - * @return string JSON + * @return array | \Illuminate\Http\JsonResponse */ public function consumables(Request $request, $id) { @@ -644,7 +646,7 @@ class UsersController extends Controller * @author [A. Gianotto] [] * @since [v4.6.14] * @param $userId - * @return string JSON + * @return array */ public function accessories($id) { @@ -663,7 +665,7 @@ class UsersController extends Controller * @author [N. Mathar] [] * @since [v5.0] * @param $userId - * @return string JSON + * @return array | \Illuminate\Http\JsonResponse */ public function licenses($id) { @@ -726,7 +728,7 @@ class UsersController extends Controller * @author [Juan Font] [] * @since [v4.4.2] * @param \Illuminate\Http\Request $request - * @return \Illuminate\Http\Response + * @return array */ public function getCurrentUserInfo(Request $request) { @@ -739,7 +741,7 @@ class UsersController extends Controller * @author [E. Taylor] [] * @param int $userId * @since [v6.0.0] - * @return JsonResponse + * @return \Illuminate\Http\JsonResponse */ public function restore($userId = null) { diff --git a/app/Http/Requests/DeleteUserRequest.php b/app/Http/Requests/DeleteUserRequest.php index 8136bd68e2..d70c7e0f56 100644 --- a/app/Http/Requests/DeleteUserRequest.php +++ b/app/Http/Requests/DeleteUserRequest.php @@ -6,7 +6,7 @@ use Illuminate\Foundation\Http\FormRequest; use Illuminate\Validation\Rule; use Illuminate\Support\Facades\Auth; use App\Models\User; -use Illuminate\Http\Request; +use Illuminate\Support\Facades\Gate; class DeleteUserRequest extends FormRequest @@ -19,18 +19,13 @@ class DeleteUserRequest extends FormRequest */ public function authorize(): bool { - return true; + return Gate::allows('delete', new User); + //return true; } - /** - * Get the validation rules that apply to the request. - * - * @return array|string> - */ - public function rules(): array + public function prepareForValidation(): void { - - $user_to_delete = User::find(request()->route('user')); + $user_to_delete = User::withTrashed()->find(request()->route('user')); if ($user_to_delete) { $this->merge([ @@ -41,30 +36,41 @@ class DeleteUserRequest extends FormRequest 'assigned_assets' => $user_to_delete->assets()->count(), 'assigned_licenses' => $user_to_delete->licenses()->count(), 'assigned_accessories' => $user_to_delete->accessories()->count(), + 'deleted_at' => $user_to_delete->deleted_at, ]); } + } + /** + * Get the validation rules that apply to the request. + * + * @return array|string> + */ + public function rules(): array + { return [ - 'id' => ['exists:users,id'], - 'user' => Rule::notIn([Auth::user()->id]), + 'user' => ['in:null|not_in: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]), + 'deleted_at' => Rule::in([null]), ]; } public function messages(): array { - $user_to_delete = User::find(request()->route('user')); - $messages = ['id.exists' => trans('admin/users/message.user_not_found')]; + $user_to_delete = User::withTrashed()->find(request()->route('user')); + $messages = []; if ($user_to_delete) { $messages = array_merge([ + 'user.exists' => 'Boop. '.trans('admin/users/message.user_not_found'), + // Cannot delete yourself 'user.not_in' => trans('admin/users/message.error.cannot_delete_yourself'), @@ -84,6 +90,8 @@ class DeleteUserRequest extends FormRequest // 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()]), + 'deleted_at.in' => trans('admin/users/message.user_deleted_warning'), + ], $messages); }