From 644d0420ceaa5bc485ce9dd0099ff39cc5d0a574 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Tue, 28 Sep 2021 16:23:24 +0200 Subject: [PATCH 1/2] creation of endpoints POST, PUT, PATCH, but still errors --- .../ChillThirdPartyExtension.php | 18 +++- .../Entity/ThirdParty.php | 10 +- .../Security/Voter/ThirdPartyVoter.php | 2 + .../chill.api.specs.yaml | 97 ++++++++++++++++++- 4 files changed, 116 insertions(+), 11 deletions(-) diff --git a/src/Bundle/ChillThirdPartyBundle/DependencyInjection/ChillThirdPartyExtension.php b/src/Bundle/ChillThirdPartyBundle/DependencyInjection/ChillThirdPartyExtension.php index 9b23222c6..2fd0f723d 100644 --- a/src/Bundle/ChillThirdPartyBundle/DependencyInjection/ChillThirdPartyExtension.php +++ b/src/Bundle/ChillThirdPartyBundle/DependencyInjection/ChillThirdPartyExtension.php @@ -59,7 +59,7 @@ class ChillThirdPartyExtension extends Extension implements PrependExtensionInte 'class' => \Chill\ThirdPartyBundle\Entity\ThirdParty::class, 'name' => 'thirdparty', 'base_path' => '/api/1.0/thirdparty/thirdparty', - 'base_role' => \Chill\ThirdPartyBundle\Security\Authorization\ThirdPartyVoter::class, + //'base_role' => \Chill\ThirdPartyBundle\Security\Authorization\ThirdPartyVoter::SHOW, //'controller' => \Chill\ThirdPartyBundle\Controller\ThirdPartyApiController::class, 'actions' => [ '_index' => [ @@ -67,19 +67,31 @@ class ChillThirdPartyExtension extends Extension implements PrependExtensionInte Request::METHOD_GET => true, Request::METHOD_HEAD => true, Request::METHOD_POST => true, + Request::METHOD_PATCH => true, + Request::METHOD_PUT => true + ], + 'roles' => [ + Request::METHOD_GET => \Chill\ThirdPartyBundle\Security\Voter\ThirdPartyVoter::SHOW, + Request::METHOD_HEAD => \Chill\ThirdPartyBundle\Security\Voter\ThirdPartyVoter::SHOW, + Request::METHOD_POST => \Chill\ThirdPartyBundle\Security\Voter\ThirdPartyVoter::CREATE, + Request::METHOD_PUT => \Chill\ThirdPartyBundle\Security\Voter\ThirdPartyVoter::CREATE, + Request::METHOD_PATCH => \Chill\ThirdPartyBundle\Security\Voter\ThirdPartyVoter::CREATE ], ], '_entity' => [ 'methods' => [ Request::METHOD_GET => true, Request::METHOD_HEAD => true, - Request::METHOD_POST=> true, + Request::METHOD_POST => true, + Request::METHOD_PUT => true, + Request::METHOD_PATCH => true ], 'roles' => [ Request::METHOD_GET => \Chill\ThirdPartyBundle\Security\Voter\ThirdPartyVoter::SHOW, Request::METHOD_HEAD => \Chill\ThirdPartyBundle\Security\Voter\ThirdPartyVoter::SHOW, Request::METHOD_POST => \Chill\ThirdPartyBundle\Security\Voter\ThirdPartyVoter::CREATE, - + Request::METHOD_PUT => \Chill\ThirdPartyBundle\Security\Voter\ThirdPartyVoter::CREATE, + Request::METHOD_PATCH => \Chill\ThirdPartyBundle\Security\Voter\ThirdPartyVoter::CREATE ], ] ] diff --git a/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php b/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php index 0b0b3e56b..842e57b8b 100644 --- a/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php +++ b/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php @@ -60,7 +60,7 @@ class ThirdParty * @var string * @ORM\Column(name="name", type="string", length=255) * @Assert\Length(min="2") - * @Groups({"read"}) + * @Groups({"read", "write"}) */ private $name; @@ -69,6 +69,7 @@ class ThirdParty * @var string * @ORM\Column(name="name_company", type="string", length=255, nullable=true) * @Assert\Length(min="3") + * @Groups({"read", "write"}) */ private $nameCompany; @@ -77,6 +78,7 @@ class ThirdParty * @var string * @ORM\Column(name="acronym", type="string", length=64, nullable=true) * @Assert\Length(min="2") + * @Groups({"read", "write"}) */ private $acronym; @@ -130,7 +132,7 @@ class ThirdParty * @Assert\Regex("/^([\+{1}])([0-9\s*]{4,20})$/", * message="Invalid phone number: it should begin with the international prefix starting with ""+"", hold only digits and be smaller than 20 characters. Ex: +33123456789" * ) - * @Groups({"read"}) + * @Groups({"read", "write"}) */ private $telephone; @@ -138,7 +140,7 @@ class ThirdParty * @var string|null * @ORM\Column(name="email", type="string", length=255, nullable=true) * @Assert\Email(checkMX=false) - * @Groups({"read"}) + * @Groups({"read", "write"}) */ private $email; @@ -147,7 +149,7 @@ class ThirdParty * @ORM\ManyToOne(targetEntity="\Chill\MainBundle\Entity\Address", * cascade={"persist", "remove"}) * @ORM\JoinColumn(nullable=true, onDelete="SET NULL") - * @Groups({"read"}) + * @Groups({"read", "write"}) */ private $address; diff --git a/src/Bundle/ChillThirdPartyBundle/Security/Voter/ThirdPartyVoter.php b/src/Bundle/ChillThirdPartyBundle/Security/Voter/ThirdPartyVoter.php index a1ce94e25..986bfb2b9 100644 --- a/src/Bundle/ChillThirdPartyBundle/Security/Voter/ThirdPartyVoter.php +++ b/src/Bundle/ChillThirdPartyBundle/Security/Voter/ThirdPartyVoter.php @@ -61,6 +61,8 @@ class ThirdPartyVoter extends AbstractChillVoter implements ProvideRoleHierarchy if (!$user instanceof User) { return false; } + + return true; $centers = $this->authorizationHelper ->getReachableCenters($user, new Role($attribute)); diff --git a/src/Bundle/ChillThirdPartyBundle/chill.api.specs.yaml b/src/Bundle/ChillThirdPartyBundle/chill.api.specs.yaml index 957bf9324..c5d9346ba 100644 --- a/src/Bundle/ChillThirdPartyBundle/chill.api.specs.yaml +++ b/src/Bundle/ChillThirdPartyBundle/chill.api.specs.yaml @@ -8,17 +8,47 @@ servers: - url: "/api" description: "Your current dev server" +components: + schemas: + Thirdparty: + type: object + properties: + id: + type: integer + readOnly: true + name: + type: string + nameCompany: + type: string + acronym: + type: string + email: + type: string + paths: /1.0/thirdparty/thirdparty.json: - get: + post: tags: - thirdparty - summary: Return a list of all thirdparty items + summary: Create a single thirdparty + requestBody: + description: "A thirdparty" + required: true + content: + application/json: + schema: + $ref: "#/components/schemas/Thirdparty" responses: 200: - description: "ok" - 401: + description: "OK" + content: + application/json: + schema: + $ref: "#/components/schemas/Thirdparty" + 403: description: "Unauthorized" + 422: + description: "Invalid data" /1.0/thirdparty/thirdparty/{id}.json: get: @@ -41,3 +71,62 @@ paths: description: "not found" 401: description: "Unauthorized" + patch: + tags: + - thirdparty + summary: "Alter a thirdparty" + parameters: + - name: id + in: path + required: true + description: The thirdparty's id + schema: + type: integer + format: integer + minimum: 1 + requestBody: + description: "A thirdparty" + required: true + content: + application/json: + schema: + $ref: "#/components/schemas/Thirdparty" + responses: + 401: + description: "Unauthorized" + 404: + description: "Not found" + 200: + description: "OK" + 422: + description: "Object with validation errors" + put: + tags: + - thirdparty + summary: Edit an existing thirdparty + parameters: + - name: id + in: path + required: true + description: The thirdparty's id + schema: + type: integer + format: integer + minimum: 1 + requestBody: + required: true + content: + application/json: + schema: + $ref: "#/components/schemas/Thirdparty" + responses: + 401: + description: "Unauthorized" + 404: + description: "Not found" + 200: + description: "OK" + 422: + description: "Unprocessable entity (validation errors)" + 400: + description: "Bad Request" From fc1dd499c48b3acc160f292e3b756ad4f6601c11 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Wed, 29 Sep 2021 11:41:08 +0200 Subject: [PATCH 2/2] POST, PUT, PATCH working for thirdparty and added to swagger. Assert for 'centers'(temporarily) removed from thirdparty entity --- .../ChillThirdPartyExtension.php | 16 ------------ .../Entity/ThirdParty.php | 7 +++-- .../chill.api.specs.yaml | 26 ++++++++++++++++--- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/Bundle/ChillThirdPartyBundle/DependencyInjection/ChillThirdPartyExtension.php b/src/Bundle/ChillThirdPartyBundle/DependencyInjection/ChillThirdPartyExtension.php index 2fd0f723d..5ebac821b 100644 --- a/src/Bundle/ChillThirdPartyBundle/DependencyInjection/ChillThirdPartyExtension.php +++ b/src/Bundle/ChillThirdPartyBundle/DependencyInjection/ChillThirdPartyExtension.php @@ -62,22 +62,6 @@ class ChillThirdPartyExtension extends Extension implements PrependExtensionInte //'base_role' => \Chill\ThirdPartyBundle\Security\Authorization\ThirdPartyVoter::SHOW, //'controller' => \Chill\ThirdPartyBundle\Controller\ThirdPartyApiController::class, 'actions' => [ - '_index' => [ - 'methods' => [ - Request::METHOD_GET => true, - Request::METHOD_HEAD => true, - Request::METHOD_POST => true, - Request::METHOD_PATCH => true, - Request::METHOD_PUT => true - ], - 'roles' => [ - Request::METHOD_GET => \Chill\ThirdPartyBundle\Security\Voter\ThirdPartyVoter::SHOW, - Request::METHOD_HEAD => \Chill\ThirdPartyBundle\Security\Voter\ThirdPartyVoter::SHOW, - Request::METHOD_POST => \Chill\ThirdPartyBundle\Security\Voter\ThirdPartyVoter::CREATE, - Request::METHOD_PUT => \Chill\ThirdPartyBundle\Security\Voter\ThirdPartyVoter::CREATE, - Request::METHOD_PATCH => \Chill\ThirdPartyBundle\Security\Voter\ThirdPartyVoter::CREATE - ], - ], '_entity' => [ 'methods' => [ Request::METHOD_GET => true, diff --git a/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php b/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php index 842e57b8b..30f28791b 100644 --- a/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php +++ b/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php @@ -96,7 +96,7 @@ class ThirdParty * @ORM\Column(name="types", type="json", nullable=true) * @Assert\Count(min=1) */ - private $type; + private $types; /** * Contact Persons: One Institutional ThirdParty has Many Contact Persons @@ -170,7 +170,6 @@ class ThirdParty * @var Collection * @ORM\ManyToMany(targetEntity="\Chill\MainBundle\Entity\Center") * @ORM\JoinTable(name="chill_3party.party_center") - * @Assert\Count(min=1) */ private $centers; @@ -327,7 +326,7 @@ class ThirdParty * @param array|null $type * @return ThirdParty */ - public function setType(array $type = null) + public function setTypes(array $type = null) { // remove all keys from the input data $this->type = \array_values($type); @@ -340,7 +339,7 @@ class ThirdParty * * @return array|null */ - public function getType() + public function getTypes() { return $this->type; } diff --git a/src/Bundle/ChillThirdPartyBundle/chill.api.specs.yaml b/src/Bundle/ChillThirdPartyBundle/chill.api.specs.yaml index c5d9346ba..50f0af70f 100644 --- a/src/Bundle/ChillThirdPartyBundle/chill.api.specs.yaml +++ b/src/Bundle/ChillThirdPartyBundle/chill.api.specs.yaml @@ -16,14 +16,32 @@ components: id: type: integer readOnly: true + type: + type: string + enum: + - "thirdparty" name: type: string - nameCompany: - type: string - acronym: - type: string email: type: string + telephone: + type: string + comment: + type: string + # centers: + # type: array + # items: + # type: object + # $ref: "#/components/schemas/CenterById" + # CenterById: + # type: object + # properties: + # type: + # type: string + # enum: + # - "Center" + # id: + # type: integer paths: /1.0/thirdparty/thirdparty.json: