Advertisement
dragonbe

Forms

Nov 24th, 2011
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.25 KB | None | 0 0
  1. <?php
  2.  
  3. class Application_Form_Login extends Zend_Form
  4. {
  5.  
  6.     public function init()
  7.     {
  8.         $this->addElement('text', 'username', array (
  9.             'Label' => 'Username',
  10.             'Required' => true,
  11.             'Filters' => array (),
  12.             'Validator' => array (),
  13.         ));
  14.         $this->addElement('password', 'password', array (
  15.             'Label' => 'Password',
  16.             'Required' => true,
  17.             'Filters' => array (),
  18.             'Validators' => array (),
  19.         ));
  20.         $this->addElement('submit', 'login', array (
  21.             'Label' => 'Login',
  22.             'Ignore' => true,
  23.         ));
  24.         $this->addElement('hash', 'token');
  25.     }
  26.  
  27.  
  28. }
  29.  
  30.  
  31. class AuthController extends Zend_Controller_Action
  32. {
  33.  
  34.     protected $_session;
  35.    
  36.     public function init()
  37.     {
  38.         $this->_session = new Zend_Session_Namespace('App_Auth');
  39.     }
  40.  
  41.     public function indexAction()
  42.     {
  43.         $form = new Application_Form_Login(array (
  44.             'action' => $this->view->url(array ('controller' => 'auth', 'action' => 'login'), null, true),
  45.             'method' => 'post',
  46.         ));
  47.         if (isset ($this->_session->username)) {
  48.             $form->getElement('username')->setValue($this->_session->username);
  49.             unset ($this->_session->username);
  50.         }
  51.         $this->view->form = $form;
  52.     }
  53.  
  54.     public function loginAction()
  55.     {
  56.         $username = $this->getRequest()->getParam('username', null);
  57.         $password = $this->getRequest()->getParam('password', null);
  58.        
  59.         $bootstrap = $this->getInvokeArg('bootstrap');
  60.         $db = $bootstrap->getResource('db');
  61.        
  62.         $auth = new Application_Model_Auth($db);
  63.         $result = $auth->validate($username, $password);
  64.         if (!$result) {
  65.             $this->_session->username = $username;
  66.             return $this->_helper->redirector('index');
  67.         } else {
  68.             return $this->_helper->redirector('index', 'index');
  69.         }
  70.          
  71.     }
  72.  
  73.     public function logoutAction()
  74.     {
  75.         if (Zend_Auth::getInstance()->hasIdentity()) {
  76.             Zend_Auth::getInstance()->clearIdentity();
  77.         }
  78.         return $this->_helper->redirector('index', 'index');
  79.     }
  80.  
  81.  
  82. }
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement