Skip to content

Commit

Permalink
Merge pull request #197 from uzulla/issue154/redirect-to-installer
Browse files Browse the repository at this point in the history
未インストール時に`/admin/`にアクセスするとログイン画面にリダイレクトをインストーラへ修正
  • Loading branch information
fc2dev authored Jan 21, 2021
2 parents b5ea444 + c8f6cb4 commit 77e22d8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 23 deletions.
18 changes: 18 additions & 0 deletions app/src/Web/Controller/Admin/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ protected function beforeFilter(Request $request)
// 親のフィルター呼び出し
parent::beforeFilter($request);

// install.lockファイルがなければインストーラーへ
if (!$this->isInstalled() && (
$request->className !== CommonController::class ||
$request->methodName !== 'install'
)) {
$this->redirect($request, ['controller' => 'Common', 'action' => 'install']);
}

if (!$this->isLogin()) {
// 未ログイン時は新規登録とログイン以外させない
$allows = array(
Expand Down Expand Up @@ -78,6 +86,16 @@ protected function isLogin()
return !!Session::get('user_id');
}

protected function getInstalledLockFilePath():string
{
return Config::get('TEMP_DIR') . "installed.lock";
}

protected function isInstalled(): bool{
$installed_lock_file_path = $this->getInstalledLockFilePath();
return file_exists($installed_lock_file_path);
}

/**
* ログイン中のIDを取得する
*/
Expand Down
40 changes: 20 additions & 20 deletions app/src/Web/Controller/Admin/CommonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
use Exception;
use Fc2blog\App;
use Fc2blog\Config;
use Fc2blog\Model\BlogSettingsModel;
use Fc2blog\Model\BlogsModel;
use Fc2blog\Model\CommentsModel;
use Fc2blog\Model\Model;
use Fc2blog\Model\MSDB;
use Fc2blog\Model\PluginsModel;
use Fc2blog\Model\UsersModel;
Expand Down Expand Up @@ -64,33 +64,34 @@ public function device_change(Request $request)
}

/**
* 初期表示ページ(ブログの設定よりリダイレクト)
* /admin/ ブログの設定より初期表示ページを決定し、リダイレクト
* @param Request $request
*/
public function initial(Request $request)
public function index(Request $request)
{
$setting = Model::load('BlogSettings')->findByBlogId($this->getBlogId($request));
if (is_array($setting)) {
// 設定読み込みをしてリダイレクト
if(is_string($blog_id = $this->getBlogId($request))) {
$blog_settings = new BlogSettingsModel();
$setting = $blog_settings->findByBlogId($blog_id);
}else{
$setting = null;
}
if (is_array($setting) && isset($setting['start_page'])) { // 設定あり
switch ($setting['start_page']) {
default:
case Config::get('BLOG.START_PAGE.NOTICE'):
$this->redirect($request, array('controller' => 'Common', 'action' => 'notice'));
case Config::get('BLOG.START_PAGE.ENTRY'):
$this->redirect($request, ['controller' => 'Entries', 'action' => 'create']);
break;

case Config::get('BLOG.START_PAGE.ENTRY'):
$this->redirect($request, array('controller' => 'Entries', 'action' => 'create'));
case Config::get('BLOG.START_PAGE.NOTICE'):
default:
$this->redirect($request, ['controller' => 'Common', 'action' => 'notice']);
break;
}
} else {
$this->redirect($request, array('controller' => 'Common', 'action' => 'notice'));
} else { // 設定なし
$this->redirect($request, ['controller' => 'Common', 'action' => 'notice']);
}
}

public function index(Request $request)
{
return $this->initial($request);
}

/**
* お知らせ一覧画面
* @param Request $request
Expand Down Expand Up @@ -120,8 +121,7 @@ public function install(Request $request): string
$state = $request->get('state', 0);

// インストール済みロックファイルをチェックする。ロックファイルがあればインストール済みと判定し、完了画面へ
$installed_lock_file_path = Config::get('TEMP_DIR') . "installed.lock";
if (file_exists($installed_lock_file_path)) {
if ($this->isInstalled()) {
$state = 3;
}

Expand Down Expand Up @@ -308,7 +308,7 @@ public function install(Request $request): string
// 完了画面

// 完了画面表示と同時に、インストール済みロックファイルの生成
file_put_contents($installed_lock_file_path, "This is installed check lockfile.\nThe blog already installed. if you want re-enable installer, please delete this file.");
file_put_contents($this->getInstalledLockFilePath(), "This is installed check lockfile.\nThe blog already installed. if you want re-enable installer, please delete this file.");

return 'admin/common/installed.twig';
}
Expand Down
11 changes: 8 additions & 3 deletions e2e_test/serial_execute_tests/installer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ describe("crawl admin pages", () => {
await c.init();
});

const start_url = "http://localhost:8080/admin/common/install";
const start_url = "http://localhost:8080/admin";
const installer_url = "http://localhost:8080/admin/common/install";

it("open installer page", async () => {
it("redirect to installer page", async () => {
const [response] = await Promise.all([
c.waitLoad(),
c.page.goto(start_url),
Expand All @@ -35,7 +36,11 @@ describe("crawl admin pages", () => {

expect(response.status()).toEqual(200);
expect(await c.isNotAnyNoticeOrWarningsFinishWithEndHtmlTag()).toBeTruthy();
expect(response.url()).toEqual("http://localhost:8080/admin/common/install");
});


it("open installer page", async () => {
const title_text = await c.page.$eval("h2", elm=>elm.textContent);
expect(//.exec(title_text)).toBeTruthy();
});
Expand Down Expand Up @@ -99,7 +104,7 @@ describe("crawl admin pages", () => {
it("re-open install page. show installed", async () => {
const [response] = await Promise.all([
c.waitLoad(),
c.page.goto(start_url),
c.page.goto(installer_url),
]);

await c.getSS("installer_installed");
Expand Down

0 comments on commit 77e22d8

Please sign in to comment.