Created
August 18, 2010 01:07
-
-
Save malclocke/532912 to your computer and use it in GitHub Desktop.
A drush command to reset a Drupal site admin password, either permanently or temporarily
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This program is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation, either version 3 of the License, or | |
* (at your option) any later version. | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with this program. If not, see <http://www.gnu.org/licenses/> | |
*/ | |
/** | |
* Simple drush command to reset uid 1 password, either temporarily | |
* or permanently. To use: | |
* | |
* $ mkdir ~/.drush | |
* $ cp adminpass.drush.inc ~/.drush | |
* $ cd /path/to/drupal/site | |
* $ drush adminpass | |
*/ | |
function adminpass_drush_command() { | |
$items = array(); | |
$items['adminpass'] = array( | |
'description' => "Reset the site admin password, temporarily or permanently.", | |
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL, | |
); | |
return $items; | |
} | |
function adminpass_drush_help($section) { | |
switch ($section) { | |
case 'drush:adminpass': | |
return dt("This command will reset the admin password to a randomly generated value, and then optionally reset the admin password back to the original."); | |
} | |
} | |
function drush_adminpass() { | |
$account = user_load(array('uid' => 1)); | |
$oldpass = $account->pass; | |
$newpass = user_password(); | |
if (drush_confirm(dt("Set !name password to !newpass ?", array('!name' => $account->name, '!newpass' => $newpass)))) { | |
if (!user_save($account, array('pass' => $newpass))) { | |
drush_log(dt('Failed to save admin user.'), 'error'); | |
return FALSE; | |
} | |
if (drush_confirm(dt("Reset password to original value?"))) { | |
if (! $res = db_query("UPDATE {users} SET pass = '%s' WHERE uid = 1", array($oldpass))) { | |
drush_log(dt("Failed to reset password md5 to !md5", array('!md5' => $oldpass))); | |
return FALSE; | |
} | |
} | |
} | |
else { | |
drush_log(dt('Command aborted'), 'ok'); | |
} | |
return TRUE; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment