【问题标题】:How to change Drupal user password programmatically?如何以编程方式更改 Drupal 用户密码?
【发布时间】:2010-10-14 04:55:46
【问题描述】:

我们将在公司的 Intranet 中部署一个 Drupal 站点。用户需要重置密码。我们有一个集中的密码重置机制(单点登录):

  • 用户在系统中提交密码更改请求
  • 请求被发送到密码服务器
  • 密码服务器将使用新密码重置所有系统中的用户密码
  • 密码服务器将新密码通过短信发送到用户手机

现在我们要将 Drupal 站点添加到所有系统。请提出一种通过外部程序更改 Drupal 登录密码的方法(假设系统可以在 Drupal 主机上运行脚本并编辑 Drupal MySQL 数据库)。

【问题讨论】:

    标签: drupal authentication drupal-modules


    【解决方案1】:

    对于 Drupal 7 -- 希望这个自定义函数代码为 匿名 用户解析 change 密码。

    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();
        }
     }
    

    【讨论】:

      【解决方案2】:

      如果您使用的是 Drupal 6,那么存储在系统中的密码就是密码的简单 md5。如果您使用 php 脚本触发密码重置,请使用 http://php.net/manual/en/function.md5.php 函数。

      用户名和密码存储在users 表中。 md5 哈希存储在pass 列中。

      【讨论】:

      • 正如答案所说,这只适用于 Drupal 6。对于任何拥有更现代 Drupal 版本的人来说,这是一个重要的考虑点。
      【解决方案3】:

      Drupal 7 的另一种可能性是:

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

      这将自动散列密码,无需直接写入数据库。

      【讨论】:

      • 对我不起作用。将密码保存到数据库而不进行哈希处理。
      【解决方案4】:

      这是基于给定$username 的另一种更复杂的 Drupal 7 方法。它还支持用作用户名的电子邮件地址。

      $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);
      }
      

      【讨论】:

        【解决方案5】:

        这里已经有很多不错的答案了,但我相信我会添加一个对我来说很容易的答案。

        // 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);
        

        如果一切设置正确,您的用户密码将会更新。

        注意:如果忘记了root密码,可以放心地忽略错误处理等。在index.phpmenu_execute_active_handler( ); 并打开任何页面以重置您的密码。完成后不要忘记删除线条!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-11-20
          • 2020-01-31
          • 2015-09-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-01-19
          相关资源
          最近更新 更多