I
I
Ivan2014-09-13 17:18:33
symfony
Ivan, 2014-09-13 17:18:33

How to make dynamic subdomains (multiple dynamic firewalls, routing, etc.) in symfony2?

I am doing a service.
In addition to the frontend and backend parts, there is also a dashboard (each company has its own) with its own authorization and other goodies.
Now the dashboard opens at site.com/dashboard/..., the firewall is the same for all, the data in the controllers is displayed depending on $company_id = $this->getUser()->getCompany()->getId()
I want to transfer the dashboard to subdomains like companyname.site.com/...
I configured nginx so that all requests like companyname.site.com/ go to site.com/.
Now the questions:
1) Now for the tests, 1 firewall is manually registered for the test company, which has slug = democompany. How to create dynamic firewalls for all hosts like companyname.site.com?

security.yml
security:
  ...
  providers:
    dashboard_user_provider:
      id: acme.dashboard.user_provider
  firewalls:
    ...
    dashboard:
      host: democompany.%domain%
      pattern: ^/
      form_login:
        login_path: /login
        check_path: /login_check
        provider: dashboard_user_provider
        csrf_provider: form.csrf_provider
        default_target_path: /
      logout:
        path: /logout
        target: /
      anonymous: true
      remember_me:
        key:      "%secret%"
        lifetime: 31536000 # 365 days in seconds
        path:     /
        domain:   ~
  access_control:
    ...
    - { host: democompany.%domain%, path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { host: democompany.%domain%, path: ^/, role: [ROLE_DASHBOARD_USER] }

2) To limit the data selection within the current company in each controller, I now do this:
public function listAction($company_slug) {
    if ($company_slug !== $this->getUser()->getCompany()->getSlug()) {
        throw $this->createNotFoundException(sprintf("No data found"));
    }
    
    ...
    ...
}

And it's a no brainer that this is shitty code.
At what level to do such checks?
Surfing on Google and Github did not give anything.
Maybe someone has already done this? Or seen an example?
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Senkevich, 2014-09-13
@0neS

Take a look here knpuniversity.com/screencast/question-answer-day/s... - it seems to be what you need.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question