public function indexAction() { $form = new LoginForm(); $error = false; $request = $this->getRequest(); if ($request->isPost()) { $form->setData($request->getPost()); if ($form->isValid()) { $data = $request->getPost()->toArray(); } $auth = new AuthenticationService(); $sessionStorage = new SessionStorage("Application"); $auth->setStorage($sessionStorage); $authAdapter = $this->getServiceLocator()->get('Application\\Auth\\DoctrineAdapter'); $authAdapter->setUsername($data['email'])->setPassword($data['password']); $result = $auth->authenticate($authAdapter); if ($result->isValid()) { $sessionStorage->write($auth->getIdentity()['user'], null); return $this->redirect()->toRoute("Application", array('controller' => 'IndexController', 'action' => 'index')); } else { $error = true; } } return new ViewModel(array('form' => $form, 'error' => $error)); }
/** * @return array */ public function loginAction() { $form = new Login($this->getAuthenticationService()); $request = $this->getRequest(); if ($request->isPost()) { if ($request->getPost('forgot') !== null) { return $this->redirect()->toRoute('forgot'); } $form->setData($request->getPost()); if ($form->isValid()) { if ($redirectTo = $this->params()->fromQuery('redirectTo')) { return $this->redirect()->toUrl($redirectTo); } return $this->redirect()->toRoute('home'); } } return array('form' => $form); }
public function loginAction() { $userLogin = new AuthenticationService(); if ($userLogin->hasIdentity()) { $identity = $userLogin->getIdentity(); return $this->forward()->dispatch('Application\\Controller\\Index', array('action' => 'listar')); } else { $form = new Login(); $form->get('submit')->setValue('Login'); $messages = null; $request = $this->getRequest(); if ($request->isPost()) { $loginFilters = new LoginValidator(); $form->setInputFilter($loginFilters->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'); $indexAdapter = new IndexAdapter($dbAdapter, 'zml_admin', 'usr_name', 'usr_password'); $indexAdapter->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($indexAdapter); switch ($result->getCode()) { case Result::FAILURE_IDENTITY_NOT_FOUND: echo "prueba"; die; break; case Result::FAILURE_CREDENTIAL_INVALID: echo "prueba"; die; break; case Result::SUCCESS: $storage = $auth->getStorage(); $storage->write($indexAdapter->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); } return $this->forward()->dispatch('Application\\Controller\\Index', array('action' => 'listar')); break; default: echo "prueba"; die; break; } foreach ($result->getMessages() as $message) { $messages .= "{$message}\n"; } } } $viewModel = new ViewModel(array('form' => $form, 'messages' => $messages)); $viewModel->setTerminal(true); return $viewModel; } }