【问题标题】:Creating own Zend_Auth_Adapter创建自己的 Zend_Auth_Adapter
【发布时间】:2010-07-25 04:43:21
【问题描述】:

我在创建自己的 Zend_Auth_Adapter 时遇到了困难。我也在使用 Doctrine 2。到目前为止,我有......下面的代码

我收到了错误

方法“hasIdentity”不存在且未被困在__call()中

怎么了?

use \Doctrine\ORM;
class Application_Auth_Adapter implements Zend_Auth_Adapter_Interface {
    private $username;
    private $password;

    function __construct($username, $password) {
        $this->username = $username;
        $this->password = $password;
    }

    function authenticate() {
        $em = Zend_Registry::get('em');
        $query = $em->createQuery('select u from Application\Entities\User u WHERE u.name = :username')
                    ->setParameter('username', $this->username);
        try {
            $user = $query->getSingleResult();
            $salt = $user->salt;
            $hashedPassword = hash_hmac('sha256', $this->password, $salt);
            if ($hashedPassword == $user->password) {
                // login success
                return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $user);
            } else {
                // wrong password
                return new Zend_Auth_Result(Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID, null);
            }
        } catch (NonUniqueResultException $e) {
            // non unique result
            return new Zend_Auth_Result(Zend_Auth_Result::FAILURE, null);
        } catch (NoResultException $e) {
            // no result found
            return new Zend_Auth_Result(Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND, null);
        } catch (Exception $e) {
            // exception occured
            return new Zend_Auth_Result(Zend_Auth_Result::FAILURE, null);
        }
    }
} 

更新

我注意到问题来自Zend_Auth 的第 123 行

if ($this->hasIdentity()) {
    $this->clearIdentity();
}

但就在那个函数的下方,authenticate(),它的

public function hasIdentity()

我注意到堆栈跟踪有些奇怪

#0 [internal function]: Zend_Controller_Action->__call('hasIdentity', Array)
#1 D:\ResourceLibrary\Frameworks\ZendFramework\library\Zend\Auth.php(123): AuthController->hasIdentity()
#2 D:\Projects\Websites\php\ZendFramework\LearningZF\application\controllers\AuthController.php(23): Zend_Auth->authenticate(Object(Application_Auth_Adapter))
#3 D:\ResourceLibrary\Frameworks\ZendFramework\library\Zend\Controller\Action.php(513): AuthController->loginAction()
#4 D:\ResourceLibrary\Frameworks\ZendFramework\library\Zend\Controller\Dispatcher\Standard.php(295): Zend_Controller_Action->dispatch('loginAction')
#5 D:\ResourceLibrary\Frameworks\ZendFramework\library\Zend\Controller\Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#6 D:\ResourceLibrary\Frameworks\ZendFramework\library\Zend\Application\Bootstrap\Bootstrap.php(97): Zend_Controller_Front->dispatch()
#7 D:\ResourceLibrary\Frameworks\ZendFramework\library\Zend\Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#8 D:\Projects\Websites\php\ZendFramework\LearningZF\public\index.php(26): Zend_Application->run()
#9 {main}

查看 AuthController->hasIdentity() 它试图从 AuthController 调用 hasIdentity()

【问题讨论】:

    标签: zend-framework authentication


    【解决方案1】:

    我发现了原因!查看我的更新...堆栈跟踪。我正在调用一个方法,就好像它是静态的一样。那不会产生错误吗?反正...我会做的

    $result = Zend_Auth::getInstance()->authenticate($adapter);

    【讨论】:

      【解决方案2】:

      不错的适配器,虽然我建议使用 $user->getPassword() 和 $user->getSalt()

      【讨论】:

      • 是的,我已经将所有魔术方法__get() __set() 转换为get*() set*(),也感谢您的补充。欢迎来到stackoverflow
      猜你喜欢
      • 2013-08-04
      • 2021-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-26
      • 2011-04-29
      • 1970-01-01
      相关资源
      最近更新 更多