Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GMCプラグインとルーティングが競合する問題を修正 #5

Merged
merged 2 commits into from
Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 33 additions & 8 deletions Controller/Admin/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use Eccube\Controller\AbstractController;
use Eccube\Entity\Member;
use Eccube\Repository\BaseInfoRepository;
use Eccube\Service\SystemService;
use Eccube\Util\CacheUtil;
use Eccube\Util\StringUtil;
use Google_Service_SiteVerification;
use Google_Service_Webmasters;
Expand All @@ -24,14 +26,16 @@
use Plugin\SiteKit\Repository\IdTokenRepository;
use Plugin\SiteKit\Service\Google_Site_Kit_Client;
use Plugin\SiteKit\Service\Google_Site_Kit_Proxy_Client;
use Plugin\SiteKit\Service\SiteKitClientFactory;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Yaml\Yaml;

class ConfigController extends AbstractController
{
Expand Down Expand Up @@ -114,20 +118,22 @@ public function analyticsCallback()
{
}

/**
* @Route("/google{token}.html")
*/
public function siteVerification(string $token)
public function siteVerification()
{
$verificationToken = file_get_contents($this->eccubeConfig['plugin_data_realdir'].'/SiteKit/google-site-verification.txt');
return new Response($verificationToken);
$file = $this->eccubeConfig['plugin_data_realdir'].'/SiteKit/google-site-verification.txt';
if (file_exists($file)) {
$verificationToken = file_get_contents($file);
return new Response($verificationToken);
}

throw new NotFoundHttpException();
}

/**
* @Route("/%eccube_admin_route%/cube_kit/action_callback", name="site_kit_action_callback", methods={"GET"})
* @Route("/cube_kit/action_callback", methods={"GET"})
*/
public function actionCallback(Request $request)
public function actionCallback(Request $request, CacheUtil $cacheUtil, SystemService $systemService, RouterInterface $router)
{
$nonce = $this->session->get(self::SESSION_KEY_SITE_KIT_NONCE);
if ($nonce !== $request->get('nonce')) {
Expand All @@ -142,6 +148,25 @@ public function actionCallback(Request $request)
$this->eccubeConfig['plugin_data_realdir'].'/SiteKit/google-site-verification.txt',
'google-site-verification: '.$token
);

$systemService->switchMaintenance(true);

// ルーティング生成
$yaml = Yaml::dump([
'site_kit_google_site_verification' => [
'path' => '/google'.$token.'.html',
'controller' => 'Plugin\SiteKit\Controller\Admin\ConfigController::siteVerification',
]
]);
$filesystem->dumpFile(
$this->eccubeConfig['plugin_data_realdir'].'/SiteKit/routes.yaml',
$yaml);

$cacheUtil->clearCache();

// sitekit.withgoogle.comへリダイレクトするため、画面描画後のメンテナンス解除ができない。
// 従来のEventでのメンテナンス解除を行う
$systemService->disableMaintenance();
}

$params = http_build_query([
Expand Down
54 changes: 54 additions & 0 deletions PluginManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Plugin\SiteKit;


use Eccube\Plugin\AbstractPluginManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Yaml\Yaml;

class PluginManager extends AbstractPluginManager
{
public function install(array $meta, ContainerInterface $container)
{
$fs = new Filesystem();
$routeYaml = $container->getParameter('plugin_data_realdir').'/SiteKit/routes.yaml';
if (!$fs->exists($routeYaml)) {
$fs->dumpFile($routeYaml, '');
}
}

public function update(array $meta, ContainerInterface $container)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一度連携したあとはサイト認証は不要なのでファイルがあってもルーティングを追加する必要はないです。
再連携するときも新しい認証ファイルを生成するのでこの処理は不要です。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kiy0taka
ありがとうございます。修正しました。

{
$fs = new Filesystem();
$routeYaml = $container->getParameter('plugin_data_realdir').'/SiteKit/routes.yaml';

// site認証ファイルがある場合はルーティング生成
$verificationFile = $container->getParameter('plugin_data_realdir').'/SiteKit/google-site-verification.txt';
if ($fs->exists($verificationFile)) {
$token = file_get_contents($verificationFile);
$token = ltrim($token, 'google-site-verification: ');
$yaml = Yaml::dump([
'site_kit_google_site_verification' => [
'path' => '/google'.$token.'.html',
'controller' => 'Plugin\SiteKit\Controller\Admin\ConfigController::siteVerification',
]
]);
$fs->dumpFile($routeYaml, $yaml);
} else {
$fs->dumpFile($routeYaml, '');
}
}
}
3 changes: 3 additions & 0 deletions Resource/config/routes.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
site_kit_routes:
resource: '../../../../PluginData/SiteKit/routes.yaml'
prefix: /