105 lines
3.5 KiB
PHP
105 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Web\Authorization;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Permission\CreatePermissionRequest;
|
|
use App\Http\Requests\Permission\UpdatePermissionRequest;
|
|
use App\Models\Permission;
|
|
use App\Models\Role;
|
|
use App\Repositories\Permission\PermissionRepository;
|
|
use App\Repositories\Role\RoleRepository;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Inertia\Inertia;
|
|
use Inertia\Response as InertiaResponse;
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
class PermissionsController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly RoleRepository $roles,
|
|
private readonly PermissionRepository $permissions
|
|
) {}
|
|
|
|
public function index(): InertiaResponse
|
|
{
|
|
$roles = $this->roles->all();
|
|
$permissions = $this->permissions->all();
|
|
|
|
return Inertia::render('permissions/Index', [
|
|
'roles' => $roles->map(fn (Role $role) => [
|
|
'id' => $role->id,
|
|
'display_name' => $role->display_name,
|
|
])->values()->all(),
|
|
|
|
'permissions' => $permissions->map(fn (Permission $permission) => [
|
|
'id' => $permission->id,
|
|
'name' => $permission->name,
|
|
'display_name' => $permission->display_name,
|
|
'removable' => (bool) $permission->removable,
|
|
'editUrl' => route('permissions.edit', $permission),
|
|
'deleteUrl' => route('permissions.destroy', $permission),
|
|
])->values()->all(),
|
|
|
|
// Replaces the per-checkbox $role->hasPermission() calls the Blade
|
|
// matrix made, so the client can render the grid from plain data.
|
|
'granted' => $roles->mapWithKeys(fn (Role $role) => [
|
|
$role->id => $permissions
|
|
->filter(fn (Permission $permission) => $role->hasPermission($permission->name))
|
|
->pluck('id')
|
|
->values()
|
|
->all(),
|
|
])->all(),
|
|
]);
|
|
}
|
|
|
|
public function create(): InertiaResponse
|
|
{
|
|
return Inertia::render('permissions/Form', ['edit' => false, 'permission' => null]);
|
|
}
|
|
|
|
public function store(CreatePermissionRequest $request): RedirectResponse
|
|
{
|
|
$this->permissions->create($request->all());
|
|
|
|
return redirect()->route('permissions.index')
|
|
->withSuccess(__('Permission created successfully.'));
|
|
}
|
|
|
|
public function edit(Permission $permission): InertiaResponse
|
|
{
|
|
return Inertia::render('permissions/Form', [
|
|
'edit' => true,
|
|
'permission' => [
|
|
'id' => $permission->id,
|
|
'name' => $permission->name,
|
|
'display_name' => $permission->display_name,
|
|
'description' => $permission->description,
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function update(Permission $permission, UpdatePermissionRequest $request): RedirectResponse
|
|
{
|
|
$this->permissions->update($permission->id, $request->all());
|
|
|
|
return redirect()->route('permissions.index')
|
|
->withSuccess(__('Permission updated successfully.'));
|
|
}
|
|
|
|
/**
|
|
* @throws NotFoundHttpException
|
|
*/
|
|
public function destroy(Permission $permission)
|
|
{
|
|
if (! $permission->removable) {
|
|
throw new NotFoundHttpException;
|
|
}
|
|
|
|
$this->permissions->delete($permission->id);
|
|
|
|
return redirect()->route('permissions.index')
|
|
->withSuccess(__('Permission deleted successfully.'));
|
|
}
|
|
}
|