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:
@@ -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', 'Группа успешно удалена.');
|
||||
}
|
||||
}
|
||||
Executable
+65
@@ -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,
|
||||
];
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user