Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-06T08:46:37.707Z
Answers.Text: 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!
Answers.UserDisplayName: PanPipes
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-05T17:57:16.500Z
Answers.LastActivityDate: 2015-10-05T17:57:16.500Z
Answers.UserDisplayName: Brad Bell
Body: <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>
  
Tags: <authentication>
Title: Intercept all requests before a Controller Action is executed
ThreadText: # 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](http://buildwithcraft.com/docs/plugins) 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!  
  
  ---
  
  