dt("Generates one time use login link for the specified user."), 'arguments' => array( 'uid' => dt("The ID of the user you would like to login as. Defaults to 1, the admin user."), ), 'options' => array( 'username'=> dt("The drupal username you wish to log in as."), ), 'examples' => array( 'drush login-link --uri=mysite.com', 'drush login-link --username=robert', 'drush login-link 23'), 'aliases' => array('ll'), ); return $items; } function drush_login_link($uid = NULL) { global $base_url; //checks to see if the site configuration forces a particular domain to be used for the site, whether via drush arguments or settings.php $domains = $base_url && $base_url != 'http://default' ? array($base_url) : login_link_watchdog_domains(); if(!$domains) { drush_log(dt('Unable to determine the proper site domain from the records in the watchdog table. See "drush help login-link".'), 'error'); $domains = array('http://default'); //drush sets the base_url to 'http://default' } $timestamp = time(); if(!$account = login_link_load_user($uid)) { drush_log(dt('Unable to find the user by the specified criteria, defaulting to user 1'), 'warning'); $account = login_link_load_user(1); } drush_log(dt("Use one of the following URLs to automatically log in to your site:"),'success'); foreach ($domains as $domain) { $url = login_link_url($domain, $account,$timestamp); drush_log($url, 'ok'); } } /** * Implementation of hook_drush_help(). */ function login_link_drush_help($section) { switch ($section) { case 'drush:login-link': return dt("Badmin generates a one time use login link for your convenience. The link only works if you're not already logged into the site. It guesses your site domain by looking at the last 1000 records in the watchdog table. If the proper domain isn't listed, try visiting a non-existent URL, like 'http://YOURSITE.COM/randomfakeurl-12312'. This will cause Drupal to insert a 404 warning record into the watchdog, after which you should run this command again. Alternatively, you can specify the site domain via --uri drush option, or by setting the \$base_url variable in settings.php."); } } function login_link_url($domain,$account,$timestamp) { //taken from user_path_reset() $reset_path = "user/reset/$account->uid/$timestamp/". user_pass_rehash($account->pass, $timestamp, $account->login).'/login'; return url($reset_path, array('absolute' => TRUE, 'base_url' => $domain)); } function login_link_watchdog_domains() { //get all domains referenced by the 100 most recent records in the watchdog table $query = 'SELECT domain FROM (SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(TRIM(LEADING "https://" FROM TRIM(LEADING "http://" FROM TRIM(location))), "/", 1), ":", 1) as domain FROM {watchdog} ORDER BY wid DESC LIMIT 1000) AS recent_domains GROUP BY domain HAVING domain != "default";'; $result = db_query($query); while($record = db_fetch_object($result)) { $domains[] = 'http://'.$record->domain; } return $domains; } function login_link_load_user($uid = NULL) { if ($username = drush_get_option('username')) { $criteria = array('name'=>$username); } elseif ($uid && is_numeric($uid)) { $criteria = $uid; } else { $criteria = 1; //super-user } $account = user_load($criteria); return $account; }