<?php
namespace AppBundle\Listener;
use AppBundle\Controller\OAuth2\OAuthUser;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Topxia\Service\Common\ServiceKernel;
class KernelResponseListener extends AbstractSecurityDisabledListener
{
private $container;
public function __construct($container)
{
$this->container = $container;
}
public function onKernelResponse(ResponseEvent $event)
{
if (HttpKernelInterface::MAIN_REQUEST != $event->getRequestType()) {
return;
}
$request = $event->getRequest();
if ($this->isSecurityDisabledRequest($this->container, $request)) {
return;
}
$currentUser = $this->getUserService()->getCurrentUser();
$auth = $this->getSettingService()->get('auth');
if ($currentUser->isLogin() && !in_array('ROLE_SUPER_ADMIN', $currentUser['roles'])
&& isset($auth['fill_userinfo_after_login']) && $auth['fill_userinfo_after_login'] && isset($auth['registerSort'])
) {
$whiteList = $this->getRouteWhiteList();
if (in_array($request->getPathInfo(), $whiteList) || strstr($request->getPathInfo(), '/admin')
|| strstr($request->getPathInfo(), '/register/submited') || strstr($request->getPathInfo(), '/mapi_v2')
) {
return;
}
$isFillUserInfo = $this->checkUserinfoFieldsFill($currentUser);
// TODO 因为移动端的第三方注册做到了web端,所以增加一个 skip 判断,如果以后移动端端这块业务剥离,这个判断要去掉
if (!$isFillUserInfo && !$request->getSession()->get(OAuthUser::SESSION_SKIP_KEY)) {
$url = $this->container->get('router')->generate('login_after_fill_userinfo', ['goto' => $this->getTargetPath($request)]);
$response = new RedirectResponse($url);
$event->setResponse($response);
return;
}
}
// $tokenInHeader = $request->cookies->get('web-view-access');
// $event->getResponse()->headers->setCookie(new Cookie('web-view-access', $tokenInHeader));
$this->container->get('app_web_view_authentication_token_helper')->createCurrentUserFromTokenAndGroupPage($request);
}
protected function getRouteWhiteList()
{
return [
'/fill/userinfo', '/login', '/logout', '/login_check', '/register/mobile/check',
'/register/email/check', '/login/bind/weixinmob/newset',
'/login/bind/weixinmob/existbind', '/login/bind/weixinweb/newset',
'/login/bind/qq/newset', '/login/bind/weibo/newset', '/login/bind/renren/newset',
'/login/bind/qq/exist', '/login/bind/weibo/exist', '/login/bind/renren/exist',
'/login/bind/weixinweb/exist', '/login/bind/weixinmob/exist',
'/login/bind/weixinmob/choose', '/login/bind/weixinmob/changetoexist',
'/login/bind/qq/new', '/login/bind/weibo/new', '/login/bind/renren/new',
'/login/bind/weixinmob/new', '/login/bind/weixinweb/new',
'/partner/phpwind/api/notify', '/partner/login', '/partner/logout',
'/login/weixinmob', '/login/bind/weixinmob/existbind',
'/captcha_num', '/register/captcha/check', '/edu_cloud/sms_send',
'/edu_cloud/sms_check/sms_bind',
];
}
protected function generateUrl($router, $params = [], $withHost = false)
{
return $this->container->get('router')->generate($router, $params, $withHost);
}
protected function getTargetPath($request)
{
if ($request->query->get('goto')) {
$targetPath = $request->query->get('goto');
} elseif ($request->getSession()->has('_target_path')) {
$targetPath = $request->getSession()->get('_target_path');
} else {
$targetPath = $request->headers->get('Referer');
}
if ($targetPath == $this->generateUrl('login', [], UrlGeneratorInterface::ABSOLUTE_URL)) {
return $this->generateUrl('homepage');
}
$url = explode('?', $targetPath);
if ($url[0] == $this->generateUrl('partner_logout', [], UrlGeneratorInterface::ABSOLUTE_URL)) {
return $this->generateUrl('homepage');
}
if ($url[0] == $this->generateUrl('password_reset_update', [], UrlGeneratorInterface::ABSOLUTE_URL)) {
$targetPath = $this->generateUrl('homepage', [], UrlGeneratorInterface::ABSOLUTE_URL);
}
return $targetPath;
}
private function checkUserinfoFieldsFill($user)
{
$auth = $this->getSettingService()->get('auth');
$userProfile = $this->getUserService()->getUserProfile($user['id']);
$userProfile['email'] = strstr($user['email'], '@edusoho.net') ? '' : $user['email'];
$userProfile['mobile'] = empty($auth['mobileSmsValidate']) ? $userProfile['mobile'] : $user['verifiedMobile'];
$isFillUserInfo = true;
if ($auth['registerSort']) {
foreach ($auth['registerSort'] as $key => $val) {
if (!$userProfile[$val]) {
$isFillUserInfo = false;
}
}
}
return $isFillUserInfo;
}
protected function getServiceKernel()
{
return ServiceKernel::instance();
}
protected function getSettingService()
{
return ServiceKernel::instance()->createService('System:SettingService');
}
protected function getUserService()
{
return ServiceKernel::instance()->createService('User:UserService');
}
}