any fix resource User

This commit is contained in:
aslan 2024-01-19 19:17:08 +03:00
parent 00d64e149a
commit 9a8db88d6d
7 changed files with 73 additions and 39 deletions

View File

@ -4,16 +4,25 @@ 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'));
}
@ -23,7 +32,9 @@ class UserController extends Controller
if (Auth::guest()) {
abort(403, 'Вы не авторизованы!');
}
if (!Auth::user('admin')) {
abort(403, 'У вас нет прав доступа');
}
$validated = $request->validated();
$user = new User();
@ -37,6 +48,9 @@ class UserController extends Controller
public function create(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
if (!Auth::user('admin')) {
abort(403, 'У вас нет прав доступа');
}
if (Auth::guest()) {
abort(403, 'Вы не авторизованы!');
}
@ -45,6 +59,9 @@ class UserController extends Controller
public function edit(User $user): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
if (!Auth::user('admin')) {
abort(403, 'У вас нет прав доступа');
}
if (Auth::guest()) {
abort(403, 'Вы не авторизованы!');
}
@ -53,13 +70,19 @@ class UserController extends Controller
public function update(UpdateUserRequest $request, User $user): RedirectResponse
{
if (!Auth::user('admin')) {
abort(403, 'У вас нет прав доступа');
}
if (Auth::guest()) {
abort(403, 'Вы не авторизованы!');
}
$validated = $request->validated();
$user->fill($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');
@ -67,6 +90,12 @@ class UserController extends Controller
public function destroy(User $user): RedirectResponse
{
if (!Auth::user('admin')) {
abort(403, 'У вас нет прав доступа');
}
if (Auth::guest()) {
abort(403, 'Вы не авторизованы!');
}
$user->delete();
return redirect()->route('users.index');

View File

@ -25,7 +25,7 @@ class StoreUserRequest extends FormRequest
{
return [
'name' => 'required|unique:users,name|max:255',
'email' => 'email:rfc,dns',
'email' => 'email',
'password' => 'required'
];
}

View File

@ -24,8 +24,8 @@ class UpdateUserRequest extends FormRequest
public function rules()
{
return [
'name' => 'required|unique:users,name|max:255',
'email' => 'email:rfc,dns',
'name' => 'required|max:255',
'email' => 'email',
'password' => 'required'
];
}

View File

@ -31,7 +31,6 @@ class User extends Authenticatable
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];

View File

@ -1,15 +1,17 @@
@auth()
<div class="grid col-span-full">
<h1 class="max-w-2xl mb-4 text-4xl leading-none tracking-tight md:text-5xl xl:text-6xl dark:text-white"> создать
пользователя</h1>
@extends('layouts.admin-layout')
@section('content')
{{ Form::open(['url' => route('users.store'), 'method' => 'POST', 'class' => 'w-50']) }}
<div class="flex flex-col">
@auth()
<div class="">
<h1 class=""> Создать администратора</h1>
{{ Form::open(['url' => route('users.store'), 'method' => 'POST', 'class' => '']) }}
<div class="col-4">
<div>
{{ Form::label('name', 'Логин') }}
</div>
<div class="mt-2">
{{ Form::text('name', '', ['class' => 'form-control rounded border-gray-300 w-1/3']) }}
{{ Form::text('name', '', ['class' => 'form-control']) }}
</div>
<div>
@if ($errors->any())
@ -21,7 +23,7 @@
{{ Form::label('email', 'электронная почта') }}
</div>
<div class="mt-2">
{{ Form::text('email', '', ['class' => 'form-control rounded border-gray-300 w-1/3']) }}
{{ Form::text('email', '', ['class' => 'form-control']) }}
</div>
<div>
@if ($errors->any())
@ -33,7 +35,7 @@
{{ Form::label('password', 'Пароль') }}
</div>
<div class="mt-2">
{{ Form::text('password', '', ['class' => 'form-control rounded border-gray-300 w-1/3']) }}
{{ Form::text('password', '', ['class' => 'form-control']) }}
</div>
<div>
@if ($errors->any())
@ -42,9 +44,10 @@
</div>
<div class="mt-4">
{{ Form::submit('создать', ['class' => 'bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded']) }}
{{ Form::submit('создать', ['class' => 'btn btn-primary']) }}
</div>
</div>
{{ Form::close() }}
</div>
@endauth
@endsection

View File

@ -1,15 +1,18 @@
@auth()
<div class="grid col-span-full">
<h1 class="max-w-2xl mb-4 text-4xl leading-none tracking-tight md:text-5xl xl:text-6xl dark:text-white"> Изменить
пользователя</h1>
@extends('layouts.admin-layout')
@section('content')
{{ Form::open(['url' => route('users.update', $user), 'method' => 'PATCH', 'class' => 'w-50']) }}
<div class="flex flex-col">
@auth()
<div>
<h1 class=""> Изменить Администратора</h1>
<br>
<br>
{{ Form::open(['url' => route('users.update', $user), 'method' => 'PATCH', 'class' => '']) }}
<div class="col-4">
<div>
{{ Form::label('name', 'Логин') }}
</div>
<div class="mt-2">
{{ Form::text('name', $user->name, ['class' => 'form-control rounded border-gray-300 w-1/3']) }}
{{ Form::text('name', $user->name, ['class' => 'form-control']) }}
</div>
<div>
@if ($errors->any())
@ -21,7 +24,7 @@
{{ Form::label('email', 'электронная почта') }}
</div>
<div class="mt-2">
{{ Form::text('email', $user->email, ['class' => 'form-control rounded border-gray-300 w-1/3']) }}
{{ Form::text('email', $user->email, ['class' => 'form-control']) }}
</div>
<div>
@if ($errors->any())
@ -33,7 +36,7 @@
{{ Form::label('password', 'Пароль') }}
</div>
<div class="mt-2">
{{ Form::text('password', '', ['class' => 'form-control rounded border-gray-300 w-1/3']) }}
{{ Form::text('password', '', ['class' => 'form-control']) }}
</div>
<div>
@if ($errors->any())
@ -42,9 +45,10 @@
</div>
<div class="mt-4">
{{ Form::submit('изменить', ['class' => 'bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded']) }}
{{ Form::submit('изменить', ['class' => 'btn btn-primary']) }}
</div>
</div>
{{ Form::close() }}
</div>
@endauth
@endsection

View File

@ -1,30 +1,29 @@
@extends('layouts.admin-layout')
@section('content')
<div class="container">
<h2>список пользователей</h2>
<h2>список Администраторов</h2>
<br>
<a href="{{ route('users.create') }}" class="btn btn-primary">Создать администратора</a>
<br>
<br>
<a href="{{ route('users.create') }}"> создать пользователя</a>
<br>
<br>
<table class="mt-4">
<table class="table">
<thead class="border-b-2 border-solid border-black text-left" style="text-align: left">
<tr>
<th>id</th>
<td>name</td>
<td>email</td>
<td>действия</td>
<th scope="col">id</th>
<th scope="col">name</th>
<th scope="col">email</th>
<th scope="col">действия</th>
</tr>
</thead>
<tbody>
@foreach($users as $user)
<tr>
<td>{{ $user->id }}</td>
<th scope="row">{{ $user->id }}</th>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td><a href="{{ route("users.edit", $user) }}">редактировать</a></td>
<td><a rel="nofollow" data-method="delete" data-confirm="Вы действительно хотите удалить?"
href="{{ route('users.destroy', $user) }}">
<td><a href="{{ route("users.edit", $user) }}" class="btn btn-secondary">редактировать</a>
<a rel="nofollow" data-method="delete" data-confirm="Вы действительно хотите удалить?"
href="{{ route('users.destroy', $user) }}" class="btn btn-danger">
удалить
</a>
</td>