diff --git a/app/Http/Controllers/Admin/GroupController.php b/app/Http/Controllers/Admin/GroupController.php new file mode 100644 index 0000000..34bcf62 --- /dev/null +++ b/app/Http/Controllers/Admin/GroupController.php @@ -0,0 +1,88 @@ +middleware('auth'); + } + + public function index(Organization $organization) + { + Gate::authorize('view', $organization); + + $groups = $organization->groups()->withCount('users')->orderBy('created_at', 'desc')->paginate(20); + + return view('admin.groups.index', compact('organization', 'groups')); + } + + public function create(Organization $organization) + { + Gate::authorize('create', Group::class); + + return view('admin.groups.create', compact('organization')); + } + + public function store(Request $request, Organization $organization) + { + Gate::authorize('create', Group::class); + + $validated = $request->validate([ + 'name' => 'required|string|max:255', + 'description' => 'nullable|string', + ]); + + $validated['is_active'] = $request->boolean('is_active'); + + $organization->groups()->create($validated); + + return redirect()->route('admin.organizations.show', $organization) + ->with('success', 'Группа успешно создана.'); + } + + public function edit(Organization $organization, Group $group) + { + Gate::authorize('update', $group); + + return view('admin.groups.edit', compact('organization', 'group')); + } + + public function update(Request $request, Organization $organization, Group $group) + { + Gate::authorize('update', $group); + + $validated = $request->validate([ + 'name' => 'required|string|max:255', + 'description' => 'nullable|string', + ]); + + $validated['is_active'] = $request->boolean('is_active'); + + $group->update($validated); + + return redirect()->route('admin.organizations.show', $organization) + ->with('success', 'Группа успешно обновлена.'); + } + + public function destroy(Organization $organization, Group $group) + { + Gate::authorize('delete', $group); + + if ($group->users()->count() > 0) { + return back()->with('error', 'Невозможно удалить группу с пользователями.'); + } + + $group->delete(); + + return redirect()->route('admin.organizations.show', $organization) + ->with('success', 'Группа успешно удалена.'); + } +} diff --git a/app/Policies/GroupPolicy.php b/app/Policies/GroupPolicy.php new file mode 100755 index 0000000..4baf7ef --- /dev/null +++ b/app/Policies/GroupPolicy.php @@ -0,0 +1,65 @@ +hasRole(['Administrator', 'Manager', 'Curator']); + } + + /** + * Determine whether the user can view the model. + */ + public function view(User $user, Group $group): bool + { + return $user->hasRole(['Administrator', 'Manager', 'Curator']); + } + + /** + * Determine whether the user can create models. + */ + public function create(User $user): bool + { + return $user->hasRole(['Administrator', 'Manager', 'Curator']); + } + + /** + * Determine whether the user can update the model. + */ + public function update(User $user, Group $group): bool + { + return $user->hasRole(['Administrator', 'Manager', 'Curator']); + } + + /** + * Determine whether the user can delete the model. + */ + public function delete(User $user, Group $group): bool + { + return $user->hasRole(['Administrator', 'Manager']); + } + + /** + * Determine whether the user can restore the model. + */ + public function restore(User $user, Group $group): bool + { + return $user->hasRole(['Administrator', 'Manager']); + } + + /** + * Determine whether the user can permanently delete the model. + */ + public function forceDelete(User $user, Group $group): bool + { + return $user->hasRole(['Administrator']); + } +} diff --git a/app/Providers/AuthServiceProvider.php b/app/Providers/AuthServiceProvider.php index c5e5d65..ca04072 100755 --- a/app/Providers/AuthServiceProvider.php +++ b/app/Providers/AuthServiceProvider.php @@ -2,7 +2,9 @@ namespace App\Providers; +use App\Models\Group; use App\Models\Organization; +use App\Policies\GroupPolicy; use App\Policies\OrganizationPolicy; use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; @@ -15,6 +17,7 @@ class AuthServiceProvider extends ServiceProvider */ protected $policies = [ Organization::class => OrganizationPolicy::class, + Group::class => GroupPolicy::class, ]; /** diff --git a/resources/views/admin/groups/create.blade.php b/resources/views/admin/groups/create.blade.php new file mode 100644 index 0000000..65464fd --- /dev/null +++ b/resources/views/admin/groups/create.blade.php @@ -0,0 +1,102 @@ +@extends('layouts.app') + +@section('title', 'Добавить группу') + +@section('content') +
+
+ + +
+
+

Добавить группу

+ + Назад + +
+ + @if ($errors->any()) +
+
    + @foreach ($errors->all() as $error) +
  • {{ $error }}
  • + @endforeach +
+
+ @endif + +
+ @csrf +
+
+
+
+
Основная информация
+ +
+ + + @error('name') +
{{ $message }}
+ @enderror +
+ +
+ + + @error('description') +
{{ $message }}
+ @enderror +
+ +
+ + +
+
+
+
+ +
+
+
+
Организация
+

{{ $organization->name }}

+ @if($organization->inn) +

ИНН: {{ $organization->inn }}

+ @endif +
+
+
+
+ +
+ + + Отмена + +
+
+
+
+
+@endsection diff --git a/resources/views/admin/groups/edit.blade.php b/resources/views/admin/groups/edit.blade.php new file mode 100644 index 0000000..08ac2b2 --- /dev/null +++ b/resources/views/admin/groups/edit.blade.php @@ -0,0 +1,108 @@ +@extends('layouts.app') + +@section('title', 'Редактировать группу') + +@section('content') +
+
+ + +
+
+

Редактировать группу

+ + Назад + +
+ + @if ($errors->any()) +
+
    + @foreach ($errors->all() as $error) +
  • {{ $error }}
  • + @endforeach +
+
+ @endif + +
+ @csrf + @method('PUT') +
+
+
+
+
Основная информация
+ +
+ + + @error('name') +
{{ $message }}
+ @enderror +
+ +
+ + + @error('description') +
{{ $message }}
+ @enderror +
+ +
+ is_active) ? 'checked' : '' }}> + +
+
+
+
+ +
+
+
+
Организация
+

{{ $organization->name }}

+ @if($organization->inn) +

ИНН: {{ $organization->inn }}

+ @endif +
+

+ Создана: {{ $group->created_at->format('d.m.Y') }}
+ Обновлена: {{ $group->updated_at->format('d.m.Y') }} +

+
+
+
+
+ +
+ + + Отмена + +
+
+
+
+
+@endsection diff --git a/resources/views/admin/groups/index.blade.php b/resources/views/admin/groups/index.blade.php new file mode 100644 index 0000000..868cd23 --- /dev/null +++ b/resources/views/admin/groups/index.blade.php @@ -0,0 +1,123 @@ +@extends('layouts.app') + +@section('title', 'Группы организации') + +@section('content') +
+
+ + +
+
+

Группы организации

+
+ + К организации + + @can('create', App\Models\Group::class) + + Добавить группу + + @endcan +
+
+ + @if(session('success')) + + @endif + + @if(session('error')) + + @endif + +
+
+
+ + + + + + + + + + + + + @forelse($groups as $group) + + + + + + + + + @empty + + + + @endforelse + +
IDНазваниеОписаниеПользователейСтатусДействия
{{ $group->id }}{{ $group->name }}{{ Str::limit($group->description, 50) ?? '—' }}{{ $group->users_count }} + @if($group->is_active) + Активна + @else + Не активна + @endif + +
+ @can('update', $group) + + + + @endcan + @can('delete', $group) +
+ @csrf + @method('DELETE') + +
+ @endcan +
+
+ +

В этой организации пока нет групп

+
+
+
+
+ + @if($groups->hasPages()) +
+ {{ $groups->links() }} +
+ @endif +
+
+
+@endsection diff --git a/resources/views/admin/organizations/show.blade.php b/resources/views/admin/organizations/show.blade.php index e7ad68f..a02539b 100644 --- a/resources/views/admin/organizations/show.blade.php +++ b/resources/views/admin/organizations/show.blade.php @@ -145,18 +145,35 @@
-
+
Группы
+ @can('create', App\Models\Group::class) + + + + @endcan
@if($organization->groups->count() > 0)
    @foreach($organization->groups->take(5) as $group) -
  • - {{ $group->name }} - @if($group->description) -
    {{ Str::limit($group->description, 50) }} - @endif +
  • +
    + {{ $group->name }} + @if($group->description) +
    {{ Str::limit($group->description, 50) }} + @endif +
    +
    + + + + @can('update', $group) + + + + @endcan +
  • @endforeach
diff --git a/routes/web.php b/routes/web.php index c58f4b0..081d7da 100644 --- a/routes/web.php +++ b/routes/web.php @@ -3,6 +3,7 @@ use App\Http\Controllers\Auth\LoginController; use App\Http\Controllers\Auth\RegisterController; use App\Http\Controllers\Admin\OrganizationController; +use App\Http\Controllers\Admin\GroupController; use App\Http\Controllers\DashboardController; use Illuminate\Support\Facades\Route; @@ -31,8 +32,9 @@ Route::middleware('auth')->group(function () { Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard'); - // Администрирование организаций + // Администрирование Route::prefix('admin')->name('admin.')->group(function () { Route::resource('organizations', OrganizationController::class); + Route::resource('organizations.groups', GroupController::class)->shallow(); }); });