Enh: Consolidate (is|set)(Database)Installed() to ApplicationTrait (#6720)

* Move `(is|set)(Database)?Installed()` to `Application`

* fixup! Move `(is|set)(Database)?Installed()` to `Application`
This commit is contained in:
Martin Rüegg 2023-12-11 11:45:52 +01:00 committed by GitHub
parent c6c00af40a
commit 24d948a12b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
29 changed files with 257 additions and 169 deletions

View File

@ -3,6 +3,7 @@ HumHub Changelog
1.16.0 (Unreleased)
-------------------
- Enh #6720: Consolidate `isInstalled()`, `setInstalled()`, and `setDatabaseInstalled`
- Fix #6693: `MigrateController::$migrationPathMap` stored rolling sum of migrations
- Enh #6697: Make state badge customizable
- Fix #6636: Module Manager test

View File

@ -11,6 +11,8 @@ Version 1.16 (Unreleased)
- `\humhub\modules\content\components\ContentAddonActiveRecord::canWrite()`
- `\humhub\modules\file\models\File::canRead()` use `canView()` instead
- `\humhub\modules\content\components\ContentAddonActiveRecord::canRead()` use `canView()` instead
- `\humhub\models\Setting::isInstalled()` use `Yii::$app->isInstalled()` instead
- `\humhub\libs\BaseSettingsManager::isDatabaseInstalled()` use `Yii::$app->isDatabaseInstalled()` instead
### Type restrictions
- `\humhub\modules\comment\models\Comment` on `canDelete()`

View File

@ -1,4 +1,5 @@
<?php
/*
* @link https://www.humhub.org/
* @copyright Copyright (c) 2023 HumHub GmbH & Co. KG
@ -7,7 +8,10 @@
namespace humhub\components;
use humhub\helpers\DatabaseHelper;
use humhub\interfaces\MailerInterface;
use humhub\libs\DynamicConfig;
use Yii;
use yii\helpers\Url;
trait ApplicationTrait
@ -66,4 +70,66 @@ trait ApplicationTrait
{
return parent::getMailer();
}
/**
* Checks if Humhub is installed
*
* @return boolean
* @since 1.16
*/
public function isInstalled(): bool
{
return isset(Yii::$app->params['installed']) && Yii::$app->params['installed'];
}
/**
* Sets application in installed state (disables installer)
*
* @since 1.16
*/
public function setInstalled()
{
$config = DynamicConfig::load();
$config['params']['installed'] = true;
DynamicConfig::save($config);
}
/**
* Checks if settings table exists or application is not installed yet
*
* @since 1.16
*/
public function isDatabaseInstalled(bool $checkConnection = false): bool
{
$dieOnError = isset(Yii::$app->params['databaseInstalled']) && $this->params['databaseInstalled'];
if (!$checkConnection) {
return $dieOnError;
}
try {
$db = Yii::$app->db;
$db->open();
} catch (\Exception $ex) {
if ($dieOnError) {
DatabaseHelper::handleConnectionErrors($ex);
}
return false;
}
return Yii::$app->params['databaseInstalled'] = in_array('setting', $db->schema->getTableNames());
}
/**
* Sets the application database in installed state
*
* @since 1.16
*/
public function setDatabaseInstalled()
{
$config = DynamicConfig::load();
$config['params']['databaseInstalled'] = true;
DynamicConfig::save($config);
}
}

View File

@ -11,7 +11,6 @@ namespace humhub\components;
use humhub\components\access\ControllerAccess;
use humhub\components\access\StrictAccess;
use humhub\components\behaviors\AccessControl;
use humhub\models\Setting;
use humhub\modules\user\services\IsOnlineService;
use Yii;
use yii\helpers\Html;
@ -28,7 +27,6 @@ use yii\web\ForbiddenHttpException;
*/
class Controller extends \yii\web\Controller
{
/**
* @event \yii\base\Event an event raised on init a controller.
*/
@ -121,6 +119,7 @@ class Controller extends \yii\web\Controller
* Renders a string as Ajax including assets without end page so it can be called several times.
*
* @param string $content
*
* @return string Rendered content
*/
public function renderAjaxPartial(string $content): string
@ -132,6 +131,7 @@ class Controller extends \yii\web\Controller
* Renders a static string by applying the layouts (sublayout + layout.
*
* @param string $content the static string being rendered
*
* @return string the rendering result of the layout with the given static string as the `$content` variable.
* If the layout is disabled, the string will be returned back.
*
@ -223,7 +223,7 @@ class Controller extends \yii\web\Controller
if (!Yii::$app->request->isAjax || Yii::$app->request->isPjax) {
$this->setJsViewStatus();
if (Setting::isInstalled()) {
if (Yii::$app->isInstalled()) {
// Update "is online" status ony on full page loads
(new IsOnlineService(Yii::$app->user->identity))->updateStatus();
}
@ -313,6 +313,7 @@ class Controller extends \yii\web\Controller
* Check if action cannot be intercepted
*
* @param string|null $actionId , NULL - to use current action
*
* @return bool
* @since 1.9
*/

View File

@ -8,7 +8,6 @@
namespace humhub\components;
use humhub\models\Setting;
use humhub\modules\like\activities\Liked;
use humhub\modules\like\models\Like;
use Throwable;
@ -76,6 +75,7 @@ class Migration extends \yii\db\Migration
* Helper function for self::up() and self::down()
*
* @param array $action
*
* @return bool|null
* @since 1.15.0
*/
@ -127,6 +127,7 @@ class Migration extends \yii\db\Migration
* @param string $table
* @param $columns
* @param string|null $options
*
* @return bool indicates if the table has been created
* @see static::createTable()
* @noinspection PhpMissingReturnTypeInspection
@ -147,6 +148,7 @@ class Migration extends \yii\db\Migration
/**
* @param string $table
*
* @return bool indicates if the table has been dropped
* @see static::dropTable()
* @noinspection PhpUnused
@ -171,6 +173,7 @@ class Migration extends \yii\db\Migration
*
* @param string $column
* @param string $table
*
* @return bool
* @since 1.9.1
*/
@ -226,6 +229,7 @@ class Migration extends \yii\db\Migration
*
* @param string $index
* @param string $table
*
* @return bool
* @throws Exception
* @since 1.9.1
@ -242,6 +246,7 @@ class Migration extends \yii\db\Migration
*
* @param string $index
* @param string $table
*
* @return bool
* @throws Exception
* @since 1.9.1
@ -263,6 +268,7 @@ class Migration extends \yii\db\Migration
* @param string $table
* @param string|array $columns
* @param bool $unique
*
* @return bool indicates if the index has been created
* @throws Exception
* @since 1.9.1
@ -288,6 +294,7 @@ class Migration extends \yii\db\Migration
*
* @param string $index
* @param string $table
*
* @return bool indicates if the index has been dropped
* @throws Exception
* @since 1.9.1
@ -315,6 +322,7 @@ class Migration extends \yii\db\Migration
* @param string $index
* @param string $table
* @param string|array $columns
*
* @return bool indicates if key has been added
* @throws Exception
* @since 1.9.1
@ -372,6 +380,7 @@ class Migration extends \yii\db\Migration
* @param string|array $refColumns
* @param string|null $delete
* @param string|null $update
*
* @return bool indicates if key has been added
* @throws Exception
* @since 1.9.1
@ -397,6 +406,7 @@ class Migration extends \yii\db\Migration
*
* @param string $index
* @param string $table
*
* @return bool indicates if key has been dropped
* @throws Exception
* @since 1.9.1
@ -483,8 +493,7 @@ class Migration extends \yii\db\Migration
public function integerReferenceKey(): ColumnSchemaBuilder
{
return $this->integer(11)
->notNull()
;
->notNull();
}
/**
@ -492,6 +501,7 @@ class Migration extends \yii\db\Migration
* being the first timestamp column in the table.
*
* @param $precision
*
* @return ColumnSchemaBuilder
* @since 1.15
* @see https://dev.mysql.com/doc/refman/8.0/en/timestamp-initialization.html
@ -516,6 +526,7 @@ class Migration extends \yii\db\Migration
*
* @param string $oldClass
* @param string $newClass
*
* @throws Exception
*/
protected function renameClass(string $oldClass, string $newClass): void
@ -561,6 +572,7 @@ class Migration extends \yii\db\Migration
* @param array|string $condition the conditions that will be put in the WHERE part. Please
* refer to [[Query::where()]] on how to specify conditions.
* @param array|Traversable $params the parameters to be bound to the query.
*
* @throws Exception
*/
public function updateSilent(string $table, $columns, $condition = '', $params = []): void
@ -571,8 +583,10 @@ class Migration extends \yii\db\Migration
/**
* Creates and executes an INSERT SQL statement without any output
* The method will properly escape the column names, and bind the values to be inserted.
*
* @param string $table the table that new rows will be inserted into.
* @param array|Traversable $columns the column data (name => value) to be inserted into the table.
*
* @throws Exception
*/
public function insertSilent(string $table, $columns): void
@ -588,14 +602,16 @@ class Migration extends \yii\db\Migration
*/
protected function isInitialInstallation(): bool
{
return (!Setting::isInstalled());
return (!Yii::$app->isInstalled());
}
/**
* Get data from database dsn config
*
* @since 1.9.3
*
* @param string $name 'host', 'port', 'dbname'
*
* @return string|null
*/
private function getDsnAttribute(string $name): ?string
@ -608,6 +624,7 @@ class Migration extends \yii\db\Migration
/**
* @param string $message Message to be logged
* @param array $params Parameters to translate in $message
*
* @return void
* @since 1.15.0
*/
@ -619,6 +636,7 @@ class Migration extends \yii\db\Migration
/**
* @param string $message Message to be logged
* @param array $params Parameters to translate in $message
*
* @return void
* @since 1.15.0
*/
@ -630,6 +648,7 @@ class Migration extends \yii\db\Migration
/**
* @param string $message Message to be logged
* @param array $params Parameters to translate in $message
*
* @return void
* @since 1.15.0
* @noinspection PhpUnused
@ -642,6 +661,7 @@ class Migration extends \yii\db\Migration
/**
* @param string $message Message to be logged
* @param array $params Parameters to translate in $message
*
* @return void
* @since 1.15.0
*/
@ -656,6 +676,7 @@ class Migration extends \yii\db\Migration
*
* @param string $message Message to be logged
* @param array $params Parameters to translate in $message
*
* @return void
* @since 1.15.0
*/
@ -680,6 +701,7 @@ class Migration extends \yii\db\Migration
*
* @param Throwable $e The Throwable to be logged
* @param string $method The Method that was running
*
* @since 1.15.0
*/
protected function logException(Throwable $e, string $method): void

View File

@ -14,7 +14,6 @@ use ArrayAccess;
use humhub\components\bootstrap\ModuleAutoLoader;
use humhub\components\console\Application as ConsoleApplication;
use humhub\exceptions\InvalidArgumentTypeException;
use humhub\libs\BaseSettingsManager;
use humhub\models\ModuleEnabled;
use humhub\modules\admin\events\ModulesEvent;
use humhub\modules\marketplace\Module as ModuleMarketplace;
@ -115,11 +114,11 @@ class ModuleManager extends Component
parent::init();
// Either database installed and not in installed state
if (!Yii::$app->params['databaseInstalled'] && !Yii::$app->params['installed']) {
if (!Yii::$app->isInstalled() && !Yii::$app->isDatabaseInstalled()) {
return;
}
if (!BaseSettingsManager::isDatabaseInstalled()) {
if (!Yii::$app->isDatabaseInstalled()) {
$this->enabledModules = [];
} else {
$this->enabledModules = ModuleEnabled::getEnabledIds();
@ -129,7 +128,9 @@ class ModuleManager extends Component
/**
* Registers a module to the manager
* This is usually done by config.php in modules root folder.
*
* @param array $configs
*
* @throws InvalidConfigException
* @see \humhub\components\bootstrap\ModuleAutoLoader::bootstrap
*
@ -146,6 +147,7 @@ class ModuleManager extends Component
*
* @param string $basePath the modules base path
* @param array $config the module configuration (config.php)
*
* @throws InvalidConfigException
*/
public function register($basePath, $config = null)
@ -179,7 +181,7 @@ class ModuleManager extends Component
}
}
if (!Yii::$app->params['installed'] && $isInstallerModule) {
if (!Yii::$app->isInstalled() && $isInstallerModule) {
$this->enabledModules[] = $config['id'];
}
@ -393,6 +395,7 @@ class ModuleManager extends Component
*
* @param Module[]|null $modules list of modules, defaulting to installed non-core modules
* @param null|string $keyword
*
* @return Module[]
*/
public function filterModulesByKeyword(?array $modules, $keyword = null): array
@ -442,6 +445,7 @@ class ModuleManager extends Component
* Returns all enabled modules and supportes further options as [[getModules()]].
*
* @param array $options
*
* @return array
* @throws Exception
* @since 1.3.10
@ -456,6 +460,7 @@ class ModuleManager extends Component
* Checks if a moduleId exists, regardless it's activated or not
*
* @param string $id
*
* @return boolean
*/
public function hasModule($id)
@ -484,6 +489,7 @@ class ModuleManager extends Component
*
* @param string $id Module Id
* @param bool $throwOnMissingModule true - to throw exception, false - to return null
*
* @return Module|object|null
* @throws Exception
* @throws InvalidConfigException
@ -560,6 +566,7 @@ class ModuleManager extends Component
*
* @param string $moduleId
* @param bool $disableBeforeRemove
*
* @throws Exception
* @throws \yii\base\ErrorException
*/
@ -603,6 +610,7 @@ class ModuleManager extends Component
* Enables a module
*
* @param Module $module
*
* @throws InvalidConfigException
* @since 1.1
*/
@ -634,6 +642,7 @@ class ModuleManager extends Component
* Disables a module
*
* @param Module $module
*
* @throws \Throwable
* @throws \yii\db\StaleObjectException
* @since 1.1
@ -658,6 +667,7 @@ class ModuleManager extends Component
/**
* @param array $modules
*
* @throws Exception
*/
public function disableModules($modules = [])

View File

@ -8,7 +8,6 @@
namespace humhub\components;
use humhub\models\Setting;
use Yii;
/**
@ -19,25 +18,26 @@ use Yii;
*/
class Request extends \yii\web\Request
{
/**
* Http header name for view context information
*
* @see \humhub\modules\ui\view\components\View::$viewContext
*/
public const HEADER_VIEW_CONTEXT = 'HUMHUB-VIEW-CONTEXT';
/**
* Whenever a secure connection is detected, force it.
*
* @var bool
* @since 1.13
*/
public $autoEnsureSecureConnection = true;
/**
* Http header name for view context information
* @see \humhub\modules\ui\view\components\View::$viewContext
*/
const HEADER_VIEW_CONTEXT = 'HUMHUB-VIEW-CONTEXT';
/**
* @inheritdoc
*/
public function init()
{
if (Setting::isInstalled()) {
if (Yii::$app->isInstalled()) {
$secret = Yii::$app->settings->get('secret');
if ($secret != "") {
$this->cookieValidationKey = $secret;
@ -48,7 +48,13 @@ class Request extends \yii\web\Request
$this->cookieValidationKey = 'installer';
}
if (defined('YII_ENV_TEST') && YII_ENV_TEST && $_SERVER['SCRIPT_FILENAME'] === 'index-test.php' && in_array($_SERVER['SCRIPT_NAME'], ['/sw.js', '/offline.pwa.html', '/manifest.json'], true)) {
if (
defined('YII_ENV_TEST') && YII_ENV_TEST && $_SERVER['SCRIPT_FILENAME'] === 'index-test.php' && in_array(
$_SERVER['SCRIPT_NAME'],
['/sw.js', '/offline.pwa.html', '/manifest.json'],
true
)
) {
$this->setScriptUrl('/index.php');
}
}

View File

@ -94,7 +94,6 @@ use yii\web\HttpException;
*/
class AccessControl extends ActionFilter
{
/**
* Rules for access to controller
*
@ -120,6 +119,7 @@ class AccessControl extends ActionFilter
/**
* Only allow logged in users access to this controller
*
* @deprecated since 1.2.2 use ['loggedInOnly'] rule instead
*/
public $loggedInOnly = false;
@ -135,9 +135,11 @@ class AccessControl extends ActionFilter
public function beforeAction($action)
{
// Bypass when not installed for installer
if (empty(Yii::$app->params['installed']) &&
Yii::$app->controller->module != null &&
Yii::$app->controller->module->id == 'installer') {
if (
!Yii::$app->isInstalled()
&& ($module = Yii::$app->controller->module)
&& $module->id === 'installer'
) {
return true;
}
@ -145,12 +147,14 @@ class AccessControl extends ActionFilter
$this->controllerAccess = $this->getControllerAccess($this->rules);
if (!$this->controllerAccess->run()) {
if (isset($this->controllerAccess->codeCallback) &&
method_exists($this, $this->controllerAccess->codeCallback)) {
if (
isset($this->controllerAccess->codeCallback)
&& method_exists($this, $this->controllerAccess->codeCallback)
) {
// Call a specific function for current action filter,
// may be used to filter a logged in user for some restriction e.g. "must change password"
call_user_func([$this, $this->controllerAccess->codeCallback]);
} else if ($this->controllerAccess->code == 401) {
} elseif ($this->controllerAccess->code == 401) {
$this->loginRequired();
} else {
$this->forbidden();
@ -226,6 +230,7 @@ class AccessControl extends ActionFilter
/**
* Force user to redirect to change password
*
* @since 1.8
*/
protected function forceChangePassword()
@ -237,6 +242,7 @@ class AccessControl extends ActionFilter
/**
* Log out all non admin users when maintenance mode is active
*
* @since 1.8
*/
protected function checkMaintenanceMode()

View File

@ -10,7 +10,6 @@ namespace humhub\components\console;
use humhub\components\ApplicationTrait;
use humhub\interfaces\ApplicationInterface;
use humhub\libs\BaseSettingsManager;
use Yii;
use yii\console\Exception;
@ -41,8 +40,8 @@ class Application extends \yii\console\Application implements ApplicationInterfa
));
}
if (BaseSettingsManager::isDatabaseInstalled(Yii::$app->params['databaseInstalled'] ?? false)) {
$baseUrl = Yii::$app->settings->get('baseUrl');
if ($this->isDatabaseInstalled(true)) {
$baseUrl = $this->settings->get('baseUrl');
if (!empty($baseUrl)) {
if (Yii::getAlias('@web', false) === false) {
Yii::setAlias('@web', $baseUrl);
@ -62,6 +61,7 @@ class Application extends \yii\console\Application implements ApplicationInterfa
/**
* Returns the configuration of the built-in commands.
*
* @return array the configuration of the built-in commands.
*/
public function coreCommands()

View File

@ -1,4 +1,5 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) HumHub GmbH & Co. KG
@ -7,7 +8,6 @@
namespace humhub\components\console;
use humhub\libs\BaseSettingsManager;
use Yii;
/**
@ -36,7 +36,7 @@ class UrlManager extends \humhub\components\UrlManager
private function getConfiguredBaseUrl()
{
if (BaseSettingsManager::isDatabaseInstalled()) {
if (Yii::$app->isDatabaseInstalled()) {
$baseUrl = Yii::$app->settings->get('baseUrl');
if (!empty($baseUrl)) {
return $baseUrl;

View File

@ -37,7 +37,7 @@ class Formatter extends \yii\i18n\Formatter
{
parent::init();
if (Yii::$app->params['installed'] && Yii::$app->getModule('admin') !== null && !empty(Yii::$app->getModule('admin')->settings->get('defaultDateInputFormat'))) {
if (Yii::$app->isInstalled() && Yii::$app->getModule('admin') !== null && !empty(Yii::$app->getModule('admin')->settings->get('defaultDateInputFormat'))) {
$this->dateInputFormat = Yii::$app->getModule('admin')->settings->get('defaultDateInputFormat');
}
}

View File

@ -50,7 +50,7 @@ class I18N extends BaseI18N
*/
public function autosetLocale()
{
if (!Yii::$app->params['installed'] || Yii::$app->user->isGuest) {
if (!Yii::$app->isInstalled() || Yii::$app->user->isGuest) {
$this->setGuestLocale();
} else {
$this->setUserLocale(Yii::$app->user->getIdentity());

View File

@ -10,7 +10,6 @@ namespace humhub\libs;
use humhub\components\SettingActiveRecord;
use humhub\exceptions\InvalidArgumentTypeException;
use humhub\helpers\DatabaseHelper;
use Stringable;
use Yii;
use yii\base\Component;
@ -57,7 +56,7 @@ abstract class BaseSettingsManager extends Component
throw new InvalidConfigException('Module id not set!', 2);
}
if (static::isDatabaseInstalled()) {
if (Yii::$app->isDatabaseInstalled()) {
$this->loadValues();
}
@ -81,7 +80,7 @@ abstract class BaseSettingsManager extends Component
}
if ($value === null) {
$this->delete($name);
$this->delete($name);
return;
}
@ -303,19 +302,10 @@ abstract class BaseSettingsManager extends Component
* Checks if settings table exists or application is not installed yet
*
* @since 1.3
* @deprecated since 1.16
*/
public static function isDatabaseInstalled(bool $dieOnError = false): bool
public static function isDatabaseInstalled(): bool
{
try {
$db = Yii::$app->db;
$db->open();
} catch (\Exception $ex) {
if ($dieOnError) {
DatabaseHelper::handleConnectionErrors($ex);
}
return false;
}
return in_array('setting', $db->schema->getTableNames());
return Yii::$app->isDatabaseInstalled(true);
}
}

View File

@ -8,12 +8,9 @@
namespace humhub\libs;
use humhub\models\Setting;
use humhub\modules\admin\libs\HumHubAPI;
use humhub\modules\ldap\helpers\LdapHelper;
use humhub\modules\marketplace\Module;
use humhub\modules\ui\icon\widgets\Icon;
use humhub\widgets\Label;
use Yii;
/**
@ -25,7 +22,6 @@ use Yii;
*/
class SelfTest
{
/**
* Get Results of the Application SelfTest.
*
@ -435,7 +431,7 @@ class SelfTest
];
}
if (Setting::isInstalled()) {
if (Yii::$app->isInstalled()) {
$title = Yii::t('AdminModule.information', 'Settings') . ' - ' . Yii::t('AdminModule.information', 'Pretty URLs');
if (Yii::$app->urlManager->enablePrettyUrl) {
$checks[] = [
@ -591,6 +587,7 @@ class SelfTest
* - hint
*
* @param array Results initialized before
*
* @return array
*/
public static function getDatabaseResults($checks = [])
@ -790,6 +787,7 @@ class SelfTest
* - hint
*
* @param array Results initialized before
*
* @return array
*/
public static function getMarketplaceResults($checks = []): array

View File

@ -1,7 +1,6 @@
<?php
use humhub\components\Migration;
use humhub\models\Setting;
use humhub\modules\user\models\Group;
/**
@ -14,7 +13,11 @@ class m201228_064513_default_group extends Migration
*/
public function safeUp()
{
$this->safeAddColumn('group', 'is_default_group', $this->boolean()->notNull()->defaultValue(0)->after('is_admin_group'));
$this->safeAddColumn(
'group',
'is_default_group',
$this->boolean()->notNull()->defaultValue(0)->after('is_admin_group')
);
$defaultUserGroupId = Yii::$app->getModule('user')->settings->get('auth.defaultUserGroup');
@ -24,11 +27,13 @@ class m201228_064513_default_group extends Migration
}
// Try to create "Default Group" only for upgrade case because on new installation the group "Users" is used as default group:
if (Setting::isInstalled()) {
if (Yii::$app->isInstalled()) {
// Move value from setting:auth.defaultUserGroup into new column group:is_default_group
if (empty($defaultUserGroupId) ||
if (
empty($defaultUserGroupId) ||
!($group = Group::findOne(['id' => $defaultUserGroupId])) ||
$group->is_admin_group) {
$group->is_admin_group
) {
// Create one default Group if setting:auth.defaultUserGroup was not selected to any group:
$group = new Group();
$group->name = 'Default Group';

View File

@ -9,6 +9,7 @@
namespace humhub\models;
use humhub\components\SettingActiveRecord;
use humhub\libs\BaseSettingsManager;
use Yii;
use yii\base\Exception;
@ -22,7 +23,6 @@ use yii\base\Exception;
*/
class Setting extends SettingActiveRecord
{
/**
* @inheritdoc
*/
@ -59,8 +59,10 @@ class Setting extends SettingActiveRecord
* Returns settings value
*
* @deprecated since version 1.1
*
* @param string $name
* @param string $moduleId
*
* @return string the settings value
*/
public static function get($name, $moduleId = '')
@ -73,6 +75,7 @@ class Setting extends SettingActiveRecord
* Sets settings value
*
* @deprecated since version 1.1
*
* @param string $name
* @param string $value
* @param string $moduleId
@ -104,8 +107,10 @@ class Setting extends SettingActiveRecord
*
* @deprecated since version 1.1
* @see \humhub\libs\BaseSettingsManager::isFixed
*
* @param string $name
* @param string $moduleId
*
* @return boolean
*/
public static function isFixed($name, $moduleId = '')
@ -117,16 +122,19 @@ class Setting extends SettingActiveRecord
* Checks if Humhub is installed
*
* @return boolean
* @deprecated since v1.16; use Yii::$app->isInstalled()
* @see Yii::$app->isInstalled()
*/
public static function isInstalled()
{
return isset(Yii::$app->params['installed']) && Yii::$app->params['installed'] == true;
return Yii::$app->isInstalled();
}
/**
* Temporary for 1.1 migration
*
* @deprecated since version 1.1
*
* @param string $name
* @param string $moduleId
*/
@ -159,6 +167,7 @@ class Setting extends SettingActiveRecord
* Temporary for 1.1 migration
*
* @deprecated since version 1.1
*
* @param string $name
* @param string $moduleId
*/
@ -178,5 +187,4 @@ class Setting extends SettingActiveRecord
return $module;
}
}

View File

@ -22,11 +22,10 @@ use yii\web\HttpException;
*/
class Module extends \humhub\components\Module
{
/**
* @event on configuration steps init
*/
const EVENT_INIT_CONFIG_STEPS = 'steps';
public const EVENT_INIT_CONFIG_STEPS = 'steps';
/**
* @inheritdoc
@ -63,7 +62,7 @@ class Module extends \humhub\components\Module
{
// Block installer, when it's marked as installed
if (Yii::$app->params['installed']) {
if (Yii::$app->isInstalled()) {
throw new HttpException(500, 'HumHub is already installed!');
}
@ -86,7 +85,6 @@ class Module extends \humhub\components\Module
// return the current connection state.
return Yii::$app->db->getIsActive();
} catch (Exception $e) {
}
return false;
@ -106,22 +104,24 @@ class Module extends \humhub\components\Module
/**
* Sets application in installed state (disables installer)
*
* @deprecated since v1.16; use Yii::$app->setInstalled()
* @see Yii::$app->setInstalled()
*/
public function setInstalled()
{
$config = DynamicConfig::load();
$config['params']['installed'] = true;
DynamicConfig::save($config);
Yii::$app->setInstalled();
}
/**
* Sets application database in installed state
* Sets the application database in installed state
*
* @deprecated since v1.16; use Yii::$app->setDatabaseInstalled()
* @see Yii::$app->setDatabaseInstalled()
*/
public function setDatabaseInstalled()
{
$config = DynamicConfig::load();
$config['params']['databaseInstalled'] = true;
DynamicConfig::save($config);
Yii::$app->setDatabaseInstalled();
}
protected function initConfigSteps()
@ -133,7 +133,7 @@ class Module extends \humhub\components\Module
$this->configSteps['basic'] = [
'sort' => 100,
'url' => Url::to(['/installer/config/basic']),
'isCurrent' => function() {
'isCurrent' => function () {
return (Yii::$app->controller->id == 'config' && Yii::$app->controller->action->id == 'basic');
},
];
@ -145,7 +145,7 @@ class Module extends \humhub\components\Module
$this->configSteps['usecase'] = [
'sort' => 150,
'url' => Url::to(['/installer/config/use-case']),
'isCurrent' => function() {
'isCurrent' => function () {
return (Yii::$app->controller->id == 'config' && Yii::$app->controller->action->id == 'use-case');
},
];
@ -156,7 +156,7 @@ class Module extends \humhub\components\Module
$this->configSteps['security'] = [
'sort' => 200,
'url' => Url::to(['/installer/config/security']),
'isCurrent' => function() {
'isCurrent' => function () {
return (Yii::$app->controller->id == 'config' && Yii::$app->controller->action->id == 'security');
},
];
@ -167,7 +167,7 @@ class Module extends \humhub\components\Module
$this->configSteps['modules'] = [
'sort' => 300,
'url' => Url::to(['/installer/config/modules']),
'isCurrent' => function() {
'isCurrent' => function () {
return (Yii::$app->controller->id == 'config' && Yii::$app->controller->action->id == 'modules');
},
];
@ -178,7 +178,7 @@ class Module extends \humhub\components\Module
$this->configSteps['admin'] = [
'sort' => 400,
'url' => Url::to(['/installer/config/admin']),
'isCurrent' => function() {
'isCurrent' => function () {
return (Yii::$app->controller->id == 'config' && Yii::$app->controller->action->id == 'admin');
},
];
@ -189,7 +189,7 @@ class Module extends \humhub\components\Module
$this->configSteps['sample-data'] = [
'sort' => 450,
'url' => Url::to(['/installer/config/sample-data']),
'isCurrent' => function() {
'isCurrent' => function () {
return (Yii::$app->controller->id == 'config' && Yii::$app->controller->action->id == 'sample-data');
},
];
@ -200,7 +200,7 @@ class Module extends \humhub\components\Module
$this->configSteps['finish'] = [
'sort' => 1000,
'url' => Url::to(['/installer/config/finish']),
'isCurrent' => function() {
'isCurrent' => function () {
return (Yii::$app->controller->id == 'config' && Yii::$app->controller->action->id == 'finish');
},
];
@ -232,9 +232,8 @@ class Module extends \humhub\components\Module
*/
protected function sortConfigSteps()
{
usort($this->configSteps, function($a, $b) {
usort($this->configSteps, function ($a, $b) {
return ($a['sort'] > $b['sort']) ? 1 : -1;
});
}
}

View File

@ -8,6 +8,7 @@
namespace humhub\modules\installer\commands;
use humhub\commands\MigrateController;
use humhub\helpers\DatabaseHelper;
use humhub\libs\DynamicConfig;
use humhub\libs\UUID;
@ -29,12 +30,11 @@ use yii\helpers\Console;
* php yii installer/install-db
* php yii installer/write-site-config "$HUMHUB_NAME" "$HUMHUB_EMAIL"
* php yii installer/create-admin-account
*
*/
class InstallController extends Controller
{
/**
* Finished install without input. Useful for testing.
* Finished installation without input. Useful for testing.
*/
public function actionAuto()
{
@ -49,7 +49,8 @@ class InstallController extends Controller
* On success: Writes given settings to config-file and reloads it.
* On failure: Throws exception
*/
public function actionWriteDbConfig($db_host, $db_name, $db_user, $db_pass) {
public function actionWriteDbConfig($db_host, $db_name, $db_user, $db_pass)
{
$connectionString = "mysql:host=" . $db_host . ";dbname=" . $db_name;
$dbConfig = [
'class' => 'yii\db\Connection',
@ -90,14 +91,14 @@ class InstallController extends Controller
Yii::$app->cache->flush();
// Disable max execution time to avoid timeouts during migrations
@ini_set('max_execution_time', 0);
\humhub\commands\MigrateController::webMigrateAll();
MigrateController::webMigrateAll();
DynamicConfig::rewrite();
$this->setDatabaseInstalled();
Yii::$app->setDatabaseInstalled();
$this->stdout(" * Finishing\n", Console::FG_YELLOW);
$this->setInstalled();
Yii::$app->setInstalled();
return ExitCode::OK;
}
@ -105,7 +106,7 @@ class InstallController extends Controller
/**
* Creates a new user account and adds it to the admin-group
*/
public function actionCreateAdminAccount($admin_user='admin', $admin_email='humhub@example.com', $admin_pass='test')
public function actionCreateAdminAccount($admin_user = 'admin', $admin_email = 'humhub@example.com', $admin_pass = 'test')
{
$user = new User();
$user->username = $admin_user;
@ -134,7 +135,8 @@ class InstallController extends Controller
/**
* Writes essential site settings to config file and sets installed state
*/
public function actionWriteSiteConfig($site_name='HumHub', $site_email='humhub@example.com'){
public function actionWriteSiteConfig($site_name = 'HumHub', $site_email = 'humhub@example.com')
{
$this->stdout("Install Site:\n\n", Console::FG_YELLOW);
InitialData::bootstrap();
@ -143,7 +145,7 @@ class InstallController extends Controller
Yii::$app->settings->set('secret', UUID::v4());
Yii::$app->settings->set('timeZone', Yii::$app->timeZone);
$this->setInstalled();
Yii::$app->setInstalled();
return ExitCode::OK;
}
@ -151,33 +153,14 @@ class InstallController extends Controller
/**
* Sets the base url
*/
public function actionSetBaseUrl($base_url){
public function actionSetBaseUrl($base_url)
{
$this->stdout("Setting base url", Console::FG_YELLOW);
Yii::$app->settings->set('baseUrl', $base_url);
return ExitCode::OK;
}
/**
* Sets application in installed state (disables installer)
*/
private function setInstalled()
{
$config = DynamicConfig::load();
$config['params']['installed'] = true;
DynamicConfig::save($config);
}
/**
* Sets application database in installed state
*/
private function setDatabaseInstalled()
{
$config = DynamicConfig::load();
$config['params']['databaseInstalled'] = true;
DynamicConfig::save($config);
}
/**
* Tries to open global db connection and checks result.
*

View File

@ -25,13 +25,13 @@ use Yii;
*/
class SetupController extends Controller
{
public const PASSWORD_PLACEHOLDER = 'n0thingToSeeHere!';
/**
* @inheritdoc
*/
public $access = ControllerAccess::class;
const PASSWORD_PLACEHOLDER = 'n0thingToSeeHere!';
public function actionIndex()
{
return $this->redirect(['prerequisites']);
@ -63,17 +63,21 @@ class SetupController extends Controller
$config = DynamicConfig::load();
$model = new DatabaseForm();
if (isset($config['params']['installer']['db']['installer_hostname']))
if (isset($config['params']['installer']['db']['installer_hostname'])) {
$model->hostname = $config['params']['installer']['db']['installer_hostname'];
}
if (isset($config['params']['installer']['db']['installer_database']))
if (isset($config['params']['installer']['db']['installer_database'])) {
$model->database = $config['params']['installer']['db']['installer_database'];
}
if (isset($config['components']['db']['username']))
if (isset($config['components']['db']['username'])) {
$model->username = $config['components']['db']['username'];
}
if (isset($config['components']['db']['password']))
if (isset($config['components']['db']['password'])) {
$model->password = self::PASSWORD_PLACEHOLDER;
}
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$connectionString = 'mysql:host=' . $model->hostname;
@ -85,8 +89,9 @@ class SetupController extends Controller
}
$password = $model->password;
if ($password == self::PASSWORD_PLACEHOLDER)
if ($password == self::PASSWORD_PLACEHOLDER) {
$password = $config['components']['db']['password'];
}
// Create Test DB Connection
$dbConfig = [
@ -121,7 +126,6 @@ class SetupController extends Controller
DynamicConfig::save($config);
return $this->redirect(['migrate']);
} catch (\Exception $e) {
$errorMessage = $e->getMessage();
}
@ -187,7 +191,6 @@ class SetupController extends Controller
DynamicConfig::rewrite();
$this->module->setDatabaseInstalled();
Yii::$app->setDatabaseInstalled();
}
}

View File

@ -88,7 +88,7 @@ class MailTarget extends BaseTarget
public function isActive(User $user = null)
{
// Do not send mail notifications for example content during installlation.
return parent::isActive() && Yii::$app->params['installed'];
return parent::isActive() && Yii::$app->isInstalled();
}
}

View File

@ -9,7 +9,6 @@
namespace humhub\modules\ui\view\bootstrap;
use humhub\components\console\Application as ConsoleApplication;
use humhub\libs\BaseSettingsManager;
use humhub\modules\installer\libs\EnvironmentChecker;
use humhub\modules\ui\view\components\Theme;
use humhub\modules\ui\view\helpers\ThemeHelper;
@ -25,7 +24,6 @@ use yii\base\BootstrapInterface;
*/
class ThemeLoader implements BootstrapInterface
{
/**
* @inheritdoc
*/
@ -36,7 +34,7 @@ class ThemeLoader implements BootstrapInterface
return;
}
if (BaseSettingsManager::isDatabaseInstalled()) {
if ($app->isDatabaseInstalled()) {
$themePath = $app->settings->get('theme');
if (!empty($themePath) && is_dir($themePath)) {
$theme = ThemeHelper::getThemeByPath($themePath);
@ -56,6 +54,5 @@ class ThemeLoader implements BootstrapInterface
$app->view->theme->register();
}
}
}
}

View File

@ -8,9 +8,7 @@
namespace humhub\modules\ui\view\components;
use humhub\assets\AppAsset;
use humhub\assets\CoreBundleAsset;
use humhub\libs\BaseSettingsManager;
use humhub\modules\ui\view\helpers\ThemeHelper;
use Yii;
use yii\base\Theme as BaseTheme;
@ -101,11 +99,12 @@ class Theme extends BaseTheme
/**
* Registers theme css and resources to the view
*
* @param bool $includeParents also register parent themes
*/
public function register($includeParents = true)
{
if(Yii::$app->request->isAjax) {
if (Yii::$app->request->isAjax) {
return;
}
@ -120,7 +119,6 @@ class Theme extends BaseTheme
$mtime = filemtime($this->getBasePath() . '/css/theme.css');
Yii::$app->view->registerCssFile($this->getBaseUrl() . '/css/theme.css?v=' . $mtime, ['depends' => CoreBundleAsset::class]);
}
}
@ -186,6 +184,7 @@ class Theme extends BaseTheme
* Publishs theme assets (e.g. images or css)
*
* @param boolean|null $force
*
* @return string url of published resources
*/
public function publishResources($force = null)
@ -205,7 +204,9 @@ class Theme extends BaseTheme
* Returns the value of a given theme variable
*
* @since 1.2
*
* @param string $key the variable name
*
* @return string the variable value
*/
public function variable($key, $default = null)
@ -226,7 +227,7 @@ class Theme extends BaseTheme
return $this->parents;
}
if ($this->isActive() && BaseSettingsManager::isDatabaseInstalled()) {
if ($this->isActive() && Yii::$app->isDatabaseInstalled()) {
$this->parents = static::getActiveParents();
}
@ -241,11 +242,10 @@ class Theme extends BaseTheme
$parentPaths[] = $theme->getBasePath();
}
if (BaseSettingsManager::isDatabaseInstalled()) {
if (Yii::$app->isDatabaseInstalled()) {
Yii::$app->settings->setSerialized('themeParents', $parentPaths);
}
}
}
return $this->parents;
@ -277,5 +277,4 @@ class Theme extends BaseTheme
}
return $parents;
}
}

View File

@ -8,14 +8,11 @@
namespace humhub\modules\ui\view\components;
use humhub\libs\BaseSettingsManager;
use humhub\modules\ui\Module;
use humhub\modules\ui\view\helpers\ThemeHelper;
use Yii;
use yii\base\Component;
/**
* ThemeVariables provides access to LESS variables of a given [[Theme]].
* The variables will be stored in the application SettingManager for fast access.
@ -56,11 +53,12 @@ class ThemeVariables extends Component
*
* @param $key
* @param $default
*
* @return string|null
*/
public function get($key, $default = null)
{
if (!BaseSettingsManager::isDatabaseInstalled()) {
if (!Yii::$app->isDatabaseInstalled()) {
return null;
}
@ -94,6 +92,7 @@ class ThemeVariables extends Component
* The prefix is necessary to separate the theme variables
*
* @param $key
*
* @return string
*/
protected function getSettingKey($key)
@ -133,5 +132,4 @@ class ThemeVariables extends Component
);
}
}
}

View File

@ -457,7 +457,7 @@ class View extends \yii\web\View
echo '<title>' . $this->getPageTitle() . '</title>';
}
if (Yii::$app->params['installed']) {
if (Yii::$app->isInstalled()) {
if (Yii::$app->getSession()->hasFlash('view-status')) {
$viewStatus = Yii::$app->getSession()->getFlash('view-status');
$type = strtolower(key($viewStatus));
@ -487,7 +487,7 @@ class View extends \yii\web\View
}
// Since the JsConfig accesses user queries it fails before installation.
if (Yii::$app->params['installed']) {
if (Yii::$app->isInstalled()) {
CoreJsConfig::widget();
}

View File

@ -8,13 +8,12 @@
namespace humhub\modules\web;
use humhub\controllers\ErrorController;
use humhub\modules\web\pwa\controllers\ManifestController;
use humhub\modules\web\pwa\controllers\OfflineController;
use humhub\modules\web\pwa\controllers\ServiceWorkerController;
use Yii;
use humhub\controllers\ErrorController;
use humhub\models\Setting;
use humhub\modules\web\security\helpers\Security;
use Yii;
/**
* Event Handling Callbacks
@ -25,7 +24,7 @@ class Events
{
public static function onBeforeAction($evt)
{
if(Yii::$app->request->isConsoleRequest) {
if (Yii::$app->request->isConsoleRequest) {
return;
}
@ -38,11 +37,12 @@ class Events
private static function generateCSPRequestCheck()
{
return !Yii::$app->request->isAjax
&& Setting::isInstalled()
&& !(Yii::$app->controller instanceof ErrorController)
&& !(Yii::$app->controller instanceof OfflineController)
&& !(Yii::$app->controller instanceof ManifestController)
&& !(Yii::$app->controller instanceof ServiceWorkerController);
&& Yii::$app->isInstalled()
&& ($controller = Yii::$app->controller)
&& !($controller instanceof ErrorController)
&& !($controller instanceof OfflineController)
&& !($controller instanceof ManifestController)
&& !($controller instanceof ServiceWorkerController);
}
public static function onAfterLogin($evt)

View File

@ -1,12 +1,9 @@
<?php
namespace humhub\modules\web\security\helpers;
use humhub\models\Setting;
use Yii;
use humhub\modules\web\security\models\SecuritySettings;
use Yii;
class Security
{
@ -26,7 +23,7 @@ class Security
static::setHeader($key, $header);
}
if($settings->hasSection(SecuritySettings::CSP_SECTION_REPORT_ONLY)) {
if ($settings->hasSection(SecuritySettings::CSP_SECTION_REPORT_ONLY)) {
$reportOnlySettings = new SecuritySettings(['cspSection' => SecuritySettings::CSP_SECTION_REPORT_ONLY]);
$header = $reportOnlySettings->getHeader(SecuritySettings::HEADER_CONTENT_SECRUITY_POLICY_REPORT_ONLY);
foreach ($reportOnlySettings->getCSPHeaderKeys() as $key) {
@ -48,7 +45,7 @@ class Security
private static function setHeader($key, $value)
{
if($value) {
if ($value) {
Yii::$app->response->headers->add($key, $value);
}
}
@ -64,7 +61,7 @@ class Security
public static function setNonce($nonce = null)
{
if(!$nonce) {
if (!$nonce) {
Yii::$app->session->remove(static::SESSION_KEY_NONCE);
} else {
Yii::$app->session->set(static::SESSION_KEY_NONCE, $nonce);
@ -73,18 +70,19 @@ class Security
/**
* @param bool $create creates a new nonce if none given
*
* @return string
* @throws \Exception
*/
public static function getNonce($create = false)
{
if(!Setting::isInstalled()) {
if (!Yii::$app->isInstalled()) {
return null;
}
$nonce = Yii::$app->session->get(static::SESSION_KEY_NONCE);
if($create && !$nonce) {
if ($create && !$nonce) {
$nonce = static::createNonce();
static::setNonce($nonce);
}

View File

@ -8,14 +8,13 @@
namespace humhub\tests\codeception\unit\components;
use Codeception\Test\Unit;
use humhub\libs\BaseSettingsManager;
use tests\codeception\_support\HumHubDbTestCase;
use Yii;
class BaseSettingsManagerTest extends HumHubDbTestCase
{
public function testIsDatabaseInstalled()
{
$this->assertTrue(BaseSettingsManager::isDatabaseInstalled());
$this->assertTrue(Yii::$app->isDatabaseInstalled());
}
}

View File

@ -267,7 +267,7 @@ class ModuleManagerTest extends HumHubDbTestCase
{
[$basePath, $config] = $this->getModuleConfig(static::$testModuleRoot . '/installerModule');
static::assertTrue(Yii::$app->params['installed']);
static::assertTrue(Yii::$app->isInstalled());
$this->registerModule($basePath, $config, false);

View File

@ -20,7 +20,6 @@ use Yii;
*/
class LayoutAddons extends BaseStack
{
/**
* @inheritdoc
*/
@ -30,15 +29,14 @@ class LayoutAddons extends BaseStack
$this->addWidget(GlobalModal::class);
$this->addWidget(GlobalConfirmModal::class);
if(Yii::$app->params['installed']) {
if (Yii::$app->isInstalled()) {
$this->addWidget(Tour::class);
$this->addWidget(TrackingWidget::class);
}
$this->addWidget(LoaderWidget::class, ['show' => false, 'id' => "humhub-ui-loader-default"]);
$this->addWidget(StatusBar::class);
if (Yii::$app->params['installed']) {
if (Yii::$app->isInstalled()) {
$this->addWidget(BlueimpGallery::class);
if (Yii::$app->params['enablePjax']) {
@ -48,5 +46,4 @@ class LayoutAddons extends BaseStack
}
parent::init();
}
}