Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Application_Form_Login extends Zend_Form
- {
- public function init()
- {
- $this->addElement('text', 'username', array (
- 'Label' => 'Username',
- 'Required' => true,
- 'Filters' => array (),
- 'Validator' => array (),
- ));
- $this->addElement('password', 'password', array (
- 'Label' => 'Password',
- 'Required' => true,
- 'Filters' => array (),
- 'Validators' => array (),
- ));
- $this->addElement('submit', 'login', array (
- 'Label' => 'Login',
- 'Ignore' => true,
- ));
- $this->addElement('hash', 'token');
- }
- }
- class AuthController extends Zend_Controller_Action
- {
- protected $_session;
- public function init()
- {
- $this->_session = new Zend_Session_Namespace('App_Auth');
- }
- public function indexAction()
- {
- $form = new Application_Form_Login(array (
- 'action' => $this->view->url(array ('controller' => 'auth', 'action' => 'login'), null, true),
- 'method' => 'post',
- ));
- if (isset ($this->_session->username)) {
- $form->getElement('username')->setValue($this->_session->username);
- unset ($this->_session->username);
- }
- $this->view->form = $form;
- }
- public function loginAction()
- {
- $username = $this->getRequest()->getParam('username', null);
- $password = $this->getRequest()->getParam('password', null);
- $bootstrap = $this->getInvokeArg('bootstrap');
- $db = $bootstrap->getResource('db');
- $auth = new Application_Model_Auth($db);
- $result = $auth->validate($username, $password);
- if (!$result) {
- $this->_session->username = $username;
- return $this->_helper->redirector('index');
- } else {
- return $this->_helper->redirector('index', 'index');
- }
- }
- public function logoutAction()
- {
- if (Zend_Auth::getInstance()->hasIdentity()) {
- Zend_Auth::getInstance()->clearIdentity();
- }
- return $this->_helper->redirector('index', 'index');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement