Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.80 KB | None | 0 0
  1. <?php
  2.  
  3. require_once 'Zend/Controller/Action.php';
  4. require_once 'Zend/Auth.php';
  5. require_once 'Zend/Db/Table.php';
  6. require_once 'Zend/Auth/Adapter/DbTable.php';
  7.  
  8. /**
  9.  * @author Erik Landvall
  10.  */
  11. class Identity_AuthController extends Zend_Controller_Action
  12. {
  13.    
  14.     public function preDispatch()
  15.     {
  16.         if ( Zend_Auth::getInstance()->hasIdentity() )
  17.         {
  18.             if ( $this->getRequest()->getActionName() != 'logout' )
  19.                 $this->render('index');
  20.         }
  21.         else
  22.         {
  23.             if ( $this->getRequest()->getActionName() == 'logout' )
  24.                 $this->render('index');
  25.         }
  26.     }
  27.    
  28.     public function init()
  29.     {
  30.         $this->view->layout()->disableLayout();
  31.     }
  32.    
  33.     public function indexAction() { }
  34.    
  35.     public function logoutAction()
  36.     {
  37.         Zend_Auth::getInstance()->clearIdentity();
  38.     }
  39.    
  40.     public function loginAction()
  41.     {
  42.         $form           = new Identity_Form_Login();
  43.         $flMessenger    = $this->getHelper( 'FlashMessenger' )
  44.             ->setNamespace( 'Identity_Form_Login_Errors' );
  45.        
  46.         // @todo not DRY
  47.         if( $flMessenger->hasMessages() )
  48.         {
  49.             $formErrors = array();
  50.            
  51.             foreach( reset( $flMessenger->getMessages() ) as $name => $errors )
  52.                 if( isset( $form->{$name} ))
  53.                     $form->{$name}->setErrors( $errors );
  54.                    
  55.                 else
  56.                     foreach( $errors as $error )
  57.                         array_push( $formErrors, $error );
  58.                    
  59.             $form->setErrors( $formErrors );
  60.         }
  61.        
  62.         if( $this->getRequest()->isPost() )
  63.         {
  64.             if ( $form->isValid( $this->getRequest()->getPost() ))
  65.             {
  66.                 $user = new Identity_Model_User();
  67.                 $user->username = $form->getElement('username')->getValue();
  68.                 $user->password = $form->getElement('password')->getValue();
  69.                 $authUser = $user->authenticate();
  70.                
  71.                 if ( $authUser )
  72.                     Zend_Auth::getInstance()->getStorage()->write(
  73.                         $authUser->toArray() );
  74.                
  75.                 else
  76.                     $flMessenger->addMessage(
  77.                         array(
  78.                             'form' => array( 'No user with the given '
  79.                                             .'credential was found' )));
  80.             }
  81.             else
  82.                 $flMessenger->addMessage( $form->getMessages() );
  83.            
  84.             $this->getHelper( 'Redirector' )->gotoUrlAndExit(
  85.                 $this->view->url() );
  86.         }
  87.        
  88.         $form->setAction( $this->view->url() );
  89.         $this->view->form = $form;
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement