Skip to content

Commit

Permalink
fix #15 baserCMS5系→4系のパスワード引き継ぎができない問題を解決
Browse files Browse the repository at this point in the history
  • Loading branch information
kato committed Dec 5, 2024
1 parent 0c671f7 commit 7e65c8a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/Controller/Admin/MigratorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public function index()
$password = $this->{$this->migrator}->getNewPassword();
if ($password) {
$this->BcMessage->setInfo('残念ながらパスワードの移行はできません。すべてのユーザーのパスワードは、「' . $password . '」にセットされています。ログイン後のパスワードの変更をお願いします。');
} elseif (env('HASH_TYPE') === 'sha1') {
$this->BcMessage->setInfo('baserCMSの設定を引き継いでユーザーのパスワードは以前のものを保存しております。データ復旧後、シークレットウィンドウなどでログイン確認をお願いします。');
}
$this->redirect(['action' => 'index']);
} else {
Expand Down
18 changes: 17 additions & 1 deletion src/Controller/Component/BcDbMigrator5Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use CakephpFixtureFactories\Error\PersistenceException;
use Cake\Core\Configure;
use Psr\Log\LogLevel;
use ReflectionClass;

/**
* include files
Expand Down Expand Up @@ -626,12 +627,27 @@ protected function _updateUser()
foreach($records as $record) {
$record['status'] = true;
$record['user_groups']['_ids'] = [$record['user_group_id']];
$this->newPassword = $record['password'] = Security::randomString(10);
// HASH_TYPE が sha1 の場合はパスワードをそのまま移行する
if (env('HASH_TYPE') === 'sha1' && !empty($record['password'])) {
$this->newPassword = '';
} else {
$this->newPassword = $record['password'] = Security::randomString(10);
}
unset($record['user_group_id']);
try {
$entity = $table->newEmptyEntity();
$entity->setAccess('id', true);
$entity = $table->patchEntity($entity, $record, ['validate' => false]);
// HASH_TYPE が sha1 の場合はパスワードをそのまま移行する
if (env('HASH_TYPE') === 'sha1' && !empty($record['password'])) {
// Entityの構造を無視して値を書き換える
$reflection = new ReflectionClass($entity);
$property = $reflection->getProperty('_fields');
$property->setAccessible(true);
$fields = $property->getValue($entity);
$fields['password'] = $record['password'];
$property->setValue($entity, $fields);
}
$table->saveOrFail($entity);
} catch (PersistenceException $e) {
$this->log('users: ' . $e->getEntity()->getMessage(), LogLevel::ERROR, 'migrate_db');
Expand Down

0 comments on commit 7e65c8a

Please # to comment.