enable interactive features of Swagger UI (#1273)

This commit is contained in:
Kevin Papst
2019-11-22 21:59:25 +01:00
committed by GitHub
parent 60d16016e8
commit 536a05210e
11 changed files with 167 additions and 152 deletions

View File

@@ -39,7 +39,7 @@ nelmio_api_doc:
description: | description: |
JSON API for the Kimai 2 time-tracking software. Read more about its usage in the [API documentation](https://www.kimai.org/documentation/rest-api.html) and then download a [Swagger file](doc.json) for import e.g. in Postman. JSON API for the Kimai 2 time-tracking software. Read more about its usage in the [API documentation](https://www.kimai.org/documentation/rest-api.html) and then download a [Swagger file](doc.json) for import e.g. in Postman.
Be aware: it is not yet considered stable and BC breaks might happen, but we try to avoid them. Be aware: it is not yet considered stable and BC breaks might happen, but we try to avoid them.
version: '0.3' version: '0.4'
securityDefinitions: securityDefinitions:
apiUser: apiUser:
type: apiKey type: apiKey

View File

@@ -22,6 +22,7 @@ use FOS\RestBundle\Controller\Annotations\RouteResource;
use FOS\RestBundle\Request\ParamFetcherInterface; use FOS\RestBundle\Request\ParamFetcherInterface;
use FOS\RestBundle\View\View; use FOS\RestBundle\View\View;
use FOS\RestBundle\View\ViewHandlerInterface; use FOS\RestBundle\View\ViewHandlerInterface;
use Nelmio\ApiDocBundle\Annotation\Security as ApiSecurity;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Swagger\Annotations as SWG; use Swagger\Annotations as SWG;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@@ -75,10 +76,10 @@ class ActivityController extends BaseApiController
* @Rest\QueryParam(name="order", requirements="ASC|DESC", strict=true, nullable=true, description="The result order. Allowed values: ASC, DESC (default: ASC)") * @Rest\QueryParam(name="order", requirements="ASC|DESC", strict=true, nullable=true, description="The result order. Allowed values: ASC, DESC (default: ASC)")
* @Rest\QueryParam(name="term", requirements="[a-zA-Z0-9 \-,:]+", strict=true, nullable=true, description="Free search term") * @Rest\QueryParam(name="term", requirements="[a-zA-Z0-9 \-,:]+", strict=true, nullable=true, description="Free search term")
* *
* @param ParamFetcherInterface $paramFetcher * @ApiSecurity(name="apiUser")
* @return Response * @ApiSecurity(name="apiToken")
*/ */
public function cgetAction(ParamFetcherInterface $paramFetcher) public function cgetAction(ParamFetcherInterface $paramFetcher): Response
{ {
$query = new ActivityQuery(); $query = new ActivityQuery();
@@ -133,10 +134,10 @@ class ActivityController extends BaseApiController
* required=true, * required=true,
* ) * )
* *
* @param int $id * @ApiSecurity(name="apiUser")
* @return Response * @ApiSecurity(name="apiToken")
*/ */
public function getAction($id) public function getAction(int $id): Response
{ {
$data = $this->repository->find($id); $data = $this->repository->find($id);
@@ -168,13 +169,10 @@ class ActivityController extends BaseApiController
* @SWG\Schema(ref="#/definitions/ActivityEditForm") * @SWG\Schema(ref="#/definitions/ActivityEditForm")
* ) * )
* *
* @param Request $request * @ApiSecurity(name="apiUser")
* @return Response * @ApiSecurity(name="apiToken")
* @throws \App\Repository\RepositoryException
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/ */
public function postAction(Request $request) public function postAction(Request $request): Response
{ {
if (!$this->isGranted('create_activity')) { if (!$this->isGranted('create_activity')) {
throw new AccessDeniedHttpException('User cannot create activities'); throw new AccessDeniedHttpException('User cannot create activities');
@@ -229,13 +227,10 @@ class ActivityController extends BaseApiController
* required=true, * required=true,
* ) * )
* *
* @param Request $request * @ApiSecurity(name="apiUser")
* @param string $id * @ApiSecurity(name="apiToken")
* @return Response
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/ */
public function patchAction(Request $request, string $id) public function patchAction(Request $request, int $id): Response
{ {
$activity = $this->repository->find($id); $activity = $this->repository->find($id);
@@ -288,13 +283,10 @@ class ActivityController extends BaseApiController
* @Rest\RequestParam(name="name", strict=true, nullable=false, description="The meta-field name") * @Rest\RequestParam(name="name", strict=true, nullable=false, description="The meta-field name")
* @Rest\RequestParam(name="value", strict=true, nullable=false, description="The meta-field value") * @Rest\RequestParam(name="value", strict=true, nullable=false, description="The meta-field value")
* *
* @param int $id * @ApiSecurity(name="apiUser")
* @param ParamFetcherInterface $paramFetcher * @ApiSecurity(name="apiToken")
* @return Response
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/ */
public function metaAction($id, ParamFetcherInterface $paramFetcher) public function metaAction(int $id, ParamFetcherInterface $paramFetcher): Response
{ {
$activity = $this->repository->find($id); $activity = $this->repository->find($id);

View File

@@ -17,8 +17,10 @@ use App\Entity\User;
use FOS\RestBundle\Controller\Annotations as Rest; use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\View\View; use FOS\RestBundle\View\View;
use FOS\RestBundle\View\ViewHandlerInterface; use FOS\RestBundle\View\ViewHandlerInterface;
use Nelmio\ApiDocBundle\Annotation\Security as ApiSecurity;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Swagger\Annotations as SWG; use Swagger\Annotations as SWG;
use Symfony\Component\HttpFoundation\Response;
/** /**
* @Security("is_granted('IS_AUTHENTICATED_REMEMBERED')") * @Security("is_granted('IS_AUTHENTICATED_REMEMBERED')")
@@ -54,8 +56,11 @@ class ConfigurationController extends BaseApiController
* ) * )
* *
* @Rest\Get(path="/config/i18n") * @Rest\Get(path="/config/i18n")
*
* @ApiSecurity(name="apiUser")
* @ApiSecurity(name="apiToken")
*/ */
public function i18nAction() public function i18nAction(): Response
{ {
/** @var User $user */ /** @var User $user */
$user = $this->getUser(); $user = $this->getUser();

View File

@@ -22,6 +22,7 @@ use FOS\RestBundle\Controller\Annotations\RouteResource;
use FOS\RestBundle\Request\ParamFetcherInterface; use FOS\RestBundle\Request\ParamFetcherInterface;
use FOS\RestBundle\View\View; use FOS\RestBundle\View\View;
use FOS\RestBundle\View\ViewHandlerInterface; use FOS\RestBundle\View\ViewHandlerInterface;
use Nelmio\ApiDocBundle\Annotation\Security as ApiSecurity;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Swagger\Annotations as SWG; use Swagger\Annotations as SWG;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@@ -72,10 +73,10 @@ class CustomerController extends BaseApiController
* @Rest\QueryParam(name="orderBy", requirements="id|name", strict=true, nullable=true, description="The field by which results will be ordered. Allowed values: id, name (default: name)") * @Rest\QueryParam(name="orderBy", requirements="id|name", strict=true, nullable=true, description="The field by which results will be ordered. Allowed values: id, name (default: name)")
* @Rest\QueryParam(name="term", requirements="[a-zA-Z0-9 \-,:]+", strict=true, nullable=true, description="Free search term") * @Rest\QueryParam(name="term", requirements="[a-zA-Z0-9 \-,:]+", strict=true, nullable=true, description="Free search term")
* *
* @param ParamFetcherInterface $paramFetcher * @ApiSecurity(name="apiUser")
* @return Response * @ApiSecurity(name="apiToken")
*/ */
public function cgetAction(ParamFetcherInterface $paramFetcher) public function cgetAction(ParamFetcherInterface $paramFetcher): Response
{ {
$query = new CustomerQuery(); $query = new CustomerQuery();
$query->setCurrentUser($this->getUser()); $query->setCurrentUser($this->getUser());
@@ -112,10 +113,10 @@ class CustomerController extends BaseApiController
* @SWG\Schema(ref="#/definitions/CustomerEntity"), * @SWG\Schema(ref="#/definitions/CustomerEntity"),
* ) * )
* *
* @param int $id * @ApiSecurity(name="apiUser")
* @return Response * @ApiSecurity(name="apiToken")
*/ */
public function getAction($id) public function getAction(int $id): Response
{ {
$data = $this->repository->find($id); $data = $this->repository->find($id);
@@ -147,13 +148,10 @@ class CustomerController extends BaseApiController
* @SWG\Schema(ref="#/definitions/CustomerEditForm") * @SWG\Schema(ref="#/definitions/CustomerEditForm")
* ) * )
* *
* @param Request $request * @ApiSecurity(name="apiUser")
* @return Response * @ApiSecurity(name="apiToken")
* @throws \App\Repository\RepositoryException
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/ */
public function postAction(Request $request) public function postAction(Request $request): Response
{ {
if (!$this->isGranted('create_customer')) { if (!$this->isGranted('create_customer')) {
throw new AccessDeniedHttpException('User cannot create customers'); throw new AccessDeniedHttpException('User cannot create customers');
@@ -208,13 +206,10 @@ class CustomerController extends BaseApiController
* required=true, * required=true,
* ) * )
* *
* @param Request $request * @ApiSecurity(name="apiUser")
* @param string $id * @ApiSecurity(name="apiToken")
* @return Response
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/ */
public function patchAction(Request $request, string $id) public function patchAction(Request $request, int $id): Response
{ {
$customer = $this->repository->find($id); $customer = $this->repository->find($id);
@@ -267,13 +262,10 @@ class CustomerController extends BaseApiController
* @Rest\RequestParam(name="name", strict=true, nullable=false, description="The meta-field name") * @Rest\RequestParam(name="name", strict=true, nullable=false, description="The meta-field name")
* @Rest\RequestParam(name="value", strict=true, nullable=false, description="The meta-field value") * @Rest\RequestParam(name="value", strict=true, nullable=false, description="The meta-field value")
* *
* @param int $id * @ApiSecurity(name="apiUser")
* @param ParamFetcherInterface $paramFetcher * @ApiSecurity(name="apiToken")
* @return Response
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/ */
public function metaAction($id, ParamFetcherInterface $paramFetcher) public function metaAction(int $id, ParamFetcherInterface $paramFetcher): Response
{ {
$customer = $this->repository->find($id); $customer = $this->repository->find($id);

View File

@@ -22,6 +22,7 @@ use FOS\RestBundle\Controller\Annotations\RouteResource;
use FOS\RestBundle\Request\ParamFetcherInterface; use FOS\RestBundle\Request\ParamFetcherInterface;
use FOS\RestBundle\View\View; use FOS\RestBundle\View\View;
use FOS\RestBundle\View\ViewHandlerInterface; use FOS\RestBundle\View\ViewHandlerInterface;
use Nelmio\ApiDocBundle\Annotation\Security as ApiSecurity;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Swagger\Annotations as SWG; use Swagger\Annotations as SWG;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@@ -73,10 +74,10 @@ class ProjectController extends BaseApiController
* @Rest\QueryParam(name="orderBy", requirements="id|name|customer", strict=true, nullable=true, description="The field by which results will be ordered. Allowed values: id, name, customer (default: name)") * @Rest\QueryParam(name="orderBy", requirements="id|name|customer", strict=true, nullable=true, description="The field by which results will be ordered. Allowed values: id, name, customer (default: name)")
* @Rest\QueryParam(name="term", requirements="[a-zA-Z0-9 \-,:]+", strict=true, nullable=true, description="Free search term") * @Rest\QueryParam(name="term", requirements="[a-zA-Z0-9 \-,:]+", strict=true, nullable=true, description="Free search term")
* *
* @param ParamFetcherInterface $paramFetcher * @ApiSecurity(name="apiUser")
* @return Response * @ApiSecurity(name="apiToken")
*/ */
public function cgetAction(ParamFetcherInterface $paramFetcher) public function cgetAction(ParamFetcherInterface $paramFetcher): Response
{ {
$query = new ProjectQuery(); $query = new ProjectQuery();
$query->setCurrentUser($this->getUser()); $query->setCurrentUser($this->getUser());
@@ -117,10 +118,10 @@ class ProjectController extends BaseApiController
* @SWG\Schema(ref="#/definitions/ProjectEntity"), * @SWG\Schema(ref="#/definitions/ProjectEntity"),
* ) * )
* *
* @param int $id * @ApiSecurity(name="apiUser")
* @return Response * @ApiSecurity(name="apiToken")
*/ */
public function getAction($id) public function getAction(int $id): Response
{ {
$data = $this->repository->find($id); $data = $this->repository->find($id);
@@ -152,13 +153,10 @@ class ProjectController extends BaseApiController
* @SWG\Schema(ref="#/definitions/ProjectEditForm") * @SWG\Schema(ref="#/definitions/ProjectEditForm")
* ) * )
* *
* @param Request $request * @ApiSecurity(name="apiUser")
* @return Response * @ApiSecurity(name="apiToken")
* @throws \App\Repository\RepositoryException
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/ */
public function postAction(Request $request) public function postAction(Request $request): Response
{ {
if (!$this->isGranted('create_project')) { if (!$this->isGranted('create_project')) {
throw new AccessDeniedHttpException('User cannot create projects'); throw new AccessDeniedHttpException('User cannot create projects');
@@ -213,13 +211,10 @@ class ProjectController extends BaseApiController
* required=true, * required=true,
* ) * )
* *
* @param Request $request * @ApiSecurity(name="apiUser")
* @param string $id * @ApiSecurity(name="apiToken")
* @return Response
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/ */
public function patchAction(Request $request, string $id) public function patchAction(Request $request, int $id): Response
{ {
$project = $this->repository->find($id); $project = $this->repository->find($id);
@@ -272,13 +267,10 @@ class ProjectController extends BaseApiController
* @Rest\RequestParam(name="name", strict=true, nullable=false, description="The meta-field name") * @Rest\RequestParam(name="name", strict=true, nullable=false, description="The meta-field name")
* @Rest\RequestParam(name="value", strict=true, nullable=false, description="The meta-field value") * @Rest\RequestParam(name="value", strict=true, nullable=false, description="The meta-field value")
* *
* @param int $id * @ApiSecurity(name="apiUser")
* @param ParamFetcherInterface $paramFetcher * @ApiSecurity(name="apiToken")
* @return Response
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/ */
public function metaAction($id, ParamFetcherInterface $paramFetcher) public function metaAction(int $id, ParamFetcherInterface $paramFetcher): Response
{ {
$project = $this->repository->find($id); $project = $this->repository->find($id);

View File

@@ -16,7 +16,9 @@ use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\View\View; use FOS\RestBundle\View\View;
use FOS\RestBundle\View\ViewHandlerInterface; use FOS\RestBundle\View\ViewHandlerInterface;
use Nelmio\ApiDocBundle\Annotation\Model; use Nelmio\ApiDocBundle\Annotation\Model;
use Nelmio\ApiDocBundle\Annotation\Security as ApiSecurity;
use Swagger\Annotations as SWG; use Swagger\Annotations as SWG;
use Symfony\Component\HttpFoundation\Response;
class StatusController extends BaseApiController class StatusController extends BaseApiController
{ {
@@ -43,8 +45,11 @@ class StatusController extends BaseApiController
* ) * )
* *
* @Rest\Get(path="/ping") * @Rest\Get(path="/ping")
*
* @ApiSecurity(name="apiUser")
* @ApiSecurity(name="apiToken")
*/ */
public function pingAction() public function pingAction(): Response
{ {
$view = new View(['message' => 'pong'], 200); $view = new View(['message' => 'pong'], 200);
@@ -61,8 +66,11 @@ class StatusController extends BaseApiController
* ) * )
* *
* @Rest\Get(path="/version") * @Rest\Get(path="/version")
*
* @ApiSecurity(name="apiUser")
* @ApiSecurity(name="apiToken")
*/ */
public function versionAction() public function versionAction(): Response
{ {
return $this->viewHandler->handle(new View(new Version(), 200)); return $this->viewHandler->handle(new View(new Version(), 200));
} }

View File

@@ -13,13 +13,13 @@ namespace App\API;
use App\Entity\Tag; use App\Entity\Tag;
use App\Form\API\TagApiEditForm; use App\Form\API\TagApiEditForm;
use App\Form\TagEditForm;
use App\Repository\TagRepository; use App\Repository\TagRepository;
use FOS\RestBundle\Controller\Annotations as Rest; use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\Annotations\RouteResource; use FOS\RestBundle\Controller\Annotations\RouteResource;
use FOS\RestBundle\Request\ParamFetcherInterface; use FOS\RestBundle\Request\ParamFetcherInterface;
use FOS\RestBundle\View\View; use FOS\RestBundle\View\View;
use FOS\RestBundle\View\ViewHandlerInterface; use FOS\RestBundle\View\ViewHandlerInterface;
use Nelmio\ApiDocBundle\Annotation\Security as ApiSecurity;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Swagger\Annotations as SWG; use Swagger\Annotations as SWG;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
@@ -65,10 +65,10 @@ class TagController extends BaseApiController
* *
* @Rest\QueryParam(name="name", strict=true, nullable=true, description="Search term to filter tag list") * @Rest\QueryParam(name="name", strict=true, nullable=true, description="Search term to filter tag list")
* *
* @param ParamFetcherInterface $paramFetcher * @ApiSecurity(name="apiUser")
* @return Response * @ApiSecurity(name="apiToken")
*/ */
public function cgetAction(ParamFetcherInterface $paramFetcher) public function cgetAction(ParamFetcherInterface $paramFetcher): Response
{ {
$filter = $paramFetcher->get('name'); $filter = $paramFetcher->get('name');
@@ -98,13 +98,10 @@ class TagController extends BaseApiController
* @SWG\Schema(ref="#/definitions/TagEditForm") * @SWG\Schema(ref="#/definitions/TagEditForm")
* ) * )
* *
* @param Request $request * @ApiSecurity(name="apiUser")
* @return Response * @ApiSecurity(name="apiToken")
* @throws \App\Repository\RepositoryException
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/ */
public function postAction(Request $request) public function postAction(Request $request): Response
{ {
if (!$this->isGranted('manage_tag')) { if (!$this->isGranted('manage_tag')) {
throw new AccessDeniedHttpException('User cannot create tags'); throw new AccessDeniedHttpException('User cannot create tags');
@@ -150,10 +147,10 @@ class TagController extends BaseApiController
* *
* @Security("is_granted('delete_tag')") * @Security("is_granted('delete_tag')")
* *
* @param int $id * @ApiSecurity(name="apiUser")
* @return Response * @ApiSecurity(name="apiToken")
*/ */
public function deleteAction($id) public function deleteAction(int $id): Response
{ {
$tag = $this->repository->find($id); $tag = $this->repository->find($id);

View File

@@ -17,6 +17,7 @@ use FOS\RestBundle\Controller\Annotations\RouteResource;
use FOS\RestBundle\Request\ParamFetcherInterface; use FOS\RestBundle\Request\ParamFetcherInterface;
use FOS\RestBundle\View\View; use FOS\RestBundle\View\View;
use FOS\RestBundle\View\ViewHandlerInterface; use FOS\RestBundle\View\ViewHandlerInterface;
use Nelmio\ApiDocBundle\Annotation\Security as ApiSecurity;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Swagger\Annotations as SWG; use Swagger\Annotations as SWG;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
@@ -55,9 +56,10 @@ class TeamController extends BaseApiController
* *
* @Security("is_granted('view_team')") * @Security("is_granted('view_team')")
* *
* @return Response * @ApiSecurity(name="apiUser")
* @ApiSecurity(name="apiToken")
*/ */
public function cgetAction(ParamFetcherInterface $paramFetcher) public function cgetAction(ParamFetcherInterface $paramFetcher): Response
{ {
$data = $this->repository->findAll(); $data = $this->repository->findAll();
@@ -76,10 +78,10 @@ class TeamController extends BaseApiController
* @SWG\Schema(ref="#/definitions/TeamEntity"), * @SWG\Schema(ref="#/definitions/TeamEntity"),
* ) * )
* *
* @param int $id * @ApiSecurity(name="apiUser")
* @return Response * @ApiSecurity(name="apiToken")
*/ */
public function getAction($id) public function getAction(int $id): Response
{ {
$data = $this->repository->find($id); $data = $this->repository->find($id);
@@ -112,10 +114,10 @@ class TeamController extends BaseApiController
* *
* @Security("is_granted('delete_team')") * @Security("is_granted('delete_team')")
* *
* @param int $id * @ApiSecurity(name="apiUser")
* @return Response * @ApiSecurity(name="apiToken")
*/ */
public function deleteAction($id) public function deleteAction(int $id): Response
{ {
$team = $this->repository->find($id); $team = $this->repository->find($id);

View File

@@ -30,6 +30,7 @@ use FOS\RestBundle\Controller\Annotations\RouteResource;
use FOS\RestBundle\Request\ParamFetcherInterface; use FOS\RestBundle\Request\ParamFetcherInterface;
use FOS\RestBundle\View\View; use FOS\RestBundle\View\View;
use FOS\RestBundle\View\ViewHandlerInterface; use FOS\RestBundle\View\ViewHandlerInterface;
use Nelmio\ApiDocBundle\Annotation\Security as ApiSecurity;
use Pagerfanta\Pagerfanta; use Pagerfanta\Pagerfanta;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Swagger\Annotations as SWG; use Swagger\Annotations as SWG;
@@ -136,11 +137,10 @@ class TimesheetController extends BaseApiController
* *
* @Security("is_granted('view_own_timesheet') or is_granted('view_other_timesheet')") * @Security("is_granted('view_own_timesheet') or is_granted('view_other_timesheet')")
* *
* @param ParamFetcherInterface $paramFetcher * @ApiSecurity(name="apiUser")
* @return Response * @ApiSecurity(name="apiToken")
* @throws \Exception
*/ */
public function cgetAction(ParamFetcherInterface $paramFetcher) public function cgetAction(ParamFetcherInterface $paramFetcher): Response
{ {
$query = new TimesheetQuery(); $query = new TimesheetQuery();
$query->setUser($this->getUser()); $query->setUser($this->getUser());
@@ -249,10 +249,10 @@ class TimesheetController extends BaseApiController
* *
* @Security("is_granted('view_own_timesheet') or is_granted('view_other_timesheet')") * @Security("is_granted('view_own_timesheet') or is_granted('view_other_timesheet')")
* *
* @param int $id * @ApiSecurity(name="apiUser")
* @return Response * @ApiSecurity(name="apiToken")
*/ */
public function getAction($id) public function getAction(int $id): Response
{ {
$data = $this->repository->find($id); $data = $this->repository->find($id);
@@ -290,13 +290,10 @@ class TimesheetController extends BaseApiController
* *
* @Security("is_granted('create_own_timesheet')") * @Security("is_granted('create_own_timesheet')")
* *
* @param Request $request * @ApiSecurity(name="apiUser")
* @return Response * @ApiSecurity(name="apiToken")
* @throws \App\Repository\RepositoryException
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/ */
public function postAction(Request $request) public function postAction(Request $request): Response
{ {
$timesheet = new Timesheet(); $timesheet = new Timesheet();
$timesheet->setUser($this->getUser()); $timesheet->setUser($this->getUser());
@@ -367,11 +364,10 @@ class TimesheetController extends BaseApiController
* required=true, * required=true,
* ) * )
* *
* @param Request $request * @ApiSecurity(name="apiUser")
* @param int $id the timesheet to update * @ApiSecurity(name="apiToken")
* @return Response
*/ */
public function patchAction(Request $request, int $id) public function patchAction(Request $request, int $id): Response
{ {
$timesheet = $this->repository->find($id); $timesheet = $this->repository->find($id);
@@ -431,10 +427,10 @@ class TimesheetController extends BaseApiController
* required=true, * required=true,
* ) * )
* *
* @param int $id * @ApiSecurity(name="apiUser")
* @return Response * @ApiSecurity(name="apiToken")
*/ */
public function deleteAction($id) public function deleteAction(int $id): Response
{ {
$timesheet = $this->repository->find($id); $timesheet = $this->repository->find($id);
@@ -470,10 +466,11 @@ class TimesheetController extends BaseApiController
* @Rest\QueryParam(name="size", requirements="\d+", strict=true, nullable=true, description="The amount of entries (default: 10)") * @Rest\QueryParam(name="size", requirements="\d+", strict=true, nullable=true, description="The amount of entries (default: 10)")
* *
* @Security("is_granted('view_own_timesheet') or is_granted('view_other_timesheet')") * @Security("is_granted('view_own_timesheet') or is_granted('view_other_timesheet')")
* @return Response *
* @throws \Doctrine\ORM\Query\QueryException * @ApiSecurity(name="apiUser")
* @ApiSecurity(name="apiToken")
*/ */
public function recentAction(ParamFetcherInterface $paramFetcher) public function recentAction(ParamFetcherInterface $paramFetcher): Response
{ {
$user = $this->getUser(); $user = $this->getUser();
$begin = $this->dateTime->createDateTime('-1 year'); $begin = $this->dateTime->createDateTime('-1 year');
@@ -515,9 +512,11 @@ class TimesheetController extends BaseApiController
* ) * )
* *
* @Security("is_granted('view_own_timesheet')") * @Security("is_granted('view_own_timesheet')")
* @return Response *
* @ApiSecurity(name="apiUser")
* @ApiSecurity(name="apiToken")
*/ */
public function activeAction() public function activeAction(): Response
{ {
$data = $this->repository->getActiveEntries($this->getUser()); $data = $this->repository->getActiveEntries($this->getUser());
@@ -543,13 +542,10 @@ class TimesheetController extends BaseApiController
* required=true, * required=true,
* ) * )
* *
* @param int $id * @ApiSecurity(name="apiUser")
* @return Response * @ApiSecurity(name="apiToken")
* @throws \App\Repository\RepositoryException
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/ */
public function stopAction($id) public function stopAction(int $id): Response
{ {
$timesheet = $this->repository->find($id); $timesheet = $this->repository->find($id);
@@ -587,13 +583,10 @@ class TimesheetController extends BaseApiController
* *
* @Rest\RequestParam(name="copy", requirements="all|tags|rates|meta|description", strict=true, nullable=true, description="Whether data should be copied to the new entry. Allowed values: all, tags, rates, description, meta (default: nothing is copied)") * @Rest\RequestParam(name="copy", requirements="all|tags|rates|meta|description", strict=true, nullable=true, description="Whether data should be copied to the new entry. Allowed values: all, tags, rates, description, meta (default: nothing is copied)")
* *
* @param int $id * @ApiSecurity(name="apiUser")
* @return Response * @ApiSecurity(name="apiToken")
* @throws \App\Repository\RepositoryException
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/ */
public function restartAction($id, ParamFetcherInterface $paramFetcher, ValidatorInterface $validator) public function restartAction(int $id, ParamFetcherInterface $paramFetcher, ValidatorInterface $validator): Response
{ {
$timesheet = $this->repository->find($id); $timesheet = $this->repository->find($id);
@@ -676,12 +669,10 @@ class TimesheetController extends BaseApiController
* required=true, * required=true,
* ) * )
* *
* @param int $id * @ApiSecurity(name="apiUser")
* @return Response * @ApiSecurity(name="apiToken")
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/ */
public function exportAction($id) public function exportAction(int $id): Response
{ {
$timesheet = $this->repository->find($id); $timesheet = $this->repository->find($id);
@@ -723,13 +714,10 @@ class TimesheetController extends BaseApiController
* @Rest\RequestParam(name="name", strict=true, nullable=false, description="The meta-field name") * @Rest\RequestParam(name="name", strict=true, nullable=false, description="The meta-field name")
* @Rest\RequestParam(name="value", strict=true, nullable=false, description="The meta-field value") * @Rest\RequestParam(name="value", strict=true, nullable=false, description="The meta-field value")
* *
* @param int $id * @ApiSecurity(name="apiUser")
* @param ParamFetcherInterface $paramFetcher * @ApiSecurity(name="apiToken")
* @return Response
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/ */
public function metaAction($id, ParamFetcherInterface $paramFetcher) public function metaAction(int $id, ParamFetcherInterface $paramFetcher): Response
{ {
$timesheet = $this->repository->find($id); $timesheet = $this->repository->find($id);

View File

@@ -19,6 +19,7 @@ use FOS\RestBundle\Controller\Annotations\RouteResource;
use FOS\RestBundle\Request\ParamFetcherInterface; use FOS\RestBundle\Request\ParamFetcherInterface;
use FOS\RestBundle\View\View; use FOS\RestBundle\View\View;
use FOS\RestBundle\View\ViewHandlerInterface; use FOS\RestBundle\View\ViewHandlerInterface;
use Nelmio\ApiDocBundle\Annotation\Security as ApiSecurity;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Swagger\Annotations as SWG; use Swagger\Annotations as SWG;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
@@ -70,10 +71,10 @@ class UserController extends BaseApiController
* *
* @Security("is_granted('view_user')") * @Security("is_granted('view_user')")
* *
* @param ParamFetcherInterface $paramFetcher * @ApiSecurity(name="apiUser")
* @return Response * @ApiSecurity(name="apiToken")
*/ */
public function cgetAction(ParamFetcherInterface $paramFetcher) public function cgetAction(ParamFetcherInterface $paramFetcher): Response
{ {
$query = new UserQuery(); $query = new UserQuery();
@@ -105,7 +106,7 @@ class UserController extends BaseApiController
* *
* @SWG\Response( * @SWG\Response(
* response=200, * response=200,
* description="Return one user entity. Required permission: view_user", * description="Return one user entity.",
* @SWG\Schema(ref="#/definitions/UserEntity"), * @SWG\Schema(ref="#/definitions/UserEntity"),
* ) * )
* @SWG\Parameter( * @SWG\Parameter(
@@ -116,10 +117,10 @@ class UserController extends BaseApiController
* required=true, * required=true,
* ) * )
* *
* @param int $id * @ApiSecurity(name="apiUser")
* @return Response * @ApiSecurity(name="apiToken")
*/ */
public function getAction($id) public function getAction(int $id): Response
{ {
$user = $this->repository->find($id); $user = $this->repository->find($id);
@@ -136,4 +137,26 @@ class UserController extends BaseApiController
return $this->viewHandler->handle($view); return $this->viewHandler->handle($view);
} }
/**
* Return the current user entity
*
* @SWG\Response(
* response=200,
* description="Return the current user entity.",
* @SWG\Schema(ref="#/definitions/UserEntity"),
* )
*
* @Rest\Get(path="/users/me")
*
* @ApiSecurity(name="apiUser")
* @ApiSecurity(name="apiToken")
*/
public function meAction(): Response
{
$view = new View($this->getUser(), 200);
$view->getContext()->setGroups(['Default', 'Entity', 'User']);
return $this->viewHandler->handle($view);
}
} }

View File

@@ -72,6 +72,22 @@ class UserControllerTest extends APIControllerBaseTest
$this->assertIsArray($result); $this->assertIsArray($result);
$this->assertStructure($result); $this->assertStructure($result);
self::assertEquals('1', $result['id']);
self::assertEquals('CFO', $result['title']);
self::assertEquals('Clara Haynes', $result['alias']);
}
public function testGetMyProfile()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_SUPER_ADMIN);
$this->assertAccessIsGranted($client, '/api/users/me');
$result = json_decode($client->getResponse()->getContent(), true);
$this->assertIsArray($result);
$this->assertStructure($result);
self::assertEquals('6', $result['id']);
self::assertEquals('Super Administrator', $result['title']);
self::assertEquals('', $result['alias']);
} }
public function testNotFound() public function testNotFound()