2016-03-23 21 views
0

我試圖在沒有電子郵件鏈接的情況下以編程方式在Drupal 8中重置用戶密碼。爲此,第一步是檢查用戶在密碼重置表單中輸入的密碼(很簡單)是否與相應用戶的散列密碼相匹配。然後保存新的密碼。如何檢查用戶在密碼重置表單中提供的當前密碼是否與Drupal 8中的散列密碼匹配

每一次我散列密碼,其給予不同的價值,這是因爲它的鹽漬。我應該如何將用戶在表格中輸入的密碼與表格中的哈希密碼進行比較。

這是我在我的控制器檢查當前密碼的代碼:

<?php 
/** 
* @file 
* contains \Drupal\customer_profile\Controller\ProfileUpdateController 
*/ 

namespace Drupal\customer_profile\Controller; 

use Drupal\Core\Controller\ControllerBase; 

use Symfony\Component\HttpFoundation\JsonResponse; 
use Drupal\Core\Password\PhpassHashedPassword; 
use Drupal\Core\Password\PasswordInterface; 

class ProfileUpdateController extends ControllerBase { 

    //$user = User::load(1); 
    //$user->setPassword('secret'); 
    //$pass = $user->save(); 
    $user =\Drupal\user\Entity\User::load(\Drupal::currentUser()->id()); 
    $ret = \Drupal\Core\Password\PasswordInterface::check('secret', $user); 

    $response->password = $ret; 
    return new JsonResponse($response); 
    } 
} 

使用\ Drupal的\核心\密碼後\ PasswordInterface ::檢查()方法,比較平淡我得到這個致命錯誤:我得到這個致命錯誤:

致命錯誤:非靜態方法Drupal \ Core \ Password \ PasswordInterface :: check()不能靜態調用,假設$ this來自C中不兼容的上下文:\ XAMPP \ htdocs中\ ijarah \模塊\定製\ customer_profile的\ src \有限公司ntroller \ ProfileUpdateController.php上線725

在Drupal 7的,我們有user_check_password檢查與字符串和散列user_hash_password密碼。 我該如何在Drupal 8中使用相同的方法。

請幫忙。

+0

我使用** \ Drupal的也嘗試了新密碼\ Core \ Password \ PasswordInterface :: check()**沒有運氣。返回一個致命錯誤,說:** _致命錯誤:非靜態方法Drupal \ Core \ Password \ PasswordInterface :: check()不能靜態調用,假設$ this來自C:\ xampp \ htdocs \ ijarah \ modules中的不兼容上下文\ custom \ customer_profile \ src \ Controller \ ProfileUpdateController.php第725行_ **。 – wilNev

回答

0

好的,解決方案是使用依賴注入,然後使用check()方法。下面是代碼:

<?php  
/** 
* @file 
* contains \Drupal\customer_profile\Controller\ProfileUpdateController 
*/ 
namespace Drupal\customer_profile\Controller; 

use Drupal\Core\Controller\ControllerBase; 

use Symfony\Component\HttpFoundation\JsonResponse; 
use Drupal\Core\Session\AccountInterface; 
use Drupal\Core\DependencyInjection\ContainerInjectionInterface; 
use Symfony\Component\DependencyInjection\ContainerInterface; 
use Drupal\Core\Password\PasswordInterface; 

class ProfileUpdateController extends ControllerBase implements ContainerInjectionInterface { 

    public function __construct(PasswordInterface $password_hasher, AccountInterface $account) { 
    $this->passwordHasher = $password_hasher; 
    $this->account = $account; 
    } 

    public static function create(ContainerInterface $container) { 
    return new static(
     $container->get('password'), 
     $container->get('current_user') 
    ); 
    } 

    public function updatePassword() { 
    //global $user; 
    $response = new \stdClass(); 
     //check the plain password with the hashed password from db 
    $pass = $this->passwordHasher->check('secret', 'hashed_password_from_db'); 

    $response->password = $pass; 
    // this will return true if the password matches or false vice-versa 
    return new JsonResponse($response); 
    } 
    } 
    ?> 

檢查密碼後,就可以節約使用

$user = User::load(1); 
    $user->setPassword('new_password_secret'); 
    $user->save(); 

希望這會幫助別人:)

相關問題