Пример #1
0
 /**
  * Logs the user in, redirecting to /admin if success, displaying errors otherwise
  *
  * @param Psr\Http\Message\ServerRequestInterface $request
  * @param Psr\Http\Message\ResponseInterface $response
  * @param array $args
  */
 public function store($request, $response, $args)
 {
     $data = $request->getParsedBody();
     // Pass only username and password to the validator
     if (isset($data['username'], $data['password'])) {
         $formData = ['username' => $data['username'], 'password' => $data['password']];
         // Submitted data to display on the form in case of errors
         $this->context['submitted']['username'] = htmlspecialchars(strip_tags($formData['username']));
         $validation = $this->loginValidator->validate($formData);
         if ($validation->passed()) {
             $login = $this->authentication->login($formData['username'], $formData['password']);
             if (true === $login) {
                 // Redirect to admin
                 return $response->withStatus(303)->withHeader('Location', '/admin');
             } else {
                 // Set error message that login data not correct
                 $this->context['errors']['form'][] = 'Cannot log you in. Please try again!';
             }
         } else {
             // Set errors from validation class
             $this->context['errors'] = $validation->getErrors();
         }
     } else {
         $this->context['errors']['form'][] = 'Each field is required';
     }
     // Display the login page again with errors
     return $this->show($request, $response, $args);
 }
Пример #2
0
 /**
  * @test
  * @covers SlimApp\Authentication::login
  * @uses SlimApp\Session::delete
  * @uses SlimApp\Session::exists
  * @uses SlimApp\Session::get
  * @param string $findRowReturns
  * @param boolean $loginResult
  * @dataProvider provider_login_sets_UserId_session_variable_and_returns_true_if_login_data_correct_returns_false_otherwise
  */
 public function login_sets_UserId_session_variable_and_returns_true_if_login_data_correct_returns_false_otherwise($findRowReturns, $loginResult)
 {
     // Clean the session before the tests
     Session::delete('UserId');
     if ('user' === $findRowReturns) {
         $userId = 12;
         $user = $this->getMockBuilder('\\SlimApp\\User')->setMethods(['getUserId'])->getMock();
         $user->expects($this->once())->method('getUserId')->will($this->returnValue($userId));
         $resultFindRow = $user;
     } else {
         $userId = false;
         $resultFindRow = false;
     }
     $mapper = $this->getMockBuilder('\\SlimApp\\Db\\Mapper')->setMethods(['findRow'])->getMock();
     $mapper->expects($this->once())->method('findRow')->will($this->returnValue($resultFindRow));
     $authentication = new Authentication($mapper);
     $login = $authentication->login('username', 'password');
     $this->assertEquals($loginResult, $login);
     $this->assertEquals($loginResult, Session::exists('UserId'));
     $this->assertEquals($loginResult, $authentication->userLoggedIn());
     $this->assertEquals($userId, Session::get('UserId'));
 }