CRUD групп - Этап 2
✅ GroupController (index, create, store, edit, update, destroy) ✅ GroupPolicy (viewAny, view, create, update, delete) ✅ Маршруты: /admin/organizations/{organization}/groups (shallow resource) ✅ Blade-шаблоны: - admin/groups/index.blade.php (список групп организации) - admin/groups/create.blade.php (форма создания) - admin/groups/edit.blade.php (форма редактирования) ✅ Обновлён admin/organizations/show.blade.php (управление группами) ✅ Обновлён AuthServiceProvider (регистрация GroupPolicy) Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
parent
32fed5d4b6
commit
4f5a615860
|
|
@ -0,0 +1,88 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Group;
|
||||
use App\Models\Organization;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
|
||||
class GroupController extends Controller
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$this->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', 'Группа успешно удалена.');
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
<?php
|
||||
|
||||
namespace App\Policies;
|
||||
|
||||
use App\Models\Group;
|
||||
use App\Models\User;
|
||||
|
||||
class GroupPolicy
|
||||
{
|
||||
/**
|
||||
* Determine whether the user can view any models.
|
||||
*/
|
||||
public function viewAny(User $user): bool
|
||||
{
|
||||
return $user->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']);
|
||||
}
|
||||
}
|
||||
|
|
@ -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,
|
||||
];
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -0,0 +1,102 @@
|
|||
@extends('layouts.app')
|
||||
|
||||
@section('title', 'Добавить группу')
|
||||
|
||||
@section('content')
|
||||
<div class="container-fluid">
|
||||
<div class="row">
|
||||
<nav class="col-md-3 col-lg-2 d-md-block sidebar collapse">
|
||||
<div class="position-sticky pt-3">
|
||||
<ul class="nav flex-column">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="{{ route('dashboard') }}">
|
||||
<i class="bi bi-speedometer2"></i> Панель управления
|
||||
</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="{{ route('admin.organizations.index') }}">
|
||||
<i class="bi bi-building"></i> Организации
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<main class="col-md-9 ms-sm-auto col-lg-10 px-md-4 main-content">
|
||||
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
|
||||
<h1 class="h2">Добавить группу</h1>
|
||||
<a href="{{ route('admin.organizations.show', $organization) }}" class="btn btn-secondary btn-sm">
|
||||
<i class="bi bi-arrow-left"></i> Назад
|
||||
</a>
|
||||
</div>
|
||||
|
||||
@if ($errors->any())
|
||||
<div class="alert alert-danger">
|
||||
<ul class="mb-0">
|
||||
@foreach ($errors->all() as $error)
|
||||
<li>{{ $error }}</li>
|
||||
@endforeach
|
||||
</ul>
|
||||
</div>
|
||||
@endif
|
||||
|
||||
<form action="{{ route('admin.groups.store', $organization) }}" method="POST">
|
||||
@csrf
|
||||
<div class="row">
|
||||
<div class="col-md-8">
|
||||
<div class="card shadow-sm mb-4">
|
||||
<div class="card-body">
|
||||
<h5 class="card-title mb-3">Основная информация</h5>
|
||||
|
||||
<div class="mb-3">
|
||||
<label for="name" class="form-label">Название группы <span class="text-danger">*</span></label>
|
||||
<input type="text" class="form-control @error('name') is-invalid @enderror"
|
||||
id="name" name="name" value="{{ old('name') }}" required autofocus>
|
||||
@error('name')
|
||||
<div class="invalid-feedback">{{ $message }}</div>
|
||||
@enderror
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label for="description" class="form-label">Описание</label>
|
||||
<textarea class="form-control @error('description') is-invalid @enderror"
|
||||
id="description" name="description" rows="3">{{ old('description') }}</textarea>
|
||||
@error('description')
|
||||
<div class="invalid-feedback">{{ $message }}</div>
|
||||
@enderror
|
||||
</div>
|
||||
|
||||
<div class="mb-3 form-check">
|
||||
<input type="checkbox" class="form-check-input" id="is_active" name="is_active" value="1" {{ old('is_active', true) ? 'checked' : '' }}>
|
||||
<label class="form-check-label" for="is_active">Активна</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="col-md-4">
|
||||
<div class="card shadow-sm mb-4">
|
||||
<div class="card-body">
|
||||
<h5 class="card-title mb-3">Организация</h5>
|
||||
<p class="text-muted mb-0">{{ $organization->name }}</p>
|
||||
@if($organization->inn)
|
||||
<p class="text-muted small">ИНН: {{ $organization->inn }}</p>
|
||||
@endif
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<button type="submit" class="btn btn-primary">
|
||||
<i class="bi bi-check-lg"></i> Создать группу
|
||||
</button>
|
||||
<a href="{{ route('admin.organizations.show', $organization) }}" class="btn btn-secondary">
|
||||
<i class="bi bi-x-lg"></i> Отмена
|
||||
</a>
|
||||
</div>
|
||||
</form>
|
||||
</main>
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
||||
|
|
@ -0,0 +1,108 @@
|
|||
@extends('layouts.app')
|
||||
|
||||
@section('title', 'Редактировать группу')
|
||||
|
||||
@section('content')
|
||||
<div class="container-fluid">
|
||||
<div class="row">
|
||||
<nav class="col-md-3 col-lg-2 d-md-block sidebar collapse">
|
||||
<div class="position-sticky pt-3">
|
||||
<ul class="nav flex-column">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="{{ route('dashboard') }}">
|
||||
<i class="bi bi-speedometer2"></i> Панель управления
|
||||
</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="{{ route('admin.organizations.index') }}">
|
||||
<i class="bi bi-building"></i> Организации
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<main class="col-md-9 ms-sm-auto col-lg-10 px-md-4 main-content">
|
||||
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
|
||||
<h1 class="h2">Редактировать группу</h1>
|
||||
<a href="{{ route('admin.organizations.show', $organization) }}" class="btn btn-secondary btn-sm">
|
||||
<i class="bi bi-arrow-left"></i> Назад
|
||||
</a>
|
||||
</div>
|
||||
|
||||
@if ($errors->any())
|
||||
<div class="alert alert-danger">
|
||||
<ul class="mb-0">
|
||||
@foreach ($errors->all() as $error)
|
||||
<li>{{ $error }}</li>
|
||||
@endforeach
|
||||
</ul>
|
||||
</div>
|
||||
@endif
|
||||
|
||||
<form action="{{ route('admin.groups.update', $organization, $group) }}" method="POST">
|
||||
@csrf
|
||||
@method('PUT')
|
||||
<div class="row">
|
||||
<div class="col-md-8">
|
||||
<div class="card shadow-sm mb-4">
|
||||
<div class="card-body">
|
||||
<h5 class="card-title mb-3">Основная информация</h5>
|
||||
|
||||
<div class="mb-3">
|
||||
<label for="name" class="form-label">Название группы <span class="text-danger">*</span></label>
|
||||
<input type="text" class="form-control @error('name') is-invalid @enderror"
|
||||
id="name" name="name" value="{{ old('name', $group->name) }}" required autofocus>
|
||||
@error('name')
|
||||
<div class="invalid-feedback">{{ $message }}</div>
|
||||
@enderror
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label for="description" class="form-label">Описание</label>
|
||||
<textarea class="form-control @error('description') is-invalid @enderror"
|
||||
id="description" name="description" rows="3">{{ old('description', $group->description) }}</textarea>
|
||||
@error('description')
|
||||
<div class="invalid-feedback">{{ $message }}</div>
|
||||
@enderror
|
||||
</div>
|
||||
|
||||
<div class="mb-3 form-check">
|
||||
<input type="checkbox" class="form-check-input" id="is_active" name="is_active" value="1" {{ old('is_active', $group->is_active) ? 'checked' : '' }}>
|
||||
<label class="form-check-label" for="is_active">Активна</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="col-md-4">
|
||||
<div class="card shadow-sm mb-4">
|
||||
<div class="card-body">
|
||||
<h5 class="card-title mb-3">Организация</h5>
|
||||
<p class="text-muted mb-0">{{ $organization->name }}</p>
|
||||
@if($organization->inn)
|
||||
<p class="text-muted small">ИНН: {{ $organization->inn }}</p>
|
||||
@endif
|
||||
<hr>
|
||||
<p class="text-muted small mb-0">
|
||||
Создана: {{ $group->created_at->format('d.m.Y') }}<br>
|
||||
Обновлена: {{ $group->updated_at->format('d.m.Y') }}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<button type="submit" class="btn btn-primary">
|
||||
<i class="bi bi-check-lg"></i> Сохранить изменения
|
||||
</button>
|
||||
<a href="{{ route('admin.organizations.show', $organization) }}" class="btn btn-secondary">
|
||||
<i class="bi bi-x-lg"></i> Отмена
|
||||
</a>
|
||||
</div>
|
||||
</form>
|
||||
</main>
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
||||
|
|
@ -0,0 +1,123 @@
|
|||
@extends('layouts.app')
|
||||
|
||||
@section('title', 'Группы организации')
|
||||
|
||||
@section('content')
|
||||
<div class="container-fluid">
|
||||
<div class="row">
|
||||
<nav class="col-md-3 col-lg-2 d-md-block sidebar collapse">
|
||||
<div class="position-sticky pt-3">
|
||||
<ul class="nav flex-column">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="{{ route('dashboard') }}">
|
||||
<i class="bi bi-speedometer2"></i> Панель управления
|
||||
</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="{{ route('admin.organizations.index') }}">
|
||||
<i class="bi bi-building"></i> Организации
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<main class="col-md-9 ms-sm-auto col-lg-10 px-md-4 main-content">
|
||||
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
|
||||
<h1 class="h2">Группы организации</h1>
|
||||
<div class="btn-toolbar">
|
||||
<a href="{{ route('admin.organizations.show', $organization) }}" class="btn btn-secondary btn-sm me-2">
|
||||
<i class="bi bi-arrow-left"></i> К организации
|
||||
</a>
|
||||
@can('create', App\Models\Group::class)
|
||||
<a href="{{ route('admin.groups.create', $organization) }}" class="btn btn-primary btn-sm">
|
||||
<i class="bi bi-plus-lg"></i> Добавить группу
|
||||
</a>
|
||||
@endcan
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@if(session('success'))
|
||||
<div class="alert alert-success alert-dismissible fade show" role="alert">
|
||||
<i class="bi bi-check-circle"></i> {{ session('success') }}
|
||||
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
|
||||
</div>
|
||||
@endif
|
||||
|
||||
@if(session('error'))
|
||||
<div class="alert alert-danger alert-dismissible fade show" role="alert">
|
||||
<i class="bi bi-exclamation-triangle"></i> {{ session('error') }}
|
||||
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
|
||||
</div>
|
||||
@endif
|
||||
|
||||
<div class="card shadow-sm">
|
||||
<div class="card-body">
|
||||
<div class="table-responsive">
|
||||
<table class="table table-hover">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
<th>Название</th>
|
||||
<th>Описание</th>
|
||||
<th>Пользователей</th>
|
||||
<th>Статус</th>
|
||||
<th>Действия</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@forelse($groups as $group)
|
||||
<tr>
|
||||
<td>{{ $group->id }}</td>
|
||||
<td><strong>{{ $group->name }}</strong></td>
|
||||
<td>{{ Str::limit($group->description, 50) ?? '—' }}</td>
|
||||
<td>{{ $group->users_count }}</td>
|
||||
<td>
|
||||
@if($group->is_active)
|
||||
<span class="badge bg-success">Активна</span>
|
||||
@else
|
||||
<span class="badge bg-secondary">Не активна</span>
|
||||
@endif
|
||||
</td>
|
||||
<td>
|
||||
<div class="btn-group btn-group-sm">
|
||||
@can('update', $group)
|
||||
<a href="{{ route('admin.groups.edit', $group) }}" class="btn btn-outline-warning" title="Редактировать">
|
||||
<i class="bi bi-pencil"></i>
|
||||
</a>
|
||||
@endcan
|
||||
@can('delete', $group)
|
||||
<form action="{{ route('admin.groups.destroy', $group) }}" method="POST" class="d-inline" onsubmit="return confirm('Вы уверены?')">
|
||||
@csrf
|
||||
@method('DELETE')
|
||||
<button type="submit" class="btn btn-outline-danger" title="Удалить">
|
||||
<i class="bi bi-trash"></i>
|
||||
</button>
|
||||
</form>
|
||||
@endcan
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
@empty
|
||||
<tr>
|
||||
<td colspan="6" class="text-center text-muted py-4">
|
||||
<i class="bi bi-inbox" style="font-size: 2rem;"></i>
|
||||
<p class="mt-2">В этой организации пока нет групп</p>
|
||||
</td>
|
||||
</tr>
|
||||
@endforelse
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@if($groups->hasPages())
|
||||
<div class="mt-3">
|
||||
{{ $groups->links() }}
|
||||
</div>
|
||||
@endif
|
||||
</main>
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
||||
|
|
@ -145,18 +145,35 @@
|
|||
|
||||
<div class="col-md-6 mb-4">
|
||||
<div class="card shadow-sm">
|
||||
<div class="card-header">
|
||||
<div class="card-header d-flex justify-content-between align-items-center">
|
||||
<h5 class="mb-0"><i class="bi bi-people-fill"></i> Группы</h5>
|
||||
@can('create', App\Models\Group::class)
|
||||
<a href="{{ route('admin.groups.create', $organization) }}" class="btn btn-sm btn-primary">
|
||||
<i class="bi bi-plus-lg"></i>
|
||||
</a>
|
||||
@endcan
|
||||
</div>
|
||||
<div class="card-body">
|
||||
@if($organization->groups->count() > 0)
|
||||
<ul class="list-group list-group-flush">
|
||||
@foreach($organization->groups->take(5) as $group)
|
||||
<li class="list-group-item">
|
||||
<strong>{{ $group->name }}</strong>
|
||||
@if($group->description)
|
||||
<br><small class="text-muted">{{ Str::limit($group->description, 50) }}</small>
|
||||
@endif
|
||||
<li class="list-group-item d-flex justify-content-between align-items-center">
|
||||
<div>
|
||||
<strong>{{ $group->name }}</strong>
|
||||
@if($group->description)
|
||||
<br><small class="text-muted">{{ Str::limit($group->description, 50) }}</small>
|
||||
@endif
|
||||
</div>
|
||||
<div class="btn-group btn-group-sm">
|
||||
<a href="#" class="btn btn-outline-primary" title="Просмотр">
|
||||
<i class="bi bi-eye"></i>
|
||||
</a>
|
||||
@can('update', $group)
|
||||
<a href="{{ route('admin.groups.edit', $group) }}" class="btn btn-outline-warning" title="Редактировать">
|
||||
<i class="bi bi-pencil"></i>
|
||||
</a>
|
||||
@endcan
|
||||
</div>
|
||||
</li>
|
||||
@endforeach
|
||||
</ul>
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue