uczę się Zenda co myślicie o tej autentykacji?
Kod w kontrolerze:
public function loginAction() { $this->view->headTitle('autoryzacja', 'PREPEND'); if(Zend_Auth::getInstance()->hasIdentity()){ $this->_redirect('/index/index'); } $request = $this->getRequest(); $form = new Form_LoginForm(); if ($request->isPost()) { if ($form->isValid($this->_request->getPost())) { $authAdapter = $this->getAuthAdapter(); $username = $form->getValue('username'); $password = $form->getValue('password'); $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'); } else { $this->view->errorMessage = 'User name or password is wrong.'; } } } $this->view->form = $form; } public function logoutAction() { // action body Zend_Auth::getInstance()->clearIdentity(); } private function getAuthAdapter() { $authAdapter = new Zend_Auth_Adapter_DbTable(Zend_Db_Table::getDefaultAdapter()); $authAdapter->setTablename('users') ->setIdentityColumn('username') ->setCredentialColumn('password'); return $authAdapter; }
Form Login:
<?php class Form_LoginForm extends Zend_Form { public function init() { /* Form Elements & Other Definitions Here ... */ $this->setName('login'); $username = new Zend_Form_Element_Text('username'); $username->setLabel('user name:') ->setRequired(); $password = new Zend_Form_Element_Password('password'); $password->setLabel('password:') ->setRequired(true); $login = new Zend_Form_Element_Submit('login'); $login->setLabel('login'); $this->setMethod('post'); $this->setAction(Zend_Controller_Front::getInstance()->getBaseUrl().'/authentication/login'); } }