/** * Allows to create new password for user and to send his login information by mail. * @param integer $userId Identifier of user we want to update password and send mail. * @param boolean $newUser TRUE in cas of new user (send login data), FALSE in case of existing user (forgotten password). * @return boolean TRUE in case of success, FALSE either. */ public function sendNewAccountInformation($userId, $newUser) { $user = $this->entityManager->getRepository('jcUserBundle:User')->find($userId); if ($user) { try { $newPassword = PasswordUtil::generatePassword(6, true, true, true, false); $user->setPassword(PasswordUtil::encodePassword($newPassword)); // Save User object in database $this->entityManager->persist($user); $this->entityManager->flush(); $messageContent = $this->templating->render('jcMailBundle:mail:password.html.twig', array('login' => $user->getUsername(), 'newPassword' => $newPassword)); $subject = $newUser ? 'Identifiants de connexion' : 'Mot de passe oublié'; if (strlen($this->prefix) > 0) { $subject = '[' . $this->prefix . '] - ' . $subject; } // Create mail and send it $mailMessage = \Swift_Message::newInstance()->setSubject($subject)->setFrom($this->fromMail, $this->fromName)->setTo($user->getMail())->setContentType('text/html')->setBody($messageContent); $this->mailer->send($mailMessage); return true; } catch (Exception $e) { return false; } } else { return false; } }
public function accountAction() { $loggedUser = $this->getUser(); $request = $this->getRequest(); $accountInfo = new AccountInfo(); // If user has submit form => save user acount info if ($request->getMethod() == 'POST') { try { $entityManager = $this->getDoctrine()->getManager(); $form = $this->createForm(new AccountInfoType(), $accountInfo); $form->bind($request); // If password changed => check password security + password confirmation if (strlen($accountInfo->getPassword()) > 0) { if (!ValidateUtil::checkPassword($accountInfo->getPassword(), 1)) { $form->get('password')->addError(new FormError("Le mot de passe n'est pas assez fort")); } else { if (strcmp($accountInfo->getPassword(), $accountInfo->getConfirmPassword()) != 0) { $form->get('confirmPassword')->addError(new FormError("La confirmation du mot de passe n'est pas correcte")); } } } // Check mail unicity if (!$entityManager->getRepository('jcUserBundle:User')->checkMailForUser($accountInfo->getMail(), $loggedUser->getId())) { $form->get('mail')->addError(new FormError("Ce mail est déjà utilisé")); } if ($form->isValid()) { // Get User object from database => for update $user = $entityManager->getRepository('jcUserBundle:User')->find($loggedUser->getId()); // Populate form data in User object $user->setFirstname($accountInfo->getFirstname()); $user->setLastname($accountInfo->getLastname()); $user->setMail($accountInfo->getMail()); // Encode password using SHA if (strlen($accountInfo->getPassword()) > 0) { $user->setPassword(PasswordUtil::encodePassword($accountInfo->getPassword())); } // Save User object in database $entityManager->persist($user); $entityManager->flush(); $request->getSession()->getFlashBag()->add('popup-message', 'Votre compte a bien été mis à jour'); } } catch (Exception $e) { $request->getSession()->getFlashBag()->add('popup-message', 'Erreur lors de la mise à jour de vos informations personnelles'); } } else { // Initialize AccountInfo with logged user data // NOTE : Ignore password (unchanged if ignored) $accountInfo->setFirstname($loggedUser->getFirstname()); $accountInfo->setLastname($loggedUser->getLastname()); $accountInfo->setMail($loggedUser->getMail()); $form = $this->createForm(new AccountInfoType(), $accountInfo); } return $this->render('jcUserBundle:FO:account.html.twig', array('accountInfo' => $form->createView())); }
public function editAction($id) { $request = $this->getRequest(); $entityManager = $this->getDoctrine()->getManager(); $user = null; // If identifier is defined => use it if ($id > 0) { $user = $entityManager->getRepository('jcUserBundle:User')->find($id); } else { $user = new User(); $user->setEnabled(true); } // If user has submit form => save user if ($request->getMethod() == 'POST') { try { $form = $this->createForm(new UserType(), $user); $form->bind($request); if ($form->isValid()) { $checkUnicity = true; // Check mail unicity (mail is used as login) if (!$entityManager->getRepository('jcUserBundle:User')->checkMailForUser($user->getMail(), $id)) { $form->get('mail')->addError(new FormError("Le mail doit être unique")); $request->getSession()->getFlashBag()->add('bo-warning-message', 'Un autre utilisateur utilise déjà le mail indiqué'); $checkUnicity = false; } // Save user only if data are unique (mail + login) if ($checkUnicity) { // Process upload $this->processUpload($user); // For new user => generate new password if (!$user->getId()) { $user->setPassword(PasswordUtil::encodePassword(PasswordUtil::generatePassword(6, true, true, true, false))); } // If no skill => erase skill note if (!$user->getHasSkill()) { $user->setSkillNote(''); } $entityManager->persist($user); $entityManager->flush(); $request->getSession()->getFlashBag()->add('bo-log-message', 'Sauvegarde de l\'utilisateur OK'); // If account must be sent to user => update password + send mail if ($request->request->get('send-account')) { $mailService = $this->get('jc_mail.mail_service'); if ($mailService->sendNewAccountInformation($user->getId(), true)) { $request->getSession()->getFlashBag()->add('bo-log-message', 'Envoi du mail à l\'utilisateur OK'); } else { $request->getSession()->getFlashBag()->add('bo-error-message', 'Erreur lors de l\'envoi du mail'); } } return $this->redirect($this->generateUrl('jc_user_bo_list')); } } else { $request->getSession()->getFlashBag()->add('bo-warning-message', 'Certains champs ne sont pas remplis correctement'); } } catch (Exception $e) { $request->getSession()->getFlashBag()->add('bo-error-message', 'Erreur lors de la sauvegarde de l\'utilisateur'); } } else { $form = $this->createForm(new UserType(), $user); } return $this->render('jcUserBundle:BO:edit.html.twig', array('userToEdit' => $form->createView())); }
/** * @Route("/admin/user/edit/{id}", defaults={"id" = 0}, name="jc_user_bo_edit") */ public function editAction(Request $request, $id) { $entityManager = $this->getDoctrine()->getManager(); $user = $id > 0 ? $entityManager->getRepository('jcUserBundle:User')->find($id) : new User(); // If user has submit form => save user if ($request->getMethod() == 'POST') { try { // Keep initial password for further use... $initialPassword = $user->getPassword(); $form = $this->createForm(new UserType(), $user); $form->handleRequest($request); $generatePassword = $request->request->get('generate-password'); if ($generatePassword) { $user->setPassword(PasswordUtil::generatePassword(6, true, true, true, false)); } else { // For new user or if password changed => check password security + password confirmation if (!$user->getId() || strlen($user->getPassword()) > 0) { if (!ValidateUtil::checkPassword($user->getPassword(), 1)) { $form->get('password')->addError(new FormError("Le mot de passe n'est pas assez fort")); } else { if (strcmp($user->getPassword(), $user->getConfirmPassword()) != 0) { $form->get('confirmPassword')->addError(new FormError("La confirmation du mot de passe n'est pas correcte")); } } } } if ($form->isValid()) { // For generated password, new user or new password => Encode password using SHA if ($generatePassword || !$user->getId() || strlen($user->getPassword()) > 0) { $user->setPassword(PasswordUtil::encodePassword($user->getPassword())); } else { $user->setPassword($initialPassword); } $entityManager->persist($user); $entityManager->flush(); $request->getSession()->getFlashBag()->add('bo-log-message', 'Sauvegarde OK'); // If account must be sent to user => update password + send mail if ($request->request->get('generate-password')) { $accountMailService = $this->get('jc_user.account_mail'); if ($accountMailService->sendNewAccountInformation($user->getId(), true)) { $request->getSession()->getFlashBag()->add('bo-log-message', 'Envoi du mail à l\'utilisateur OK'); } else { $request->getSession()->getFlashBag()->add('bo-error-message', 'Erreur lors de l\'envoi du mail'); } } return $this->redirect($this->generateUrl('jc_user_bo_list')); } else { $request->getSession()->getFlashBag()->add('bo-warning-message', 'Certains champs ne sont pas remplis correctement'); } } catch (Exception $e) { $request->getSession()->getFlashBag()->add('bo-error-message', 'Erreur lors de la sauvegarde'); } } else { // Erase password information $user->setPassword(''); $form = $this->createForm(new UserType(), $user); } // Get role list to select user's role $roleList = $this->getDoctrine()->getManager()->getRepository('jcUserBundle:Role')->findAll(); return $this->render('jcUserBundle:BO:edit.html.twig', array('userToEdit' => $form->createView(), 'roleList' => $roleList)); }