Skip to content

Commit

Permalink
Исправления ошибок, использование gates, страницы для ошибок
Browse files Browse the repository at this point in the history
  • Loading branch information
delaynore committed May 12, 2024
1 parent a3f86d2 commit fd90868
Show file tree
Hide file tree
Showing 15 changed files with 189 additions and 42 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@
1. [x] Атрибуты
2. [x] Теги
3. [x] Главная
4. [x] Error pages

5 changes: 3 additions & 2 deletions app/Http/Controllers/ConceptController.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ public function update(Request $request, Dictionary $dictionary, Concept $concep
'definition' => 'max:1000',
'parent' => [Rule::excludeIf(empty($request->input('parent'))), 'uuid', Rule::in($dictionary->concepts->pluck('id'))],
]);

if ($validated['parent']) {
if (in_array('parent', $validated)) {
$children = $concept->allChildren();
$searchId = $validated['parent'];
$filtered = array_filter($children, function ($child) use ($searchId) {
Expand All @@ -107,6 +107,7 @@ public function update(Request $request, Dictionary $dictionary, Concept $concep
}
}


$concept->updateOrFail([
'name' => $validated['name'],
'definition' => $validated['definition'],
Expand Down
32 changes: 24 additions & 8 deletions app/Http/Controllers/DictionaryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Storage;
Expand All @@ -24,6 +26,8 @@ class DictionaryController extends Controller
*/
public function index(): View
{
Gate::authorize('index-dictionary');

$dictionaries = Dictionary::where('fk_user_id', auth()->user()->id);
if (request('search')) {
$dictionaries = $dictionaries->where('name', 'ilike', '%' . request('search') . '%');
Expand All @@ -40,6 +44,8 @@ public function index(): View
*/
public function create()
{
Gate::authorize('create-dictionary');

return view('dictionary.create');
}

Expand All @@ -48,6 +54,8 @@ public function create()
*/
public function store(Request $request): RedirectResponse
{
Gate::authorize('store-dictionary');

$validated = $request->validate([
'name' => ['required', 'max:50', Rule::unique('dictionaries', 'name')->where(function ($query) {
return $query->where('fk_user_id', auth()->user()->id);
Expand Down Expand Up @@ -81,10 +89,9 @@ public function store(Request $request): RedirectResponse
/**
* Display the specified resource.
*/
public function show(string $id): View
public function show(Dictionary $dictionary): View
{

$dictionary = Dictionary::findOrFail($id);
Gate::authorize('show-dictionary', $dictionary);

if ($dictionary->visibility == Visibility::PRIVATE && $dictionary->fk_user_id !== auth()->user()->id) {
return abort(404);
Expand All @@ -102,11 +109,14 @@ public function show(string $id): View
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id): View
public function edit(string $dictionaryId): View
{
$dictionary = Dictionary::find($dictionaryId);
Gate::authorize('edit-dictionary', $dictionary);

return view(
'dictionary.edit',
['dictionary' => Dictionary::where('fk_user_id', '=', auth()->user()->id)->findOrFail($id)]
compact('dictionary')
);
}

Expand All @@ -115,7 +125,8 @@ public function edit(string $id): View
*/
public function update(Request $request, string $dictionaryId): RedirectResponse
{
$dictionary = Dictionary::where('fk_user_id', '=', auth()->user()->id)->findOrFail($dictionaryId);
$dictionary = Dictionary::find($dictionaryId);
Gate::authorize('update-dictionary', $dictionary);

$validated = $request->validate([
'name' => ['required', 'max:50', Rule::unique('dictionaries', 'name')->where(function ($query) {
Expand Down Expand Up @@ -156,9 +167,10 @@ public function update(Request $request, string $dictionaryId): RedirectResponse
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id): RedirectResponse
public function destroy(string $dictionaryId): RedirectResponse
{
$dictionary = Dictionary::where('fk_user_id', '=', auth()->user()->id)->findOrFail($id);
$dictionary = Dictionary::find($dictionaryId);
Gate::authorize('destroy-dictionary', $dictionary);

$dictionary->delete();

Expand All @@ -167,7 +179,11 @@ public function destroy(string $id): RedirectResponse

public function export(string $id)
{

$dictionary = Dictionary::where('fk_user_id', '=', auth()->user()->id)->findOrFail($id);

Gate::authorize('export-dictionary', $dictionary);

$concepts = $dictionary->concepts()->pluck('name');
if (count($concepts) == 0) {
return redirect()->back()->with('export.error', 'В словаре нет понятий');
Expand Down
31 changes: 31 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

namespace App\Providers;

use App\Enums\Visibility;
use App\Models\Dictionary;
use App\Models\User;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
Expand All @@ -20,5 +25,31 @@ public function register(): void
public function boot(): void
{
//
#region Dictionary gates
Gate::define('index-dictionary', function () {
return Auth::check();
});
Gate::define('destroy-dictionary', function (User $user, Dictionary $dictionary) {
return $user->id === $dictionary->fk_user_id;
});
Gate::define('edit-dictionary', function (User $user, Dictionary $dictionary) {
return $user->id === $dictionary->fk_user_id;
});
Gate::define('update-dictionary', function (User $user, Dictionary $dictionary) {
return $user->id === $dictionary->fk_user_id;
});
Gate::define('show-dictionary', function (User $user, Dictionary $dictionary) {
return $user->id === $dictionary->fk_user_id || ($dictionary->visibility === Visibility::PUBLIC && Auth::check());
});
Gate::define('export-dictionary', function (User $user, Dictionary $dictionary) {
return $user->id === $dictionary->fk_user_id || ($dictionary->visibility === Visibility::PUBLIC && Auth::check());
});
Gate::define('store-dictionary', function () {
return Auth::check();
});
Gate::define('create-dictionary', function () {
return Auth::check();
});
#endregion
}
}
20 changes: 20 additions & 0 deletions lang/ru/error.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

return [
'403' => [
'title' => 'Данное действие не доступно',
'back' => 'Назад',
'home' => 'На главную',
'message' => 'Доступ запрещен.',
],
'404' => [
'title' => 'Страница не найдена',
'back' => 'Назад',
'home' => 'На главную',
'message' => 'Извините, страница не найдена. Попробуйте вернуться назад или перейти на главную страницу',
],
'500' => [
'title' => 'Внутренняя ошибка сервера.',
'message' => 'Мы уже решаем эту проблему. Повторите попытку позже.',
]
];
7 changes: 7 additions & 0 deletions resources/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,10 @@
.el {
@apply text-primary-700 visited:text-purple-700 underline-offset-2 underline dark:text-blue-400 dark:visited:text-purple-400
}

.text-3 {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
overflow: hidden;
}
2 changes: 1 addition & 1 deletion resources/views/components/layout/main.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
@endif -->

<!-- Page Content -->
<main class="flex-grow container flex w-full">
<main class="flex-grow flex w-full">
@if (isset($slot))
{{ $slot }}
@endif
Expand Down
11 changes: 9 additions & 2 deletions resources/views/components/tree-view/tree-item.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
<h2 id="header-{{$concept->id}} " class="w-full">
<div data-dropdown-trigger="click" data-dropdown-toggle="dropdown{{$concept->id}}" data-dropdown-placement="right" data-dropdown-offset-skidding="100">
<button type="button" class="flex border-2 border-gray-100 hover:border-gray-300 items-center justify-between w-full px-2 py-1 font-medium text-gray-500 dark:border-gray-700 dark:text-gray-400 hover:bg-gray-100 dark:hover:bg-gray-800 rounded-sm" data-accordion-target="#body-{{$concept->id}}" aria-expanded="false" aria-controls="body-{{$concept->id}}">
<div class="">
@if ($concept->fk_parent_concept_id)
<span class="text-gray-300 -ml-3 mr-1">-</span>
@endif
<span class="overflow-hidden text-ellipsis">{{$concept->name}}</span>
</div>
<x-tree-view.icon />
</button>
</div>
Expand All @@ -17,7 +22,6 @@
<div class="ms-1 border-l-2">
<!-- Nested accordion -->
<div id="{{$unique}}" data-accordion="open">

@each('components.tree-view.tree-item', $children, 'concept')

</div>
Expand All @@ -27,7 +31,10 @@
@else

<h2 class="w-full">
<div data-dropdown-trigger="click" data-dropdown-toggle="dropdown{{$concept->id}}" data-dropdown-placement="right" data-dropdown-offset-skidding="100" class="flex border-2 border-gray-100 hover:border-gray-300 items-center justify-between w-full px-2 py-1 font-medium text-gray-500 focus:ring-4 focus:ring-gray-200 dark:focus:ring-gray-800 dark:border-gray-700 dark:text-gray-400 hover:bg-gray-100 dark:hover:bg-gray-800 rounded-sm">
<div data-dropdown-trigger="click" data-dropdown-toggle="dropdown{{$concept->id}}" data-dropdown-placement="right" data-dropdown-offset-skidding="100" class="justify-start flex border-2 border-gray-100 hover:border-gray-300 items-center w-full px-2 py-1 font-medium text-gray-500 focus:ring-4 focus:ring-gray-200 dark:focus:ring-gray-800 dark:border-gray-700 dark:text-gray-400 hover:bg-gray-100 dark:hover:bg-gray-800 rounded-sm">
@if ($concept->fk_parent_concept_id)
<span class="text-gray-300 -ml-3 mr-1">-</span>
@endif
{{$concept->name}}
</div>
</h2>
Expand Down
2 changes: 1 addition & 1 deletion resources/views/dictionary/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
</div>
<div class="sm:col-span-2">
<label for="description" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">{{ __('dictionary-page.create.description.label')}}</label>
<textarea id="description" name="description" value="{{old('description')}}" rows="4" class="block p-2.5 w-full min-h-12 text-sm text-gray-900 bg-gray-50 rounded-lg border border-gray-300 focus:ring-primary-500 focus:border-primary-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500" placeholder="{{__('dictionary-page.create.description.placeholder')}}"></textarea>
<textarea maxlength="500" id="description" name="description" rows="4" class="block p-2.5 w-full min-h-12 text-sm text-gray-900 bg-gray-50 rounded-lg border border-gray-300 focus:ring-primary-500 focus:border-primary-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500" placeholder="{{__('dictionary-page.create.description.placeholder')}}">{{ old('description') }}</textarea>
<x-input-error :messages="$errors->get('description')" class="mt-2" />
</div>
</div>
Expand Down
10 changes: 5 additions & 5 deletions resources/views/dictionary/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<h3 class="text-lg font-semibold text-gray-900 dark:text-white">
{{ __('dictionary-page.edit.title') }}
</h3>
<a href="{{url()->previous()}}" class="text-gray-400 bg-transparent hover:bg-gray-200 hover:text-gray-900 rounded-lg text-sm p-1.5 ml-auto inline-flex items-center dark:hover:bg-gray-600 dark:hover:text-white">
<a href="{{ route('my') }}" class="text-gray-400 bg-transparent hover:bg-gray-200 hover:text-gray-900 rounded-lg text-sm p-1.5 ml-auto inline-flex items-center dark:hover:bg-gray-600 dark:hover:text-white">
<svg aria-hidden="true" class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z" clip-rule="evenodd"></path>
</svg>
Expand All @@ -27,8 +27,8 @@
<div class="col-span-2">
<label for="visibility" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">{{__('dictionary-page.edit.visibility.label')}}</label>
<select id="visibility" name="visibility" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-primary-500 focus:border-primary-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500">
<option value="public" @selected($dictionary->visibility == App\Enums\Visibility::PUBLIC)>{{ __('dictionary-page.edit.visibility.public') }}</option>
<option value="private" @selected($dictionary->visibility == App\Enums\Visibility::PRIVATE)>{{ __('dictionary-page.create.visibility.private') }}</option>
<option value="public" @selected((old('visibility') ?? $dictionary->visibility->value) == App\Enums\Visibility::PUBLIC->value)>{{ __('dictionary-page.edit.visibility.public') }}</option>
<option value="private" @selected((old('visibility') ?? $dictionary->visibility->value) == App\Enums\Visibility::PRIVATE->value)>{{ __('dictionary-page.create.visibility.private') }}</option>
</select>
<x-input-error :messages="$errors->get('visibility')" class="mt-2" />
</div>
Expand All @@ -39,13 +39,13 @@
$tags = \App\Models\Tag::all();
@endphp
@foreach($tags as $tag)
<option value="{{ $tag->id }}" @selected(in_array($tag->id, $dictionary->tags->pluck('id')->toArray())) >{{ $tag->name }}</option>
<option value="{{ $tag->id }}" @selected(in_array($tag->id, old('tags') ?? $dictionary->tags->pluck('id')->toArray())) >{{ $tag->name }}</option>
@endforeach
</select>
</div>
<div class="sm:col-span-2">
<label for="description" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">{{ __('dictionary-page.edit.description.label')}}</label>
<textarea id="description" name="description" rows="4" class="block p-2.5 w-full min-h-12 text-sm text-gray-900 bg-gray-50 rounded-lg border border-gray-300 focus:ring-primary-500 focus:border-primary-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500" placeholder="{{__('dictionary-page.edit.description.placeholder')}}">{{$dictionary->description}}</textarea>
<textarea maxlength="500" id="description" name="description" rows="4" class="block p-2.5 w-full min-h-12 text-sm text-gray-900 bg-gray-50 rounded-lg border border-gray-300 focus:ring-primary-500 focus:border-primary-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500" placeholder="{{__('dictionary-page.edit.description.placeholder')}}">{{old('description') ?? $dictionary->description}}</textarea>
</div>
<x-input-error :messages="$errors->get('description')" class="mt-2" />
</div>
Expand Down
6 changes: 4 additions & 2 deletions resources/views/dictionary/index.blade.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@props(['dictionaries'])

<x-layout.main>
<x-slot:title>{{ __('dictionary-page.title') }}</x-slot:title>

Expand Down Expand Up @@ -66,7 +68,7 @@
@endempty
@isset($dictionaries)
<table class="w-full text-sm text-left text-gray-500 dark:text-gray-400">
<thead class="text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-700 dark:text-gray-400">
<thead class="text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-700 dark:text-gray-400 text-center">
<tr>
<th scope="col" class="px-4 py-3">{{ __('dictionary-page.table.headers.name') }}</th>
<th scope="col" class="px-4 py-3">{{ __('dictionary-page.table.headers.description') }}</th>
Expand All @@ -78,7 +80,7 @@
</tr>
</thead>
<tbody>
@each('dictionary.item', $dictionaries, 'dictionary')
@each('dictionary.item', $dictionaries->items(), 'dictionary')
</tbody>
</table>
<div class="my-2 mx-4">
Expand Down
Loading

0 comments on commit fd90868

Please sign in to comment.