2011-12-12 80 views
3

我已經繼承了一個Symfony項目(我實際上曾經工作過),需要重置密碼才能登錄到後端。失去的sfGuard管理員密碼 - 需要重置

我有權訪問MySQL數據庫。我嘗試了連接鹽和新密碼,然後用sha1(它似乎已經登錄到數據庫中的算法)對此進行哈希處理,但沒有運氣。

任何人都可以提供任何幫助,如何我可以更改密碼而無需登錄到Web應用程序?

謝謝,豐富。

回答

1

您可以從代碼更容易做到這一點..

$sf_guard_user = sfGuardUserPeer::retrieveByUsername('USERNAME_HERE'); 

if(is_null($sf_guard_user)){ 
    throw new \Exception('Could not find user'); 
} 

$sf_guard_user->setPassword($password); 
$sf_guard_user->save(); 

$this->logSection("Password change for user: ", $sf_guard_user->getUsername()); 

我使用的是帕克的任務。

創建項目/ lib目錄/任務文件並調用它setUserPasswordTask.class.php(名稱必須與「任務」結尾)

類會是這個樣子:

<?php 


class setClientPasswordTask extends sfBaseTask { 

    /** 
    * @see sfTask 
    */ 
    protected function configure() { 

     parent::configure(); 

     $this->addArguments(array(
      new sfCommandArgument('username', sfCommandArgument::REQUIRED, 'Username of the user to change', null), 
      new sfCommandArgument('password', sfCommandArgument::REQUIRED, 'Password to set', null) 
     )); 

     $this->addOptions(array(
      new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name', 'frontend'), 
      new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'prod'), 
      new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'propel'), 
     )); 


     $this->namespace = 'guard'; 

     $this->name = 'set-user-password'; 

     $this->briefDescription = 'Changes a User\'s password.'; 

     $this->detailedDescription = 'Changes a User\'s password.'; 
    } 



    /** 
    * @see sfTask 
    */ 
    protected function execute($arguments = array(), $options = array()) { 

     // initialize the database connection 
      $databaseManager = new sfDatabaseManager($this->configuration); 
      $connection = $databaseManager->getDatabase($options['connection'])->getConnection();  
      $configuration = ProjectConfiguration::getApplicationConfiguration($options['application'], $options['env'], true); 

      sfContext::createInstance($configuration); 


     // Change user password 

      $username   = $arguments['username']; 
      $password   = $arguments['password']; 


      $sf_guard_user = sfGuardUserPeer::retrieveByUsername('USERNAME_HERE'); 

      if(is_null($sf_guard_user)){ 
       throw new \Exception('Could not find user'); 
      } 

      $sf_guard_user->setPassword($password); 
      $sf_guard_user->save(); 

      $this->logSection("Password changed for user: ", $sf_guard_user->getUsername()); 


    } 

} 

?> 
+0

謝謝! 我會給它一個去。在此之前,對數據庫的任何幫助都會很好。 – Rich

+0

要從數據庫中改變它,你可以定義一個鹽,比如說「123」,並用該值設置salt字段,然後計算123的sha1值加上你想要的密碼。 「123password」。設置密碼值爲,你的密碼現在是「密碼」 – SuitedSloth

+0

你可以用它來計算SHA1值:http://hash.online-convert.com/sha1-generator – SuitedSloth

7

爲你可以看到here

已經有可用的內部sfGuardPlugin一個任務,你可以在命令行推出

./symfony guard:change-password your_username new_password 
+0

這是sfGuardPlugin的最新版本中添加的嗎?我在7月份發佈的那個中看不到它今年。謝謝你指出的tho – SuitedSloth

+0

也許它只在svn版本的插件中提供,並沒有包含在zip/plugin中,我不知道我只使用svn。 – Pascal

+0

我試過添加這個文件(它並不存在於我的版本中)使用juand的答案在下面,但是當在cli上運行任務時,我得到警告(未定義的索引argv和array_shift()期望參數1爲數組,null給定)來自sfCommandM anager - 然後給我一個可用的symfony命令列表。 – Rich