src/AppBundle/Listener/KernelResponseListener.php line 22

Open in your IDE?
  1. <?php
  2. namespace AppBundle\Listener;
  3. use AppBundle\Controller\OAuth2\OAuthUser;
  4. use Symfony\Component\HttpFoundation\Cookie;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  7. use Symfony\Component\HttpKernel\HttpKernelInterface;
  8. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  9. use Topxia\Service\Common\ServiceKernel;
  10. class KernelResponseListener extends AbstractSecurityDisabledListener
  11. {
  12.     private $container;
  13.     public function __construct($container)
  14.     {
  15.         $this->container $container;
  16.     }
  17.     public function onKernelResponse(ResponseEvent $event)
  18.     {
  19.         if (HttpKernelInterface::MAIN_REQUEST != $event->getRequestType()) {
  20.             return;
  21.         }
  22.         $request $event->getRequest();
  23.         if ($this->isSecurityDisabledRequest($this->container$request)) {
  24.             return;
  25.         }
  26.         $currentUser $this->getUserService()->getCurrentUser();
  27.         $auth $this->getSettingService()->get('auth');
  28.         if ($currentUser->isLogin() && !in_array('ROLE_SUPER_ADMIN'$currentUser['roles'])
  29.             && isset($auth['fill_userinfo_after_login']) && $auth['fill_userinfo_after_login'] && isset($auth['registerSort'])
  30.         ) {
  31.             $whiteList $this->getRouteWhiteList();
  32.             if (in_array($request->getPathInfo(), $whiteList) || strstr($request->getPathInfo(), '/admin')
  33.                 || strstr($request->getPathInfo(), '/register/submited') || strstr($request->getPathInfo(), '/mapi_v2')
  34.             ) {
  35.                 return;
  36.             }
  37.             $isFillUserInfo $this->checkUserinfoFieldsFill($currentUser);
  38.             // TODO 因为移动端的第三方注册做到了web端,所以增加一个 skip 判断,如果以后移动端端这块业务剥离,这个判断要去掉
  39.             if (!$isFillUserInfo && !$request->getSession()->get(OAuthUser::SESSION_SKIP_KEY)) {
  40.                 $url $this->container->get('router')->generate('login_after_fill_userinfo', ['goto' => $this->getTargetPath($request)]);
  41.                 $response = new RedirectResponse($url);
  42.                 $event->setResponse($response);
  43.                 return;
  44.             }
  45.         }
  46.         // $tokenInHeader = $request->cookies->get('web-view-access');
  47.         // $event->getResponse()->headers->setCookie(new Cookie('web-view-access', $tokenInHeader));
  48.         $this->container->get('app_web_view_authentication_token_helper')->createCurrentUserFromTokenAndGroupPage($request);
  49.     }
  50.     protected function getRouteWhiteList()
  51.     {
  52.         return [
  53.             '/fill/userinfo''/login''/logout''/login_check''/register/mobile/check',
  54.             '/register/email/check''/login/bind/weixinmob/newset',
  55.             '/login/bind/weixinmob/existbind''/login/bind/weixinweb/newset',
  56.             '/login/bind/qq/newset''/login/bind/weibo/newset''/login/bind/renren/newset',
  57.             '/login/bind/qq/exist''/login/bind/weibo/exist''/login/bind/renren/exist',
  58.             '/login/bind/weixinweb/exist''/login/bind/weixinmob/exist',
  59.             '/login/bind/weixinmob/choose''/login/bind/weixinmob/changetoexist',
  60.             '/login/bind/qq/new''/login/bind/weibo/new''/login/bind/renren/new',
  61.             '/login/bind/weixinmob/new''/login/bind/weixinweb/new',
  62.             '/partner/phpwind/api/notify''/partner/login''/partner/logout',
  63.             '/login/weixinmob''/login/bind/weixinmob/existbind',
  64.             '/captcha_num''/register/captcha/check''/edu_cloud/sms_send',
  65.             '/edu_cloud/sms_check/sms_bind',
  66.         ];
  67.     }
  68.     protected function generateUrl($router$params = [], $withHost false)
  69.     {
  70.         return $this->container->get('router')->generate($router$params$withHost);
  71.     }
  72.     protected function getTargetPath($request)
  73.     {
  74.         if ($request->query->get('goto')) {
  75.             $targetPath $request->query->get('goto');
  76.         } elseif ($request->getSession()->has('_target_path')) {
  77.             $targetPath $request->getSession()->get('_target_path');
  78.         } else {
  79.             $targetPath $request->headers->get('Referer');
  80.         }
  81.         if ($targetPath == $this->generateUrl('login', [], UrlGeneratorInterface::ABSOLUTE_URL)) {
  82.             return $this->generateUrl('homepage');
  83.         }
  84.         $url explode('?'$targetPath);
  85.         if ($url[0] == $this->generateUrl('partner_logout', [], UrlGeneratorInterface::ABSOLUTE_URL)) {
  86.             return $this->generateUrl('homepage');
  87.         }
  88.         if ($url[0] == $this->generateUrl('password_reset_update', [], UrlGeneratorInterface::ABSOLUTE_URL)) {
  89.             $targetPath $this->generateUrl('homepage', [], UrlGeneratorInterface::ABSOLUTE_URL);
  90.         }
  91.         return $targetPath;
  92.     }
  93.     private function checkUserinfoFieldsFill($user)
  94.     {
  95.         $auth $this->getSettingService()->get('auth');
  96.         $userProfile $this->getUserService()->getUserProfile($user['id']);
  97.         $userProfile['email'] = strstr($user['email'], '@edusoho.net') ? '' $user['email'];
  98.         $userProfile['mobile'] = empty($auth['mobileSmsValidate']) ? $userProfile['mobile'] : $user['verifiedMobile'];
  99.         $isFillUserInfo true;
  100.         if ($auth['registerSort']) {
  101.             foreach ($auth['registerSort'] as $key => $val) {
  102.                 if (!$userProfile[$val]) {
  103.                     $isFillUserInfo false;
  104.                 }
  105.             }
  106.         }
  107.         return $isFillUserInfo;
  108.     }
  109.     protected function getServiceKernel()
  110.     {
  111.         return ServiceKernel::instance();
  112.     }
  113.     protected function getSettingService()
  114.     {
  115.         return ServiceKernel::instance()->createService('System:SettingService');
  116.     }
  117.     protected function getUserService()
  118.     {
  119.         return ServiceKernel::instance()->createService('User:UserService');
  120.     }
  121. }