Zend Framework User Authentication

What is the best practice for custom website / REST authentication in ZV MVC? How and where to put the code in the ZF structure? Can you provide me some sample code?

I have a website and a REST server written in the Zend Framework but not involving a user session.

thanks!

+4
source share
1 answer

Authentication is set in the _initAutoload file, for example. eg:

 if(Zend_Auth::getInstance()->hasIdentity()) { Zend_Registry::set('role', Zend_Auth::getInstance() ->getStorage()->read()->role); }else{ Zend_Registry::set('role', 'guests'); } 

In the case of REST authentication, you may need authentication by simply passing in the login parameters rather than logging in through the form.

So it might look like this: AuthenticationController :

 private function getAuthAdapter() { $authAdapter = new Zend_Auth_Adapter_DbTable( Zend_Db_Table::getDefaultAdapter()); $authAdapter->setTableName('users') // the db table where users are stored ->setIdentityColumn('email') ->setCredentialColumn('password') ->setCredentialTreatment('SHA1(CONCAT(?,salt))'); return $authAdapter; } public function logoutAction() { Zend_Auth::getInstance()->clearIdentity(); $this->_redirect('index/index'); } public function loginAction(){ if (Zend_Auth::getInstance()->hasIdentity()){ $this->_redirect('index/index'); } if ($request->isPost()){ $username = $request->getPost('username'); $password = $request->getPost('password'); if ($username != "" && $password != "") { $authAdapter = $this->getAuthAdapter (); $authAdapter->setIdentity($username) ->setCredential($password); $auth = Zend_Auth::getInstance(); $result = $auth->authenticate($authAdapter); if($result->isValid()){ $identity = $authAdapter->getResultRowObject(); $authStorage = $auth->getStorage(); $authStorage->write($identity); $this->_redirect ( 'index/index' ); } } } } 

If you need more help with zend_auth and zend_acl , you can take a look.

+1
source

All Articles