Guest User

Untitled

a guest
Jun 22nd, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. <?php
  2.  
  3. class IndexController extends Zend_Controller_Action
  4. {
  5.  
  6. public function init()
  7. {
  8. /* Initialize action controller here */
  9. }
  10.  
  11. public function indexAction()
  12. {
  13. if(!Zend_Auth::getInstance()->hasIdentity())
  14. {
  15. $this->getHelper('redirector')->gotoRouteAndExit(array('controller'=>'index','action'=>'login'));
  16.  
  17. $redirector = $this->_helper->Redirector;
  18. $redirector->gotoRouteAndExit(array('controller'=>'index','action'=>'login'));
  19. }
  20. $this->view->text = 'you are logged in.';
  21.  
  22. }
  23.  
  24. public function loginAction()
  25. {
  26. if(Zend_Auth::getInstance()->hasIdentity())
  27. {
  28. $this->getHelper('redirector')->gotoRouteAndExit(array('controller'=>'index','action'=>'index'));
  29. }
  30.  
  31. $request = $this->getRequest();
  32.  
  33. $form = new Form_Login();
  34.  
  35. if($request->isPost())
  36. {
  37. if($form->isValid($this->_request->getPost()))
  38. {
  39. // form is valid
  40. // forward to authenticate login
  41. $this->_forward('login','authentication');
  42. }
  43. }
  44.  
  45. $this->view->form = $form;
  46. }
  47.  
  48.  
  49. }
  50.  
  51.  
  52. <?php
  53.  
  54. class AuthenticationController extends Zend_Controller_Action
  55. {
  56.  
  57. public function init()
  58. {
  59. /* Initialize action controller here */
  60. $this->_helper->viewRenderer->setNoRender(true);
  61. }
  62.  
  63. public function loginAction()
  64. {
  65. $form = new Form_Login();
  66. $form->isValid($this->_request->getPost());
  67.  
  68. $authAdapter = $this->getAuthAdapter();
  69.  
  70. $username = $form->getValue('username');
  71. $password = $form->getValue('password');
  72.  
  73. $authAdapter->setIdentity($username)
  74. ->setCredential($password);
  75.  
  76. $auth = Zend_Auth::getInstance();
  77. $result = $auth->authenticate($authAdapter);
  78.  
  79. if($result->isValid()){
  80. $identity = $authAdapter->getResultRowObject();
  81.  
  82. $authStorage = $auth->getStorage();
  83. $authStorage->write($identity);
  84. $this->_helper->redirector->gotoRouteAndExit(array('controller'=>'index','action'=>'index'));
  85. }
  86.  
  87. // if we are here we are not logged in. goto login form
  88. $this->_helper->redirector->gotoRoute(array('controller'=>'index','action'=>'login'));
  89. }
  90.  
  91. public function logoutAction()
  92. {
  93. $auth = Zend_Auth::getInstance();
  94. $result = $auth->clearIdentity();
  95.  
  96. $this->_helper->redirector->gotoRouteAndExit(array('controller'=>'index','action'=>'index'));
  97. }
  98.  
  99. private function getAuthAdapter()
  100. {
  101. $authAdapter = new Zend_Auth_Adapter_DbTable(Zend_db_table::getDefaultAdapter());
  102. $authAdapter->setTableName('contact_users')
  103. ->setIdentityColumn('username')
  104. ->setCredentialColumn('password');
  105.  
  106. return $authAdapter;
  107. }
  108.  
  109. }
Add Comment
Please, Sign In to add comment