Добавить группу
+ + Назад + +-
+ @foreach ($errors->all() as $error)
+
- {{ $error }} + @endforeach +
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') +
| 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)
+
+ @endcan
+
+ |
+
|
+
+ В этой организации пока нет групп + |
+ |||||