FOSUserBundle Integration
Installing the Bundle
The installation procedure of the FOSUserBundle is described in the main Symfony docs
FOSUserBundle のインストール手順は、メインの Symfony ドキュメントに記載されています
You can:
あなたはできる:
- Skip step 3 (Create your User class)
and use the class provided in the next paragraph to set up serialization groups the correct way手順 3 (ユーザー クラスの作成) をスキップし、次の段落で提供されるクラスを使用して、シリアル化グループを正しい方法で設定します。
- Skip step 4 (Configure your application's security.yml)
if you are planning to use a JWT-based authentication using
LexikJWTAuthenticationBundleLexikJWTAuthenticationBundle を使用して JWT ベースの認証を使用する予定の場合は、ステップ 4 (アプリケーションの security.yml を構成する) をスキップします。
If you are using the API Platform Standard Edition, you will need to enable the form services in the symfony framework configuration options:
API Platform Standard Edition を使用している場合、symfony フレームワーク構成オプションでフォーム サービスを有効にする必要があります。
# api/config/packages/framework.yaml
framework:
form: { enabled: true }
Creating a User Entity with Serialization Groups
Here's an example of declaration of a Doctrine ORM User class.
There's also an example for a Doctrine MongoDB ODM.
You need to use serialization groups to hide some properties like plainPassword (only in read) and password. The properties
shown are handled with normalizationContext, while the properties
you can modify are handled with denormalizationContext.
Doctrine ORM User クラスの宣言の例を次に示します。Doctrine MongoDB ODM の例もあります。plainPassword (読み取りのみ) やパスワードなどのプロパティを非表示にするには、シリアライゼーション グループを使用する必要があります。表示されているプロパティは normalizationContext で処理されますが、変更できるプロパティは denormalizationContext で処理されます。
Create your User entity with serialization groups:
シリアル化グループを使用して User エンティティを作成します。
<?php
// api/src/Entity/User.php
namespace App\Entity;
use ApiPlatform\Metadata\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
use FOS\UserBundle\Model\UserInterface;
use Symfony\Component\Serializer\Annotation\Groups;
#[ORM\Entity]
#[ORM\Table(name: 'fos_user')]
#[ApiResource(
normalizationContext: ['groups' => ['user']],
denormalizationContext: ['groups' => ['user', 'user:write']],
)]
class User extends BaseUser
{
#[ORM\Id, ORM\Column, ORM\GeneratedValue]
protected ?int $id = null;
#[Groups("user")]
protected string $email;
#[ORM\Column(nullable: true)]
#[Groups("user")]
protected string $fullname;
#[Groups("user:write")]
protected string $plainPassword;
#[Groups("user")]
protected string $username;
public function setFullname(?string $fullname): void
{
$this->fullname = $fullname;
}
public function getFullname(): ?string
{
return $this->fullname;
}
public function isUser(?UserInterface $user = null): bool
{
return $user instanceof self && $user->id === $this->id;
}
}