/**
  * 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);
 }
 function isCountRequest(Psr\Http\Message\ServerRequestInterface $request)
 {
     return strpos($request->getUri()->getPath(), '/count') !== false;
 }
Example #3
0
 /**
  * Creates a new user or displays the form again with errors
  *
  * @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, email, password and passwordConfirm to the validator
     if (isset($data['username'], $data['email'], $data['password'], $data['passwordConfirm'])) {
         $formData = ['username' => $data['username'], 'email' => $data['email'], 'password' => $data['password'], 'passwordConfirm' => $data['passwordConfirm']];
         // Submitted data to display on the form in case of errors
         $this->context['submitted'] = ['username' => htmlspecialchars(strip_tags($data['username'])), 'email' => htmlspecialchars(strip_tags($data['email']))];
         $validation = $this->usersStoreValidator->validate($formData);
         if ($validation->passed()) {
             // Create a hash of the password
             $password = password_hash($formData['password'], PASSWORD_DEFAULT);
             $columnNames = ['username', 'password', 'email'];
             $values = [$formData['username'], $password, $formData['email']];
             $newUserStored = $this->userMapper->insert($columnNames, $values);
             if (!empty($this->context['user'])) {
                 // Registration ( /register )
                 $template = 'users/store.twig';
                 $data['title'] = 'Registration successful';
             } else {
                 // Logged in user creating new user ( /admin/users/create )
                 $template = 'admin/users/store.twig';
                 $data['title'] = 'New User Created';
             }
             if (true === $newUserStored) {
                 $data['user'] = ['username' => $formData['username'], 'email' => $formData['email']];
                 // Display confirmation message
                 $this->view->render($response, $template, $data);
                 $this->resetContextValues();
                 return $response;
             } else {
                 // Set error message that login data not correct
                 $this->context['errors']['form'][] = 'Could not create new user. 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 register / add new user page with errors
     return $this->create($request, $response, $args);
 }