.env Auto Install (#7239)

* .env Auto Install

* Autocommit PHP CS Fixer

* .env Auto Install

* .env Auto Install

* Autocommit PHP CS Fixer

* .env Auto Install

---------

Co-authored-by: gevorgmansuryan <gevorgmansuryan@users.noreply.github.com>
This commit is contained in:
Gevorg Mansuryan 2024-10-06 00:31:06 +04:00 committed by GitHub
parent 366823d3b1
commit 21c2b0973d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 62 additions and 17 deletions

View File

@ -41,6 +41,7 @@ HumHub Changelog
- Enh #7233: Mobile view: enlarge images in gallery preview of entries for the wall stream - Enh #7233: Mobile view: enlarge images in gallery preview of entries for the wall stream
- Enh #7237: Changed PoweredBy URL - Enh #7237: Changed PoweredBy URL
- Enh #7238: Fixed PHP warning related to `.env` - Enh #7238: Fixed PHP warning related to `.env`
- Enh #7239: Added option `\humhub\modules\installer\Module::$enableAutoSetup` to enable/disable auto setup of humhub installation
- Enh #7240: Fixed maximum file size setting in initialization - Enh #7240: Fixed maximum file size setting in initialization
1.16.3 (Unreleased) 1.16.3 (Unreleased)

View File

@ -204,4 +204,9 @@ class DynamicConfig extends BaseObject
{ {
return Yii::getAlias(Yii::$app->params['dynamicConfigFile']); return Yii::getAlias(Yii::$app->params['dynamicConfigFile']);
} }
public static function exist()
{
return file_exists(self::getConfigFilePath());
}
} }

View File

@ -9,7 +9,6 @@
namespace humhub\modules\installer; namespace humhub\modules\installer;
use Exception; use Exception;
use humhub\libs\DynamicConfig;
use Yii; use Yii;
use yii\console\Application; use yii\console\Application;
use yii\helpers\Url; use yii\helpers\Url;
@ -32,6 +31,11 @@ class Module extends \humhub\components\Module
*/ */
public $controllerNamespace = 'humhub\modules\installer\controllers'; public $controllerNamespace = 'humhub\modules\installer\controllers';
/**
* @var bool enable auto setup
*/
public bool $enableAutoSetup = false;
/** /**
* Array of config steps * Array of config steps
* *
@ -126,7 +130,6 @@ class Module extends \humhub\components\Module
protected function initConfigSteps() protected function initConfigSteps()
{ {
/** /**
* Step: Basic Configuration * Step: Basic Configuration
*/ */

View File

@ -553,7 +553,7 @@ class ConfigController extends Controller
Yii::$app->settings->set('timeZone', Yii::$app->timeZone); Yii::$app->settings->set('timeZone', Yii::$app->timeZone);
// Set to installed // Set to installed
$this->module->setInstalled(); Yii::$app->setInstalled();
try { try {
Yii::$app->user->logout(); Yii::$app->user->logout();

View File

@ -10,6 +10,7 @@ namespace humhub\modules\installer\controllers;
use humhub\components\access\ControllerAccess; use humhub\components\access\ControllerAccess;
use humhub\components\Controller; use humhub\components\Controller;
use humhub\libs\DynamicConfig;
/** /**
* Index Controller shows a simple welcome page. * Index Controller shows a simple welcome page.
@ -38,7 +39,7 @@ class IndexController extends Controller
*/ */
public function actionGo() public function actionGo()
{ {
if ($this->module->checkDBConnection()) { if (DynamicConfig::exist() && $this->module->checkDBConnection()) {
return $this->redirect(['setup/finalize']); return $this->redirect(['setup/finalize']);
} else { } else {
return $this->redirect(['setup/prerequisites']); return $this->redirect(['setup/prerequisites']);

View File

@ -48,6 +48,11 @@ class SetupController extends Controller
{ {
Yii::$app->cache->flush(); Yii::$app->cache->flush();
if ($this->module->enableAutoSetup) {
return $this->redirect(['database']);
}
return $this->render('prerequisites', ['hasError' => PrerequisitesList::hasError()]); return $this->render('prerequisites', ['hasError' => PrerequisitesList::hasError()]);
} }
@ -80,7 +85,8 @@ class SetupController extends Controller
$model->password = self::PASSWORD_PLACEHOLDER; $model->password = self::PASSWORD_PLACEHOLDER;
} }
if ($model->load(Yii::$app->request->post()) && $model->validate()) { if (($modelLoaded = $model->load(Yii::$app->request->post()) && $model->validate()) || $this->module->enableAutoSetup) {
if ($modelLoaded) {
$connectionString = 'mysql:host=' . $model->hostname; $connectionString = 'mysql:host=' . $model->hostname;
if ($model->port !== '') { if ($model->port !== '') {
$connectionString .= ';port=' . $model->port; $connectionString .= ';port=' . $model->port;
@ -88,23 +94,44 @@ class SetupController extends Controller
if (!$model->create) { if (!$model->create) {
$connectionString .= ';dbname=' . $model->database; $connectionString .= ';dbname=' . $model->database;
} }
$username = $model->username;
$password = $model->password; $password = $model->password;
if ($password == self::PASSWORD_PLACEHOLDER) { if ($password == self::PASSWORD_PLACEHOLDER) {
$password = $config['components']['db']['password']; $password = $config['components']['db']['password'];
} }
} elseif ($this->module->enableAutoSetup) {
$username = $model->username = Yii::$app->db->username;
$password = Yii::$app->db->password;
$connectionString = Yii::$app->db->dsn;
$model->create = 1;
if (preg_match('/host=([^;]+)/', $connectionString ?: '', $matches)) {
$model->hostname = $matches[1];
}
if (preg_match('/port=([^;]+)/', $connectionString ?: '', $matches)) {
$model->port = $matches[1];
}
if (preg_match('/dbname=([^;]+)/', $connectionString ?: '', $matches)) {
$model->database = $matches[1];
}
$connectionString = preg_replace('/;dbname=[^;]*/', '', $connectionString);
} else {
$username = '';
$password = '';
$connectionString = '';
}
// Create Test DB Connection // Create Test DB Connection
$dbConfig = [ $dbConfig = [
'class' => 'yii\db\Connection', 'class' => 'yii\db\Connection',
'dsn' => $connectionString, 'dsn' => $connectionString,
'username' => $model->username, 'username' => $username,
'password' => $password, 'password' => $password,
'charset' => 'utf8', 'charset' => 'utf8',
]; ];
try { try {
/** @var yii\db\Connection $temporaryConnection */ /** @var yii\db\Connection $temporaryConnection */
$temporaryConnection = Yii::createObject($dbConfig); $temporaryConnection = Yii::createObject($dbConfig);
@ -153,6 +180,10 @@ class SetupController extends Controller
*/ */
public function actionCron() public function actionCron()
{ {
if ($this->module->enableAutoSetup) {
return $this->redirect(['finalize']);
}
return $this->render('cron', []); return $this->render('cron', []);
} }
@ -161,6 +192,10 @@ class SetupController extends Controller
*/ */
public function actionPrettyUrls() public function actionPrettyUrls()
{ {
if ($this->module->enableAutoSetup) {
return $this->redirect(['finalize']);
}
return $this->render('pretty-urls'); return $this->render('pretty-urls');
} }