public function loginAction() { $form = new LoginForm('', false, $this->getServiceLocator()); $request = $this->getRequest(); if ($request->isPost()) { $inputFilter = new InputFilter(); $factory = new InputFactory(); $inputFilter->add($factory->createInput(array('name' => 'username', 'required' => true, 'filters' => array(array('name' => 'StripTags'), array('name' => 'StringTrim')), 'validators' => array(array('name' => 'NotEmpty'))))); $inputFilter->add($factory->createInput(array('name' => 'password', 'required' => true, 'filters' => array(array('name' => 'StripTags'), array('name' => 'StringTrim')), 'validators' => array(array('name' => 'NotEmpty'))))); $form->setInputFilter($inputFilter); $form->setData($request->getPost()); if ($form->isValid()) { //check authentication... $this->getAuthService()->getAdapter()->setIdentity('' . $request->getPost('username'))->setCredential('' . $request->getPost('password')); $result = $this->getAuthService()->authenticate(); foreach ($result->getMessages() as $message) { //save message temporary into flashmessenger $this->flashmessenger()->addMessage($message); } if ($result->isValid()) { $this->getAuthService()->getStorage()->write($this->getAuthService()->getAdapter()->getResultRowObject(null)); return $this->redirect()->toRoute('bhome'); } } } return array('form' => $form); }
public function indexAction() { $this->layout('layout/login'); $auth = new AuthenticationService(); $viewmodel = new ViewModel(); $form = new LoginForm(); $request = $this->getRequest(); $filters = new LoginFormFilter(); $form->get('submit')->setValue('Login'); $message = ""; //Message if ($auth->hasIdentity()) { return $this->redirect()->toRoute('backend_index'); } if ($request->isPost()) { $form->setInputFilter($filters); $form->setData($request->getPost()); if ($form->isValid()) { $data = $form->getData(); $sm = $this->getServiceLocator(); $dbAdapter = $sm->get('Zend\\Db\\Adapter\\Adapter'); $authAdapter = new AuthAdapter($dbAdapter, 'user', 'username', 'password', 'sha1(?)'); $authAdapter->setIdentity($data['username'])->setCredential($data['password']); $result = $auth->authenticate($authAdapter); switch ($result->getCode()) { case Result::SUCCESS: $storage = $auth->getStorage(); $storage->write($authAdapter->getResultRowObject(null, 'password')); return $this->redirect()->toRoute('backend_index'); break; default: $message = "Usuario o clave incorrecto."; break; } } } $viewmodel->form = $form; $viewmodel->message = $message; return $viewmodel; }