/**
  * Login
  */
 public function loginAction()
 {
     $form = new Form_UserLogin();
     if ($this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost())) {
         $auth = Zend_Auth::getInstance();
         $options = array('userIdKey' => 'userIdKey:*', 'userDataKey' => 'users:*', 'userDataIsArray' => true);
         $adapter = new Rediska_Zend_Auth_Adapter_Redis($options);
         // Set login and password
         $adapter->setIdentity($form->getElement('login')->getValue())->setCredential($form->getElement('password')->getValue());
         // Authorization
         $result = $auth->authenticate($adapter);
         if ($result->isValid()) {
             $userData = $adapter->getResultUserData();
             $session = new Zend_Session_Namespace('Zend_Auth');
             $storage = $auth->getStorage();
             $storage->write($userData);
             $this->_redirect('/post/index/');
         } else {
             $form->getElement('login')->addError('Wrong login/password combination');
         }
     }
     $this->view->form = $form;
 }
Example #2
0
 public function loginAction()
 {
     $form = new Form_UserLogin();
     $this->view->form = $form;
     if ($this->getRequest()->isPost()) {
         $formData = $this->getRequest()->getPost();
         if ($form->isValid($formData)) {
             $email = $form->getValue('email');
             $password = $form->getValue('password');
             // Setup DbTable adapter
             $dbAdapter = Zend_Db_Table::getDefaultAdapter();
             $authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);
             $authAdapter->setTableName('users')->setIdentityColumn('email')->setCredentialColumn('password');
             $authAdapter->setIdentity($email)->setCredential(hash('SHA256', $password));
             // authentication attempt
             $auth = Zend_Auth::getInstance();
             $result = $auth->authenticate($authAdapter);
             // authentication succeeded
             if ($result->isValid()) {
                 // get all info about this user from the login table
                 // ommit only the password, we don't need that
                 $userInfo = $authAdapter->getResultRowObject(null, 'password');
                 // the default storage is a session with namespace Zend_Auth
                 $authStorage = $auth->getStorage();
                 $authStorage->write($userInfo);
                 $request = $this->getRequest();
                 $uri = $request->getRequestUri();
                 if ($uri != "/user/login") {
                     //send the user to their intended destination
                     $this->_redirect($uri);
                 } else {
                     //just send them to the front page
                     $this->_redirect('/dish/toprated');
                 }
             } else {
                 // or not! Back to the login page!
                 $this->view->errorMessage = "Incorrect email or password. Please try again.";
             }
         }
     } else {
         $this->view->loginForm = $form;
     }
 }