Skip to content

Commit

Permalink
Merge branch 'dev-5' into dev-5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuring committed Apr 23, 2024
2 parents 73395b3 + 79f7ad1 commit 944cb60
Show file tree
Hide file tree
Showing 20 changed files with 104 additions and 217 deletions.
4 changes: 1 addition & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,5 @@
"cakephp/plugin-installer": true,
"dealerdirect/phpcodesniffer-composer-installer": true
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
}
26 changes: 10 additions & 16 deletions plugins/baser-core/config/setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,16 @@
/**
* 現在のリクエストのホスト
*/
'host' => (isset($_SERVER['HTTP_HOST']))? $_SERVER['HTTP_HOST'] : null
'host' => (isset($_SERVER['HTTP_HOST']))? $_SERVER['HTTP_HOST'] : null,
/**
* インストール済かどうか
*
* BaserCorePlugin::bootstrap() で設定する
* bootstrap の方が呼び出し順が早いため、こちらで設定すると再初期化となってしまうため
* コメントアウトのままとする
* ここで別途判定を入れた場合ユニットテストがやりにくくなるのでそのままにしておく
*/
// 'isInstalled' => null,
],

/**
Expand Down Expand Up @@ -561,21 +570,6 @@
]
],

/**
* リクエスト情報
*/
'BcRequest' => [
// アセットファイルかどうか
'asset' => false,
// Router がロード済かどうか
// TODO 不要か確認
'routerLoaded' => false,
// アップデーターかどうか
'isUpdater' => false,
// メンテナンスかどうか
'isMaintenance' => false,
],

/**
* プレフィックス認証
*
Expand Down
18 changes: 15 additions & 3 deletions plugins/baser-core/src/BaserCorePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,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 @@ -91,8 +92,8 @@ public function bootstrap(PluginApplicationInterface $app): void
* インストールされてない場合のテストをできるようにするため、Configure の設定を優先する
*/
$hasInstall = file_exists(CONFIG . 'install.php');
if (is_null(Configure::read('BcRequest.isInstalled'))) {
Configure::write('BcRequest.isInstalled', $hasInstall);
if (is_null(Configure::read('BcEnv.isInstalled'))) {
Configure::write('BcEnv.isInstalled', $hasInstall);
}

/**
Expand Down Expand Up @@ -281,7 +282,7 @@ function loadPlugin(PluginApplicationInterface $application, $plugin, $priority)
public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
{
$middlewareQueue
->prepend(new BcRequestFilterMiddleware())
->insertBefore(RoutingMiddleware::class, new BcRequestFilterMiddleware())
->insertBefore(CsrfProtectionMiddleware::class, new AuthenticationMiddleware($this))
->add(new BcAdminMiddleware())
->add(new BcFrontMiddleware())
Expand Down Expand Up @@ -607,6 +608,17 @@ function(RouteBuilder $routes) {
parent::routes($routes);
}

/**
* 初期データ読み込み時の更新処理
* @param array $options
* @return void
*/
public function updateDefaultData($options = []) : void
{
// コンテンツの作成日を更新
$this->updateDateNow('BaserCore.Contents', ['created_date'], [], $options);
}

/**
* services
* @param ContainerInterface $container
Expand Down
6 changes: 5 additions & 1 deletion plugins/baser-core/src/BcPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,11 @@ public function updateDateNow(string $table, array $fields, array $conditions =
$options = array_merge([
'connection' => 'default'
], $options);
$table = TableRegistry::getTableLocator()->get($table, ['connectionName' => $options['connection']]);
$tableOptions = [];
if($options['connection'] && $options['connection'] !== 'default') {
$tableOptions = ['connectionName' => $options['connection']];
}
$table = TableRegistry::getTableLocator()->get($table, $tableOptions);
$beforeSaveEvents = BcUtil::offEvent($table->getEventManager(), 'Model.beforeSave');
$afterSaveEvents = BcUtil::offEvent($table->getEventManager(), 'Model.afterSave');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class PluginsController extends BcAdminAppController
/**
* Before Filter
* @param \Cake\Event\EventInterface $event An Event instance
* @return Response|void
* @checked
* @unitTest
* @noTodo
Expand All @@ -43,7 +44,6 @@ public function beforeFilter(EventInterface $event)
$response = parent::beforeFilter($event);
if($response) return $response;
$this->FormProtection->setConfig('unlockedActions', ['reset_db', 'update_sort', 'batch']);
if(Configure::read('BcRequest.isUpdater')) $this->Authentication->allowUnauthenticated(['update']);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,13 @@ class BcRedirectMainSiteMiddleware implements MiddlewareInterface
* @param RequestHandlerInterface $handler
* @return ResponseInterface
* @checked
* @noTodo
*/
public function process(
ServerRequestInterface $request,
RequestHandlerInterface $handler
): ResponseInterface
{
// TODO 対象サイトでの存在確認ができていない
// ルーティング後だと、ルーティングで失敗するので、ルーティング前に実行する必要があるが、
// ルーティング前だと対象サイトでの存在確認ができないため、現在は利用していない。
// ルーティングに組み込むことを検討する
if (Configure::read('BcRequest.isUpdater')) {
return $handler->handle($request);
}
if ($request->is('admin') || !BcUtil::isInstalled()) {
return $handler->handle($request);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ public function process(
RequestHandlerInterface $handler
): ResponseInterface
{
if (Configure::read('BcRequest.isUpdater')) {
return $handler->handle($request);
}
if ($request->is('admin') || !BcUtil::isInstalled()) {
return $handler->handle($request);
}
Expand Down
31 changes: 0 additions & 31 deletions plugins/baser-core/src/Middleware/BcRequestFilterMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,6 @@ public function process(
$response = $this->redirectIfIsDeviceFile($request);
if($response) return $response;
}

if ($this->isAsset($request)) {
Configure::write('BcRequest.asset', true);
return new Response();
}

$request = $this->addDetectors($request);

/**
Expand Down Expand Up @@ -122,7 +116,6 @@ public function getDetectorConfigs()
{
$configs = [];
$configs['admin'] = $this->isAdmin(...);
$configs['asset'] = $this->isAsset(...);
$configs['install'] = $this->isInstall(...);
$configs['maintenance'] = $this->isMaintenance(...);
$configs['page'] = $this->isPage(...);
Expand Down Expand Up @@ -167,30 +160,6 @@ public function isAdmin(ServerRequestInterface $request)
return (bool)preg_match($regex, $request->getPath());
}

/**
* アセットのURLかどうかを判定
*
* @param ServerRequestInterface $request リクエスト
* @return bool
* @checked
* @noTodo
* @unitTest
*/
public function isAsset(ServerRequestInterface $request)
{
$dirs = ['css', 'js', 'img'];
$exts = ['css', 'js', 'gif', 'jpg', 'jpeg', 'png', 'ico', 'svg', 'swf'];

$dirRegex = implode('|', $dirs);
$extRegex = implode('|', $exts);

$assetRegex = '/^\/(' . $dirRegex . ')\/.+\.(' . $extRegex . ')$/';
$themeAssetRegex = '/^\/theme\/[^\/]+?\/(' . $dirRegex . ')\/.+\.(' . $extRegex . ')$/';

$uri = $request->getPath();
return preg_match($assetRegex, $uri) || preg_match($themeAssetRegex, $uri);
}

/**
* インストール用のURLかどうかを判定
* [注]ルーターによるURLパース後のみ
Expand Down
8 changes: 6 additions & 2 deletions plugins/baser-core/src/Service/BcDatabaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,14 +411,18 @@ protected function _loadDefaultDataPattern($pattern, $theme, $plugin = 'BaserCor
if (!$this->loadCsv(['path' => $file, 'encoding' => 'auto', 'dbConfigKeyName' => $dbConfigKeyName])) {
$this->log(sprintf(__d('baser_core', '%s の読み込みに失敗。'), $file));
$result = false;
} else {
break;
}
} catch(\Throwable $e) {
throw $e;
}
}
}
try {
$pluginClass = Plugin::getCollection()->get($plugin);
if(method_exists($pluginClass, 'updateDefaultData')) {
$pluginClass->updateDefaultData();
}
} catch (\Throwable) {}
return $result;
}

Expand Down
14 changes: 7 additions & 7 deletions plugins/baser-core/src/Utility/BcUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,7 @@ public static function getContentsItem(): array
*/
public static function isInstalled()
{
return (bool)Configure::read('BcRequest.isInstalled');
return (bool)Configure::read('BcEnv.isInstalled');
}

/**
Expand Down Expand Up @@ -1463,6 +1463,8 @@ public static function isWindows()
*
* @param mixed $url
* @return mixed
* @checked
* @noTodo
*/
public static function addSessionId($url, $force = false)
{
Expand All @@ -1474,12 +1476,10 @@ public static function addSessionId($url, $force = false)
return $url;
}

$site = null;
if (!Configure::read('BcRequest.isUpdater')) {
$currentUrl = \Cake\Routing\Router::getRequest()->getPath();
$sites = \Cake\ORM\TableRegistry::getTableLocator()->get('BaserCore.Sites');
$site = $sites->findByUrl($currentUrl);
}
$currentUrl = \Cake\Routing\Router::getRequest()->getPath();
$sites = \Cake\ORM\TableRegistry::getTableLocator()->get('BaserCore.Sites');
$site = $sites->findByUrl($currentUrl);

// use_trans_sid が有効になっている場合、2重で付加されてしまう
if ($site && $site->device == 'mobile' && Configure::read('BcAgent.mobile.sessionId') && (!ini_get('session.use_trans_sid') || $force)) {
if (is_array($url)) {
Expand Down
10 changes: 6 additions & 4 deletions plugins/baser-core/src/View/Helper/BcToolbarHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ public function isAvailableLogin(): bool
if (BcUtil::loginUser()) return false;
if ($this->_View->getName() === 'Installations') return false;
if ($this->isLoginUrl()) return false;
if (Configure::read('BcRequest.isUpdater')) return false;
return true;
}

Expand Down Expand Up @@ -286,11 +285,14 @@ public function isLoginUrl(): bool
*/
public function getLogoType(): string
{
if ($this->_View->getName() === 'Installations') {
if ($this->getView()->getName() === 'Installations') {
return 'install';
} elseif (Configure::read('BcRequest.isUpdater')) {
} elseif ($this->getView()->getRequest()->getParam('controller') === 'Plugins'
&& $this->getView()->getRequest()->getParam('action') === 'update'
&& empty($this->getView()->getRequest()->getParam('pass'))
) {
return 'update';
} elseif ($this->_View->getRequest()->getParam('prefix') === "Admin" || $this->isLoginUrl()) {
} elseif ($this->getView()->getRequest()->getParam('prefix') === "Admin" || $this->isLoginUrl()) {
return 'normal';
} else {
if ($this->BcAuth->isCurrentUserAdminAvailable()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,9 @@ public function tearDown(): void
*/
public function testBeforeFilter()
{
Configure::write('BcRequest.isUpdater', true);
$event = new Event('Controller.beforeFilter', $this->PluginsController);
$this->PluginsController->beforeFilter($event);
$this->assertEquals($this->PluginsController->FormProtection->getConfig('unlockedActions'), ['reset_db', 'update_sort', 'batch']);
$this->assertEquals(['update'], $this->PluginsController->Authentication->getUnauthenticatedActions());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ public function testProcess(): void
$this->loadFixtureScenario(MultiSiteScenario::class);
$this->_response = $this->BcRequestFilterMiddleware->process($this->getRequest(), $this->Application);
$this->assertResponseOk();
$url = '/img/test.png';
$this->_response = $this->BcRequestFilterMiddleware->process($this->getRequest($url), $this->Application);
$this->assertTrue(Configure::read('BcRequest.asset'));
}

/**
Expand Down Expand Up @@ -156,44 +153,6 @@ public static function isAdminDataProvider()
];
}

/**
* アセットのURLかどうかを判定
*
* @param bool $expect 期待値
* @param string $url URL文字列
* @return void
* @dataProvider isAssetDataProvider
*/
public function testIsAsset($expect, $url)
{
$this->assertEquals($expect, $this->BcRequestFilterMiddleware->isAsset($this->getRequest($url)));
}

/**
* isAsset用データプロバイダ
*
* @return array
*/
public static function isAssetDataProvider()
{
return [
[false, '/'],
[false, '/about'],
[false, '/img/test.html'],
[false, '/js/test.php'],
[false, '/css/file.cgi'],
[true, '/img/image.png'],
[true, '/js/startup.js'],
[true, '/css/main.css'],
[false, '/theme/example_theme/img/test.html'],
[false, '/theme/example_theme/js/test.php'],
[false, '/theme/example_theme/css/file.cgi'],
[true, '/theme/example_theme/img/image.png'],
[true, '/theme/example_theme/js/startup.js'],
[true, '/theme/example_theme/css/main.css']
];
}

/**
* インストール用のURLかどうかを判定
*
Expand Down
7 changes: 5 additions & 2 deletions plugins/baser-core/tests/TestCase/PluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace BaserCore\Test\TestCase;

use App\Application;
use Authentication\Middleware\AuthenticationMiddleware;
use BaserCore\BaserCorePlugin;
use BaserCore\Service\SiteConfigsServiceInterface;
use BaserCore\Test\Scenario\ContentsScenario;
Expand All @@ -29,6 +30,7 @@
use Cake\Event\EventManager;
use Cake\Http\Middleware\CsrfProtectionMiddleware;
use Cake\Http\MiddlewareQueue;
use Cake\Routing\Middleware\RoutingMiddleware;
use Cake\Routing\Router;
use CakephpFixtureFactories\Scenario\ScenarioAwareTrait;

Expand Down Expand Up @@ -173,9 +175,10 @@ public function testMiddleware(): void
{
$middleware = new MiddlewareQueue();
$middleware->add(CsrfProtectionMiddleware::class);
$middleware->add(RoutingMiddleware::class);
$middlewareQueue = $this->Plugin->middleware($middleware);
$this->assertInstanceOf(BcRequestFilterMiddleware::class, $middlewareQueue->current());
$this->assertEquals(6, $middlewareQueue->count());
$this->assertInstanceOf(AuthenticationMiddleware::class, $middlewareQueue->current());
$this->assertEquals(7, $middlewareQueue->count());
}

/**
Expand Down
Loading

0 comments on commit 944cb60

Please sign in to comment.