Create a client when creating a user using the api

While creating a new user using the API, we also create a new client for the current user.
So the app which just create the user can use its newly created client to configure the app.

That new client is only return after creating the user.
When calling the endpoint /api/user to get user information, the new client information won’t be return.
This commit is contained in:
Jeremy Benoist 2017-06-07 23:23:28 +02:00
parent 7bb3aa3177
commit 0c00e52516
No known key found for this signature in database
GPG key ID: BCA73962457ACC3C
5 changed files with 82 additions and 13 deletions

View file

@ -9,6 +9,7 @@ use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Wallabag\UserBundle\Entity\User;
use Wallabag\ApiBundle\Entity\Client;
class UserRestController extends WallabagRestController
{
@ -97,29 +98,38 @@ class UserRestController extends WallabagRestController
->setStatusCode(JsonResponse::HTTP_BAD_REQUEST);
}
// create a default client
$client = new Client($user);
$client->setName('Default client');
$this->getDoctrine()->getManager()->persist($client);
$user->addClient($client);
$userManager->updateUser($user);
// dispatch a created event so the associated config will be created
$event = new UserEvent($user, $request);
$this->get('event_dispatcher')->dispatch(FOSUserEvents::USER_CREATED, $event);
return $this->sendUser($user, JsonResponse::HTTP_CREATED);
return $this->sendUser($user, 'user_api_with_client', JsonResponse::HTTP_CREATED);
}
/**
* Send user response.
*
* @param User $user
* @param int $status HTTP Status code to send
* @param User $user
* @param string $group Used to define with serialized group might be used
* @param int $status HTTP Status code to send
*
* @return JsonResponse
*/
private function sendUser(User $user, $status = JsonResponse::HTTP_OK)
private function sendUser(User $user, $group = 'user_api', $status = JsonResponse::HTTP_OK)
{
$json = $this->get('serializer')->serialize(
$user,
'json',
SerializationContext::create()->setGroups(['user_api'])
SerializationContext::create()->setGroups([$group])
);
return (new JsonResponse())

View file

@ -5,6 +5,9 @@ namespace Wallabag\ApiBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\OAuthServerBundle\Entity\Client as BaseClient;
use Wallabag\UserBundle\Entity\User;
use JMS\Serializer\Annotation\Groups;
use JMS\Serializer\Annotation\SerializedName;
use JMS\Serializer\Annotation\VirtualProperty;
/**
* @ORM\Table("oauth2_clients")
@ -23,6 +26,8 @@ class Client extends BaseClient
* @var string
*
* @ORM\Column(name="name", type="text", nullable=false)
*
* @Groups({"user_api_with_client"})
*/
protected $name;
@ -36,6 +41,14 @@ class Client extends BaseClient
*/
protected $accessTokens;
/**
* @var string
*
* @SerializedName("client_secret")
* @Groups({"user_api_with_client"})
*/
protected $secret;
/**
* @ORM\ManyToOne(targetEntity="Wallabag\UserBundle\Entity\User", inversedBy="clients")
*/
@ -78,4 +91,14 @@ class Client extends BaseClient
{
return $this->user;
}
/**
* @VirtualProperty
* @SerializedName("client_id")
* @Groups({"user_api_with_client"})
*/
public function getClientId()
{
return $this->getId().'_'.$this->getRandomId();
}
}

View file

@ -33,7 +33,7 @@
<table class="striped">
<tr>
<td>{{ 'developer.existing_clients.field_id'|trans }}</td>
<td><strong><code>{{ client.id }}_{{ client.randomId }}</code></strong></td>
<td><strong><code>{{ client.clientId }}</code></strong></td>
</tr>
<tr>
<td>{{ 'developer.existing_clients.field_secret'|trans }}</td>

View file

@ -6,6 +6,7 @@ use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation\Groups;
use JMS\Serializer\Annotation\XmlRoot;
use JMS\Serializer\Annotation\Accessor;
use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface;
use Scheb\TwoFactorBundle\Model\TrustedComputerInterface;
use FOS\UserBundle\Model\User as BaseUser;
@ -36,7 +37,7 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*
* @Groups({"user_api"})
* @Groups({"user_api", "user_api_with_client"})
*/
protected $id;
@ -45,21 +46,21 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf
*
* @ORM\Column(name="name", type="text", nullable=true)
*
* @Groups({"user_api"})
* @Groups({"user_api", "user_api_with_client"})
*/
protected $name;
/**
* @var string
*
* @Groups({"user_api"})
* @Groups({"user_api", "user_api_with_client"})
*/
protected $username;
/**
* @var string
*
* @Groups({"user_api"})
* @Groups({"user_api", "user_api_with_client"})
*/
protected $email;
@ -68,7 +69,7 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf
*
* @ORM\Column(name="created_at", type="datetime")
*
* @Groups({"user_api"})
* @Groups({"user_api", "user_api_with_client"})
*/
protected $createdAt;
@ -77,7 +78,7 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf
*
* @ORM\Column(name="updated_at", type="datetime")
*
* @Groups({"user_api"})
* @Groups({"user_api", "user_api_with_client"})
*/
protected $updatedAt;
@ -97,7 +98,8 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf
private $authCode;
/**
* @var bool Enabled yes/no
* @var bool
*
* @ORM\Column(type="boolean")
*/
private $twoFactorAuthentication = false;
@ -112,6 +114,14 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf
*/
protected $clients;
/**
* @see getFirstClient() below
*
* @Groups({"user_api_with_client"})
* @Accessor(getter="getFirstClient")
*/
protected $default_client;
public function __construct()
{
parent::__construct();
@ -288,4 +298,18 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf
{
return $this->clients;
}
/**
* Only used by the API when creating a new user it'll also return the first client (which was also created at the same time).
*
* @return Client
*/
public function getFirstClient()
{
if (empty($this->clients)) {
return $this->clients;
}
return $this->clients->first();
}
}

View file

@ -61,10 +61,16 @@ class UserRestControllerTest extends WallabagApiTestCase
$this->assertArrayHasKey('username', $content);
$this->assertArrayHasKey('created_at', $content);
$this->assertArrayHasKey('updated_at', $content);
$this->assertArrayHasKey('default_client', $content);
$this->assertEquals('wallabag@google.com', $content['email']);
$this->assertEquals('google', $content['username']);
$this->assertArrayHasKey('client_secret', $content['default_client']);
$this->assertArrayHasKey('client_id', $content['default_client']);
$this->assertEquals('Default client', $content['default_client']['name']);
$this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
$this->client->getContainer()->get('craue_config')->set('api_user_registration', 0);
@ -90,10 +96,16 @@ class UserRestControllerTest extends WallabagApiTestCase
$this->assertArrayHasKey('username', $content);
$this->assertArrayHasKey('created_at', $content);
$this->assertArrayHasKey('updated_at', $content);
$this->assertArrayHasKey('default_client', $content);
$this->assertEquals('wallabag@google.com', $content['email']);
$this->assertEquals('google', $content['username']);
$this->assertArrayHasKey('client_secret', $content['default_client']);
$this->assertArrayHasKey('client_id', $content['default_client']);
$this->assertEquals('Default client', $content['default_client']['name']);
$this->assertEquals('application/json', $client->getResponse()->headers->get('Content-Type'));
$client->getContainer()->get('craue_config')->set('api_user_registration', 0);