forked from aslan/applicant-site
104 lines
3.1 KiB
PHP
104 lines
3.1 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers;
|
||
|
||
use App\Http\Requests\UpdateUserRequest;
|
||
use App\Models\User;
|
||
use Illuminate\Auth\Events\PasswordReset;
|
||
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
|
||
{
|
||
if (!Auth::user('admin')) {
|
||
abort(403, 'У вас нет прав доступа');
|
||
}
|
||
if (Auth::guest()) {
|
||
abort(403, 'Вы не авторизованы!');
|
||
}
|
||
$users = User::all();
|
||
return view('users.index', compact('users'));
|
||
}
|
||
|
||
public function store(UpdateUserRequest $request): RedirectResponse
|
||
{
|
||
if (Auth::guest()) {
|
||
abort(403, 'Вы не авторизованы!');
|
||
}
|
||
if (!Auth::user('admin')) {
|
||
abort(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
|
||
{
|
||
if (!Auth::user('admin')) {
|
||
abort(403, 'У вас нет прав доступа');
|
||
}
|
||
if (Auth::guest()) {
|
||
abort(403, 'Вы не авторизованы!');
|
||
}
|
||
return view('users.create');
|
||
}
|
||
|
||
public function edit(User $user): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
|
||
{
|
||
if (!Auth::user('admin')) {
|
||
abort(403, 'У вас нет прав доступа');
|
||
}
|
||
if (Auth::guest()) {
|
||
abort(403, 'Вы не авторизованы!');
|
||
}
|
||
return view('users.edit', compact('user'));
|
||
}
|
||
|
||
public function update(UpdateUserRequest $request, User $user): RedirectResponse
|
||
{
|
||
if (!Auth::user('admin')) {
|
||
abort(403, 'У вас нет прав доступа');
|
||
}
|
||
if (Auth::guest()) {
|
||
abort(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
|
||
{
|
||
if (!Auth::user('admin')) {
|
||
abort(403, 'У вас нет прав доступа');
|
||
}
|
||
if (Auth::guest()) {
|
||
abort(403, 'Вы не авторизованы!');
|
||
}
|
||
$user->delete();
|
||
|
||
return redirect()->route('users.index');
|
||
}
|
||
}
|