Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once 'Zend/Controller/Action.php';
- require_once 'Zend/Auth.php';
- require_once 'Zend/Db/Table.php';
- require_once 'Zend/Auth/Adapter/DbTable.php';
- /**
- * @author Erik Landvall
- */
- class Identity_AuthController extends Zend_Controller_Action
- {
- public function preDispatch()
- {
- if ( Zend_Auth::getInstance()->hasIdentity() )
- {
- if ( $this->getRequest()->getActionName() != 'logout' )
- $this->render('index');
- }
- else
- {
- if ( $this->getRequest()->getActionName() == 'logout' )
- $this->render('index');
- }
- }
- public function init()
- {
- $this->view->layout()->disableLayout();
- }
- public function indexAction() { }
- public function logoutAction()
- {
- Zend_Auth::getInstance()->clearIdentity();
- }
- public function loginAction()
- {
- $form = new Identity_Form_Login();
- $flMessenger = $this->getHelper( 'FlashMessenger' )
- ->setNamespace( 'Identity_Form_Login_Errors' );
- // @todo not DRY
- if( $flMessenger->hasMessages() )
- {
- $formErrors = array();
- foreach( reset( $flMessenger->getMessages() ) as $name => $errors )
- if( isset( $form->{$name} ))
- $form->{$name}->setErrors( $errors );
- else
- foreach( $errors as $error )
- array_push( $formErrors, $error );
- $form->setErrors( $formErrors );
- }
- if( $this->getRequest()->isPost() )
- {
- if ( $form->isValid( $this->getRequest()->getPost() ))
- {
- $user = new Identity_Model_User();
- $user->username = $form->getElement('username')->getValue();
- $user->password = $form->getElement('password')->getValue();
- $authUser = $user->authenticate();
- if ( $authUser )
- Zend_Auth::getInstance()->getStorage()->write(
- $authUser->toArray() );
- else
- $flMessenger->addMessage(
- array(
- 'form' => array( 'No user with the given '
- .'credential was found' )));
- }
- else
- $flMessenger->addMessage( $form->getMessages() );
- $this->getHelper( 'Redirector' )->gotoUrlAndExit(
- $this->view->url() );
- }
- $form->setAction( $this->view->url() );
- $this->view->form = $form;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement