8000 [Security] make it possible to override the default success/failure handler by fabpot · Pull Request #11993 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Security] make it possible to override the default success/failure handler #11993

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 25, 2014

Conversation

fabpot
Copy link
Member
@fabpot fabpot commented Sep 23, 2014
Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #5432, #9272, #10417, #11926
License MIT
Doc PR symfony/symfony-docs#4258

Overriding the default success/failure handler of the security firewalls is possible via the success_handler and failure_handler setting but this approach is not flexible as it does not allow you to get the options/provider key.

To sum up the problem:

  • Overriding the default success/failure handler is possible via a service;
  • When not overridden, the default success/failure handler gets options and the provider key;
  • Those options and the provider key are injected by the factory as they are dynamic (they depend on the firewall and the provider key), so getting those options/provider key is not possible for a custom service that is only configured via the container configuration;
  • Extending the default handler does not help as the injection mechanism is only triggered when no custom provider is set;
  • Wrapping the default handler is not possible as the service id is dynamic.

... and of course we need to keep BC and make it work for people extending the default handler but also for people just using the interface.

Instead of the current PR, I propose this slightly different approach. It's not perfect, but given the above constraint, I think this is an acceptable trade-of.

So, several use cases:

  • Using the default handler (no change);
  • Using a custom handler that implements AuthenticationSuccessHandlerInterface directly and does not need any options (no change);
  • Using a custom handler that needs the options/provider key (that's the new use case this PR supports).

This PR introduces 2 new classes that wrap custom handlers. If those classes define the setOptions() and/or setProviderKey() methods, they are automatically called with the correct arguments. Yours handler does not need to extend the default handler DefaultAuthentication*Handler, but doing so helps as the setters are already defined there.

@fabpot
Copy link
Member Author
fabpot commented Sep 23, 2014

This implementation does not work as we need to introspect the custom handler definition, which we cannot as we are in the security bundle extension at this point without any access to externally defined services. The only option would be to move some code to a compiler pass.

@fabpot fabpot force-pushed the custom-success-failure-handlers branch 2 times, most recently from 842360c to 13d37e7 Compare September 23, 2014 09:06
@fabpot fabpot force-pushed the custom-success-failure-handlers branch from 13d37e7 to 36116fc Compare September 23, 2014 09:07
@fabpot
Copy link
Member Author
fabpot commented Sep 23, 2014

I'm going to rewrite this PR with a different approach. Stay tuned.

@ogizanagi
Copy link
Contributor

@fabpot : This ties my previous suggestion and should cover all cases 👍
Thank you for spending some time on this issue.

@fabpot fabpot force-pushed the custom-success-failure-handlers branch 2 times, most recently from 167c3f2 to f1e4d45 Compare September 23, 2014 09:38
@fabpot
Copy link
Member Author
fabpot commented Sep 23, 2014

@ogizanagi The new implementation I've just merged should work properly now and there is no ugly hacks/conventions anymore.

There is one slight side-effect: as we are passing options and the provider key, you must not use the same custom handler in two different firewalls. It was not a problem in the current implementation as the handler was independent of the firewall, but that's not the case anymore. The documentation should mention this to avoid hard to debug problems.

@fabpot fabpot force-pushed the custom-success-failure-handlers branch from 4c08db8 to 810eeaf Compare September 24, 2014 06:07
@fabpot
Copy link
Member Author
fabpot commented Sep 24, 2014

ping @symfony/deciders

@Seldaek
Copy link
Member
Seldaek commented Sep 24, 2014

Seems ok to me, not very symfonish as we usually don't call custom things custom_lala but just lala but BC is BC so I'd guess 👍

@@ -34,6 +34,12 @@ class DefaultAuthenticationFailureHandler implements AuthenticationFailureHandle
protected $httpUtils;
protected $logger;
protected $options;
protected $defaultOptions = array(
8000 Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

couldn't it be private instead ?

@fabpot fabpot merged commit 810eeaf into symfony:master Sep 25, 2014
fabpot added a commit that referenced this pull request Sep 25, 2014
…ccess/failure handler (fabpot)

This PR was merged into the 2.6-dev branch.

Discussion
----------

[Security] make it possible to override the default success/failure handler

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #5432, #9272, #10417, #11926
| License       | MIT
| Doc PR        | symfony/symfony-docs#4258

Overriding the default success/failure handler of the security firewalls is possible via the `success_handler` and `failure_handler` setting but this approach is not flexible as it does not allow you to get the options/provider key.

To sum up the problem:

* Overriding the default success/failure handler is possible via a service;
* When not overridden, the default success/failure handler gets options and the provider key;
* Those options and the provider key are injected by the factory as they are dynamic (they depend on the firewall and the provider key), so getting those options/provider key is not possible for a custom service that is only configured via the container configuration;
* Extending the default handler does not help as the injection mechanism is only triggered when no custom provider is set;
* Wrapping the default handler is not possible as the service id is dynamic.

... and of course we need to keep BC and make it work for people extending the default handler but also for people just using the interface.

Instead of the current PR, I propose this slightly different approach. It's not perfect, but given the above constraint, I think this is an acceptable trade-of.

So, several use cases:

 * Using the default handler (no change);
 * Using a custom handler that implements `AuthenticationSuccessHandlerInterface` directly and does not need any options (no change);
 * Using a custom handler that needs the options/provider key (that's the new use case this PR supports).

This PR introduces 2 new classes that wrap custom handlers. If those classes define the `setOptions()` and/or `setProviderKey()` methods, they are automatically called with the correct arguments. Yours handler does not need to extend the default handler `DefaultAuthentication*Handler`, but doing so helps as the setters are already defined there.

Commits
-------

810eeaf [Security] made it possible to override the default success/failure handler (take 2)
36116fc [Security] made it possible to override the default success/failure handler
@fabpot fabpot deleted the custom-success-failure-handlers branch September 26, 2014 06:44
@ureimers
Copy link
Contributor
ureimers commented Oct 1, 2014

I like this aproach. It's a tricky problem but this seems to solve it. Thanks Fabien! 👍
Looking forward for Symfony Live in Berlin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants
0