src/AppBundle/Controller/LoginController.php line 142

Open in your IDE?
  1. <?php
  2. namespace AppBundle\Controller;
  3. use ApiBundle\Api\Exception\ErrorCode;
  4. use AppBundle\Common\ArrayToolkit;
  5. use AppBundle\Common\Exception\RuntimeException;
  6. use AppBundle\Component\OAuthClient\OAuthClientFactory;
  7. use Biz\Common\BizSms;
  8. use CorporateTrainingBundle\System;
  9. use Firebase\JWT\JWT;
  10. use Firebase\JWT\Key;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  13. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  14. use Symfony\Component\Security\Core\Security;
  15. class LoginController extends BaseController
  16. {
  17.     public function indexAction(Request $request)
  18.     {
  19.         $user $this->getCurrentUser();
  20.         if ($user->isLogin()) {
  21.             return $this->createMessageResponse('info''login.message.repeat_login'null3000$this->generateUrl('homepage'));
  22.         }
  23.         $error $this->checkLoginError($request);
  24.         if ($error) {
  25.             $request->getSession()->remove(Security::AUTHENTICATION_ERROR);
  26.         } else {
  27.             $url $this->checkInviteCodeLogin($request);
  28.             if (!empty($url)) {
  29.                 return $this->redirect($url);
  30.             }
  31.         }
  32.         $this->callRemoteService($request$user);
  33.         $_target_path $this->getTargetPath($request);
  34.         $liveLogin $this->checkLiveLogin($request$_target_path);
  35.         return $this->render(
  36.             'login/index.html.twig',
  37.             [
  38.                 'last_username' => $request->getSession()->get(Security::LAST_USERNAME),
  39.                 'error' => $error,
  40.                 '_target_path' => $_target_path,
  41.                 'liveLogin' => $liveLogin,
  42.             ]
  43.         );
  44.     }
  45.     protected function callRemoteService(Request $request$user)
  46.     {
  47.         if (in_array('ROLE_SUPER_ADMIN'$user['roles'])) {
  48.             $siteInfo $this->getSettingService()->get('site_info', []);
  49.             if (!empty($siteInfo) && false == $siteInfo['status']) {
  50.                 $siteInfo['applicationVersion'] = System::CT_VERSION;
  51.                 $siteInfo['domainName'] = $request->getHttpHost();
  52.                 if (empty($siteInfo['cloud_key'])) {
  53.                     $settings $this->getSettingService()->get('storage', []);
  54.                     if (!empty($settings['cloud_access_key'])) {
  55.                         $siteInfo['accessKey'] = $settings['cloud_access_key'];
  56.                         $this->postRequest('http://ct.edusoho.com/api/app_install'json_encode($siteInfo));
  57.                     }
  58.                 }
  59.             }
  60.         }
  61.     }
  62.     public function externalLoginAction(Request $request)
  63.     {
  64.         // 新增开关校验
  65.         $setting $this->getSettingService()->get('api');
  66.         if (empty($setting['external_switch'])) {
  67.             throw new BadRequestHttpException('API设置未开启'nullErrorCode::INVALID_ARGUMENT);
  68.         }
  69.         $token $request->get('token''');
  70.         if (!$token) {
  71.             throw new BadRequestHttpException('请求参数错误'nullErrorCode::INVALID_ARGUMENT);
  72.         }
  73.         $data JWT::decode($token, new Key($setting['api_app_secret_key'], 'HS256'));
  74.         if (empty($data) || empty($data->identifyValue) || empty($data->identifyType) || !in_array($data->identifyType, ['username''mobile''email'])) {
  75.             throw new BadRequestHttpException('请求参数错误'nullErrorCode::INVALID_ARGUMENT);
  76.         }
  77.         $user $this->getUserService()->getUserByLoginTypeAndField($data->identifyType$data->identifyValue);
  78.         if (empty($user)) {
  79.             return $this->createMessageResponse('error''external.login.message.error'null0);
  80.         }
  81.         $this->authenticateUser($user);
  82.         return $this->redirect($this->generateUrl('homepage'));
  83.     }
  84.     protected function checkLiveLogin(Request $request, &$_target_path)
  85.     {
  86.         $liveLogin preg_match('/\/live\/(.*?)\/(.*)/'$_target_path$match);
  87.         if ($liveLogin && in_array($match[2], ['login''entry''show''replay'])) {
  88.             $_target_path $this->generateUrl('live_activity_play', ['activityId' => $match[1]]);
  89.         }
  90.         return $liveLogin $match[1] : 0;
  91.     }
  92.     public function ajaxAction(Request $request)
  93.     {
  94.         $clients OAuthClientFactory::clients();
  95.         return $this->render('login/ajax.html.twig', [
  96.             '_target_path' => $this->getTargetPath($request),
  97.             'clients' => $clients,
  98.         ]);
  99.     }
  100.     public function checkEmailAction(Request $request)
  101.     {
  102.         $email $request->query->get('value');
  103.         $user $this->getUserService()->getUserByEmail($email);
  104.         if ($user) {
  105.             $response = ['success' => true'message' => '该Email地址可以登录'];
  106.         } else {
  107.             $response = ['success' => false'message' => '该Email地址尚未注册'];
  108.         }
  109.         return $this->createJsonResponse($response);
  110.     }
  111.     public function oauth2LoginsBlockAction($targetPath$displayName true$isMayday 0)
  112.     {
  113.         $clients OAuthClientFactory::clients();
  114.         return $this->render('login/oauth2-logins-block.html.twig', [
  115.             'clients' => $clients,
  116.             'targetPath' => $targetPath,
  117.             'displayName' => $displayName,
  118.             'isMayday' => $isMayday,
  119.         ]);
  120.     }
  121.     public function smsAction(Request $request)
  122.     {
  123.         $user $this->getCurrentUser();
  124.         if ($user->isLogin()) {
  125.             return $this->createMessageResponse('info''你已经登录了'null3000$this->getTargetPath($request));
  126.         }
  127.         if ($request->isMethod('POST')) {
  128.             if (!$this->checkSmsCode($request)) {
  129.                 throw new RuntimeException('短信验证码错误!');
  130.             }
  131.             // 按手机号获取用户,没有就注册
  132.             $user $this->getUserService()->getUserByVerifiedMobile($request->request->get('login_mobile'));
  133.             if (empty($user)) {
  134.                 throw new RuntimeException('该手机账号不存在');
  135.             }
  136.             if ($user['locked']) {
  137.                 throw new RuntimeException('该用户已被封禁!');
  138.             }
  139.             $this->authenticateUser($user);
  140.             return $this->createJsonResponse(['goto' => $this->getTargetPath($request)]);
  141.         }
  142.         return $this->render('login/sms.html.twig', [
  143.             '_target_path' => $this->getTargetPath($request),
  144.         ]);
  145.     }
  146.     public function checkMobileAction(Request $request)
  147.     {
  148.         $mobile $request->request->get('mobile''');
  149.         $result = !empty($mobile) && !empty($this->getUserService()->getUserByVerifiedMobile($mobile));
  150.         return $this->createJsonResponse($result);
  151.     }
  152.     public function checkSmsCodeAction(Request $request)
  153.     {
  154.         return $this->createJsonResponse($this->checkSmsCode($request));
  155.     }
  156.     public function captchaCheckAction(Request $request)
  157.     {
  158.         $captchaFilledByUser strtolower($request->request->get('value'));
  159.         if ($request->getSession()->get('captcha_code') == $captchaFilledByUser) {
  160.             $response true;
  161.         } else {
  162.             $request->getSession()->set('captcha_code'mt_rand(0999999999));
  163.             $response false;
  164.         }
  165.         return $this->createJsonResponse($response);
  166.     }
  167.     protected function checkInviteCodeLogin(Request $request)
  168.     {
  169.         if ($this->getWebExtension()->isMicroMessenger() && $this->setting('login_bind.enabled'0) && $this->setting('login_bind.weixinmob_enabled'0)) {
  170.             $inviteCode $request->query->get('inviteCode''');
  171.             return $this->generateUrl('login_bind', ['type' => 'weixinmob''_target_path' => $this->getTargetPath($request), 'inviteCode' => $inviteCode]);
  172.         }
  173.         if ($this->getCTWebExtension()->isDingTalk() && $this->setting('login_bind.enabled'0) && $this->setting('login_bind.dingtalkmob_enabled'0)) {
  174.             $inviteCode $request->query->get('inviteCode''');
  175.             return $this->generateUrl('login_bind', ['type' => 'dingtalkmob''_target_path' => $this->getTargetPath($request), 'inviteCode' => $inviteCode]);
  176.         }
  177.         if ($this->getCTWebExtension()->isWorkWechat() && $this->setting('login_bind.enabled'0) && $this->setting('login_bind.workwechatmob_enabled'0)) {
  178.             $inviteCode $request->query->get('inviteCode''');
  179.             return $this->generateUrl('login_bind', ['type' => 'workwechatmob''_target_path' => $this->getTargetPath($request), 'inviteCode' => $inviteCode]);
  180.         }
  181.         if ($this->getCTWebExtension()->isFeiShu() && $this->setting('login_bind.enabled'0) && $this->setting('login_bind.feishumob_enabled'0)) {
  182.             $inviteCode $request->query->get('inviteCode''');
  183.             return $this->generateUrl('login_bind', ['type' => 'feishumob''_target_path' => $this->getTargetPath($request), 'inviteCode' => $inviteCode]);
  184.         }
  185.         return 0;
  186.     }
  187.     protected function getCTWebExtension()
  188.     {
  189.         return $this->container->get('corporatetrainingbundle.twig.web_extension');
  190.     }
  191.     protected function checkLoginError(Request $request)
  192.     {
  193.         if ($request->attributes->has(Security::AUTHENTICATION_ERROR)) {
  194.             return $request->attributes->get(Security::AUTHENTICATION_ERROR);
  195.         }
  196.         return $request->getSession()->get(Security::AUTHENTICATION_ERROR);
  197.     }
  198.     protected function checkSmsCode(Request $request)
  199.     {
  200.         $fields $request->request->all();
  201.         if (!ArrayToolkit::requireds($fields, ['login_mobile''sms_token''login_sms_code'])) {
  202.             return false;
  203.         }
  204.         // 检查短信验证码
  205.         $status $this->getBizSms()->check(BizSms::SMS_LOGIN$fields['login_mobile'], $fields['sms_token'], $fields['login_sms_code']);
  206.         if (BizSms::STATUS_SUCCESS !== $status) {
  207.             return false;
  208.         }
  209.         return true;
  210.     }
  211.     protected function getTargetPath(Request $request)
  212.     {
  213.         if ($request->query->get('goto')) {
  214.             $targetPath $request->query->get('goto');
  215.         } elseif ($request->getSession()->has('_target_path')) {
  216.             $targetPath $request->getSession()->get('_target_path');
  217.         } else {
  218.             $targetPath $request->headers->get('Referer''');
  219.         }
  220.         if ($targetPath == $this->generateUrl('login', [], UrlGeneratorInterface::ABSOLUTE_URL)) {
  221.             return $this->generateUrl('homepage');
  222.         }
  223.         $url explode('?'$targetPath);
  224.         if ($url[0] == $this->generateUrl('partner_logout', [], UrlGeneratorInterface::ABSOLUTE_URL)) {
  225.             return $this->generateUrl('homepage');
  226.         }
  227.         if ($url[0] == $this->generateUrl('password_reset_update', [], UrlGeneratorInterface::ABSOLUTE_URL)) {
  228.             $targetPath $this->generateUrl('homepage', [], UrlGeneratorInterface::ABSOLUTE_URL);
  229.         }
  230.         if (=== strpos($targetPath'/app.php')) {
  231.             $targetPath str_replace('/app.php'''$targetPath);
  232.         }
  233.         return $targetPath;
  234.     }
  235.     protected function getWebExtension()
  236.     {
  237.         return $this->container->get('web.twig.extension');
  238.     }
  239.     /**
  240.      * @return BizSms
  241.      */
  242.     protected function getBizSms()
  243.     {
  244.         $biz $this->getBiz();
  245.         return $biz['biz_sms'];
  246.     }
  247.     protected function getSettingService()
  248.     {
  249.         return $this->container->get('biz')->service('System:SettingService');
  250.     }
  251. }