Detect access denied and redirect to custom page w/ boost

Public

I needed to redirect to our custom login system (cosign) so I used a snippet very similar to this in order to detect that someone was logged out, and was about to be given an access denied message by Boost module. Then I redirect to a path that (in our system) will log them in if they are already in via a pervasive login system and then redirect them back to where they were requesting. This is common if you have multiple systems handling drupal sessions on different domains but the same centralized authentication system across all systems.

Get raw version
php
  1. /**
  2.  * Implements hook_page_delivery_callback_alter().
  3.  */
  4. function YOURMODULE_page_delivery_callback_alter(&$callback) {
  5. global $_boost;
  6. global $user;
  7. global $base_url;
  8. $location = 'THEPATHYOUWANT';
  9. // if person is logged out and getting access denied
  10. // force them to login and redirect
  11. if ($user->uid == 0 && is_array($_boost) && $_boost['menu_item']['status'] == 403) {
  12. $dest = drupal_get_destination();
  13. $options = array('query' => array('redirect_url' => $base_url . base_path() . $dest['destination']));
  14. drupal_goto($location, $options);
  15. }
  16. }