/**
  * Handle login form
  *
  * @return array|\Zend\Http\Response array (form => \Console\Form\Login) or redirect response
  */
 public function loginAction()
 {
     // Don't show the login form if the user is already logged in
     if ($this->_authenticationService->hasIdentity()) {
         return $this->redirectToRoute('client');
     }
     $vars = array('form' => $this->_form);
     if ($this->getRequest()->isPost()) {
         $this->_form->setData($this->params()->fromPost());
         if ($this->_form->isValid()) {
             // Check credentials
             $data = $this->_form->getData();
             if ($this->_authenticationService->login($data['User'], $data['Password'])) {
                 // Authentication successful. Redirect to appropriate page.
                 $session = new \Zend\Session\Container('login');
                 if (isset($session->originalUri)) {
                     // We got redirected here from another page. Redirect to original page.
                     $response = $this->redirect()->toUrl($session->originalUri);
                 } else {
                     // Redirect to default page (client listing)
                     $response = $this->redirectToRoute('client');
                 }
                 $session->getManager()->getStorage()->clear('login');
                 return $response;
             }
         }
         $vars['invalidCredentials'] = true;
     }
     return $vars;
 }
 public function testLoginActionRedidectsToPreviousPageAfterSuccessfulLogin()
 {
     $this->_mockAuthenticationService(false);
     $postData = array('userid' => 'gooduser', 'password' => 'goodpassword');
     $session = new \Zend\Session\Container('login');
     $session->originalUri = 'redirectTest';
     $this->_form->expects($this->once())->method('isValid')->will($this->returnValue(true));
     $this->_form->expects($this->once())->method('getData')->will($this->returnValue(array('User' => 'gooduser', 'Password' => 'goodpassword')));
     $this->dispatch('/console/login/login', 'POST', $postData);
     $this->assertRedirectTo('redirectTest');
     $this->assertArrayNotHasKey('login', $_SESSION);
     // Should be cleared by action
 }