2020-12-28 19:47:08 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Application\Roles;
|
|
|
|
|
2021-03-05 17:03:12 +00:00
|
|
|
use Illuminate\Http\Response;
|
2020-12-28 19:47:08 +00:00
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
use Pterodactyl\Models\AdminRole;
|
2021-07-14 22:43:59 +00:00
|
|
|
use Spatie\QueryBuilder\QueryBuilder;
|
|
|
|
use Pterodactyl\Exceptions\Http\QueryValueOutOfRangeHttpException;
|
2021-08-07 23:10:24 +00:00
|
|
|
use Pterodactyl\Transformers\Api\Application\AdminRoleTransformer;
|
2021-01-04 18:50:43 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Roles\GetRoleRequest;
|
2020-12-28 19:47:08 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Roles\GetRolesRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Roles\StoreRoleRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Roles\DeleteRoleRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Roles\UpdateRoleRequest;
|
|
|
|
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
|
|
|
|
|
|
|
|
class RoleController extends ApplicationApiController
|
|
|
|
{
|
|
|
|
/**
|
2021-01-05 21:52:49 +00:00
|
|
|
* RoleController constructor.
|
2020-12-28 19:47:08 +00:00
|
|
|
*/
|
2021-01-05 21:52:49 +00:00
|
|
|
public function __construct()
|
2020-12-28 19:47:08 +00:00
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array of all roles.
|
|
|
|
*
|
2021-01-01 22:55:30 +00:00
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
2020-12-28 19:47:08 +00:00
|
|
|
*/
|
2021-01-01 22:55:30 +00:00
|
|
|
public function index(GetRolesRequest $request): array
|
2020-12-28 19:47:08 +00:00
|
|
|
{
|
2021-07-14 22:43:59 +00:00
|
|
|
$perPage = $request->query('per_page', 10);
|
|
|
|
if ($perPage < 1 || $perPage > 100) {
|
|
|
|
throw new QueryValueOutOfRangeHttpException('per_page', 1, 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
$roles = QueryBuilder::for(AdminRole::query())
|
|
|
|
->allowedFilters(['id', 'name'])
|
|
|
|
->allowedSorts(['id', 'name'])
|
|
|
|
->paginate($perPage);
|
|
|
|
|
|
|
|
return $this->fractal->collection($roles)
|
2021-08-07 21:32:40 +00:00
|
|
|
->transformWith(AdminRoleTransformer::class)
|
2020-12-28 19:47:08 +00:00
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a single role.
|
|
|
|
*
|
2021-01-01 22:55:30 +00:00
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
2020-12-28 19:47:08 +00:00
|
|
|
*/
|
2021-01-04 18:50:43 +00:00
|
|
|
public function view(GetRoleRequest $request, AdminRole $role): array
|
2020-12-28 19:47:08 +00:00
|
|
|
{
|
|
|
|
return $this->fractal->item($role)
|
2021-08-07 21:32:40 +00:00
|
|
|
->transformWith(AdminRoleTransformer::class)
|
2020-12-28 19:47:08 +00:00
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new role.
|
|
|
|
*
|
2021-01-01 22:55:30 +00:00
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
2020-12-28 19:47:08 +00:00
|
|
|
*/
|
2021-01-01 22:55:30 +00:00
|
|
|
public function store(StoreRoleRequest $request): JsonResponse
|
2020-12-28 19:47:08 +00:00
|
|
|
{
|
2021-03-05 15:46:14 +00:00
|
|
|
$data = array_merge($request->validated(), [
|
|
|
|
'sort_id' => 99,
|
|
|
|
]);
|
|
|
|
$role = AdminRole::query()->create($data);
|
2020-12-28 19:47:08 +00:00
|
|
|
|
|
|
|
return $this->fractal->item($role)
|
2021-08-07 21:32:40 +00:00
|
|
|
->transformWith(AdminRoleTransformer::class)
|
2020-12-28 19:47:08 +00:00
|
|
|
->respond(JsonResponse::HTTP_CREATED);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates a role.
|
|
|
|
*
|
2021-01-01 22:55:30 +00:00
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
2020-12-28 19:47:08 +00:00
|
|
|
*/
|
2021-01-01 22:55:30 +00:00
|
|
|
public function update(UpdateRoleRequest $request, AdminRole $role): array
|
2020-12-28 19:47:08 +00:00
|
|
|
{
|
|
|
|
$role->update($request->validated());
|
|
|
|
|
|
|
|
return $this->fractal->item($role)
|
2021-08-07 21:32:40 +00:00
|
|
|
->transformWith(AdminRoleTransformer::class)
|
2020-12-28 19:47:08 +00:00
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes a role.
|
|
|
|
*
|
2021-01-05 21:52:49 +00:00
|
|
|
* @throws \Exception
|
2020-12-28 19:47:08 +00:00
|
|
|
*/
|
2021-03-05 17:03:12 +00:00
|
|
|
public function delete(DeleteRoleRequest $request, AdminRole $role): Response
|
2020-12-28 19:47:08 +00:00
|
|
|
{
|
2021-01-05 21:52:49 +00:00
|
|
|
$role->delete();
|
2020-12-28 19:47:08 +00:00
|
|
|
|
2021-03-05 17:03:12 +00:00
|
|
|
return $this->returnNoContent();
|
2020-12-28 19:47:08 +00:00
|
|
|
}
|
|
|
|
}
|