public function loginAction() { $messages = null; $form = new AuthForm(); $form->get('submit')->setvalue('Login'); $request = $this->getRequest(); if ($request->isPost()) { $authFormFilters = new Auth(); $form->setInputFilter($authFormFilters->getInputFilter()); $form->setData($request->getPost()); if ($form->isValid()) { $data = $form->getData(); $sm = $this->getServiceLocator(); $dbAdapter = $sm->get('Zend\\Db\\Adapter\\Adapter'); $config = $this->getServiceLocator()->get('Config'); $staticSalt = $config['static_salt']; $authAdapter = new AuthAdapter($dbAdapter, 'users', 'usr_name', 'usr_password', "MD5(CONCAT('{$staticSalt}', ?, usr_password_salt)) AND usr_active = 1"); $authAdapter->setIdentity($data['usr_name'])->setCredential($data['usr_password']); $auth = new AuthenticationService(); // or prepare in the globa.config.php and get it from there. Better to be in a module, so we can replace in another module. // $auth = $this->getServiceLocator()->get('Zend\Authentication\AuthenticationService'); // $sm->setService('Zend\Authentication\AuthenticationService', $auth); // You can set the service here but will be loaded only if this action called. $result = $auth->authenticate($authAdapter); // echo '<pre>'; // print_r($result); // echo '</pre>'; switch ($result->getCode()) { case Result::FAILURE_IDENTITY_NOT_FOUND: // do stuff for nonexistent identity break; case Result::FAILURE_CREDENTIAL_INVALID: // do stuff for invalid credential break; case Result::SUCCESS: $storage = $auth->getStorage(); $storage->write($authAdapter->getResultRowObject(null, 'usr_password')); $time = 1209600; // 14 days 1209600/3600 = 336 hours => 336/24 = 14 days // if ($data['rememberme']) $storage->getSession()->getManager()->rememberMe($time); // no way to get the session // if ($data['rememberme']) { // $sessionManager = new \Zend\Session\SessionManager(); // $sessionManager->rememberMe($time); // } break; default: // do stuff for other failure break; } foreach ($result->getMessages() as $message) { $messages .= "{$message}\n"; } } else { echo '<h1> The form is NOT valid </h1>'; } } // echo '<pre>'; // print_r($_SESSION); // echo '</pre>'; return new ViewModel(array('form' => $form, 'messages' => $messages)); }
public function loginAction() { $messages = null; $form = new AuthForm(); $form->get('submit')->setValue('Login'); $request = $this->getRequest(); if ($request->isPost()) { $authFormFilters = new Auth(); $form->setInputFilter($authFormFilters->getInputFilter()); $form->setData($request->getPost()); if ($form->isValid()) { $data = $form->getData(); $sm = $this->getServiceLocator(); $dbAdapter = $sm->get('Zend\\Db\\Adapter\\Adapter'); $config = $this->getServiceLocator()->get('Config'); $staticSalt = $config['static_salt']; $authAdapter = new AuthAdapter($dbAdapter, 'users', 'usr_name', 'usr_password', "MD5 (CONCAT('{$staticSalt}', ?, usr_password_salt)) AND usr_active = 1"); $authAdapter->setIdentity($data['usr_name'])->setCredential($data['usr_password']); $auth = new AuthenticationService(); $result = $auth->authenticate($authAdapter); switch ($result->getCode()) { case Result::FAILURE_IDENTITY_NOT_FOUND: // do stuff for nonexistent identity break; case Result::FAILURE_CREDENTIAL_INVALID: // do stuff for invalid credential break; case Result::SUCCESS: $storage = $auth->getStorage(); $storage->write($authAdapter->getResultRowObject(null, 'usr_password')); /*$time = 1209600; // 14 days 1209600/3600 = 336 hours => 336/24 = 14 days // if ($data['rememberme']) $storage->getSession()->getManager()->rememberMe($time); // no way to get the session if ($data['rememberme']) { $sessionManager = new \Zend\Session\SessionManager(); $sessionManager->rememberMe($time); }*/ break; default: // do stuff for other failure break; } foreach ($result->getMessages() as $message) { $messages .= "{$message}\n"; } //echo '<pre>'; //print_r($_SESSION); //echo '</pre>'; } else { //echo 'Form is not valid!'; } } return new viewModel(array('form' => $form, 'messages' => $messages)); }