Skip to content

Commit

Permalink
メインサイトリダイレクトを実装
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuring committed Oct 25, 2023
1 parent 6dd51c1 commit 28bfebf
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 146 deletions.
118 changes: 0 additions & 118 deletions plugins/baser-core/src/Middleware/BcRedirectMainSiteFilter.php

This file was deleted.

80 changes: 80 additions & 0 deletions plugins/baser-core/src/Middleware/BcRedirectMainSiteMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
/**
* baserCMS : Based Website Development Project <https://basercms.net>
* Copyright (c) NPO baser foundation <https://baserfoundation.org/>
*
* @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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
use BaserCore\Annotation\UnitTest;

/**
* Class BcRedirectSubSiteFilter
* Class BcRedirectSubSiteMiddleware
*
* ユーザーエージェントにより、関連するサブサイトにリダイレクトを行う
*
*/
class BcRedirectSubSiteFilter implements MiddlewareInterface
class BcRedirectSubSiteMiddleware implements MiddlewareInterface
{

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions plugins/baser-core/src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
<?php
// TODO ucmitz : コード確認要
return;
/**
* baserCMS : Based Website Development Project <https://basercms.net>
* Copyright (c) baserCMS Users Community <https://basercms.net/community/>
* Copyright (c) NPO baser foundation <https://baserfoundation.org/>
*
* @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
{
Expand All @@ -26,7 +27,7 @@ class BcRedirectMainSiteFilterTest extends BcTestCase
*
* @return void
*/
public function setUp()
public function setUp(): void
{
parent::setUp();
}
Expand All @@ -36,7 +37,7 @@ public function setUp()
*
* @return void
*/
public function tearDown()
public function tearDown(): void
{
parent::tearDown();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -41,7 +41,7 @@ class BcRedirectSubSiteFilterTest extends BcTestCase
public function setUp(): void
{
parent::setUp();
$this->BcRedirectSubSiteFilter = new BcRedirectSubSiteFilter();
$this->BcRedirectSubSiteMiddleware = new BcRedirectSubSiteMiddleware();
}

/**
Expand All @@ -51,7 +51,7 @@ public function setUp(): void
*/
public function tearDown(): void
{
unset($this->BcRedirectSubSiteFilter);
unset($this->BcRedirectSubSiteMiddleware);
parent::tearDown();
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand Down

0 comments on commit 28bfebf

Please sign in to comment.