<?php
namespace CorporateTrainingBundle\Controller;
use AppBundle\Common\SmsToolkit;
use AppBundle\Controller\BaseController;
use Biz\System\Service\SettingService;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Topxia\Service\Common\ServiceKernel;
class PasswordResetController extends BaseController
{
public function indexAction(Request $request)
{
$user = $this->getCurrentUser();
$data = ['email' => ''];
if ($user->isLogin()) {
if (!$user['setup'] || false != stripos($user['email'], '@edusoho.net')) {
return $this->redirect($this->generateUrl('homepage'));
} else {
$data['email'] = '';
}
}
$form = $this->container->get('form.factory')->createBuilder(FormType::class, $data)
->add('email', EmailType::class)
->getForm();
$error = null;
if ('POST' == $request->getMethod()) {
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$user = $this->getUserService()->getUserByEmail($data['email']);
if (empty($user)) {
list($result, $message) = $this->getAuthService()->checkEmail($data['email']);
if ('error_duplicate' == $result) {
$error = ServiceKernel::instance()->trans('password_reset.message.error_duplicate');
return $this->render('password-reset/index.html.twig', [
'form' => $form->createView(),
'error' => $error,
]);
}
}
if ($user) {
$token = $this->getUserService()->makeToken('password-reset', $user['id'], strtotime('+1 day'));
try {
$site = $this->setting('site', []);
$mailOptions = [
'to' => $user['email'],
'template' => 'email_reset_password',
'format' => 'html',
'params' => [
'nickname' => $user['nickname'],
'verifyurl' => $this->generateUrl('password_reset_update', ['token' => $token], UrlGeneratorInterface::ABSOLUTE_URL),
'sitename' => $site['name'],
'siteurl' => $site['url'],
],
];
$mailFactory = $this->getBiz()->offsetGet('mail_factory');
$mail = $mailFactory($mailOptions);
$mail->send();
} catch (\Exception $e) {
$this->getLogService()->error('user', 'password-reset', '重设密码邮件发送失败:'.$e->getMessage());
return $this->createMessageResponse('error', 'password_reset.message.reset_error');
}
$this->getLogService()->info('user', 'password-reset', "{$user['email']}向发送了找回密码邮件。");
return $this->render(
'password-reset/sent.html.twig',
[
'user' => $user,
'emailLoginUrl' => $this->getEmailLoginUrl($user['email']),
]
);
} else {
$error = ServiceKernel::instance()->trans('password_reset.message.email_login_error');
}
}
}
return $this->render(
'password-reset/index.html.twig',
[
'form' => $form->createView(),
'error' => $error,
]
);
}
public function updateAction(Request $request)
{
$token = $this->getUserService()->getToken('password-reset', $request->query->get('token') ?: $request->request->get('token'));
if (empty($token)) {
return $this->render(
'password-reset/error.html.twig'
);
}
$form = $this->container->get('form.factory')->createBuilder(FormType::class)
->add('password', PasswordType::class)
->add('confirmPassword', PasswordType::class)
->getForm();
if ('POST' == $request->getMethod()) {
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$this->getAuthService()->changePassword($token['userId'], null, $data['password']);
$this->getUserService()->deleteToken('password-reset', $token['token']);
return $this->render(
'password-reset/success.html.twig'
);
}
}
return $this->render(
'password-reset/update.html.twig',
[
'form' => $form->createView(),
]
);
}
public function changeRawPasswordAction(Request $request)
{
$fields = $request->query->all();
$user_token = $this->getTokenService()->verifyToken('email_password_reset', $fields['token']);
$flag = $this->getUserService()->changeRawPassword($user_token['data']['userId'], $user_token['data']['rawPassword']);
if (!$flag) {
return $this->render(
'password-reset/raw-error.html.twig'
);
} else {
return $this->render(
'password-reset/raw-success.html.twig'
);
}
}
public function resetBySmsAction(Request $request)
{
if ('POST' == $request->getMethod()) {
$data = $request->request->all();
list($result, $sessionField, $requestField) = SmsToolkit::smsCheck($request, $scenario = 'sms_forget_password');
if ($result) {
$targetUser = $this->getUserService()->getUserByVerifiedMobile($request->request->get('mobile'));
if (empty($targetUser)) {
return $this->createMessageResponse('error', 'password_reset.reset_by_sms.message.user_empty');
}
$token = $this->getUserService()->makeToken('password-reset', $targetUser['id'], strtotime('+1 day'));
$request->request->set('token', $token);
return $this->redirect($this->generateUrl('password_reset_update', [
'token' => $token,
]));
} else {
return $this->createMessageResponse('error', 'password_reset.reset_by_sms.message.reset_error');
}
}
return $this->createJsonResponse('GET method');
}
public function getEmailLoginUrl($email)
{
$host = substr($email, strpos($email, '@') + 1);
if ('hotmail.com' == $host) {
return 'http://www.'.$host;
}
if ('gmail.com' == $host) {
return 'http://mail.google.com';
}
return 'http://mail.'.$host;
}
public function checkMobileExistsAction(Request $request)
{
$mobile = $request->query->get('value');
list($result, $message) = $this->getAuthService()->checkMobile($mobile);
if ('success' == $result) {
$response = ['success' => false, 'message' => ServiceKernel::instance()->trans('password_reset.check_mobile_exists.mobile_empty')];
} else {
$response = ['success' => true, 'message' => ''];
}
return $this->createJsonResponse($response);
}
protected function getAuthService()
{
return $this->createService('User:AuthService');
}
protected function getTokenService()
{
return $this->createService('User:TokenService');
}
/**
* @return SettingService
*/
protected function getSettingService()
{
return $this->createService('System:SettingService');
}
}