public function indexAction()
 {
     $formlogin = new \Application\Form\Login();
     $formlogin->bind($this->request->getPost());
     $messages = array();
     if ($this->request->isPost()) {
         $formlogin->setData($this->request->getPost());
         if ($formlogin->isValid()) {
             $loginCredentials = $this->request->getPost('Login');
             $messages = $this->cs->auth($loginCredentials['username'], $this->cs->_hashing($loginCredentials['password']));
             //$messages = $this->cs->auth($loginCredentials['username'],$loginCredentials['password']);
             if (empty($messages)) {
                 $identity = $this->authservice->getIdentity();
                 $this->userid = $identity['pkUserid'];
                 //If valid, check if account password requires resetting, if true direct user to renew password
                 if (!$this->cs->hasPasswordExpired($this->userid, $this->em)) {
                     //Log time and ip address
                     $ipaddress = new RemoteAddress();
                     $pr = new \Application\Model\Preferences($this->em);
                     //Get user entity
                     $userentity = $this->em->getRepository("\\Application\\Entity\\User")->find($this->userid);
                     $userentity->setLastloginip($userentity->getIpaddress());
                     $userentity->setIpaddress($ipaddress->getIpAddress());
                     $userentity->setLastlogindate($userentity->getLogindate());
                     $userentity->setLogindate(new \Datetime());
                     $logintimes = (int) $userentity->getLogintimes() + 1;
                     $userentity->setLogintimes($logintimes);
                     //Update session information
                     $pr->saveUser($userentity);
                     return $this->redirect()->toRoute('home', array('action' => 'index'));
                 }
                 $usersession = new Container('USER');
                 $usersession->userid = $this->userid;
                 //Clear session
                 $this->authservice->clearIdentity();
                 return $this->redirect()->toRoute('login', array('action' => 'renewpassword'));
             } else {
                 //If it new student then authenticate using email address in enrolment
                 //                   $enrollmentauth = $this->cs->authNewStudent($loginCredentials['username'], $loginCredentials['password'],$this->em);
                 //                   if(count($enrollmentauth)){
                 //                       $registersession = new Container('ENROLLMENT');
                 //                       $registersession->emailaddress = $loginCredentials['username'];
                 //                       return $this->redirect()->toRoute('login', array('action' => 'register'));
                 //                   }
                 //                   //Not new student and user account does not exist
                 //                   if(!empty($messages['username']))
                 //                        $formlogin->get('Login')->get('username')->setMessages(array($messages['username']));
                 //                   if(!empty($messages['password']))
                 //                        $formlogin->get('Login')->get('password')->setMessages(array($messages['password']));
             }
         } else {
             $messages = $formlogin->getMessages();
         }
     }
     return new ViewModel(array("frmlogin" => $formlogin, "errormessage" => $messages));
 }
 /**
  * Implementing route "login"
  * If it's GET, render login form
  * If it's POST, run data validation and authentication
  * @return \Zend\Http\Response|\Zend\View\Model\ViewModel
  */
 public function loginAction()
 {
     /* @var $form \Application\Form\Login */
     $form = new \Application\Form\Login();
     $request = $this->getRequest();
     if ($request->isPost()) {
         $form->setData($request->getPost());
         if ($form->isValid()) {
             $value = $form->getData();
             /* @var $auth \Zend\Authentication\AuthenticationService */
             $auth = $this->getServiceLocator()->get('Zend\\Authentication\\AuthenticationService');
             $auth->getAdapter()->setIdentity($value['username'])->setCredential($value['password']);
             /* @var $rs \Zend\Authentication\Result */
             $rs = $auth->authenticate();
             if ($rs->isValid()) {
                 return $this->redirect()->toRoute('profile');
             } else {
                 $this->flashMessenger()->addMessage('Login Failed');
                 return $this->redirect()->toRoute('login');
             }
         }
     }
     return new ViewModel(array('form' => $form));
 }