Drupal goto breaks drush

Posted by: 
Dominique De Cooman

Sometimes there is a need to make drupal redirect using drupal_goto. We had to do it in an SSO scenario. We had to make drupal goto a configurable admin page (node page) when no roles where assigned to you by the external authentication (shibboleth). So we would put a check in hook_init() to redirect drupal when our criteria where met.

This breaks drush. But, there is a solution. You can detect by who php is called using php_sapi_name(). When it is your webserver it will output:'apache2handler', when it is drush it will output 'cli'. In following snippet we check if drush is using php. You also see a check to check on which pages not to redirect, $path could be mapped to admin settings or hardcoded.

<?php
$paths
[] = $redirect;
$result db_result(db_query("SELECT uid FROM {extranet_role_assignment} WHERE uid = %d LIMIT 1"$user->uid));
if (!
$result && 
    !
user_access('administer era settings') && 
    !
in_array($_GET['q'], $paths) && 
    
php_sapi_name() != 'cli') {    
  
drupal_goto($redirect);
}
?>

Add new comment