feat: Implement initial agent integration management system with role-based dashboards, status tracking, and activity timelines.
This commit is contained in:
100
app/Http/Controllers/RoleController.php
Normal file
100
app/Http/Controllers/RoleController.php
Normal file
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Inertia\Inertia;
|
||||
use Spatie\Permission\Models\Role;
|
||||
use Spatie\Permission\Models\Permission;
|
||||
|
||||
class RoleController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
if (!auth()->user()->hasRole('Admin')) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
return Inertia::render('Role/Index', [
|
||||
'roles' => Role::with('permissions')->get(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
if (!auth()->user()->hasRole('Admin')) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
return Inertia::render('Role/Create', [
|
||||
'permissions' => Permission::all(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
if (!auth()->user()->hasRole('Admin')) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string|unique:roles,name',
|
||||
'permissions' => 'array',
|
||||
]);
|
||||
|
||||
$role = Role::create(['name' => $validated['name']]);
|
||||
|
||||
if (!empty($validated['permissions'])) {
|
||||
$role->syncPermissions($validated['permissions']);
|
||||
}
|
||||
|
||||
return redirect()->route('roles.index')->with('success', 'Rôle créé avec succès.');
|
||||
}
|
||||
|
||||
public function edit(Role $role)
|
||||
{
|
||||
if (!auth()->user()->hasRole('Admin')) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
return Inertia::render('Role/Edit', [
|
||||
'role' => $role->load('permissions'),
|
||||
'permissions' => Permission::all(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(Request $request, Role $role)
|
||||
{
|
||||
if (!auth()->user()->hasRole('Admin')) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string|unique:roles,name,' . $role->id,
|
||||
'permissions' => 'array',
|
||||
]);
|
||||
|
||||
$role->update(['name' => $validated['name']]);
|
||||
|
||||
if (isset($validated['permissions'])) {
|
||||
$role->syncPermissions($validated['permissions']);
|
||||
}
|
||||
|
||||
return redirect()->route('roles.index')->with('success', 'Rôle mis à jour avec succès.');
|
||||
}
|
||||
|
||||
public function destroy(Role $role)
|
||||
{
|
||||
if (!auth()->user()->hasRole('Admin')) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
if ($role->name === 'Admin') {
|
||||
return back()->with('error', 'Le rôle Admin ne peut pas être supprimé.');
|
||||
}
|
||||
|
||||
$role->delete();
|
||||
|
||||
return redirect()->route('roles.index')->with('success', 'Rôle supprimé avec succès.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user