Currently I am developing a web application with Symfony 4.2.
The application needs to be modular. So I am using bundles. Currently I am facing a problem with authentication.
Let's assume there are two bundles:
- CustomerLoginBundle
- AdminBundle
Both bundles implement their own authentication using a guard.
The application can have multiple use-cases. Some of those won't use the CustomerLoginBundle, some will.
How is it possible, to dynamically load the corresponding security configuration on runtime depending on which of the above bundles are installed?
Examples:
Both bundles are installed, security config should look like this:
encoders:
Acme\Bundle\AdminBundle\Entity\Admin:
algorithm: bcrypt
Acme\Bundle\CustomerLoginBundle\Entity\Customer:
algorithm: bcrypt
providers:
admin_provider:
entity:
class: Acme\Bundle\AdminBundle\Entity\Admin
property: email
customer_provider:
entity:
class: Acme\Bundle\CustomerLoginBundle\Entity\Customer
property: email
chain_provider:
chain:
providers: [customer_provider, admin_provider]
firewalls:
main:
provider: chain_provider
anonymous: true
guard:
entry_point: Acme\Bundle\CustomerLoginBundle\Security\CustomerFormAuthenticator
authenticators:
- Acme\Bundle\AdminBundle\Security\AdminFormAuthenticator
- Acme\Bundle\CustomerLoginBundle\Security\CustomerFormAuthenticator
Only Admin Bundle is installed:
encoders:
Acme\Bundle\AdminBundle\Entity\Admin:
algorithm: bcrypt
providers:
admin_provider:
entity:
class: Acme\Bundle\AdminBundle\Entity\Admin
property: email
chain_provider:
chain:
providers: [admin_provider]
firewalls:
main:
provider: chain_provider
anonymous: true
guard:
authenticators:
- Acme\Bundle\AdminBundle\Security\AdminFormAuthenticator
I mean I could manually set configuration for every use case for itself, but that seems to be a bit lazy solution to me.