开发者

How to get authentication adapter in zend?

I am this tutorial to implement login system in my application. It is giving me following error in process action:

Message: Method "getAuthAdapter" does not exist and was not trapped in __call() 

on following line:

$adapter = $this->getAuthAdapter($form->getValues());

So now I have to implement开发者_StackOverflow社区 getAuthAdapter() function but how to code in this function.

Thanks


If you want to authentication via database you can use this code:

protected function _getAuthAdapter($userLogin, $userPass){
    $authAdapter = new Zend_Auth_Adapter_DbTable(
        Zend_Db_Table::getDefaultAdapter(),
        'user', //db table name
        'login', // identity column
        'password' // credential column
    );
    $authAdapter->setIdentity($userLogin)->setCredential($userPass);
    return $authAdapter;
}

Database authentication docs you can find here

For other adapters look here


You can create your own authentication function using this example


So this is my complete solution:

<?php

class AuthenticationController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
    }

    public function indexAction() {
        // action body
    }

    /**
     * Show login form
     */
    public function loginAction() {

        $this->view->heading = 'Login';
        $this->view->form = new Form_Login();
        $this->view->form->setAction( 'process' );
    }

    /**
     * preDispatch: If user is already logged in then 
     * redirect to index, if not then redirect to login
     */
    public function preDispatch() {

        if (Zend_Auth::getInstance()->hasIdentity()) {
            // If the user is logged in, we don't want to show the login form;
            // however, the logout action should still be available
            if ('logout' != $this->getRequest()->getActionName()) {
                $this->_helper->redirector('index', 'index');
            }
        } else {
            // If they aren't, they can't logout, so that action should 
            // redirect to the login form
            if ('logout' == $this->getRequest()->getActionName()) {
                $this->_helper->redirector('login');
            }
        }
    }

    /**
     * Provide authentication adapter
     * 
     * @param unknown_type $values
     */
    public function getAuthAdapter( $values ) {

        $authAdapter = new Zend_Auth_Adapter_DbTable( Zend_Db_Table::getDefaultAdapter(),
                                                      'Authentication',
                                                      'username',
                                                      'password',
                                                      'MD5(CONCAT(salt,?))'
                                                    );

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

        return $authAdapter;
    }

    /**
     * Process login request. If user is authenticated
     * then redirect to index otherwise show login form again
     */
    public function processAction() {

        $request = $this->getRequest();

        // Check if we have a POST request
        if (!$request->isPost()) {
            return $this->_helper->redirector('login');
        }

        // Get our form and validate it
        $form = new Form_Login();
        if (!$form->isValid($request->getPost())) {
            // Invalid entries
            $this->view->form = $form;
            return $this->render('login'); // re-render the login form
        }

        // Get login form values
        $values = $form->getValues();

        // Get our authentication adapter and check credentials
        $adapter = $this->getAuthAdapter( $values );
        $auth    = Zend_Auth::getInstance();
        $result  = $auth->authenticate($adapter);

        if (!$result->isValid()) {
            // Invalid credentials
            $form->setDescription('Invalid credentials provided');
            $this->view->form = $form;
            $this->_helper->redirector('login'); // re-render the login form
        }

        // Create session
        $session = new Zend_Session_Namespace('user');
        $session->userEmail = $values['username'];
        $session->userId = '1';

        // We're authenticated! Redirect to the home page
        $this->_helper->redirector('index', 'index');
    }

    /**
     * logout request
     */
    public function logoutAction() {

        // Destroy session
        $session = new Zend_Session_Namespace('user');
        $session->userEmail = null;
        $session->userId = null;

        Zend_Auth::getInstance()->clearIdentity();
        $this->_helper->redirector('login'); // back to login page
    }


} // end class


?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜