src/CorporateTrainingBundle/Controller/PasswordResetController.php line 17

Open in your IDE?
  1. <?php
  2. namespace CorporateTrainingBundle\Controller;
  3. use AppBundle\Common\SmsToolkit;
  4. use AppBundle\Controller\BaseController;
  5. use Biz\System\Service\SettingService;
  6. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  7. use Symfony\Component\Form\Extension\Core\Type\FormType;
  8. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  11. use Topxia\Service\Common\ServiceKernel;
  12. class PasswordResetController extends BaseController
  13. {
  14.     public function indexAction(Request $request)
  15.     {
  16.         $user $this->getCurrentUser();
  17.         $data = ['email' => ''];
  18.         if ($user->isLogin()) {
  19.             if (!$user['setup'] || false != stripos($user['email'], '@edusoho.net')) {
  20.                 return $this->redirect($this->generateUrl('homepage'));
  21.             } else {
  22.                 $data['email'] = '';
  23.             }
  24.         }
  25.         $form $this->container->get('form.factory')->createBuilder(FormType::class, $data)
  26.                 ->add('email'EmailType::class)
  27.                 ->getForm();
  28.         $error null;
  29.         if ('POST' == $request->getMethod()) {
  30.             $form->handleRequest($request);
  31.             if ($form->isSubmitted() && $form->isValid()) {
  32.                 $data $form->getData();
  33.                 $user $this->getUserService()->getUserByEmail($data['email']);
  34.                 if (empty($user)) {
  35.                     list($result$message) = $this->getAuthService()->checkEmail($data['email']);
  36.                     if ('error_duplicate' == $result) {
  37.                         $error ServiceKernel::instance()->trans('password_reset.message.error_duplicate');
  38.                         return $this->render('password-reset/index.html.twig', [
  39.                             'form' => $form->createView(),
  40.                             'error' => $error,
  41.                         ]);
  42.                     }
  43.                 }
  44.                 if ($user) {
  45.                     $token $this->getUserService()->makeToken('password-reset'$user['id'], strtotime('+1 day'));
  46.                     try {
  47.                         $site $this->setting('site', []);
  48.                         $mailOptions = [
  49.                             'to' => $user['email'],
  50.                             'template' => 'email_reset_password',
  51.                             'format' => 'html',
  52.                             'params' => [
  53.                                 'nickname' => $user['nickname'],
  54.                                 'verifyurl' => $this->generateUrl('password_reset_update', ['token' => $token], UrlGeneratorInterface::ABSOLUTE_URL),
  55.                                 'sitename' => $site['name'],
  56.                                 'siteurl' => $site['url'],
  57.                             ],
  58.                         ];
  59.                         $mailFactory $this->getBiz()->offsetGet('mail_factory');
  60.                         $mail $mailFactory($mailOptions);
  61.                         $mail->send();
  62.                     } catch (\Exception $e) {
  63.                         $this->getLogService()->error('user''password-reset''重设密码邮件发送失败:'.$e->getMessage());
  64.                         return $this->createMessageResponse('error''password_reset.message.reset_error');
  65.                     }
  66.                     $this->getLogService()->info('user''password-reset'"{$user['email']}向发送了找回密码邮件。");
  67.                     return $this->render(
  68.                         'password-reset/sent.html.twig',
  69.                         [
  70.                             'user' => $user,
  71.                             'emailLoginUrl' => $this->getEmailLoginUrl($user['email']),
  72.                         ]
  73.                     );
  74.                 } else {
  75.                     $error ServiceKernel::instance()->trans('password_reset.message.email_login_error');
  76.                 }
  77.             }
  78.         }
  79.         return $this->render(
  80.             'password-reset/index.html.twig',
  81.             [
  82.                 'form' => $form->createView(),
  83.                 'error' => $error,
  84.             ]
  85.         );
  86.     }
  87.     public function updateAction(Request $request)
  88.     {
  89.         $token $this->getUserService()->getToken('password-reset'$request->query->get('token') ?: $request->request->get('token'));
  90.         if (empty($token)) {
  91.             return $this->render(
  92.                 'password-reset/error.html.twig'
  93.             );
  94.         }
  95.         $form $this->container->get('form.factory')->createBuilder(FormType::class)
  96.             ->add('password'PasswordType::class)
  97.             ->add('confirmPassword'PasswordType::class)
  98.             ->getForm();
  99.         if ('POST' == $request->getMethod()) {
  100.             $form->handleRequest($request);
  101.             if ($form->isSubmitted() && $form->isValid()) {
  102.                 $data $form->getData();
  103.                 $this->getAuthService()->changePassword($token['userId'], null$data['password']);
  104.                 $this->getUserService()->deleteToken('password-reset'$token['token']);
  105.                 return $this->render(
  106.                     'password-reset/success.html.twig'
  107.                 );
  108.             }
  109.         }
  110.         return $this->render(
  111.             'password-reset/update.html.twig',
  112.             [
  113.                 'form' => $form->createView(),
  114.             ]
  115.         );
  116.     }
  117.     public function changeRawPasswordAction(Request $request)
  118.     {
  119.         $fields $request->query->all();
  120.         $user_token $this->getTokenService()->verifyToken('email_password_reset'$fields['token']);
  121.         $flag $this->getUserService()->changeRawPassword($user_token['data']['userId'], $user_token['data']['rawPassword']);
  122.         if (!$flag) {
  123.             return $this->render(
  124.                 'password-reset/raw-error.html.twig'
  125.             );
  126.         } else {
  127.             return $this->render(
  128.                 'password-reset/raw-success.html.twig'
  129.             );
  130.         }
  131.     }
  132.     public function resetBySmsAction(Request $request)
  133.     {
  134.         if ('POST' == $request->getMethod()) {
  135.             $data $request->request->all();
  136.             list($result$sessionField$requestField) = SmsToolkit::smsCheck($request$scenario 'sms_forget_password');
  137.             if ($result) {
  138.                 $targetUser $this->getUserService()->getUserByVerifiedMobile($request->request->get('mobile'));
  139.                 if (empty($targetUser)) {
  140.                     return $this->createMessageResponse('error''password_reset.reset_by_sms.message.user_empty');
  141.                 }
  142.                 $token $this->getUserService()->makeToken('password-reset'$targetUser['id'], strtotime('+1 day'));
  143.                 $request->request->set('token'$token);
  144.                 return $this->redirect($this->generateUrl('password_reset_update', [
  145.                     'token' => $token,
  146.                 ]));
  147.             } else {
  148.                 return $this->createMessageResponse('error''password_reset.reset_by_sms.message.reset_error');
  149.             }
  150.         }
  151.         return $this->createJsonResponse('GET method');
  152.     }
  153.     public function getEmailLoginUrl($email)
  154.     {
  155.         $host substr($emailstrpos($email'@') + 1);
  156.         if ('hotmail.com' == $host) {
  157.             return 'http://www.'.$host;
  158.         }
  159.         if ('gmail.com' == $host) {
  160.             return 'http://mail.google.com';
  161.         }
  162.         return 'http://mail.'.$host;
  163.     }
  164.     public function checkMobileExistsAction(Request $request)
  165.     {
  166.         $mobile $request->query->get('value');
  167.         list($result$message) = $this->getAuthService()->checkMobile($mobile);
  168.         if ('success' == $result) {
  169.             $response = ['success' => false'message' => ServiceKernel::instance()->trans('password_reset.check_mobile_exists.mobile_empty')];
  170.         } else {
  171.             $response = ['success' => true'message' => ''];
  172.         }
  173.         return $this->createJsonResponse($response);
  174.     }
  175.     protected function getAuthService()
  176.     {
  177.         return $this->createService('User:AuthService');
  178.     }
  179.     protected function getTokenService()
  180.     {
  181.         return $this->createService('User:TokenService');
  182.     }
  183.     /**
  184.      * @return SettingService
  185.      */
  186.     protected function getSettingService()
  187.     {
  188.         return $this->createService('System:SettingService');
  189.     }
  190. }