2024-01-11 15:10:01 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2024-01-12 16:42:16 +03:00
|
|
|
use App\Http\Requests\UpdateUserRequest;
|
2024-01-11 15:10:01 +03:00
|
|
|
use App\Models\User;
|
2024-01-19 19:17:08 +03:00
|
|
|
use Illuminate\Auth\Events\PasswordReset;
|
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-25 10:51:39 +03:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->middleware('auth');
|
|
|
|
}
|
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();
|
|
|
|
return view('users.index', compact('users'));
|
|
|
|
}
|
|
|
|
|
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-01-12 16:42:16 +03:00
|
|
|
return view('users.create');
|
|
|
|
}
|
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-01-12 16:42:16 +03:00
|
|
|
return view('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
|
|
|
}
|