丢失的sfGuard管理员密码 - 需要重置

时间:2011-12-12 16:50:27

标签: php symfony1 sfguard

我继承了一个Symfony项目(我实际上刚刚开始工作),需要重置密码才能登录到后端。

我可以访问MySQL数据库。我已经尝试连接salt和新密码,然后使用sha1(它似乎记录在数据库中的算法)对此进行哈希处理,但没有运气。

任何人都可以提供有关如何在不登录Web应用程序的情况下更改密码的任何帮助吗?

谢谢,Rich。

2 个答案:

答案 0 :(得分:7)

正如您所见here

在sfGuardPlugin中已经有一个可以在cli中启动的任务

./symfony guard:change-password your_username new_password

答案 1 :(得分: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() );

我使用pake任务。

在project / lib / task中创建一个文件并将其命名为setUserPasswordTask.class.php(name必须以“Task”结尾)

该课程看起来像这样:

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


    }

}

?>