0

今後のプロジェクトで ZF2 を使用する予定なので、現在 Zend Framework 2 RC1 を試しています。認証手順から始めましたが、セッション ストレージの名前空間に「Zend_Auth」以外の名前を選択すると、セッションに保存されたオブジェクトにアクセスできないことに気付きました (AuthenticationService クラスの hasIdentity メソッドが false を返しましたが、ユーザー オブジェクト データがセッションに設定されています)。 )。

<?php
namespace Application\Controller;

use Zend\Authentication\Adapter\DbTable as AuthAdapter;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Storage\Session as SessionStorage;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Model\User;
use Application\Form\LoginForm;

class LoginController extends AbstractActionController
{
    public function indexAction()
    {
        $auth = new AuthenticationService();

        if ($auth->hasIdentity()) {
            return $this->redirect()->toRoute('application');
        }

        $form = new LoginForm();
        return array('form' => $form);
    }

    public function loginAction()
    {
        $auth = new AuthenticationService();

        $form = new LoginForm();
        $form->get('submit')->setAttribute('value', 'Add');

        $request = $this->getRequest();

        if ($request->isPost()) {
            $user = new User();
            $form->setInputFilter($user->getInputFilter('login'));

            $form->setData($request->getPost());
            if ($form->isValid()) {
                $data = $form->getData();

                // Configure the instance with constructor parameters...
                $sm          = $this->getServiceLocator();
                $dbAdapter   = $sm->get('db-adapter');
                $authAdapter = new AuthAdapter($dbAdapter, 'users', 'username', 'password');

                $authAdapter
                    ->setIdentity($data['username'])
                    ->setCredential(sha1($data['password']));

                // Use 'users' instead of 'Zend_Auth'
                $auth->setStorage(new SessionStorage('users'));

                $result = $auth->authenticate($authAdapter);

                if ($result->isValid()) {
                    // store the identity as an object where only the username and
                    // real_name have been returned
                    $storage = $auth->getStorage();

                    // store the identity as an object where the password column has
                    // been omitted
                    $storage->write($authAdapter->getResultRowObject(
                        null,
                        'password'
                    ));

                    // Redirect to list of application
                    return $this->redirect()->toRoute('application');
                }
            }    
        }

        // processed if form is not valid
        return array('form' => $form);
    }
}

このコードでは、以下の行を変更すると、

$auth->setStorage(new SessionStorage('users'));

このような:

$auth->setStorage(new SessionStorage());

hasIdentity メソッドが true を返しました。

Zend\Authentication\AuthenticationService と Zend\Authentication\Storage\Session の 2 つのクラスを確認しましたが、デフォルト以外の異なるセッション名前空間を持つセッション データにアクセスする方法が見つかりませんでした。

What i need to understand is how can i access session data which has a different namespace and if there is no way to do it for now, should we define this as a bug?

I can update the question if any other information needed.

4

1 に答える 1

2

コードの一部、つまりユーザー ID を取得しようとしている部分が欠けています。同じ名前空間で SessionStorage オブジェクトを渡すのを忘れていると思います。

また、このような問題が発生しないように、Authentication オブジェクトの構成をファクトリに移動する必要があります。

それは少なくとも私の5セントです:)

于 2012-08-03T08:15:48.950 に答える