Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 453 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to change Drupal user password programmatically?

#1
We are going to deploy a Drupal site inside company's intranet. There is a requirement for user to reset password. We have a centralized password reset mechanism (for single sign on):

- user submits a password change request in system
- the request is sent to a password server
- the password server will reset the user's password in _all systems_ with a new password
- the password server will send the new password to user's mobile phone via sms

Now we are going to add the Drupal site to _all systems_. Please suggest a way to change the Drupal's logon password by an external program (assume the system can run script on the Drupal host and edit Drupal MySQL database).
Reply

#2
If you're using Drupal 6 then the password stored in the system is a simple md5 of the password. If you're using php scripts to trigger the password reset then use the

[To see links please register here]

function.

Username and Password is stored in the `users` table. The md5 hash is stored in the `pass` column.
Reply

#3
For Drupal 7 -- Hope this custom function code resolves **change** password for *anonymous* user.

function password_reset(){
global $user;
$hashthepass = 'password'; /* Your password value*/
require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');
$hashthepass = user_hash_password(trim($hashthepass));
// Abort if the hashing failed and returned FALSE.
if (!$hashthepass) {
return FALSE;
}
else {
db_update('users')
->fields(array(
'pass' => $hashthepass
))
->condition('uid', $user->uid)
->execute();
}
}
Reply

#4
Another possibility for Drupal 7 is:

$user = user_load($GLOBALS['user']->uid);
$user->pass = 'the new password';
user_save((object) array('uid' => $user->uid), (array) $user);

This will automatically hash the password, without the need to write directly to the database.
Reply

#5
Here is another more sophisticated Drupal 7 approach based on the given `$username`. It also supports e-mail addresses used as username.

$users = user_load_multiple(array(), array('mail' => $username, 'status' => '1'));
$account = reset($users);
if (!$account) {
// No success, try to load by name.
$users = user_load_multiple(array(), array('name' => $username, 'status' => '1'));
$account = reset($users);
}

if ($account) {
$account->pass = 'new password';
user_save($account);
}
else {
watchdog('user', 'Cannot load user: %user', array('%user' => $username), array(), WATCHDOG_ERROR);
}



[1]:

[To see links please register here]

Reply

#6
There are already many nice answers in here, but I believe I add the one which I found easy for me.

// ID of the user whose password you wish to change.
$uid = 1;

// Load the user account.
$account = user_load($uid);

// Load hashing libraries.
// You can use module_load_include() if you want to make it cleaner.
require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');

// Generate new password hash.
$password_hash = user_hash_password('enter-new-password-here');
if (!$password_hash) {
// Password could not be hashed. Handle the error.
exit('Password could not be hashed.');
}

$account->pass = $password_hash;
user_save($account);

Given everything is set up correctly, your user's password would be updated.

**Note:** If you have forgotten your root password, you can safely ignore the error handling, etc. Add these lines in *index.php* before *menu_execute_active_handler();* and open any page to reset your password. Don't forget to remove the lines after you're done though!
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through