The best way to do this is to use (as phpisuber said) dependency injection through a service container . But instead of introducing the entire container (which is considered bad practice, as it makes your class less susceptible to testing and breaking the link), you should introduce the security.context service as follows:
acme_foo.bar_service: class: %acme_foo.bar_service.class% arguments: - @security.context
Your service might be something like this:
<?php namespace Acme\FooBundle\Service; use Symfony\Component\Security\Core\SecurityContext; class BarService { protected $context; public function __construct($context) { $this->context = $context; } public function doSomething() { return $this->context->isGranted('ROLE_USER'); } }
source share