Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class IndexController extends Zend_Controller_Action
- {
- public function init()
- {
- /* Initialize action controller here */
- }
- public function indexAction()
- {
- if(!Zend_Auth::getInstance()->hasIdentity())
- {
- $this->getHelper('redirector')->gotoRouteAndExit(array('controller'=>'index','action'=>'login'));
- $redirector = $this->_helper->Redirector;
- $redirector->gotoRouteAndExit(array('controller'=>'index','action'=>'login'));
- }
- $this->view->text = 'you are logged in.';
- }
- public function loginAction()
- {
- if(Zend_Auth::getInstance()->hasIdentity())
- {
- $this->getHelper('redirector')->gotoRouteAndExit(array('controller'=>'index','action'=>'index'));
- }
- $request = $this->getRequest();
- $form = new Form_Login();
- if($request->isPost())
- {
- if($form->isValid($this->_request->getPost()))
- {
- // form is valid
- // forward to authenticate login
- $this->_forward('login','authentication');
- }
- }
- $this->view->form = $form;
- }
- }
- <?php
- class AuthenticationController extends Zend_Controller_Action
- {
- public function init()
- {
- /* Initialize action controller here */
- $this->_helper->viewRenderer->setNoRender(true);
- }
- public function loginAction()
- {
- $form = new Form_Login();
- $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->_helper->redirector->gotoRouteAndExit(array('controller'=>'index','action'=>'index'));
- }
- // if we are here we are not logged in. goto login form
- $this->_helper->redirector->gotoRoute(array('controller'=>'index','action'=>'login'));
- }
- public function logoutAction()
- {
- $auth = Zend_Auth::getInstance();
- $result = $auth->clearIdentity();
- $this->_helper->redirector->gotoRouteAndExit(array('controller'=>'index','action'=>'index'));
- }
- private function getAuthAdapter()
- {
- $authAdapter = new Zend_Auth_Adapter_DbTable(Zend_db_table::getDefaultAdapter());
- $authAdapter->setTableName('contact_users')
- ->setIdentityColumn('username')
- ->setCredentialColumn('password');
- return $authAdapter;
- }
- }
Add Comment
Please, Sign In to add comment