0

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?

1 Answers1

1

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]

CodeIsLife
  • 1,205
  • 8
  • 14
  • 3
    To avoid misleadings/typos with new Symfony users it is better to correct in `ROLE_DRIVER`, `ROLE_CUSTOMER` your example, because In Symfony each role assigned to the User must begin with the `ROLE_` prefix. – gp_sflover Jul 27 '18 at 11:41
  • 1
    hi, thanks for your reply. it's very helpful. I have followup question tho. What if user has `aboutMe` property that needs to be changed depending on which profile (driver or customer) he switched to? –  Jul 27 '18 at 13:21
  • 1
    Then you'd better separate the 2 kind of users. A little reading on handling this with the associated firewalls: https://medium.com/@bpolaszek/expose-a-rest-api-to-different-kinds-of-users-with-api-platform-part-1-4-713f3c6db86f – Ben Jun 07 '19 at 09:12