Question

on my website developed with Symfony 2 I have a bundle to control my Raspberry Pi to do some automations at home but the website is online and I want to prohibit access to this bundle to everybody with a public IP, to hide links in my menu I do this in twig:

{% if (app.request.server.get("REMOTE_ADDR") == '192.168') or (app.request.server.get("REMOTE_ADDR") == '127.0.0.1')%} <li> <a href="{{ path('admin_rpi_homepage') }}">administration RPI</a> </li> {% endif %}

But i can still access by typing direct URL for all route, how can I modify that, I think about put if in every controller but that look a little bit "heavy"

Was it helpful?

Solution

You can use Voter to black list IP.

create your voter here : http://symfony.com/doc/current/cookbook/security/voters.html

In your controller something like this :

use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;

...
public function indexAction() {
    $securityContext = $this->get('security.context');
              $objectIdentity = new ObjectIdentity('class', 'Vendor\Bundle\Class');
              if (false === $securityContext->isGranted('VIEW', $objectIdentity)) {
                  return $this->redirect($this->generateUrl('_welcome'));
              }

...

Your route will be accessible, but controller will kick out the request and redirect it to home page

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top