Esempio n. 1
0
 public function login(Request $request, Response $response, $args)
 {
     $this->logger->info("Login page action start");
     if ($request->getMethod() == 'POST') {
         $uri = $request->getUri();
         $data = $request->getParsedBody();
         $this->logger->info("Login with parameters POST");
         $v = new $this->validator($data);
         $v->lang('es');
         $v->rule('required', array('username', 'password'));
         if ($v->validate()) {
             $this->logger->info("Login with parameters VALIDATION PASS");
             try {
                 $this->logger->info("Login with parameters: " . $data['username'] . " - " . $data['password']);
                 $this->currentUser = UserRepository::logIn($data['username'], $data['password']);
                 if ($this->currentUser != null && $this->currentUser->isAuthenticated()) {
                     $this->flash->addMessage('info', 'Sample flash message');
                     $this->logger->info("Login successfull redirected to Home");
                     return $response->withStatus(302)->withHeader('Location', $uri->withPath(''));
                 }
             } catch (ParseException $e) {
                 ParseErrorHandler::handleParseError($e);
                 $this->flash->addMessage('error', $e->getMessage());
                 $this->logger->error("Login parse exception ·" . $e->getMessage() . " REDIRECT  Login");
                 return $response->withStatus(302)->withHeader('Location', $uri->withPath('login'));
             }
         }
         foreach ($v->errors() as $field => $errors) {
             foreach ($errors as $error) {
                 $this->flash->addMessage('error', $error);
             }
         }
         $this->logger->error("Login form validation fail·- REDIRECT  Login");
         return $response->withStatus(302)->withHeader('Location', $uri->withPath('login'));
     }
     // Get Messages
     $messages = $this->flash->getMessages();
     // Fetch Template
     $body = $this->view->fetch('login/login.twig', ['flash' => $messages]);
     $this->logger->info("Login page dispathed");
     // Write Response
     return $response->write($body);
 }