Added pro edition registration to Marketplace module

This commit is contained in:
Lucas Bartholemy 2019-11-05 17:08:50 +01:00
parent ec66148eaa
commit 6245a0fe35
10 changed files with 388 additions and 3 deletions

View File

@ -1,8 +1,13 @@
<?php
use humhub\modules\marketplace\widgets\AboutVersion;
use yii\helpers\Html;
?>
<?= AboutVersion::widget(); ?>
<br />
<?php if ($isNewVersionAvailable) : ?>
<div class="alert alert-danger">
<p>
@ -19,9 +24,6 @@ use yii\helpers\Html;
</div>
<?php endif; ?>
<p>
<?= Yii::t('AdminModule.information', 'Currently installed version: %currentVersion%', ['%currentVersion%' => '<strong>' . Yii::$app->version . '</strong>']); ?><br>
</p>
<br>
<?php if (YII_DEBUG) : ?>

View File

@ -9,6 +9,7 @@
namespace humhub\modules\marketplace;
use humhub\components\Module as BaseModule;
use humhub\modules\marketplace\models\Licence;
use humhub\modules\marketplace\components\OnlineModuleManager;
use Yii;
@ -79,4 +80,28 @@ class Module extends BaseModule
return $this->_onlineModuleManager;
}
/**
* @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 {
// ToDo Check valid EE module
$l->type = Licence::LICENCE_TYPE_CE;
}
return $l;
}
}

View File

@ -0,0 +1,44 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2019 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\modules\marketplace\controllers;
use humhub\modules\admin\components\Controller;
use humhub\modules\marketplace\models\Licence;
use humhub\modules\marketplace\Module;
use Yii;
/**
* Licence controller
*
* @property Module $module
* @package humhub\modules\marketplace\controllers
*/
class LicenceController extends Controller
{
public function actionIndex()
{
$model = $this->module->getLicence();
if ($model->load(Yii::$app->request->post()) && $model->register()) {
return $this->redirect(['index']);
}
return $this->render('index', ['model' => $model]);
}
public function actionRemove()
{
Licence::remove();
return $this->redirect(['/marketplace/licence']);
}
}

View File

@ -0,0 +1,175 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2019 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 Yii;
use yii\base\Exception;
use yii\base\Model;
use yii\db\StaleObjectException;
class Licence extends Model
{
/**
* Licence types
*/
const LICENCE_TYPE_CE = 'community';
const LICENCE_TYPE_PRO = 'pro';
const LICENCE_TYPE_EE = 'enterprise';
/**
* @var string the licence type
*/
public $type;
/**
* @var string the licence key
*/
public $licenceKey;
/**
* @var string name of the licensee
*/
public $licencedTo;
/**
* @var int the number of maximum users
*/
public $maxUsers;
/**
* @inheritdoc
*/
public function init()
{
$this->type = static::LICENCE_TYPE_CE;
parent::init();
}
/**
* @inheritdoc
*/
public function rules()
{
return [
['licenceKey', 'safe'],
['licenceKey', 'trim'],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'licenceKey' => Yii::t('MarketplaceModule.base', 'Licence key'),
];
}
/**
* @return bool
* @throws Exception
*/
public function register()
{
$params = array_merge(['licenceKey' => $this->licenceKey], $this->getStats());
$result = HumHubAPI::request('v1/pro/register', $params);
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()) {
return true;
}
$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;
}
}

View File

@ -0,0 +1,64 @@
<?php
use humhub\modules\marketplace\models\Licence;
use humhub\widgets\Button;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\ActiveForm;
/* @var $this \humhub\components\View */
/* @var $model Licence */
?>
<div class="panel">
<div class="panel-heading">
<?= Yii::t('MarketplaceModule.base', '<strong>Activate</strong> your Professional Edition'); ?>
</div>
<div class="panel-body">
<?php if ($model->type === Licence::LICENCE_TYPE_PRO): ?>
<div class="alert alert-success">
<p>
<strong>
<?= Yii::t('MarketplaceModule.base', 'Professional Edition is activated!'); ?>
</strong><br/>
<?= Yii::t('MarketplaceModule.base',
'Licenced for max. {number} users.', ['number' => $model->maxUsers]); ?>
</p>
</div>
<?php endif; ?>
<p>
<?= Yii::t('MarketplaceModule.base',
'No license key? Find out more about the {pro} or contact us.',
['pro' => Html::a('Professional Edition', 'https://www.humhub.com',
['target' => '_blank', 'style' => 'text-decoration:underline'])]
); ?></p>
<hr>
<?php $form = ActiveForm::begin([
'id' => 'licence-form',
'enableAjaxValidation' => false,
'enableClientValidation' => false]); ?>
<?= $form->errorSummary($model); ?>
<?= $form->field($model, 'licenceKey')->textInput(); ?>
<hr>
<?= Button::save(Yii::t('MarketplaceModule.base', 'Save and update'))->submit(); ?>
<?php ActiveForm::end(); ?>
<?php if ($model->type === Licence::LICENCE_TYPE_PRO): ?>
<a href="<?= Url::to(['/marketplace/licence/remove']); ?>" class="pull-right">
<small><?= Yii::t('MarketplaceModule.base', 'Remove licence key'); ?></small>
</a>
<?php endif; ?>
</div>
</div>

View File

@ -0,0 +1,35 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2019 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\modules\marketplace\widgets;
use humhub\components\Widget;
use humhub\modules\marketplace\models\Licence;
use humhub\modules\marketplace\Module;
use Yii;
class AboutVersion extends Widget
{
/**
* @inheritDoc
*/
public function run()
{
/** @var Module $module */
$module = Yii::$app->getModule('marketplace');
$licence = $module->getLicence();
if ($licence->type === Licence::LICENCE_TYPE_CE) {
return $this->render('about_version');
} else {
return $this->render('about_version_pro', ['licence' => $licence]);
}
}
}

View File

@ -0,0 +1,18 @@
<?php
use yii\helpers\Url;
?>
<div style="padding:20px" class="jumbotron">
<div class="pull-left" style="padding-right:12px">
<img src="<?= Yii::getAlias('@web-static/img/humhub.png'); ?>" style="height:96px;">
</div>
<span style="font-size:36px">HumHub&nbsp;&nbsp;</span><span style="font-size:24px">Community Edition</span><br/>
<a href="<?= Url::to(['/marketplace/licence']); ?>" class="btn btn-success pull-right"><i
class="fa fa-rocket">&nbsp;</i> <?= Yii::t('MarketplaceModule.base', 'Upgrade to Professional Edition'); ?>
</a>
<span
style="font-size:18px"><?= Yii::t('MarketplaceModule.base', 'Version:'); ?>&nbsp;<?= Yii::$app->version ?></span><br/>
<br/>
</div>

View File

@ -0,0 +1,22 @@
<?php
use humhub\modules\marketplace\models\Licence;
use yii\helpers\Url;
/* @var $this \humhub\components\View */
/* @var $licence Licence */
?>
<div style="padding:20px" class="jumbotron">
<div class="pull-left" style="padding-right:24px">
<img src="<?= Yii::getAlias('@web-static/img/humhub_pro.jpg'); ?>" style="height:124px;">
</div>
<div class="pull-right">
<a href="<?= Url::to(['/marketplace/licence']); ?>" class="btn btn-primary btn-sm"><i class="fa fa-cogs">&nbsp;</i>
<?= Yii::t('MarketplaceModule.base', 'Edit licence'); ?></a>
</div>
<span style="font-size:36px">HumHub&nbsp;&nbsp;</span><span style="font-size:24px">Professional Edition</span><br/>
<span style="font-size:18px"><?= Yii::t('MarketplaceModule.base', 'Version:'); ?> <?= Yii::$app->version ?></span><br/>
<span style="font-size:18px"><?= Yii::t('MarketplaceModule.base', 'Licenced to:'); ?> <?= $licence->licencedTo; ?></span><br/>
<span style="font-size:18px"><?= Yii::t('MarketplaceModule.base', 'Max. users:'); ?>: <?= $licence->maxUsers; ?></span><br/>
</div>

BIN
static/img/humhub.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

BIN
static/img/humhub_pro.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.0 KiB