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 Answer 1

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]

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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?
Then you'd better separate the 2 kind of users. A little reading on handling this with the associated firewalls: medium.com/@bpolaszek/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.