Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
サブサイトのサブドメイン、別ドメインが動作しない問題を改善
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuring committed Apr 15, 2023
1 parent ee0e927 commit 5915116
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 53 deletions.
39 changes: 38 additions & 1 deletion plugins/baser-core/src/Controller/Admin/BcAdminAppController.php
Expand Up @@ -78,7 +78,10 @@ public function beforeFilter(EventInterface $event)
if (!$usersService->reload($this->request)) {
return $this->redirect($this->Authentication->logout());
}
return parent::beforeFilter($event);
$response = parent::beforeFilter($event);
if ($response) return $response;
$response = $this->redirectIfIsNotSameSite();
if ($response) return $response;
}

/**
Expand Down Expand Up @@ -177,4 +180,38 @@ protected function _checkReferer(): bool
return true;
}

/**
* siteUrlや、sslUrlと現在のURLが違う場合には、そちらのURLにリダイレクトを行う
* setting.php にて、cmsUrlとして、cmsUrlを定義した場合にはそちらを優先する
* @return Response|void|null
* @checked
* @noTodo
* @unitTest
*/
public function redirectIfIsNotSameSite()
{
if (Configure::read('BcEnv.cmsUrl')) {
$siteUrl = Configure::read('BcEnv.cmsUrl');
} elseif ($this->getRequest()->is('ssl')) {
$siteUrl = Configure::read('BcEnv.sslUrl');
} else {
$siteUrl = Configure::read('BcEnv.siteUrl');
}
if (!$siteUrl) {
return;
}
if (BcUtil::siteUrl() !== $siteUrl) {
$params = $this->getRequest()->getAttributes()['params'];
unset($params['Content']);
unset($params['Site']);
$url = Router::reverse($params, false);
$webroot = $this->request->getAttributes()['webroot'];
if($webroot) {
$webrootReg = '/^\/' . preg_quote($webroot, '/') . '/';
$url = preg_replace($webrootReg, '', $url);
}
return $this->redirect(preg_replace('/\/$/', '', $siteUrl) . $url);
}
}

}
35 changes: 0 additions & 35 deletions plugins/baser-core/src/Controller/BcFrontAppController.php
Expand Up @@ -67,8 +67,6 @@ public function beforeFilter(EventInterface $event)

$response = parent::beforeFilter($event);
if($response) return $response;
$response = $this->redirectIfIsNotSameSite();
if ($response) return $response;
}

/**
Expand Down Expand Up @@ -96,37 +94,4 @@ public function beforeRender(EventInterface $event): void
}
}

/**
* siteUrlや、sslUrlと現在のURLが違う場合には、そちらのURLにリダイレクトを行う
* setting.php にて、cmsUrlとして、cmsUrlを定義した場合にはそちらを優先する
* @return Response|void|null
* @checked
* @noTodo
* @unitTest
*/
public function redirectIfIsNotSameSite()
{
if (Configure::read('BcEnv.cmsUrl')) {
$siteUrl = Configure::read('BcEnv.cmsUrl');
} elseif ($this->getRequest()->is('ssl')) {
$siteUrl = Configure::read('BcEnv.sslUrl');
} else {
$siteUrl = Configure::read('BcEnv.siteUrl');
}
if (!$siteUrl) {
return;
}
if (BcUtil::siteUrl() !== $siteUrl) {
$params = $this->getRequest()->getAttributes()['params'];
unset($params['Content']);
unset($params['Site']);
$url = Router::reverse($params, false);
$webroot = $this->request->getAttributes()['webroot'];
if($webroot) {
$webrootReg = '/^\/' . preg_quote($webroot, '/') . '/';
$url = preg_replace($webrootReg, '', $url);
}
return $this->redirect(preg_replace('/\/$/', '', $siteUrl) . $url);
}
}
}
1 change: 0 additions & 1 deletion plugins/baser-core/src/Service/ContentsService.php
Expand Up @@ -11,7 +11,6 @@

namespace BaserCore\Service;

use BaserCore\Model\Entity\Site;
use Cake\Core\Plugin;
use Cake\Datasource\QueryInterface;
use Exception;
Expand Down
Expand Up @@ -215,4 +215,20 @@ public function checkRefererDataProvider()
// $this->assertEquals('test', $this->BcAdminApp->viewBuilder()->getTheme());
// }

/**
* test redirectIfIsNotSameSite
*/
public function testRedirectIfIsNotSameSite()
{
$this->BcAdminApp->setRequest($this->getRequest('https://localhost/index'));
$this->_response = $this->BcAdminApp->redirectIfIsNotSameSite();
$this->assertNull($this->_response);
$this->BcAdminApp->setRequest($this->getRequest('http://localhost/index'));
$this->_response = $this->BcAdminApp->redirectIfIsNotSameSite();
$this->assertRedirect('https://localhost/index');
$this->BcAdminApp->setRequest($this->getRequest('https://localhost/baser/admin'));
$this->_response = $this->BcAdminApp->redirectIfIsNotSameSite();
$this->assertNull($this->_response);
}

}
Expand Up @@ -81,20 +81,4 @@ public function testBeforeRender()
<<< */
}

/**
* test redirectIfIsNotSameSite
*/
public function testRedirectIfIsNotSameSite()
{
$this->BcFrontAppController->setRequest($this->getRequest('https://localhost/index'));
$this->_response = $this->BcFrontAppController->redirectIfIsNotSameSite();
$this->assertNull($this->_response);
$this->BcFrontAppController->setRequest($this->getRequest('http://localhost/index'));
$this->_response = $this->BcFrontAppController->redirectIfIsNotSameSite();
$this->assertRedirect('https://localhost/index');
$this->BcFrontAppController->setRequest($this->getRequest('https://localhost/baser/admin'));
$this->_response = $this->BcFrontAppController->redirectIfIsNotSameSite();
$this->assertNull($this->_response);
}

}

0 comments on commit 5915116

Please sign in to comment.