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

104 lines
3.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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');
}
}