/** * Utility function that generates a unique username. */ public static function uniqueUserName(User $user, \Doctrine\ORM\EntityManager $em) { // create basic username and increment until it is unique $username = strtolower(substr($user->getFirstName(), 0, 1) . $user->getLastName()); $append = 0; $new_username = $username; do { // check the username $user = $em->getRepository('Bkstg\\CoreBundle\\Entity\\User')->findOneBy(array('username' => $new_username)); // user found, create a new user if ($user !== null) { $new_username = $username . '_' . $append; $append++; } } while ($user !== null); // return the new unique username return $new_username; }
/** * @Route\Route("/edit/{user}/profile", name="bkstg_user_edit_profile_user") * @Route\ParamConverter("user", class="BkstgCoreBundle:User") */ public function editProfileAction(User $user, Http\Request $request) { // get entity manager $em = $this->getDoctrine()->getManager(); $app_user = $this->get('security.token_storage')->getToken()->getUser(); if ($app_user->getId() !== $user->getId()) { throw new AccessDeniedException(); } // create the form for this $form = $this->createForm(new ProfileType('Bkstg\\CoreBundle\\Entity\\User'), $user); // handle form request $form->handleRequest($request); if ($form->isValid()) { // set user defaults $user->setUsername($user->getEmail()); // persist the user $em->persist($user); $em->flush(); // add success message and redirect $this->addFlash('success', 'User "' . $user . '" edited successfully!'); return $this->redirectToRoute('bkstg_user_home'); } // get message manager $message_manager = $this->get('message.manager'); return $this->render('BkstgCoreBundle:User:profile_form.html.twig', array('form' => $form->createView(), 'message_manager' => $message_manager)); }