-1

I started learning Symfony and already know how to create a user and registration / login forms for him using php bin/console make:user php bin/console make:auth, but I need to have several types of users like Client, Company, Admin and what if I try go to example.com/company, we were thrown to the login window for the Company account (I’m now thrown to the login form for the User).

After searching I found these questions Symfony / Doctrine - Multiple Users Types and Symfony: Firewalls, multiple login forms, but I did not understand what to do next and how to combine them. Please tell me.

1
  • Hi Didra and welcom on SO, plesae have a look to this page stackoverflow.com/help/how-to-ask to improve your question. We will be happy to answer if you provide a prescise question including coding examples. Commented Jun 7, 2021 at 6:38

1 Answer 1

0

I think better solution for You will be using roles to determine them ;)

You can have one entity and make some getters like:

public function isAdmin():bool
{
    return in_array(self::ROLE_ADMIN, $this->roles, true);
}

And if you want split roles to different path prefixes you can have one firewall eg: app that matches all your application and next make correct access_controll rules like this:

  access_control:
    - { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/admin, roles: ROLE_ADMIN }
    - { path: ^/company, roles: ROLE_CLIENT }

And are You sure company is another user class or permission? Maybe better assign admin to company and allow them to access this companies? ;)

If you realy want to split them you could use doctrine discriminator, eg. create Abstract user and next 3 classes extending them, you can configure security using that abstract class.

Or create 3 providers, 3 firewalls, and same access_controll like above

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.