代码之家  ›  专栏  ›  技术社区  ›  emeraldjava

登录后zend身份验证和重定向用户

  •  1
  • emeraldjava  · 技术社区  · 16 年前

    所以我使用一个简单的zend_auth机制来确保我的用户在登录之前 可以访问某些控制器。我的admincontroller包含此方法

    function preDispatch()
    {
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            $this->_redirect('auth/login');
        }
    }
    

    用户被重定向到authcontroller,但在成功登录后,当被重定向到 我的网站的默认“索引”页,而不是“管理”页。

    function loginAction()
    {
        if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
            // collect the data from the user
            Zend_Loader::loadClass('Zend_Filter_StripTags');
            $filter = new Zend_Filter_StripTags();
            $username = $filter->filter($this->_request->getPost('username'));
            $password = $filter->filter($this->_request->getPost('password'));
    
            if (empty($username)) {
                $this->view->message = 'Please provide a username.';
            } else {
                // setup Zend_Auth adapter for a database table
                $dbAdapter = Zend_Db_Table::getDefaultAdapter();
    
                //Zend_Loader::loadClass('Zend_Auth_Adapter_DbTable');
                $authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);
                $authAdapter->setTableName('login');
                $authAdapter->setIdentityColumn('email');
                $authAdapter->setCredentialColumn('password');
    
                // Set the input credential values to authenticate against
                $authAdapter->setIdentity($username);
                $authAdapter->setCredential($password);
    
                // do the authentication 
                $auth = Zend_Auth::getInstance();
                $result = $auth->authenticate($authAdapter);
                if ($result->isValid()) {
                    // success : store database row to auth's storage system (not the password though!)
                    $data = $authAdapter->getResultRowObject(null, 'password');
                    $auth->getStorage()->write($data);
                    // I THINK I NEED TO CHANGE THIS LINE
                    $this->_redirect('/'); 
                } else {
                    // failure: clear database row from session
                    $this->view->message = 'Login failed.';
                }
            }
        }
        $this->render();   
    }
    

    Zend中配置predispatch()方法的正确方法是什么,这样我就可以告诉authcontroller重定向到最初请求的页面?

    2 回复  |  直到 15 年前
        1
  •  2
  •   Vladimir Mihailenco    16 年前

    你可以从这个开始 http://pastebin.com/f3472a877 写你自己的。用途:

    public function preDispatch()
    {
        $this->_helper->lastDecline()->saveRequestUri();
    
        // redirect to login action
    }
    
    public function loginAction()
    {
        // authorize user
    
        // this will redirect us to the saved in preDispatch uri
        // if there is no any saved uris in session we will go to the root "/"
        $this->_helper->lastDecline();
        return;
    }
    
        2
  •  5
  •   Vladimir Mihailenco    16 年前

    您应该记住predispatch中的当前url,然后在登录操作中重定向到该url。例子:

    public function preDispatch()
    {
        $requestUri = Zend_Controller_Front::getInstance()->getRequest()->getRequestUri();
    
        $session = new Zend_Session_Namespace('lastRequest');
        $session->lastRequestUri = requestUri;
    
        // ...
    }
    
    public function loginAction()
    {
        // ...
    
        $session = new Zend_Session_Namespace('lastRequest');
        if (isset($session->lastRequestUri)) {
            $this->_redirect($session->lastRequestUri);
            return;
        }
    
        // ...
    }
    

    为此,您可以编写自己的zend_controller_action_helper。您也可以检查http_referer,但对我来说,会话是存储此类数据的更可靠的方法。