Пример #1
0
 /**
  * @param Request $request
  * @param $type
  * @return array|\Symfony\Component\HttpFoundation\RedirectResponse
  * @throws Exception
  * @Template()
  */
 public function createAction(Request $request, $type)
 {
     if ($this->hasPerm(Role::ROLE_USER)) {
         throw new Exception($this->get("translator")->trans("you.have.no.permission"), 404, null);
     }
     $repository = $this->em()->getRepository("ErikUserBundle:Role");
     $user = new User();
     $role = $this->getRole($repository, $type);
     $user->addRole($role);
     $form = $this->createForm(new UserType(), $user, array('action' => $this->generateUrl("erik_user_create", array("type" => $type))));
     $form->handleRequest($request);
     if ($form->isValid()) {
         $user = $form->getData();
         $response = $this->get("auth")->createUser($user, $this->get("security.encoder_factory"));
         switch ($response) {
             case 1:
                 $this->get("session")->getFlashBag()->add('error', $this->get("translator")->trans("email.already.exists"));
                 break;
             case 2:
                 $this->get("session")->getFlashBag()->add('error', $this->get("translator")->trans("register.failed"));
                 break;
             case 0:
                 $this->get("session")->getFlashBag()->add('success', $this->get("translator")->trans("register.ok"));
                 return $this->redirect($this->generateUrl("erik_user_signin"));
         }
     }
     return array("form" => $form->createView());
 }
Пример #2
0
 /**
  * returns int
  * 0 - success
  * 1 - wrong email
  * 2 - any error
  */
 public function createUser(User $user, \Symfony\Component\Security\Core\Encoder\EncoderFactory $ef)
 {
     if ($this->em->getRepository("ErikUserBundle:User")->findBy(array("email" => $user->getEmail()))) {
         return 1;
     }
     $encoder = $ef->getEncoder($user);
     $password = $encoder->encodePassword($user->getPassword(), $user->getSalt());
     $user->setPassword($password);
     $user->setActive(true);
     $user->setBlocked(false);
     $user->addRole($this->em->getReference("ErikUserBundle:Role", Role::ROLE_USER));
     $this->em->persist($user);
     $this->em->flush();
     return $user->getId() > 0 ? 0 : 2;
 }