Skip to content

Commit c500601

Browse files
committed
Update FOSUser integration doc for v2
1 parent 9956bb8 commit c500601

File tree

1 file changed

+40
-45
lines changed

1 file changed

+40
-45
lines changed

core/fosuser-bundle.md

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,34 @@
11
# FOSUser Bundle integration
22

3-
This bundle is shipped with a bridge for the [FOSUserBundle](https://github.com/FriendsOfSymfony/FOSUserBundle). If the FOSUserBundle is enabled, this bridges registers to the persist, update and delete events to pass user objects to the UserManager, before redispatching the event.
3+
API Platform Core is shipped with a bridge for [FOSUserBundle](https://github.com/FriendsOfSymfony/FOSUserBundle). If the
4+
FOSUser bundle is enabled, this bridge will use its `UserManager` to create, update and delete user resources.
45

56
## Creating a `User` entity with serialization groups
67

7-
Here's an example of declaration of a [doctrine ORM User class](https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/index.rst#a-doctrine-orm-user-class). As shown you can use serialization groups to hide properties like `plainPassword` (only in read) and `password`. The properties shown are handled with the [`normalizationContext`](serialization-groups-and-relations.md#normalization), while the properties you can modify are handled with [`denormalizationContext`](serialization-groups-and-relations.md#denormalization).
8+
Here's an example of declaration of a [Doctrine ORM User class](https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/index.rst#a-doctrine-orm-user-class).
9+
You need to use serialization groups to hide some properties like `plainPassword` (only in read) and `password`. The properties
10+
shown are handled with the [`normalization_context`](serialization-groups-and-relations.md#normalization), while the properties
11+
you can modify are handled with [`denormalization_context`](serialization-groups-and-relations.md#denormalization).
812

913
First register the following service:
1014

11-
```yaml
12-
# app/config/services.yml
13-
14-
resource.user:
15-
parent: "api.resource"
16-
arguments: [ "AppBundle\Entity\User" ]
17-
calls:
18-
- method: "initNormalizationContext"
19-
arguments: [ { groups: [ "user_read" ] } ]
20-
- method: "initDenormalizationContext"
21-
arguments: [ { groups: [ "user_write" ] } ]
22-
tags: [ { name: "api.resource" } ]
23-
```
24-
25-
Then create your User entity with serialization groups:
26-
2715
```php
28-
<?php
16+
// src/AppBundle/Entity/User.php
2917

3018
namespace AppBundle\Entity;
3119

20+
use ApiPlatform\Core\Annotation\ApiResource;
3221
use Doctrine\ORM\Mapping as ORM;
33-
use FOS\UserBundle\Entity\User as BaseUser;
22+
use FOS\UserBundle\Model\User as BaseUser;
23+
use FOS\UserBundle\Model\UserInterface;
3424
use Symfony\Component\Serializer\Annotation\Groups;
35-
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
3625

3726
/**
3827
* @ORM\Entity
39-
*
40-
* @UniqueEntity("email")
41-
* @UniqueEntity("username")
28+
* @ApiResource(attributes={
29+
* "normalization_context"={"groups"={"user", "user-read"}},
30+
* "denormalization_context"={"groups"={"user", "user-write"}}
31+
* })
4232
*/
4333
class User extends BaseUser
4434
{
@@ -50,38 +40,43 @@ class User extends BaseUser
5040
protected $id;
5141

5242
/**
53-
* @var string The username of the author.
54-
*
55-
* @Groups({"user_read", "user_write"})
43+
* @Groups({"user"})
5644
*/
57-
protected $username;
45+
protected $email;
5846

5947
/**
60-
* @var string The email of the user.
61-
*
62-
* @Groups({"user_read", "user_write"})
48+
* @ORM\Column(type="string", length=255, nullable=true)
49+
* @Groups({"user"})
6350
*/
64-
protected $email;
51+
protected $fullname;
6552

6653
/**
67-
* @var string Plain password. Used for model validation. Must not be persisted.
68-
*
69-
* @Groups({"user_write"})
54+
* @Groups({"user-write"})
7055
*/
7156
protected $plainPassword;
7257

7358
/**
74-
* @var boolean Shows that the user is enabled
75-
*
76-
* @Groups({"user_read", "user_write"})
59+
* @Groups({"user"})
7760
*/
78-
protected $enabled;
61+
protected $username;
7962

80-
/**
81-
* @var array Array, role(s) of the user
82-
*
83-
* @Groups({"user_read", "user_write"})
84-
*/
85-
protected $roles;
63+
public function setFullname($fullname)
64+
{
65+
$this->fullname = $fullname;
66+
67+
return $this;
68+
}
69+
public function getFullname()
70+
{
71+
return $this->fullname;
72+
}
73+
74+
public function isUser(UserInterface $user = null)
75+
{
76+
return $user instanceof self && $user->id === $this->id;
77+
}
8678
}
8779
```
80+
81+
Previous chapter: [NelmioApiDocBundle integration](nelmio-api-doc.md)<br>
82+
Next chapter: [AngularJS integration](angular-integration.md)

0 commit comments

Comments
 (0)