<p>You're correct about both <code>beforeAction()</code> and <code>onBeginRequest()</code>.</p>

<p>However, you could put an <code>init()</code> method in your main <a href="http://buildwithcraft.com/docs/plugins" rel="nofollow">plugin</a> class file and this will execute on every request as long as the plugin is installed and enabled.</p>

<pre><code>public function init()
{
    // Whatever code you were going to put in onBeginRequest can go here.
    parent::init();
}
</code></pre>

CC BY-SA 3.0
2015-10-06T08:46:37.707Z
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
CC BY-SA 3.0
2015-10-05T17:57:16.500Z
2015-10-05T17:57:16.500Z
Brad Bell
<p>Is there a way to intercept a request before a controller action is run, so that you could preform additional authorisation checks?</p>

<p>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. </p>

<p>I can see the <code>beforeAction()</code> 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.</p>

<p>I can see the <code>onBeginRequest</code> 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?</p>

<authentication>
Intercept all requests before a Controller Action is executed
# Intercept all requests before a Controller Action is executed

(Asked by: username_0 on 2015-10-05)

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?

**Tags:** authentication

---

## 1 answers

### Answer by username_1 on 2015-10-05. Score: 4 | Accepted answer

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();
}

> **Comments:**
> **username_0**: 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!  

---

