Khaled Boussoffara
Khaled Boussoffara

Reputation: 1771

Symfony 5 generate authentication for multi users types

I'm working on a symfony 5 project .

I need to create multi user types : an organizer and a participant …

And i want to generate authentication for those users

I generated a user : php bin/console make:user

And then :

/**
 * @ORM\Entity(repositoryClass=UserRepository::class)
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="type", type="string")
 * @ORM\DiscriminatorMap({
 *     "organizer" = "Organizer",
 *     "participant" = "Participant",
 * })
 */
class User implements UserInterface

I created an Organizer entity : class Organizer extends User

I added a Participant entity : class Participant extends User

I will be thankful if someone explain how can i generate an authentication for those users Organizer and Participant using php bin/console make:auth

Upvotes: 0

Views: 487

Answers (1)

Ngatia Frankline
Ngatia Frankline

Reputation: 3216

I once did this where i had two user types as author and admin. Very briefly the admin had his/her own dashboard and the author too, with different entity. I used make:entity to make user and author entity and made sure their content were similar (but you can add to match your app needs). After generating the admin user via php bin/console make:user, i had to copy the user templates, controller e.t.c php files content and create author templates, controller e.t.c with its content, them make migrations/schema update. This is manual and i welcome automated/script generated solution.

admin entity

/**
 * @ORM\Entity(repositoryClass=UserRepository::class)
 * @ORM\Table(name="`user`")
 * @UniqueEntity(fields={"email"}, message="There is already an account with this email")
 * @ORM\HasLifecycleCallbacks()
 */
class User implements UserInterface
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private int $id;

    /**
     * @ORM\Column(type="string", length=180, unique=true)
     */
    private ?string $email;
}

author entity

/**
 * @ORM\Entity(repositoryClass=AuthorsRepository::class)
 * @ORM\Table(name="authors")
 * @UniqueEntity(fields={"email"}, message="There is already an Author account with this email")
 * @ORM\HasLifecycleCallbacks()
 */
class AllAuthors implements UserInterface
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private ?int $id;

    /**
     * @ORM\Column(type="string", length=255, unique=true)
     */
    private ?string $email;
}

For this to work you need to configure Multiple User Provider and Multiple Guard Authenticators (Separate or Shared Entry Point). You can learn this from symfony docs here.

Also read this article it may help Multiple authentication in Symfony 5 with LDAP and DB

Upvotes: 2

Related Questions