Symfony2: How to get Listener Listener configuration values ​​for another service?

I am working on creating an authentication provider for Symfony 2 that allows users to authenticate using a single-sign protocol called CAS .

My Authentication Listener exits from AbstractAuthenticationListener . One of the configuration options is check_path , which is the route / route that runs the authentication listener to authenticate the request.

I need check_path when I create the URL of the CAS server (so the CAS server knows where to return the user), which is easy since my custom Entry Point class is passed when it is designed in my Factory security.

The tough part is that I also need to check_path outside the listener, for example, during authentication inside my Authentication Provider . I need this because when the CAS server sends the user back to the application, it passes the “ticket” parameter, which needs to be checked. To check it, I send a curl request to the CAS server, which should contain a ticket, as well as the original check_path that was used.

As I wrote, I realized that I can get the current page request URL when I'm in the Authentication Provider (since it has a check_path that calls it anyway), but it seems to be turned off, and I would rather get the configuration value directly. to rebuild the service url. It also does not help me when I want to use check_path elsewhere, for example when creating an exit URL to a CAS server that also required check_path .

EDIT: The createAuthProvider AbstractFactory method is passed with both the configuration and the container, but I cannot change any of my services here because they are not yet part of the container. Maybe if I had a way to add a compiler pass after loading my services and somehow access the listener configuration?

+5
source share
2 answers

Can you pass check_path as a parameter to your listener?

If it is defined in your configuration or parameter file, you can pass it to your listener as follows:

 your_authentication_listener: class: YourBundle\Listener\AuthenticationListener arguments: ['%check_path%'] tags: ... 

(If I realized that you are right.)

0
source

You can make %check_path% (or the version with named names) a "normal" parameter: Inside DependencyInjection there are (by default) two classes responsible for defining and loading the package configuration. There you can also enter your configuration into your service container.

  • DependencyInjection\Configuration is where you determine which configurations are available in your bundle, what type they should be, etc.

  • DependencyInjection\YourBundleNameExtension is the place where you can load your configuration and add it to the service container.

If you haven't done anything yet, your load() extension method should look something like this:

 public function load(array $configs, ContainerBuilder $container) { $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); $loader = new Loader\YamlFileLoader( $container, new FileLocator(__DIR__.'/../Resources/config') ); $loader->load('services.yml'); } 

$config stores the configuration of your package as an array, so if you imagine that your YAML configuration file looks like this:

 your_bundle_name: check_path: foo 

Your $config will look like this:

 array( 'check_path' => 'foo' ) 

So now you need to add this configuration to the container. Inside your load() method, just add something like:

 $container->setParameter( 'my_bundle_name.check_path', $config['check_path'] ); 

Inside services.yml you can now use %my_bundle_name.check_path% , like any other parameter:

 my_bundle_name.security.authentication.provider: class: MyBundleName\Security\Core\Authentication\Provider\MyAuthenticationProvider arguments: ['%my_bundle_name.check_path%'] 

See the Symfony documentation [ 1 , 2 ] for more information.

0
source

Source: https://habr.com/ru/post/1202810/


All Articles