Exemplo n.º 1
0
 public function indexAction()
 {
     $request = $this->getRequest();
     if (!$request->isPost()) {
         return array('query' => $this->params()->fromQuery());
     }
     $item = $request->getPost();
     $form = new \User\Form\LoginForm();
     $form->bind($item);
     if ($form->isValid()) {
         $callback = $this->params()->fromPost('callback');
         $callback = $callback ? $callback : '/';
         $item = $form->getData();
         $itemModel = Api::_()->getModel('User\\Model\\Login');
         $itemModel->setItem($item)->login();
         $loginResult = $itemModel->getLoginResult();
         if ($item['rememberMe']) {
             $tokenString = $itemModel->createToken();
             //Cookie expired after 60 days
             $this->cookie()->crypt(false)->write('realm', $tokenString, 3600 * 24 * 60);
         }
         if ($loginResult->isValid()) {
             return $this->redirect()->toUrl($callback);
         } else {
             $this->flashMessenger()->setNamespace('login-result')->addMessage($loginResult);
             return $this->redirect()->toUrl('/login/');
         }
     } else {
         $item = $form->getData();
     }
     return array('form' => $form, 'item' => $item, 'query' => $this->params()->fromQuery());
 }
Exemplo n.º 2
0
 /**
  *  login action test
  */
 public function testLoginAction()
 {
     $form = new \User\Form\LoginForm('form-login', ['serviceLocator' => $this->getApplicationServiceLocator()]);
     $userData = ['name' => 'User', 'email' => 'user@nix.com', 'password' => '123456'];
     $this->createUser($userData, \User\Entity\User::ROLE_USER);
     $postData = ['email' => $userData['email'], 'password' => $userData['password'], 'security' => $form->get('security')->getValue()];
     $this->dispatch('/user/auth/login', 'POST', $postData);
     $this->assertResponseStatusCode(302);
     $this->assertRedirectTo('/');
 }