dispatch:beforeExecuteRoute and return false [BUG]:
Describe the bug If we have two listeners "dispatch:beforeExecuteRoute" and first listener "return false" and second listener don't "return false" then dispatcher don't stop. In documentation is written: "If we do not have access, then the method will return false stopping the execution, right after we forward the user to the home page." (https://docs.phalcon.io/4.0/en/tutorial-invo)
To Reproduce
$container->set(
'dispatcher',
function () {
$eventsManager = new Manager();
$eventsManager->attach(
'dispatch:beforeExecuteRoute',
new SecurityPlugin()
);
$eventsManager->attach(
'dispatch:beforeException',
new NotFoundPlugin()
);
$containerspatcher = new Dispatcher();
$containerspatcher->setEventsManager($eventsManager);
return $containerspatcher;
}
);
class SecurityPlugin extends Injectable
{
public function beforeExecuteRoute(
Event $event,
Dispatcher $containerspatcher
) {
return false;
}
}
class NotFoundPlugin extends Injectable
{
public function beforeExecuteRoute(
Event $event,
Dispatcher $containerspatcher
) {
// different than false
return true;
}
}
Expected behavior Follow documentation "return false" should stop dispatcher.
Details
- Phalcon version: 4.0.0 (also 3.4.1)
- PHP Version: 7.4 (7.2)
- Operating System: Linux Debian
- Installation type: Compiling from source in Docker
- Server: Nginx
- Other related info (Database, table schema): MySQL 5.5
Hello! It would be great this bug was labelled with highest priority because it presents since 2.x, can be easily reproduced in plugins and also in middleware of micro application (just returning false has no effect), and was reported many times. It really spoils the advantages of using dispatcher events.
@zak-wojtek77
On the example above the NotFoundPlugin is attached to dispatch:beforeException but it implements beforeExecuteRoute and not beforeException. Is this an oversight or a copy paste error?
Using the above example if I attach both listeners to the beforeExecuteRoute and return false and true respectively, both listeners get executed.
Your question/request is that the second listener should stop executing after a false is returned?
@zak-wojtek77 @kirill-voronov I think I figured this out.
Ignore the fact that in the example (first post) we bind to different events. Let's assume that both listeners attach to the same event and we want to stop execution when the first one returns false.
Right now, returning false will not stop the execution, the second listener will run.
So here is what is going on. The dispatcher tries to find all the attached listeners for that particular event. It finds 2 in our case. Then the Events Manager loops through them and runs the method in the handler (i.e. beforeExecuteRoute). Because that is a loop, the return value of the first handler is overwritten by the second one, and therefore we see what you guys are looking at.
I am afraid to change that part of the code to break the loop when we see a false returned because it could very well break a lot of applications. Right now I do not know what the side effects will be since the Dispatcher and Events tests are not thorough and I do not have confidence on making that change to be honest.
The easiest way to achieve what you are looking for is to use stop on the event like this:
public function beforeExecuteRoute(Event $event, Dispatcher $dispatcher)
{
$event->stop();
return true;
}
We will need to change the documents for sure, but the above will stop execution of the listeners/events when you need to.
Down the line in future 4.x versions we will address the Dispatcher but not right now. Once that part of the framework is refactored we will have a lot more tests and thus more confidence in fixing stuff like this.
Please confirm if the above is working OK for your needs and I can make the necessary changes to the documentation so that others can benefit from this.
On the example above the
NotFoundPluginis attached todispatch:beforeExceptionbut it implementsbeforeExecuteRouteand notbeforeException. Is this an oversight or a copy paste error?
It is correct code.
Using the above example if I attach both listeners to the
beforeExecuteRouteand returnfalseandtruerespectively, both listeners get executed.Your question/request is that the second listener should stop executing after a false is returned?
Yes, in my opinion it is logical error that is running second the same listener if the first return false, because if first one return false should stop dispatcher for what is run second listener?
$event->stop();
Before I submit this problem I resolved this problem by $event->stop() and then it works.
But I submit this problem because I think it is very dangerous problem, because often is using with security. Please you image this situation: first programmer implement only one listener (for Security) and return false if any user cannot login. After some time next programmer implement second listener for example check ajax. When first listener (Security) return FALSE and second return TRUE security stop working correctly. It is very dangerous.
I am not sure that this code solve this problem: https://github.com/niden/cphalcon/commit/5586721c93dac935ed988e99b8e0f85d83c01323
because "return false" is only written in documentation about events of dispatcher, not for every events: https://docs.phalcon.io/4.0/en/dispatcher#events "...Some events when returning boolean false could stop the active operation. The following events are supported..."
@zak-wojtek77 We have the following issues here:
- The documentation is not correct, at least not reflecting what the code does.
- We have different events and those are processed one after another
- Each event can exist in different listeners so for example the
beforeExecuteRoutecan exist in two or more listeners.
Imagine this example:
Event beforeExecuteRoute
- ListenerA
- ListenerB
Event beforeException
- ListenerC
If ListenerA returns false should the code stop and not execute ListenerB OR it should stop and not execute ListenerB and ListenerC?
The test code here https://github.com/niden/cphalcon/commit/5586721c93dac935ed988e99b8e0f85d83c01323 will stop the current event from calling any more listeners (so ListenerB will not execute if ListenerA returns false) but again this will break backwards compatibility.
I think the best way to resolve this is to change the documents and instruct developers to use stop() so as to stop the execution when they need to instead of returning false. false works (from what I have seen) only if you have one listener for the same event.
I think the above is the best middle ground to keep everyone happy. As I mentioned before we do have plans to revisit the events manager when we restructure Phalcon later on but for now it is too much of a change to introduce which will definitely break current applications and semver.
If ListenerA returns false should the code stop and not execute ListenerB OR it should stop and not execute ListenerB and ListenerC?
in my opinion correct option is: "it should stop and not execute ListenerB and ListenerC"
in my opinion correct option is: "it should stop and not execute ListenerB and ListenerC"
I do not disagree. I would prefer to have some sort of global variable where you set the events manager to stop execution on an event level i.e. for all listeners with the same event and also have another option to stop events completely.
I will open a new issue for this to be addressed. It will have to wait though until the next big version since as I mentioned it will break semver.
For this issue we need to update the documentation.
https://github.com/phalcon/cphalcon/issues/14687
From looking at the code, we could do something about it before the next major version by taking advantage of the contructor of the Events Manager.
Closing this for now and leaving the documentation label so that we can update the documents to explain more about the return false.
Thanks for reporting this guys!
disappointment