Roman_applicant-site/app/Http/Controllers/admin/UserController.php

78 lines
2.2 KiB
PHP
Raw Normal View History

2024-01-11 15:10:01 +03:00
<?php
2024-02-14 09:54:11 +03:00
namespace App\Http\Controllers\admin;
2024-01-11 15:10:01 +03:00
2024-02-14 09:54:11 +03:00
use App\Http\Controllers\Controller;
use App\Http\Requests\admin\UpdateUserRequest;
2024-01-11 15:10:01 +03:00
use App\Models\User;
2024-01-12 16:42:16 +03:00
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Foundation\Application;
use Illuminate\Http\RedirectResponse;
2024-01-11 15:10:01 +03:00
use Illuminate\Support\Facades\Auth;
2024-01-19 19:17:08 +03:00
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
2024-01-11 15:10:01 +03:00
class UserController extends Controller
{
2024-01-12 16:42:16 +03:00
public function index(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
2024-01-11 15:10:01 +03:00
{
2024-01-29 09:44:53 +03:00
abort_if(Auth::user()->name !== 'admin', 403);
2024-01-25 10:51:39 +03:00
2024-01-11 15:10:01 +03:00
$users = User::all();
2024-02-14 09:54:11 +03:00
return view('admin.users.index', compact('users'));
2024-01-11 15:10:01 +03:00
}
2024-01-12 16:42:16 +03:00
public function store(UpdateUserRequest $request): RedirectResponse
2024-01-11 15:10:01 +03:00
{
2024-01-29 09:44:53 +03:00
abort_if(Auth::user()->name !== 'admin', 403);
2024-01-25 10:51:39 +03:00
2024-01-11 15:10:01 +03:00
$validated = $request->validated();
2024-01-12 16:42:16 +03:00
$user = new User();
$user->name = $validated['name'];
$user->password = $validated['password'];
$user->email = $validated['email'];
$user->save();
2024-01-11 15:10:01 +03:00
2024-01-12 16:42:16 +03:00
return redirect()->route('users.index');
}
public function create(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
2024-01-29 09:44:53 +03:00
abort_if(Auth::user()->name !== 'admin', 403);
2024-01-25 10:51:39 +03:00
2024-02-14 09:54:11 +03:00
return view('admin.users.create');
2024-01-12 16:42:16 +03:00
}
2024-01-11 15:10:01 +03:00
2024-01-12 16:42:16 +03:00
public function edit(User $user): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
2024-01-29 09:44:53 +03:00
abort_if(Auth::user()->name !== 'admin', 403);
2024-01-25 10:51:39 +03:00
2024-02-14 09:54:11 +03:00
return view('admin.users.edit', compact('user'));
2024-01-11 15:10:01 +03:00
}
2024-01-12 09:56:06 +03:00
2024-01-12 16:42:16 +03:00
public function update(UpdateUserRequest $request, User $user): RedirectResponse
2024-01-12 09:56:06 +03:00
{
2024-01-29 09:44:53 +03:00
abort_if(Auth::user()->name !== 'admin', 403);
2024-01-12 16:42:16 +03:00
$validated = $request->validated();
2024-01-19 19:17:08 +03:00
$user->name = $validated['name'];
$user->password = Hash::make($validated['password']);
$user->email = $validated['email'];
$user->remember_token = Str::random(60);
2024-01-12 16:42:16 +03:00
$user->save();
return redirect()->route('users.index');
}
public function destroy(User $user): RedirectResponse
{
2024-01-29 09:44:53 +03:00
abort_if(Auth::user()->name !== 'admin', 403);
2024-01-12 16:42:16 +03:00
$user->delete();
return redirect()->route('users.index');
2024-01-12 09:56:06 +03:00
}
2024-01-11 15:10:01 +03:00
}