mirror of
https://github.com/humhub/humhub.git
synced 2025-01-17 22:28:51 +01:00
Merge branch 'develop' of github.com:humhub/humhub into develop
This commit is contained in:
commit
2422509e36
@ -12,6 +12,7 @@ use Yii;
|
||||
use yii\base\Component;
|
||||
use yii\base\Exception;
|
||||
use yii\db\conditions\LikeCondition;
|
||||
use yii\db\StaleObjectException;
|
||||
use yii\helpers\Json;
|
||||
|
||||
/**
|
||||
@ -140,14 +141,18 @@ abstract class BaseSettingsManager extends Component
|
||||
* Deletes setting
|
||||
*
|
||||
* @param string $name
|
||||
* @throws \Throwable
|
||||
* @throws \yii\db\StaleObjectException
|
||||
*/
|
||||
public function delete($name)
|
||||
{
|
||||
$record = $this->find()->andWhere(['name' => $name])->one();
|
||||
if ($record !== null) {
|
||||
try {
|
||||
$record->delete();
|
||||
} catch (StaleObjectException $e) {
|
||||
Yii::error('Could not delete setting "' . $name . '". Error: ' . $e->getMessage(), 'base');
|
||||
} catch (\Throwable $e) {
|
||||
Yii::error('Could not delete setting "' . $name . '". Error: ' . $e->getMessage(), 'base');
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($this->_loaded[$name])) {
|
||||
@ -246,8 +251,8 @@ abstract class BaseSettingsManager extends Component
|
||||
/**
|
||||
* Checks if settings table exists or application is not installed yet
|
||||
*
|
||||
* @since 1.3
|
||||
* @return bool
|
||||
* @since 1.3
|
||||
*/
|
||||
public static function isDatabaseInstalled()
|
||||
{
|
||||
|
@ -10,6 +10,7 @@ namespace humhub\modules\marketplace;
|
||||
|
||||
use humhub\components\Module as BaseModule;
|
||||
use humhub\models\Setting;
|
||||
use humhub\modules\marketplace\components\LicenceManager;
|
||||
use humhub\modules\marketplace\models\Licence;
|
||||
use humhub\modules\marketplace\components\OnlineModuleManager;
|
||||
use Yii;
|
||||
@ -84,32 +85,12 @@ class Module extends BaseModule
|
||||
|
||||
|
||||
/**
|
||||
* Returns the currently active licence object
|
||||
*
|
||||
* @return Licence
|
||||
*/
|
||||
public function getLicence()
|
||||
{
|
||||
Licence::fetch();
|
||||
|
||||
$l = new Licence();
|
||||
|
||||
$l->licenceKey = $this->settings->get('licenceKey');
|
||||
$l->licencedTo = $this->settings->get('licencedTo');
|
||||
|
||||
if (!empty($l->licencedTo)) {
|
||||
$l->maxUsers = (int)$this->settings->get('maxUsers');
|
||||
$l->type = Licence::LICENCE_TYPE_PRO;
|
||||
} else {
|
||||
$l->type = Licence::LICENCE_TYPE_CE;
|
||||
|
||||
if (Yii::$app->hasModule('enterprise')) {
|
||||
/** @var \humhub\modules\enterprise\Module $enterprise */
|
||||
$enterprise = Yii::$app->getModule('enterprise');
|
||||
if ($enterprise->settings->get('licence') !== null && $enterprise->settings->get('licence_valid') == 1) {
|
||||
$l->type = Licence::LICENCE_TYPE_EE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $l;
|
||||
return LicenceManager::get();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,179 @@
|
||||
<?php
|
||||
|
||||
|
||||
/**
|
||||
* @link https://www.humhub.org/
|
||||
* @copyright Copyright (c) 2020 HumHub GmbH & Co. KG
|
||||
* @license https://www.humhub.com/licences
|
||||
*/
|
||||
|
||||
namespace humhub\modules\marketplace\components;
|
||||
|
||||
use humhub\modules\admin\libs\HumHubAPI;
|
||||
use humhub\modules\marketplace\models\Licence;
|
||||
use humhub\modules\marketplace\Module;
|
||||
use humhub\modules\space\models\Space;
|
||||
use humhub\modules\user\models\User;
|
||||
use Yii;
|
||||
use yii\base\InvalidConfigException;
|
||||
use yii\db\StaleObjectException;
|
||||
|
||||
|
||||
/**
|
||||
* Class LicenceManager
|
||||
*
|
||||
* @package humhub\modules\marketplace\components
|
||||
*/
|
||||
class LicenceManager
|
||||
{
|
||||
|
||||
const SETTING_KEY_PE_LICENCE_KEY = 'licenceKey';
|
||||
const SETTING_KEY_PE_LAST_FETCH = 'lastFetch';
|
||||
const SETTING_KEY_PE_LICENCED_TO = 'licencedTo';
|
||||
const SETTING_KEY_PE_MAX_USERS = 'maxUsers';
|
||||
|
||||
const PE_FETCH_INTERVAL = 60 * 60 * 2;
|
||||
const PE_FETCH_TOLERANCE = 60 * 60 * 24 * 2;
|
||||
|
||||
/**
|
||||
* Returns the current licence object
|
||||
*
|
||||
* @return Licence
|
||||
*/
|
||||
public static function get()
|
||||
{
|
||||
$settings = static::getModule()->settings;
|
||||
|
||||
$licence = new Licence(['type' => Licence::LICENCE_TYPE_CE]);
|
||||
|
||||
$lastFetch = (int)$settings->get(static::SETTING_KEY_PE_LAST_FETCH);
|
||||
if (!empty($settings->get(static::SETTING_KEY_PE_LICENCE_KEY))) {
|
||||
|
||||
// Update
|
||||
if ($lastFetch + static::PE_FETCH_INTERVAL < time()) {
|
||||
if (!static::fetch() && $lastFetch + static::PE_FETCH_TOLERANCE < time()) {
|
||||
$lastFetchDateTime = 'empty';
|
||||
try {
|
||||
$lastFetchDateTime = Yii::$app->formatter->asDatetime($lastFetch, 'full');
|
||||
} catch (InvalidConfigException $e) {
|
||||
Yii::error($e->getMessage(), 'marketplace');
|
||||
}
|
||||
Yii::error('Could not fetch PE licence since: ' . $lastFetchDateTime, 'marketplace');
|
||||
return $licence;
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($settings->get(static::SETTING_KEY_PE_LICENCED_TO)) && !empty($settings->get(static::SETTING_KEY_PE_MAX_USERS))) {
|
||||
$licence->type = Licence::LICENCE_TYPE_PRO;
|
||||
$licence->maxUsers = $settings->get(static::SETTING_KEY_PE_MAX_USERS);
|
||||
$licence->licencedTo = $settings->get(static::SETTING_KEY_PE_LICENCED_TO);
|
||||
$licence->licenceKey = $settings->get(static::SETTING_KEY_PE_LICENCE_KEY);
|
||||
return $licence;
|
||||
}
|
||||
}
|
||||
|
||||
if (isset(Yii::$app->params['hosting'])) {
|
||||
// In our demo hosting, we allow pro licences without registration
|
||||
$licence->type = Licence::LICENCE_TYPE_PRO;
|
||||
} elseif (Yii::$app->hasModule('enterprise')) {
|
||||
/** @var \humhub\modules\enterprise\Module $enterprise */
|
||||
$enterprise = Yii::$app->getModule('enterprise');
|
||||
if ($enterprise->settings->get('licence') !== null && $enterprise->settings->get('licence_valid') == 1) {
|
||||
$licence->type = Licence::LICENCE_TYPE_EE;
|
||||
}
|
||||
}
|
||||
|
||||
return $licence;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the licence from the HumHub API
|
||||
*
|
||||
* @return bool The retrieval of the license worked, whether it is valid or not.
|
||||
*/
|
||||
public static function fetch()
|
||||
{
|
||||
$result = static::request('v1/pro/get');
|
||||
|
||||
if (empty($result) || !is_array($result) || !isset($result['status'])) {
|
||||
// Connection failure
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($result['status'] === 'ok') {
|
||||
static::getModule()->settings->set(static::SETTING_KEY_PE_LICENCE_KEY, $result['licence']['licenceKey']);
|
||||
static::getModule()->settings->set(static::SETTING_KEY_PE_LICENCED_TO, $result['licence']['licencedTo']);
|
||||
static::getModule()->settings->set(static::SETTING_KEY_PE_MAX_USERS, $result['licence']['maxUsers']);
|
||||
static::getModule()->settings->set(static::SETTING_KEY_PE_LAST_FETCH, time());
|
||||
|
||||
return true;
|
||||
} elseif ($result['status'] === 'not-found') {
|
||||
try {
|
||||
if (static::remove()) {
|
||||
return true;
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
Yii::error('Could not fetch/remove licence: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Removes the licence from this installation and the HumHub Marketplace
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public static function remove()
|
||||
{
|
||||
$licenceKey = static::getModule()->settings->get('licenceKey');
|
||||
if (!empty($licenceKey)) {
|
||||
$result = static::request('v1/pro/unregister', ['licenceKey' => $licenceKey]);
|
||||
}
|
||||
|
||||
static::getModule()->settings->delete(static::SETTING_KEY_PE_LICENCE_KEY);
|
||||
static::getModule()->settings->delete(static::SETTING_KEY_PE_LICENCED_TO);
|
||||
static::getModule()->settings->delete(static::SETTING_KEY_PE_MAX_USERS);
|
||||
static::getModule()->settings->delete(static::SETTING_KEY_PE_LAST_FETCH);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Request HumHub API backend
|
||||
*
|
||||
* @param $url
|
||||
* @param array $params
|
||||
* @return array
|
||||
*/
|
||||
public static function request($url, $params = [])
|
||||
{
|
||||
return HumHubAPI::request($url, array_merge($params, static::getStats()));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array some basic stats
|
||||
*/
|
||||
private static function getStats()
|
||||
{
|
||||
return [
|
||||
'tua' => User::find()->andWhere(['status' => User::STATUS_ENABLED])->count(),
|
||||
'tu' => User::find()->count(),
|
||||
'ts' => Space::find()->count(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Module the marketplace module
|
||||
*/
|
||||
private static function getModule()
|
||||
{
|
||||
return Yii::$app->getModule('marketplace');
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -8,7 +8,7 @@
|
||||
namespace humhub\modules\marketplace\controllers;
|
||||
|
||||
use humhub\modules\admin\components\Controller;
|
||||
use humhub\modules\marketplace\models\Licence;
|
||||
use humhub\modules\marketplace\components\LicenceManager;
|
||||
use humhub\modules\marketplace\Module;
|
||||
use Yii;
|
||||
|
||||
@ -26,6 +26,7 @@ class LicenceController extends Controller
|
||||
$model = $this->module->getLicence();
|
||||
|
||||
if ($model->load(Yii::$app->request->post()) && $model->register()) {
|
||||
LicenceManager::fetch();
|
||||
return $this->redirect(['index']);
|
||||
}
|
||||
|
||||
@ -35,8 +36,7 @@ class LicenceController extends Controller
|
||||
|
||||
public function actionRemove()
|
||||
{
|
||||
Licence::remove();
|
||||
|
||||
LicenceManager::remove();
|
||||
return $this->redirect(['/marketplace/licence']);
|
||||
}
|
||||
|
||||
|
@ -1,20 +1,15 @@
|
||||
<?php
|
||||
/**
|
||||
* @link https://www.humhub.org/
|
||||
* @copyright Copyright (c) 2019 HumHub GmbH & Co. KG
|
||||
* @copyright Copyright (c) 2020 HumHub GmbH & Co. KG
|
||||
* @license https://www.humhub.com/licences
|
||||
*/
|
||||
|
||||
namespace humhub\modules\marketplace\models;
|
||||
|
||||
use humhub\modules\admin\libs\HumHubAPI;
|
||||
use humhub\modules\space\models\Space;
|
||||
use humhub\modules\marketplace\Module;
|
||||
use humhub\modules\user\models\User;
|
||||
use humhub\modules\marketplace\components\LicenceManager;
|
||||
use Yii;
|
||||
use yii\base\Exception;
|
||||
use yii\base\Model;
|
||||
use yii\db\StaleObjectException;
|
||||
|
||||
class Licence extends Model
|
||||
{
|
||||
@ -77,99 +72,26 @@ class Licence extends Model
|
||||
|
||||
|
||||
/**
|
||||
* Registers the licence
|
||||
*
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$params = array_merge(['licenceKey' => $this->licenceKey], $this->getStats());
|
||||
$result = HumHubAPI::request('v1/pro/register', $params);
|
||||
$result = LicenceManager::request('v1/pro/register', ['licenceKey' => $this->licenceKey]);
|
||||
|
||||
if (empty($result) || !is_array($result) || !isset($result['status'])) {
|
||||
$this->addError('licenceKey', Yii::t('MarketplaceModule.base', 'Could not connect to licence server!'));
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($result['status'] === 'ok' && static::fetch()) {
|
||||
if ($result['status'] === 'ok') {
|
||||
return true;
|
||||
}
|
||||
|
||||
LicenceManager::remove();
|
||||
$this->addError('licenceKey', Yii::t('MarketplaceModule.base', 'Could not update licence. Error: ') . $result['message']);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Removes the licence from this installation
|
||||
*
|
||||
* @throws StaleObjectException
|
||||
* @throws \Throwable
|
||||
*/
|
||||
public static function remove()
|
||||
{
|
||||
$licenceKey = static::getModule()->settings->get('licenceKey');
|
||||
|
||||
if (!empty($licenceKey)) {
|
||||
$params = array_merge(['licenceKey' => $licenceKey], static::getStats());
|
||||
$result = HumHubAPI::request('v1/pro/unregister', $params);
|
||||
}
|
||||
|
||||
static::getModule()->settings->delete('licenceKey');
|
||||
static::getModule()->settings->delete('licencedTo');
|
||||
static::getModule()->settings->delete('maxUsers');
|
||||
static::getModule()->settings->delete('lastSave');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Module
|
||||
*/
|
||||
private static function getModule()
|
||||
{
|
||||
return Yii::$app->getModule('marketplace');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array some basic stats
|
||||
*/
|
||||
private static function getStats()
|
||||
{
|
||||
return [
|
||||
'tua' => User::find()->andWhere(['status' => User::STATUS_ENABLED])->count(),
|
||||
'tu' => User::find()->count(),
|
||||
'ts' => Space::find()->count(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the licence from the server
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function fetch()
|
||||
{
|
||||
$result = HumHubAPI::request('v1/pro/get', static::getStats());
|
||||
|
||||
if (empty($result) || !is_array($result) || !isset($result['status'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($result['status'] === 'ok') {
|
||||
static::getModule()->settings->set('licenceKey', $result['licence']['licenceKey']);
|
||||
static::getModule()->settings->set('licencedTo', $result['licence']['licencedTo']);
|
||||
static::getModule()->settings->set('maxUsers', $result['licence']['maxUsers']);
|
||||
static::getModule()->settings->set('lastFetch', time());
|
||||
|
||||
return true;
|
||||
} elseif ($result['status'] === 'not-found') {
|
||||
try {
|
||||
Licence::remove();
|
||||
} catch (StaleObjectException $e) {
|
||||
} catch (\Throwable $e) {
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -26,7 +26,11 @@ class AboutVersion extends Widget
|
||||
$licence = $module->getLicence();
|
||||
|
||||
if ($licence->type === Licence::LICENCE_TYPE_PRO) {
|
||||
if (isset(Yii::$app->params['hosting'])) {
|
||||
return $this->render('about_version_pro_cloud', ['licence' => $licence]);
|
||||
} else {
|
||||
return $this->render('about_version_pro', ['licence' => $licence]);
|
||||
}
|
||||
} elseif ($licence->type === Licence::LICENCE_TYPE_EE) {
|
||||
return $this->render('about_version_ee');
|
||||
} else {
|
||||
|
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
use yii\helpers\Url;
|
||||
|
||||
/* @var $this \humhub\components\View */
|
||||
?>
|
||||
<div style="padding:20px;min-height:164px" class="jumbotron">
|
||||
<div class="pull-left" style="padding-right:24px;">
|
||||
<img src="<?= Yii::getAlias('@web-static/img/humhub_pro.jpg'); ?>" style="height:124px;">
|
||||
</div>
|
||||
<span style="font-size:36px">HumHub </span><span
|
||||
style="font-size:24px">Professional Edition - SaaS</span><br/>
|
||||
<span
|
||||
style="font-size:18px"><?= Yii::t('MarketplaceModule.base', 'Version:'); ?> <?= Yii::$app->version ?></span><br/>
|
||||
<?php if (!empty($licence->licencedTo)): ?>
|
||||
<span
|
||||
style="font-size:18px"><?= Yii::t('MarketplaceModule.base', 'Licenced to:'); ?> <?= $licence->licencedTo; ?></span>
|
||||
<br/>
|
||||
<?php endif; ?>
|
||||
<?php if (!empty($licence->maxUsers)): ?>
|
||||
<span
|
||||
style="font-size:18px"><?= Yii::t('MarketplaceModule.base', 'Max. users:'); ?> <?= $licence->maxUsers ?></span>
|
||||
<?php endif; ?>
|
||||
<br/>
|
||||
|
||||
</div>
|
Loading…
x
Reference in New Issue
Block a user