Skip to content

Commit

Permalink
Merge pull request #200 from Zales0123/me-endpoint
Browse files Browse the repository at this point in the history
Currently logged in customer endpoint
  • Loading branch information
pjedrzejewski authored Aug 7, 2017
2 parents fd15e1a + 448000e commit d226bbf
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 2 deletions.
29 changes: 27 additions & 2 deletions doc/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -756,21 +756,34 @@ paths:
- "users"
summary: "Request resetting password of user with passed email."
description: "Email with reset password path will be sent to user. Default path for password resetting is `/password-reset/{token}`. To change it, you need to override template `@SyliusShopApi\\Email\\passwordReset.html.twig`."
opearationId: "requestPasswordReset"
operationId: "requestPasswordReset"
consumes:
- "application/json"
parameters:
- name: "email"
in: "body"
description: "Email of user which want to reset password."
required: true
schama:
schema:
$ref: "#/definitions/RequestPasswordResetting"
responses:
204:
description: "Reset password request has been sent."
500:
description: "User with provided email has not been found."
/me:
get:
tags:
- "users"
summary: "Provides currently logged in user details."
operationId: "me"
responses:
200:
schema:
type: "object"
$ref: "#/definitions/LoggedInCustomerDetails"
500:
description: "There is no currently logged in user."

definitions:
PickupCartRequest:
Expand Down Expand Up @@ -1330,6 +1343,18 @@ definitions:
email:
type: "string"
example: "test@example.com"
LoggedInCustomerDetails:
type: "object"
properties:
firstName:
type: "string"
example: "Sherlock"
lastName:
type: "string"
example: "Holmes"
email:
type: "string"
example: "sherlock@holmes.com"
GeneralError:
type: "object"
properties:
Expand Down
52 changes: 52 additions & 0 deletions src/Controller/Customer/LoggedInCustomerDetailsAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Sylius\ShopApiPlugin\Controller\Customer;

use FOS\RestBundle\View\View;
use FOS\RestBundle\View\ViewHandlerInterface;
use Sylius\Component\Core\Model\ShopUserInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Webmozart\Assert\Assert;

final class LoggedInCustomerDetailsAction
{
/**
* @var ViewHandlerInterface
*/
private $viewHandler;

/**
* @var TokenStorageInterface
*/
private $tokenStorage;

/**
* @param ViewHandlerInterface $viewHandler
* @param TokenStorageInterface $tokenStorage
*/
public function __construct(ViewHandlerInterface $viewHandler, TokenStorageInterface $tokenStorage)
{
$this->viewHandler = $viewHandler;
$this->tokenStorage = $tokenStorage;
}

public function __invoke(Request $request): Response
{
/** @var ShopUserInterface $user */
$user = $this->tokenStorage->getToken()->getUser();

Assert::isInstanceOf($user, ShopUserInterface::class);

$customer = $user->getCustomer();

return $this->viewHandler->handle(View::create([
'firstName' => $customer->getFirstName(),
'lastName' => $customer->getLastName(),
'email' => $customer->getEmail(),
], Response::HTTP_OK));
}
}
4 changes: 4 additions & 0 deletions src/Resources/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ shop_api_register:
shop_api_checkout:
resource: "@ShopApiPlugin/Resources/config/routing/checkout.yml"
prefix: /shop-api/checkout

shop_api_customer:
resource: "@ShopApiPlugin/Resources/config/routing/customer.yml"
prefix: /shop-api
5 changes: 5 additions & 0 deletions src/Resources/config/routing/customer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
shop_api_me:
path: /me
methods: [GET]
defaults:
_controller: sylius.shop_api_plugin.controller.customer.logged_in_customer_details_action
7 changes: 7 additions & 0 deletions src/Resources/config/services/actions/customer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,12 @@
<argument type="service" id="fos_rest.view_handler" />
<argument type="service" id="tactician.commandbus" />
</service>

<service id="sylius.shop_api_plugin.controller.customer.logged_in_customer_details_action"
class="Sylius\ShopApiPlugin\Controller\Customer\LoggedInCustomerDetailsAction"
>
<argument type="service" id="fos_rest.view_handler" />
<argument type="service" id="security.token_storage" />
</service>
</services>
</container>
40 changes: 40 additions & 0 deletions tests/Controller/LoggedInCustomerDetailsActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Tests\Sylius\ShopApiPlugin\Controller\Customer;

use Lakion\ApiTestCase\JsonApiTestCase;
use Symfony\Component\HttpFoundation\Response;

final class LoggedInCustomerDetailsActionTest extends JsonApiTestCase
{
/**
* @test
*/
public function it_shows_currently_logged_in_customer_details()
{
$this->loadFixturesFromFile('customer.yml');

$data =
<<<EOT
{
"_username": "oliver@queen.com",
"_password": "123pa\$\$word"
}
EOT;

$this->client->request('POST', '/shop-api/login_check', [], [], ['CONTENT_TYPE' => 'application/json', 'ACCEPT' => 'application/json'], $data);

$response = json_decode($this->client->getResponse()->getContent(), true);
$this->client->setServerParameter('HTTP_Authorization', sprintf('Bearer %s', $response['token']));

$this->client->request('GET', '/shop-api/me', [], [], [
'CONTENT_TYPE' => 'application/json',
'ACCEPT' => 'application/json',
]);

$response = $this->client->getResponse();
$this->assertResponse($response, 'customer/logged_in_customer_details_response', Response::HTTP_OK);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"firstName": "Oliver",
"lastName": "Queen",
"email": "oliver@queen.com"
}

0 comments on commit d226bbf

Please sign in to comment.