Search code examples
securitysymfony1sfguard

Two tables relation with sfDoctrineGuard user table (symfony)


Hy,

I have started in my web application a part who users needs to be autenticated to work with it. I have two tables related: Customer and Enterprise.... the first one are users who want to buy a product and the second one are "users" who want to sell products.

What is better way to do that? Relation 1:1 with user_table? how can i differentiate wich one user type is? Because user types only can edit some information and enterprise have acces to another modules...

Thanks a lot.


Solution

  • You could use the hasReference() function on the model.

    E.g. your model Customer has a relation to User like this:

    Customer:
      relations:
        user:
          local: id
          foreignID: id
          foreignAlias: customer
    

    Then you can test whether the user is of type customer (in the controller):

    $this->getUser()->getGuardUser()->hasReference('customer');
    

    To make this easier you can add this method to your myUser class:

    public function isCustomer() {
        return $this->getGuardUser()->hasReference('customer');
    }
    

    Same of course for Enterprise.

    Even using two different tables, you can make use of the hasCredential() method, and this is the easiest way if you only want to check for permission.
    If you want to access certain attributes of the enterprise and customer user you can also combine both approaches.

    Update:

    Well, assuming that Customers and Enterprise users have different permissions, I would go with the user group approach. This fits better to the group model of sfGuard. If you then know that a user is in group Customer, you know that the user has a reference to a customer object.

    But this means of course that you have to assign the the right group to a new user in order to work correctly.