/**
  * genNewPassword Action
  *
  * @param guid $id
  * @param string $code
  *
  * @return RedirectResponse|Response
  */
 public function genNewPasswordAction($id, $code)
 {
     if ($this->hasRole('IS_AUTHENTICATED_FULLY')) {
         return $this->redirect($this->generateUrl('_security_profile'));
     }
     $em = $this->getEntityManager();
     try {
         $user = null;
         $user = $em->getRepository('AcfDataBundle:User')->find($id);
         if (null != $user) {
             $now = new \DateTime('now');
             if (null == $user->getRecoveryExpiration() || $user->getRecoveryExpiration() < $now) {
                 $this->flashMsgSession('error', $this->translate('_security.genNewPassword.errorparams2'));
             } elseif ($user->getRecoveryCode() != $code) {
                 $this->flashMsgSession('error', $this->translate('_security.genNewPassword.errorparams3'));
             } else {
                 $user->setSalt(md5(uniqid(null, true)));
                 $user->setClearPassword(User::generateRandomChar(8, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'));
                 $user->setRecoveryExpiration(null);
                 $user->setRecoveryCode(null);
                 $em->persist($user);
                 $em->flush();
                 $mvars = array();
                 $mvars['user'] = $user;
                 $from = $this->getParameter('mail_from');
                 $fromName = $this->getParameter('mail_from_name');
                 $subject = $this->translate('_mail.genNewPassword.subject', array(), 'messages');
                 $message = \Swift_Message::newInstance();
                 $message->setFrom($from, $fromName);
                 $message->setTo($user->getEmail(), $user->getFullname());
                 $message->setSubject($subject);
                 $mvars['logo'] = $message->embed(\Swift_Image::fromPath($this->getParameter('kernel.root_dir') . '/../web/bundles/acfres/images/logo_acf.jpg'));
                 $message->setBody($this->renderView('AcfSecurityBundle:Mail:genNewPassword.html.twig', $mvars), 'text/html');
                 $this->sendmail($message);
                 $this->flashMsgSession('success', $this->translate('_security.genNewPassword.ok'));
             }
         } else {
             $this->flashMsgSession('error', $this->translate('_security.genNewPassword.errorparams1'));
         }
     } catch (\Exception $e) {
         $logger = $this->getLogger();
         $logger->error($e->getMessage());
         $this->flashMsgSession('error', $this->translate('_security.genNewPassword.errorparams4'));
     }
     return $this->redirect($this->generateUrl('_security_login'));
 }