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

78 lines
2.2 KiB
PHP

<?php
namespace App\Http\Controllers\admin;
use App\Http\Controllers\Controller;
use App\Http\Requests\admin\UpdateUserRequest;
use App\Models\User;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Foundation\Application;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
class UserController extends Controller
{
public function index(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
abort_if(Auth::user()->name !== 'admin', 403);
$users = User::all();
return view('admin.users.index', compact('users'));
}
public function store(UpdateUserRequest $request): RedirectResponse
{
abort_if(Auth::user()->name !== 'admin', 403);
$validated = $request->validated();
$user = new User();
$user->name = $validated['name'];
$user->password = $validated['password'];
$user->email = $validated['email'];
$user->save();
return redirect()->route('users.index');
}
public function create(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
abort_if(Auth::user()->name !== 'admin', 403);
return view('admin.users.create');
}
public function edit(User $user): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
abort_if(Auth::user()->name !== 'admin', 403);
return view('admin.users.edit', compact('user'));
}
public function update(UpdateUserRequest $request, User $user): RedirectResponse
{
abort_if(Auth::user()->name !== 'admin', 403);
$validated = $request->validated();
$user->name = $validated['name'];
$user->password = Hash::make($validated['password']);
$user->email = $validated['email'];
$user->remember_token = Str::random(60);
$user->save();
return redirect()->route('users.index');
}
public function destroy(User $user): RedirectResponse
{
abort_if(Auth::user()->name !== 'admin', 403);
$user->delete();
return redirect()->route('users.index');
}
}