diff --git a/plugins/baser-core/src/Middleware/BcRedirectMainSiteFilter.php b/plugins/baser-core/src/Middleware/BcRedirectMainSiteFilter.php deleted file mode 100644 index d0d0c24bbb..0000000000 --- a/plugins/baser-core/src/Middleware/BcRedirectMainSiteFilter.php +++ /dev/null @@ -1,118 +0,0 @@ - - * Copyright (c) NPO baser foundation - * - * @copyright Copyright (c) NPO baser foundation - * @link https://basercms.net baserCMS Project - * @since 5.0.0 - * @license https://basercms.net/license/index.html MIT License - */ - -namespace BaserCore\Middleware; - -use Psr\Http\Message\ResponseInterface; -use Psr\Http\Message\ServerRequestInterface; -use Psr\Http\Server\MiddlewareInterface; -use Psr\Http\Server\RequestHandlerInterface; - -/** - * Class BcRedirectMainSiteFilter - * - * サブサイトにコンテンツが存在しない場合、同階層のメインサイトのコンテンツを確認し、 - * 存在していれば、メインサイトへリダイレクトをする。 - * - * (例) - * /s/service → /service - * - */ -class BcRedirectMainSiteFilter implements MiddlewareInterface -{ - - /** - * Trait - */ - use \BaserCore\Utility\BcContainerTrait; - - /** - * priority - * - * URLの存在確認が完了しているタイミングを前提としている為、 - * Dispacher::parseParams() より後に実行される必要がある - * - * @var int - */ - public $priority = 10; - - /** - * Process - * @param ServerRequestInterface $request - * @param RequestHandlerInterface $handler - * @return ResponseInterface - */ - public function process( - ServerRequestInterface $request, - RequestHandlerInterface $handler - ): ResponseInterface - { - $request = $event->getData('request'); - $response = $event->getData('response'); - if (!empty($request->getAttribute('currentContent'))) { - return $handler->handle($request); - } else { - if ($this->_existController($request)) { - return $handler->handle($request); - } - } - $sites = \Cake\ORM\TableRegistry::getTableLocator()->get('BaserCore.Sites'); - $site = $sites->findByUrl($request->getPath()); - if (!$site || !$site->enabled) { - return $handler->handle($request); - } - $mainSite = $site->getMain(); - if (!$mainSite) { - return $handler->handle($request); - } - $mainSiteUrl = '/' . preg_replace('/^' . $site->alias . '\//', '', $request->url); - if ($mainSite->alias) { - $mainSiteUrl = '/' . $mainSite->alias . $mainSiteUrl; - } - if ($mainSiteUrl) { - $request = new CakeRequest($mainSiteUrl); - $params = Router::parse($request->url); - $request->addParams($params); - if ($this->_existController($request)) { - $response->header('Location', $request->base . $mainSiteUrl); - $response->statusCode(302); - return $response; - } - } - return $handler->handle($request); - } - - /** - * コントローラーが存在するか確認 - * - * @param $request - * @return bool - */ - protected function _existController($request) - { - $pluginName = $pluginPath = $controller = null; - if (!empty($request->getParam('plugin'))) { - $pluginName = $controller = Inflector::camelize($request->getParam('plugin')); - $pluginPath = $pluginName . '.'; - } - if (!empty($request->getParam('controller'))) { - $controller = Inflector::camelize($request->getParam('controller')); - } - if ($pluginPath . $controller) { - $class = $controller . 'Controller'; - App::uses('AppController', 'Controller'); - App::uses($pluginName . 'AppController', $pluginPath . 'Controller'); - App::uses($class, $pluginPath . 'Controller'); - return class_exists($class); - } - return false; - } -} diff --git a/plugins/baser-core/src/Middleware/BcRedirectMainSiteMiddleware.php b/plugins/baser-core/src/Middleware/BcRedirectMainSiteMiddleware.php new file mode 100644 index 0000000000..57927661f1 --- /dev/null +++ b/plugins/baser-core/src/Middleware/BcRedirectMainSiteMiddleware.php @@ -0,0 +1,80 @@ + + * Copyright (c) NPO baser foundation + * + * @copyright Copyright (c) NPO baser foundation + * @link https://basercms.net baserCMS Project + * @since 5.0.0 + * @license https://basercms.net/license/index.html MIT License + */ + +namespace BaserCore\Middleware; + +use BaserCore\Utility\BcUtil; +use Cake\Core\Configure; +use Cake\Http\Response; +use Cake\ORM\TableRegistry; +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ServerRequestInterface; +use Psr\Http\Server\MiddlewareInterface; +use Psr\Http\Server\RequestHandlerInterface; +use BaserCore\Annotation\NoTodo; +use BaserCore\Annotation\Checked; +use BaserCore\Annotation\UnitTest; + +/** + * Class BcRedirectMainSiteMiddleware + * + * サブサイトにコンテンツが存在しない場合、同階層のメインサイトのコンテンツを確認し、 + * 存在していれば、メインサイトへリダイレクトをする。 + * + * (例) + * /s/service → /service + * + */ +class BcRedirectMainSiteMiddleware implements MiddlewareInterface +{ + + /** + * Process + * @param ServerRequestInterface $request + * @param RequestHandlerInterface $handler + * @return ResponseInterface + * @checked + * @noTodo + */ + public function process( + ServerRequestInterface $request, + RequestHandlerInterface $handler + ): ResponseInterface + { + if (Configure::read('BcRequest.isUpdater')) { + return $handler->handle($request); + } + if ($request->is('admin') || !BcUtil::isInstalled()) { + return $handler->handle($request); + } + $sites = TableRegistry::getTableLocator()->get('BaserCore.Sites'); + $site = $sites->findByUrl($request->getPath()); + if (!$site || !$site->status || $site->id === 1) { + return $handler->handle($request); + } + $mainSite = $site->getMain(); + if (!$mainSite) { + return $handler->handle($request); + } + $mainSiteUrl = '/' . preg_replace('/^\/' . $site->alias . '\//', '', $request->getPath()); + if ($mainSite->alias) { + $mainSiteUrl = '/' . $mainSite->alias . $mainSiteUrl; + } + if ($mainSiteUrl) { + $response = new Response([ + 'status' => 302 + ]); + return $response->withLocation($request->getAttribute('base') . $mainSiteUrl); + } + return $handler->handle($request); + } + +} diff --git a/plugins/baser-core/src/Middleware/BcRedirectSubSiteFilter.php b/plugins/baser-core/src/Middleware/BcRedirectSubSiteMiddleware.php similarity index 96% rename from plugins/baser-core/src/Middleware/BcRedirectSubSiteFilter.php rename to plugins/baser-core/src/Middleware/BcRedirectSubSiteMiddleware.php index 4165113646..5ed4c19a79 100644 --- a/plugins/baser-core/src/Middleware/BcRedirectSubSiteFilter.php +++ b/plugins/baser-core/src/Middleware/BcRedirectSubSiteMiddleware.php @@ -23,12 +23,12 @@ use BaserCore\Annotation\UnitTest; /** - * Class BcRedirectSubSiteFilter + * Class BcRedirectSubSiteMiddleware * * ユーザーエージェントにより、関連するサブサイトにリダイレクトを行う * */ -class BcRedirectSubSiteFilter implements MiddlewareInterface +class BcRedirectSubSiteMiddleware implements MiddlewareInterface { /** diff --git a/plugins/baser-core/src/Middleware/BcRequestFilterMiddleware.php b/plugins/baser-core/src/Middleware/BcRequestFilterMiddleware.php index c1c81b839b..331bfdfcd4 100644 --- a/plugins/baser-core/src/Middleware/BcRequestFilterMiddleware.php +++ b/plugins/baser-core/src/Middleware/BcRequestFilterMiddleware.php @@ -15,7 +15,6 @@ use BaserCore\Utility\BcUtil; use Cake\Core\Configure; use Cake\Http\Response; -use Cake\Http\ServerRequest; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; @@ -125,12 +124,12 @@ public function getDetectorConfigs() /** * リクエスト検出器を追加する * - * @param ServerRequest $request リクエスト + * @param ServerRequestInterface $request リクエスト * @checked * @noTodo * @unitTest */ - public function addDetectors(ServerRequest $request): ServerRequest + public function addDetectors(ServerRequestInterface $request): ServerRequestInterface { foreach($this->getDetectorConfigs() as $name => $callback) { $request->addDetector($name, $callback); diff --git a/plugins/baser-core/src/Plugin.php b/plugins/baser-core/src/Plugin.php index 53bd386361..f5f86acf96 100644 --- a/plugins/baser-core/src/Plugin.php +++ b/plugins/baser-core/src/Plugin.php @@ -27,7 +27,8 @@ use BaserCore\Event\BcViewEventDispatcher; use BaserCore\Middleware\BcAdminMiddleware; use BaserCore\Middleware\BcFrontMiddleware; -use BaserCore\Middleware\BcRedirectSubSiteFilter; +use BaserCore\Middleware\BcRedirectMainSiteMiddleware; +use BaserCore\Middleware\BcRedirectSubSiteMiddleware; use BaserCore\Middleware\BcRequestFilterMiddleware; use BaserCore\ServiceProvider\BcServiceProvider; use BaserCore\Utility\BcEvent; @@ -45,6 +46,7 @@ use Cake\I18n\I18n; use Cake\Log\Log; use Cake\ORM\TableRegistry; +use Cake\Routing\Middleware\RoutingMiddleware; use Cake\Routing\RouteBuilder; use Cake\Routing\Router; use Cake\Utility\Inflector; @@ -280,9 +282,10 @@ public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue $middlewareQueue ->prepend(new BcRequestFilterMiddleware()) ->insertBefore(CsrfProtectionMiddleware::class, new AuthenticationMiddleware($this)) + ->insertBefore(RoutingMiddleware::class, new BcRedirectMainSiteMiddleware()) ->add(new BcAdminMiddleware()) ->add(new BcFrontMiddleware()) - ->add(new BcRedirectSubSiteFilter()); + ->add(new BcRedirectSubSiteMiddleware()); // APIへのアクセスの場合、セッションによる認証以外は、CSRFを利用しない設定とする $ref = new ReflectionClass($middlewareQueue); diff --git a/plugins/baser-core/tests/TestCase/Middleware/BcRedirectMainSiteFilterTest.php b/plugins/baser-core/tests/TestCase/Middleware/BcRedirectMainSiteFilterTest.php index 279d5ac369..4f6f3a6668 100644 --- a/plugins/baser-core/tests/TestCase/Middleware/BcRedirectMainSiteFilterTest.php +++ b/plugins/baser-core/tests/TestCase/Middleware/BcRedirectMainSiteFilterTest.php @@ -1,22 +1,23 @@ - * Copyright (c) baserCMS Users Community + * Copyright (c) NPO baser foundation * - * @copyright Copyright (c) baserCMS Users Community - * @link https://basercms.net baserCMS Project - * @since baserCMS v 4.0.9 - * @license https://basercms.net/license/index.html + * @copyright Copyright (c) NPO baser foundation + * @link https://basercms.net baserCMS Project + * @since 5.0.0 + * @license https://basercms.net/license/index.html MIT License */ -App::uses('BcRedirectMainSiteFilter', 'Routing/Filter'); +namespace BaserCore\Test\TestCase\Middleware; + +use BaserCore\Middleware\BcRedirectMainSiteMiddleware; +use BaserCore\TestSuite\BcTestCase; /** * Class BcRedirectMainSiteFilterTest * - * @property BcRedirectMainSiteFilter $BcRedirectMainSiteFilter + * @property BcRedirectMainSiteMiddleware $BcRedirectMainSiteMiddleware */ class BcRedirectMainSiteFilterTest extends BcTestCase { @@ -26,7 +27,7 @@ class BcRedirectMainSiteFilterTest extends BcTestCase * * @return void */ - public function setUp() + public function setUp(): void { parent::setUp(); } @@ -36,7 +37,7 @@ public function setUp() * * @return void */ - public function tearDown() + public function tearDown(): void { parent::tearDown(); } diff --git a/plugins/baser-core/tests/TestCase/Middleware/BcRedirectSubSiteFilterTest.php b/plugins/baser-core/tests/TestCase/Middleware/BcRedirectSubSiteMiddlewareTest.php similarity index 86% rename from plugins/baser-core/tests/TestCase/Middleware/BcRedirectSubSiteFilterTest.php rename to plugins/baser-core/tests/TestCase/Middleware/BcRedirectSubSiteMiddlewareTest.php index 2965c6ee81..d7606d2ce1 100644 --- a/plugins/baser-core/tests/TestCase/Middleware/BcRedirectSubSiteFilterTest.php +++ b/plugins/baser-core/tests/TestCase/Middleware/BcRedirectSubSiteMiddlewareTest.php @@ -11,7 +11,7 @@ namespace BaserCore\Test\TestCase\Middleware; -use BaserCore\Middleware\BcRedirectSubSiteFilter; +use BaserCore\Middleware\BcRedirectSubSiteMiddleware; use BaserCore\Service\SitesServiceInterface; use BaserCore\Test\Factory\ContentFactory; use BaserCore\Test\Factory\PageFactory; @@ -23,10 +23,10 @@ use CakephpFixtureFactories\Scenario\ScenarioAwareTrait; /** - * Class BcRedirectSubSiteFilterTest - * @property BcRedirectSubSiteFilter $BcRedirectSubSiteFilter + * Class BcRedirectSubSiteMiddlewareTest + * @property BcRedirectSubSiteMiddleware $BcRedirectSubSiteMiddleware */ -class BcRedirectSubSiteFilterTest extends BcTestCase +class BcRedirectSubSiteMiddlewareTest extends BcTestCase { /** * Trait @@ -41,7 +41,7 @@ class BcRedirectSubSiteFilterTest extends BcTestCase public function setUp(): void { parent::setUp(); - $this->BcRedirectSubSiteFilter = new BcRedirectSubSiteFilter(); + $this->BcRedirectSubSiteMiddleware = new BcRedirectSubSiteMiddleware(); } /** @@ -51,7 +51,7 @@ public function setUp(): void */ public function tearDown(): void { - unset($this->BcRedirectSubSiteFilter); + unset($this->BcRedirectSubSiteMiddleware); parent::tearDown(); } @@ -98,7 +98,7 @@ public function test_process(): void $_SERVER['HTTP_USER_AGENT'] = 'iPhone'; $request = $this->getRequest('/about')->withParam('plugin', 'BaserCore')->withParam('controller', 'Pages')->withParam('action', 'view'); - $this->_response = $this->BcRedirectSubSiteFilter->process($request, $this->Application); + $this->_response = $this->BcRedirectSubSiteMiddleware->process($request, $this->Application); $this->assertResponseCode(302); } @@ -130,7 +130,7 @@ public function test_process_auto_redirect_off(): void ->withParam('plugin', 'BaserCore') ->withParam('controller', 'Pages') ->withParam('action', 'view'); - $this->_response = $this->BcRedirectSubSiteFilter->process($request, $this->Application); + $this->_response = $this->BcRedirectSubSiteMiddleware->process($request, $this->Application); //リダイレクトしない確認 $this->assertResponseSuccess(); } @@ -149,7 +149,7 @@ public function test_process_site_private(): void $SitesService->unpublish(1); $request = $this->loginAdmin($this->getRequest('/baser/admin/?site_id=1')); - $this->_response = $this->BcRedirectSubSiteFilter->process($request, $this->Application); + $this->_response = $this->BcRedirectSubSiteMiddleware->process($request, $this->Application); //リダイレクトしない確認 $this->assertResponseSuccess(); } @@ -165,7 +165,7 @@ public function test_process_admin(): void //管理画面へのアクセスを確認 $request = $this->loginAdmin($this->getRequest('/baser/admin')); - $this->_response = $this->BcRedirectSubSiteFilter->process($request, $this->Application); + $this->_response = $this->BcRedirectSubSiteMiddleware->process($request, $this->Application); //リダイレクトしない確認 $this->assertResponseSuccess(); }