mirror of
https://github.com/pelican-dev/panel.git
synced 2025-05-20 00:34:44 +02:00

* remove `guard_name` from api and add id to transformer * disallow update/ delete for root admin role via api * disallow assigning root admin via api * add api to remove user roles * fix assignRoles & removeRoles
98 lines
2.8 KiB
PHP
98 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Application\Roles;
|
|
|
|
use App\Exceptions\PanelException;
|
|
use Illuminate\Http\Response;
|
|
use Illuminate\Http\JsonResponse;
|
|
use App\Models\Role;
|
|
use Spatie\QueryBuilder\QueryBuilder;
|
|
use App\Transformers\Api\Application\RoleTransformer;
|
|
use App\Http\Controllers\Api\Application\ApplicationApiController;
|
|
use App\Http\Requests\Api\Application\Roles\GetRoleRequest;
|
|
use App\Http\Requests\Api\Application\Roles\StoreRoleRequest;
|
|
use App\Http\Requests\Api\Application\Roles\DeleteRoleRequest;
|
|
use App\Http\Requests\Api\Application\Roles\UpdateRoleRequest;
|
|
|
|
class RoleController extends ApplicationApiController
|
|
{
|
|
/**
|
|
* Return all the roles currently registered on the Panel.
|
|
*/
|
|
public function index(GetRoleRequest $request): array
|
|
{
|
|
$roles = QueryBuilder::for(Role::query())
|
|
->allowedFilters(['id', 'name'])
|
|
->allowedSorts(['id', 'name'])
|
|
->paginate($request->query('per_page') ?? 10);
|
|
|
|
return $this->fractal->collection($roles)
|
|
->transformWith($this->getTransformer(RoleTransformer::class))
|
|
->toArray();
|
|
}
|
|
|
|
/**
|
|
* Return a single role.
|
|
*/
|
|
public function view(GetRoleRequest $request, Role $role): array
|
|
{
|
|
return $this->fractal->item($role)
|
|
->transformWith($this->getTransformer(RoleTransformer::class))
|
|
->toArray();
|
|
}
|
|
|
|
/**
|
|
* Store a new role on the Panel and return an HTTP/201 response code with the
|
|
* new role attached.
|
|
*
|
|
* @throws \Throwable
|
|
*/
|
|
public function store(StoreRoleRequest $request): JsonResponse
|
|
{
|
|
$role = Role::create($request->validated());
|
|
|
|
return $this->fractal->item($role)
|
|
->transformWith($this->getTransformer(RoleTransformer::class))
|
|
->addMeta([
|
|
'resource' => route('api.application.roles.view', [
|
|
'role' => $role->id,
|
|
]),
|
|
])
|
|
->respond(201);
|
|
}
|
|
|
|
/**
|
|
* Update a role on the Panel and return the updated record to the user.
|
|
*
|
|
* @throws \Throwable
|
|
*/
|
|
public function update(UpdateRoleRequest $request, Role $role): array
|
|
{
|
|
if ($role->isRootAdmin()) {
|
|
throw new PanelException('Can\'t update root admin role!');
|
|
}
|
|
|
|
$role->update($request->validated());
|
|
|
|
return $this->fractal->item($role)
|
|
->transformWith($this->getTransformer(RoleTransformer::class))
|
|
->toArray();
|
|
}
|
|
|
|
/**
|
|
* Delete a role from the Panel.
|
|
*
|
|
* @throws \Exception
|
|
*/
|
|
public function delete(DeleteRoleRequest $request, Role $role): Response
|
|
{
|
|
if ($role->isRootAdmin()) {
|
|
throw new PanelException('Can\'t delete root admin role!');
|
|
}
|
|
|
|
$role->delete();
|
|
|
|
return $this->returnNoContent();
|
|
}
|
|
}
|