Beispiel #1
0
 public function createAction()
 {
     $request = $this->get('request');
     if ($request->getMethod() == 'POST') {
         $validations = array('firstname' => 'words', 'lastname' => 'words', 'email' => 'email', 'password' => 'password');
         $required = array('firstname', 'lastname', 'email', 'password');
         $sanitize = array('firstname', 'lastname', 'email', 'password');
         $validator = new FormValidator($validations, $required, $sanitize);
         if ($validator->validate($_POST)) {
             // Create new user object
             $newUser = new User();
             $newUser->setUsername($request->get("email"));
             $newUser->setEmail($request->get("email"));
             $newUser->setFirstName($request->get("firstname"));
             $newUser->setLastName($request->get("lastname"));
             $em = $this->getDoctrine()->getManager();
             $defaultRole = $em->getRepository('RockITTechgamesBundle:Role')->findOneBy(array('role' => 'ROLE_USER'));
             $newUser->addRole($defaultRole);
             // Encode password
             $factory = $this->container->get('security.encoder_factory');
             $encoder = $factory->getEncoder($newUser);
             $password = $encoder->encodePassword($request->get("password"), null);
             $newUser->setPassword($password);
             // Save new user
             try {
                 $em->persist($newUser);
                 $em->flush();
                 $userId = $newUser->getId();
                 return $this->redirect($this->generateUrl('user_edit', array('userId' => $userId)));
             } catch (\Exception $e) {
                 // Get last variables
                 $firstname = "";
                 if ($request->get("firstname")) {
                     $firstname = $request->get("firstname");
                 }
                 $lastname = "";
                 if ($request->get("lastname")) {
                     $lastname = $request->get("lastname");
                 }
                 $email = "";
                 if ($request->get("email")) {
                     $email = $request->get("email");
                 }
                 return $this->render('RockITTechgamesBundle:Profile:create.html.twig', array('message' => "Unable to register this username. Please try a different username.", 'firstname' => $firstname, 'lastname' => $lastname, 'email' => $email, 'password' => "", 'errors' => $validator->getErrors()));
             }
         } else {
             // Get last variables
             $firstname = "";
             if ($request->get("firstname")) {
                 $firstname = $request->get("firstname");
             }
             $lastname = "";
             if ($request->get("lastname")) {
                 $lastname = $request->get("lastname");
             }
             $email = "";
             if ($request->get("email")) {
                 $email = $request->get("email");
             }
             return $this->render('RockITTechgamesBundle:Profile:create.html.twig', array('message' => "Unable to register this user.", 'firstname' => $firstname, 'lastname' => $lastname, 'email' => $email, 'password' => "", 'errors' => $validator->getErrors()));
         }
     }
     return $this->render('RockITTechgamesBundle:Profile:create.html.twig', array('message' => "", 'firstname' => "", 'lastname' => "", 'email' => "", 'password' => "", 'errors' => array()));
 }
 public function registerAction(Request $request)
 {
     if ($request->getMethod() == 'POST') {
         // Validate input paramters
         $validations = array('firstname' => 'words', 'lastname' => 'words', 'email' => 'email', 'password' => 'password');
         $required = array('firstname', 'lastname', 'email', 'password');
         $sanitize = array('firstname', 'lastname', 'email', 'password');
         $validator = new FormValidator($validations, $required, $sanitize);
         if ($validator->validate($_POST)) {
             // Create new user object
             $newUser = new User();
             $newUser->setUsername($request->get("email"));
             $newUser->setEmail($request->get("email"));
             $newUser->setFirstName($request->get("firstname"));
             $newUser->setLastName($request->get("lastname"));
             $em = $this->getDoctrine()->getManager();
             $defaultRole = $em->getRepository('RockITTechgamesBundle:Role')->findOneBy(array('role' => 'ROLE_USER'));
             $newUser->addRole($defaultRole);
             // Encode password
             $factory = $this->container->get('security.encoder_factory');
             $encoder = $factory->getEncoder($newUser);
             $password = $encoder->encodePassword($request->get("password"), null);
             $newUser->setPassword($password);
             // Save new user
             try {
                 $em->persist($newUser);
                 $em->flush();
                 // creates a token and assigns it, effectively logging the user in with the credentials they just registered
                 $token = new UsernamePasswordToken($newUser, null, 'secured_area', array('ROLE_USER'));
                 $this->get('security.context')->setToken($token);
                 $this->get('session')->set('_security_secured_area', serialize($token));
                 return $this->redirect($this->generateUrl('rock_it_techgames_homepage'));
             } catch (\Exception $e) {
                 // Get last variables
                 $firstname = "";
                 if ($request->get("firstname")) {
                     $firstname = $request->get("firstname");
                 }
                 $lastname = "";
                 if ($request->get("lastname")) {
                     $lastname = $request->get("lastname");
                 }
                 $email = "";
                 if ($request->get("email")) {
                     $email = $request->get("email");
                 }
                 return $this->render('RockITTechgamesBundle:Security:register.html.twig', array('message' => "Unable to register this username. Please try a different username.", 'last_firstname' => $firstname, 'last_lastname' => $lastname, 'last_email' => $email, 'errors' => $validator->getErrors()));
             }
         } else {
             //                $response = new JsonResponse();
             //
             //                $response->setStatusCode(400);
             //
             //                $response->setData(array(
             //                    'message' => "Invalid form",
             //                    "errors" => $validator->getErrors()
             //                ));
             // Get last variables
             $firstname = "";
             if ($request->get("firstname")) {
                 $firstname = $request->get("firstname");
             }
             $lastname = "";
             if ($request->get("lastname")) {
                 $lastname = $request->get("lastname");
             }
             $email = "";
             if ($request->get("email")) {
                 $email = $request->get("email");
             }
             return $this->render('RockITTechgamesBundle:Security:register.html.twig', array('message' => "Unable to register this user.", 'last_firstname' => $firstname, 'last_lastname' => $lastname, 'last_email' => $email, 'errors' => $validator->getErrors()));
         }
     } else {
         return $this->render('RockITTechgamesBundle:Security:register.html.twig', array('last_firstname' => "", 'last_lastname' => "", 'last_email' => "", 'errors' => array()));
     }
 }