any fix resource User
This commit is contained in:
parent
00d64e149a
commit
9a8db88d6d
|
@ -4,16 +4,25 @@ namespace App\Http\Controllers;
|
||||||
|
|
||||||
use App\Http\Requests\UpdateUserRequest;
|
use App\Http\Requests\UpdateUserRequest;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
|
use Illuminate\Auth\Events\PasswordReset;
|
||||||
use Illuminate\Contracts\View\Factory;
|
use Illuminate\Contracts\View\Factory;
|
||||||
use Illuminate\Contracts\View\View;
|
use Illuminate\Contracts\View\View;
|
||||||
use Illuminate\Foundation\Application;
|
use Illuminate\Foundation\Application;
|
||||||
use Illuminate\Http\RedirectResponse;
|
use Illuminate\Http\RedirectResponse;
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
use Illuminate\Support\Facades\Hash;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
class UserController extends Controller
|
class UserController extends Controller
|
||||||
{
|
{
|
||||||
public function index(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
|
public function index(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
|
||||||
{
|
{
|
||||||
|
if (!Auth::user('admin')) {
|
||||||
|
abort(403, 'У вас нет прав доступа');
|
||||||
|
}
|
||||||
|
if (Auth::guest()) {
|
||||||
|
abort(403, 'Вы не авторизованы!');
|
||||||
|
}
|
||||||
$users = User::all();
|
$users = User::all();
|
||||||
return view('users.index', compact('users'));
|
return view('users.index', compact('users'));
|
||||||
}
|
}
|
||||||
|
@ -23,7 +32,9 @@ class UserController extends Controller
|
||||||
if (Auth::guest()) {
|
if (Auth::guest()) {
|
||||||
abort(403, 'Вы не авторизованы!');
|
abort(403, 'Вы не авторизованы!');
|
||||||
}
|
}
|
||||||
|
if (!Auth::user('admin')) {
|
||||||
|
abort(403, 'У вас нет прав доступа');
|
||||||
|
}
|
||||||
$validated = $request->validated();
|
$validated = $request->validated();
|
||||||
|
|
||||||
$user = new User();
|
$user = new User();
|
||||||
|
@ -37,6 +48,9 @@ class UserController extends Controller
|
||||||
|
|
||||||
public function create(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
|
public function create(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
|
||||||
{
|
{
|
||||||
|
if (!Auth::user('admin')) {
|
||||||
|
abort(403, 'У вас нет прав доступа');
|
||||||
|
}
|
||||||
if (Auth::guest()) {
|
if (Auth::guest()) {
|
||||||
abort(403, 'Вы не авторизованы!');
|
abort(403, 'Вы не авторизованы!');
|
||||||
}
|
}
|
||||||
|
@ -45,6 +59,9 @@ class UserController extends Controller
|
||||||
|
|
||||||
public function edit(User $user): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
|
public function edit(User $user): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
|
||||||
{
|
{
|
||||||
|
if (!Auth::user('admin')) {
|
||||||
|
abort(403, 'У вас нет прав доступа');
|
||||||
|
}
|
||||||
if (Auth::guest()) {
|
if (Auth::guest()) {
|
||||||
abort(403, 'Вы не авторизованы!');
|
abort(403, 'Вы не авторизованы!');
|
||||||
}
|
}
|
||||||
|
@ -53,13 +70,19 @@ class UserController extends Controller
|
||||||
|
|
||||||
public function update(UpdateUserRequest $request, User $user): RedirectResponse
|
public function update(UpdateUserRequest $request, User $user): RedirectResponse
|
||||||
{
|
{
|
||||||
|
if (!Auth::user('admin')) {
|
||||||
|
abort(403, 'У вас нет прав доступа');
|
||||||
|
}
|
||||||
if (Auth::guest()) {
|
if (Auth::guest()) {
|
||||||
abort(403, 'Вы не авторизованы!');
|
abort(403, 'Вы не авторизованы!');
|
||||||
}
|
}
|
||||||
|
|
||||||
$validated = $request->validated();
|
$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();
|
$user->save();
|
||||||
|
|
||||||
return redirect()->route('users.index');
|
return redirect()->route('users.index');
|
||||||
|
@ -67,6 +90,12 @@ class UserController extends Controller
|
||||||
|
|
||||||
public function destroy(User $user): RedirectResponse
|
public function destroy(User $user): RedirectResponse
|
||||||
{
|
{
|
||||||
|
if (!Auth::user('admin')) {
|
||||||
|
abort(403, 'У вас нет прав доступа');
|
||||||
|
}
|
||||||
|
if (Auth::guest()) {
|
||||||
|
abort(403, 'Вы не авторизованы!');
|
||||||
|
}
|
||||||
$user->delete();
|
$user->delete();
|
||||||
|
|
||||||
return redirect()->route('users.index');
|
return redirect()->route('users.index');
|
||||||
|
|
|
@ -25,7 +25,7 @@ class StoreUserRequest extends FormRequest
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'name' => 'required|unique:users,name|max:255',
|
'name' => 'required|unique:users,name|max:255',
|
||||||
'email' => 'email:rfc,dns',
|
'email' => 'email',
|
||||||
'password' => 'required'
|
'password' => 'required'
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,8 +24,8 @@ class UpdateUserRequest extends FormRequest
|
||||||
public function rules()
|
public function rules()
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'name' => 'required|unique:users,name|max:255',
|
'name' => 'required|max:255',
|
||||||
'email' => 'email:rfc,dns',
|
'email' => 'email',
|
||||||
'password' => 'required'
|
'password' => 'required'
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,7 +31,6 @@ class User extends Authenticatable
|
||||||
* @var array<int, string>
|
* @var array<int, string>
|
||||||
*/
|
*/
|
||||||
protected $hidden = [
|
protected $hidden = [
|
||||||
'password',
|
|
||||||
'remember_token',
|
'remember_token',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
|
@ -1,15 +1,17 @@
|
||||||
@auth()
|
@extends('layouts.admin-layout')
|
||||||
<div class="grid col-span-full">
|
@section('content')
|
||||||
<h1 class="max-w-2xl mb-4 text-4xl leading-none tracking-tight md:text-5xl xl:text-6xl dark:text-white"> создать
|
|
||||||
пользователя</h1>
|
|
||||||
|
|
||||||
{{ Form::open(['url' => route('users.store'), 'method' => 'POST', 'class' => 'w-50']) }}
|
@auth()
|
||||||
<div class="flex flex-col">
|
<div class="">
|
||||||
|
<h1 class=""> Создать администратора</h1>
|
||||||
|
|
||||||
|
{{ Form::open(['url' => route('users.store'), 'method' => 'POST', 'class' => '']) }}
|
||||||
|
<div class="col-4">
|
||||||
<div>
|
<div>
|
||||||
{{ Form::label('name', 'Логин') }}
|
{{ Form::label('name', 'Логин') }}
|
||||||
</div>
|
</div>
|
||||||
<div class="mt-2">
|
<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>
|
||||||
<div>
|
<div>
|
||||||
@if ($errors->any())
|
@if ($errors->any())
|
||||||
|
@ -21,7 +23,7 @@
|
||||||
{{ Form::label('email', 'электронная почта') }}
|
{{ Form::label('email', 'электронная почта') }}
|
||||||
</div>
|
</div>
|
||||||
<div class="mt-2">
|
<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>
|
||||||
<div>
|
<div>
|
||||||
@if ($errors->any())
|
@if ($errors->any())
|
||||||
|
@ -33,7 +35,7 @@
|
||||||
{{ Form::label('password', 'Пароль') }}
|
{{ Form::label('password', 'Пароль') }}
|
||||||
</div>
|
</div>
|
||||||
<div class="mt-2">
|
<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>
|
||||||
<div>
|
<div>
|
||||||
@if ($errors->any())
|
@if ($errors->any())
|
||||||
|
@ -42,9 +44,10 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="mt-4">
|
<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>
|
||||||
</div>
|
</div>
|
||||||
{{ Form::close() }}
|
{{ Form::close() }}
|
||||||
</div>
|
</div>
|
||||||
@endauth
|
@endauth
|
||||||
|
@endsection
|
||||||
|
|
|
@ -1,15 +1,18 @@
|
||||||
@auth()
|
@extends('layouts.admin-layout')
|
||||||
<div class="grid col-span-full">
|
@section('content')
|
||||||
<h1 class="max-w-2xl mb-4 text-4xl leading-none tracking-tight md:text-5xl xl:text-6xl dark:text-white"> Изменить
|
|
||||||
пользователя</h1>
|
|
||||||
|
|
||||||
{{ Form::open(['url' => route('users.update', $user), 'method' => 'PATCH', 'class' => 'w-50']) }}
|
@auth()
|
||||||
<div class="flex flex-col">
|
<div>
|
||||||
|
<h1 class=""> Изменить Администратора</h1>
|
||||||
|
<br>
|
||||||
|
<br>
|
||||||
|
{{ Form::open(['url' => route('users.update', $user), 'method' => 'PATCH', 'class' => '']) }}
|
||||||
|
<div class="col-4">
|
||||||
<div>
|
<div>
|
||||||
{{ Form::label('name', 'Логин') }}
|
{{ Form::label('name', 'Логин') }}
|
||||||
</div>
|
</div>
|
||||||
<div class="mt-2">
|
<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>
|
||||||
<div>
|
<div>
|
||||||
@if ($errors->any())
|
@if ($errors->any())
|
||||||
|
@ -21,7 +24,7 @@
|
||||||
{{ Form::label('email', 'электронная почта') }}
|
{{ Form::label('email', 'электронная почта') }}
|
||||||
</div>
|
</div>
|
||||||
<div class="mt-2">
|
<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>
|
||||||
<div>
|
<div>
|
||||||
@if ($errors->any())
|
@if ($errors->any())
|
||||||
|
@ -33,7 +36,7 @@
|
||||||
{{ Form::label('password', 'Пароль') }}
|
{{ Form::label('password', 'Пароль') }}
|
||||||
</div>
|
</div>
|
||||||
<div class="mt-2">
|
<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>
|
||||||
<div>
|
<div>
|
||||||
@if ($errors->any())
|
@if ($errors->any())
|
||||||
|
@ -42,9 +45,10 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="mt-4">
|
<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>
|
||||||
</div>
|
</div>
|
||||||
{{ Form::close() }}
|
{{ Form::close() }}
|
||||||
</div>
|
</div>
|
||||||
@endauth
|
@endauth
|
||||||
|
@endsection
|
||||||
|
|
|
@ -1,30 +1,29 @@
|
||||||
@extends('layouts.admin-layout')
|
@extends('layouts.admin-layout')
|
||||||
@section('content')
|
@section('content')
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<h2>список пользователей</h2>
|
<h2>список Администраторов</h2>
|
||||||
|
<br>
|
||||||
|
<a href="{{ route('users.create') }}" class="btn btn-primary">Создать администратора</a>
|
||||||
<br>
|
<br>
|
||||||
<br>
|
<br>
|
||||||
<a href="{{ route('users.create') }}"> создать пользователя</a>
|
<table class="table">
|
||||||
<br>
|
|
||||||
<br>
|
|
||||||
<table class="mt-4">
|
|
||||||
<thead class="border-b-2 border-solid border-black text-left" style="text-align: left">
|
<thead class="border-b-2 border-solid border-black text-left" style="text-align: left">
|
||||||
<tr>
|
<tr>
|
||||||
<th>id</th>
|
<th scope="col">id</th>
|
||||||
<td>name</td>
|
<th scope="col">name</th>
|
||||||
<td>email</td>
|
<th scope="col">email</th>
|
||||||
<td>действия</td>
|
<th scope="col">действия</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
@foreach($users as $user)
|
@foreach($users as $user)
|
||||||
<tr>
|
<tr>
|
||||||
<td>{{ $user->id }}</td>
|
<th scope="row">{{ $user->id }}</th>
|
||||||
<td>{{ $user->name }}</td>
|
<td>{{ $user->name }}</td>
|
||||||
<td>{{ $user->email }}</td>
|
<td>{{ $user->email }}</td>
|
||||||
<td><a href="{{ route("users.edit", $user) }}">редактировать</a></td>
|
<td><a href="{{ route("users.edit", $user) }}" class="btn btn-secondary">редактировать</a>
|
||||||
<td><a rel="nofollow" data-method="delete" data-confirm="Вы действительно хотите удалить?"
|
<a rel="nofollow" data-method="delete" data-confirm="Вы действительно хотите удалить?"
|
||||||
href="{{ route('users.destroy', $user) }}">
|
href="{{ route('users.destroy', $user) }}" class="btn btn-danger">
|
||||||
удалить
|
удалить
|
||||||
</a>
|
</a>
|
||||||
</td>
|
</td>
|
||||||
|
|
Loading…
Reference in New Issue