2

Is there a way to intercept a request before a controller action is run, so that you could preform additional authorisation checks?

Say I have an authorised user, but their access to actions is dependant on a 3rd party API. So I need a way to check, this 3rd party API or a stored session version, before each action is performed.

I can see the beforeAction() method, which is fine for new Controllers I have added, but I need site wide solution that will catch request that go through Craft and doesn't touch any plugins.

I can see the onBeginRequest Event, but aware this fires before Craft has had chance to setup. So I feel this might not be the best solution for me, as I would like to use services inside custom plugins. If there is no other way, I assume this is best place to look. If so what is the best way to add an Event listener for this?

4

You're correct about both beforeAction() and onBeginRequest().

However, you could put an init() method in your main plugin class file and this will execute on every request as long as the plugin is installed and enabled.

public function init()
{
    // Whatever code you were going to put in onBeginRequest can go here.
    parent::init();
}
1
  • I had considered this and even started to write code! However it made me feel like I was hacking at it, so I stopped. Thanks for letting us know that at present I wasn't far off the mark!
    – PanPipes
    Oct 6 '15 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.