phpsymfonyfosuserbundlesymfony4fosoauthserverbundle

Symfony multiple user types and custom user provider


I am designing architecture and database for an application similar to Uber. And I am stuck with designing User entity and creating custom User provider. There are 2 types of User driver and customer. User can register his emails as both driver and customer.

Is there good reading or projects where I can learn about best approach to creating app with multiple user types.

This is what I currently came up with:

I started with abstract User entity

/**
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="discr", type="string")
 * @ORM\DiscriminatorMap({"customer" = "Customer", "driver" = "Driver"})
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
 */
abstract class User {
     /**
      * @ORM\Id()
      * @ORM\GeneratedValue()
      * @ORM\Column(type="integer")
      */
      protected $id;
}

and Customer class looks like:

class Customer extends User
{
  /**
   * @ORM\Id()
   * @ORM\GeneratedValue()
   * @ORM\Column(type="integer")
   */
  protected $id;

  /**
   * @var string
   *
   * @ORM\Column(type="string", nullable=false, name="email", unique=true)
   */
  protected $email;

Driver class:

 class Driver extends User
   {
  /**
   * @ORM\Id()
   * @ORM\GeneratedValue()
   * @ORM\Column(type="integer")
   */
  protected $id;

  /**
   * @var string
   *
   * @ORM\Column(type="string", nullable=false, name="email", unique=true)
   */
  protected $email;

However I am not sure if this User entities structure is a good idea. But if I continue with this db model what would be best strategy for user authorisation? I am planning to use FOS oAuth for security. Is it better to have separate firewalls for each type of User?


Solution

  • you should consider a roles based approach instead of basing your design on type

    if you need polymorphic queries and relationships, the joined strategy is probably your best option

    like that, a User can register his emails as both driver and customer. User A has roles [DRIVER_ROLE, USER_ROLE]