3

我继承了一个 Symfony 项目(我实际上在不久前工作过)并且需要重置密码才能登录到后端。

我可以访问 MySQL 数据库。我试过连接盐和新密码,然后用 sha1 散列(它似乎已经登录数据库的算法),但没有运气。

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

谢谢,里奇。

4

2 回答 2

7

正如你在这里看到的

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

./symfony guard:change-password your_username new_password
于 2011-12-12T17:02:20.550 回答
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(名称必须以“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() );


    }

}

?>
于 2011-12-12T16:54:27.710 回答