/** * Registration step 2: Create user and set registration token * * @Route("/user/registration") * @Method("POST") * @Template("FOMUserBundle:Registration:form.html.twig") */ public function register() { $user = new User(); $form = $this->createForm(new UserRegistrationType(), $user); $form->bind($this->get('request')); //@TODO: Check if username and email are unique if ($form->isValid()) { $helper = new UserHelper($this->container); $helper->setPassword($user, $user->getPassword()); $user->setRegistrationToken(hash("sha1", rand())); $user->setRegistrationTime(new \DateTime()); $groupRepository = $this->getDoctrine()->getRepository('FOMUserBundle:Group'); foreach ($this->container->getParameter('fom_user.self_registration_groups') as $groupTitle) { $group = $groupRepository->findOneByTitle($groupTitle); if ($group) { $user->addGroups($group); } else { $msg = sprintf('Self-registration group "%s" not found for user "%s"', $groupTitle, $user->getUsername()); $this->get('logger')->err($msg); } } $this->sendEmail($user); $em = $this->getDoctrine()->getManager(); $em->persist($user); $em->flush(); $helper->giveOwnRights($user); return $this->redirect($this->generateUrl('fom_user_registration_send')); } return array('user' => $user, 'form' => $form->createView(), 'form_name' => $form->getName()); }
/** * @ManagerRoute("/user") * @Method({ "POST" }) * @Template("FOMUserBundle:User:form.html.twig") */ public function createAction() { $user = new User(); // ACL access check $securityContext = $this->get('security.context'); $oid = new ObjectIdentity('class', get_class($user)); if (false === $securityContext->isGranted('CREATE', $oid)) { throw new AccessDeniedException(); } $groupPermission = $securityContext->isGranted('EDIT', new ObjectIdentity('class', 'FOM\\UserBundle\\Entity\\Group')) || $securityContext->isGranted('OWNER', $oid); $profile = $this->addProfileForm($user); $form = $this->createForm(new UserType(), $user, array('profile_formtype' => $profile['formtype'], 'group_permission' => $groupPermission, 'acl_permission' => $securityContext->isGranted('OWNER', $oid))); $form->bind($this->get('request')); if ($form->isValid()) { // Set encrypted password and create new salt // The unencrypted password is already set on the user! $helper = new UserHelper($this->container); $helper->setPassword($user, $user->getPassword()); $user->setRegistrationTime(new \DateTime()); $em = $this->getDoctrine()->getManager(); $em->getConnection()->beginTransaction(); try { $em->getConnection()->beginTransaction(); $profile = $user->getProfile(); $user->setProfile(null); $em->persist($user); // SQLite needs a flush here $em->flush(); // Check and persists profile if exists if ($profile) { $profile->setUid($user); $em->persist($profile); } $em->flush(); $em->getConnection()->commit(); if ($form->has('acl')) { $aclManager = $this->get('fom.acl.manager'); $aclManager->setObjectACLFromForm($user, $form->get('acl'), 'object'); } $em->flush(); // Make sure, the new user has VIEW & EDIT permissions $helper->giveOwnRights($user); $em->getConnection()->commit(); } catch (\Exception $e) { $em->getConnection()->rollback(); throw $e; } $this->get('session')->getFlashBag()->set('success', 'The user has been saved.'); return $this->redirect($this->generateUrl('fom_user_user_index')); } $this->get('session')->getFlashBag()->set('error', 'There are field validation errors.'); return array('user' => $user, 'form' => $form->createView(), 'form_name' => $form->getName(), 'edit' => false, 'profile_template' => $profile['template'], 'profile_assets' => $profile['assets']); }