Exemplo n.º 1
0
 public function registerAction(Request $request, Response $response, $args)
 {
     $data = $request->getParsedBody();
     $username = $data['username'];
     $email = $data['email'];
     $pass = $data['password'];
     $hash = new Hash();
     $res = true;
     if (!Validate::isValidUsername($username)) {
         $res = false;
         $this->flash->addMessage('error', 'ERROR-NAME !');
     }
     if (!Validate::isValidEmail($email)) {
         $res = false;
         $this->flash->addMessage('error', 'ERROR-EMAIL !');
     }
     if (!Validate::isValidPass($pass)) {
         $res = false;
         $this->flash->addMessage('error', 'ERROR-PASSWORD !');
     }
     $listUser = $this->em->getRepository('App\\Model\\Users')->findOneBy(['username' => $data['username']]);
     $listEmail = $this->em->getRepository('App\\Model\\Users')->findOneBy(['email' => $data['email']]);
     if ($listUser) {
         $res = false;
         $this->flash->addMessage('error', 'NAME DONE !');
     }
     if ($listEmail) {
         $res = false;
         $this->flash->addMessage('error', 'EMAIL HAVE !');
     }
     if ($res) {
         $user = new Users();
         $data['password'] = $hash->create($data['password'], SALT);
         $user->setUsername($data['username']);
         $user->setFullname($data['fullname']);
         $user->setEmail($data['email']);
         $user->setPassword($data['password']);
         $user->setJob($data['job']);
         //            $user->getCreatedAt(new \DateTime());
         try {
             $this->em->persist($user);
             $this->em->flush();
         } catch (\Exception $e) {
             $this->flash->addMessage('error', $e->getMessage());
             return $response->withStatus(301)->withHeader('Location', '/login');
         }
         $this->flash->addMessage('success', "SUCCESS !");
     }
     return $response->withStatus(301)->withHeader('Location', '/login');
 }