Refactoring

This commit is contained in:
Lucas Bartholemy 2025-01-16 18:36:41 +01:00
parent 05a65cd03e
commit d8636539a2
21 changed files with 142 additions and 42 deletions

View File

@ -0,0 +1,23 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2025 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\components\captcha;
class AltchaCaptcha implements CaptchaInterface
{
public function createInputWidget($config = []): string
{
return AltchaCaptchaInput::widget($config);
}
public function getValidatorClass(): string
{
return AltchaCaptchaValidator::class;
}
}

View File

@ -2,11 +2,11 @@
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2017 HumHub GmbH & Co. KG
* @copyright Copyright (c) 2025 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\captcha;
namespace humhub\components\captcha;
use AltchaOrg\Altcha\Altcha;
use AltchaOrg\Altcha\ChallengeOptions;

View File

@ -2,11 +2,11 @@
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2017 HumHub GmbH & Co. KG
* @copyright Copyright (c) 2025 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\captcha;
namespace humhub\components\captcha;
use humhub\components\assets\AssetBundle;

View File

@ -2,11 +2,11 @@
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2017 HumHub GmbH & Co. KG
* @copyright Copyright (c) 2025 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\captcha;
namespace humhub\components\captcha;
use humhub\helpers\Html;
use JsonException;

View File

@ -2,11 +2,11 @@
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2017 HumHub GmbH & Co. KG
* @copyright Copyright (c) 2025 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\captcha;
namespace humhub\components\captcha;
use AltchaOrg\Altcha\Altcha;
use Exception;
@ -25,7 +25,7 @@ class AltchaCaptchaValidator extends Validator
public $skipOnEmpty = false;
/**
* @inerhitdoc
* @inheritdoc
*/
public function validateAttribute($model, $attribute)
{
@ -38,7 +38,11 @@ class AltchaCaptchaValidator extends Validator
Yii::error('AltchaCaptcha verification error: ' . $e->getMessage());
}
$this->addError($model, $attribute, Yii::t('base', 'We couldn\'t verify that you\'re human. Please check the box again.'));
$this->addError(
$model,
$attribute,
Yii::t('base', 'We couldn\'t verify that you\'re human. Please check the box again.')
);
}
public static function getHmacKey(): ?string

View File

@ -0,0 +1,17 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2025 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\components\captcha;
interface CaptchaInterface
{
public function createInputWidget($config = []): string;
public function getValidatorClass(): string;
}

View File

@ -0,0 +1,22 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2025 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\components\captcha;
class YiiCaptcha implements CaptchaInterface
{
public function createInputWidget($config = []): string
{
return YiiCaptchaInput::widget($config);
}
public function getValidatorClass(): string
{
return YiiCaptchaValidator::class;
}
}

View File

@ -2,11 +2,11 @@
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2017 HumHub GmbH & Co. KG
* @copyright Copyright (c) 2025 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\captcha;
namespace humhub\components\captcha;
use Yii;
use yii\captcha\Captcha;

View File

@ -2,11 +2,11 @@
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2017 HumHub GmbH & Co. KG
* @copyright Copyright (c) 2025 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\captcha;
namespace humhub\components\captcha;
use yii\captcha\CaptchaValidator;

View File

@ -49,6 +49,7 @@ class __Application
* Include only Web application related components here
* @property \humhub\modules\user\components\User $user
* @property \humhub\components\mail\Mailer $mailer
* @property \humhub\components\captcha\CaptchaInterface $captcha
*/
class __WebApplication extends \humhub\components\Application
{

View File

@ -28,6 +28,10 @@ $config = [
'response' => [
'class' => \humhub\components\Response::class,
],
'captcha' => [
// 'class' => \humhub\components\captcha\AltchaCaptcha::class
'class' => \humhub\components\captcha\YiiCaptcha::class
],
'user' => [
'class' => \humhub\modules\user\components\User::class,
'identityClass' => \humhub\modules\user\models\User::class,
@ -67,12 +71,6 @@ $config = [
],
],
],
'params' => [
'captcha' => [
'inputClass' => \humhub\captcha\AltchaCaptchaInput::class, // or \humhub\captcha\YiiCaptchaInput::class
'validatorClass' => \humhub\captcha\AltchaCaptchaValidator::class, // or \humhub\captcha\YiiCaptchaValidator::class
],
],
'container' => [
'definitions' => [
'yii\web\Cookie' => ['\humhub\libs\CookieBuilder', 'build'],

View File

@ -2,31 +2,44 @@
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2017 HumHub GmbH & Co. KG
* @copyright Copyright (c) 2025 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\controllers;
use humhub\captcha\AltchaCaptchaAction;
use humhub\components\access\ControllerAccess;
use humhub\components\captcha\AltchaCaptcha;
use humhub\components\captcha\AltchaCaptchaAction;
use humhub\components\captcha\YiiCaptcha;
use humhub\components\Controller;
use Yii;
use yii\captcha\CaptchaAction;
/**
* Controller for build-in captcha actions
*
* @since 1.18
*/
class CaptchaController extends Controller
{
public $access = ControllerAccess::class;
public function actions()
{
return [
'yii' => [
'class' => CaptchaAction::class,
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
'altcha' => [
'class' => AltchaCaptchaAction::class,
],
];
if (Yii::$app->captcha instanceof AltchaCaptcha) {
return [
'altcha' => [
'class' => AltchaCaptchaAction::class,
],
];
} elseif (Yii::$app->captcha instanceof YiiCaptcha) {
return [
'yii' => [
'class' => CaptchaAction::class,
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
}
}

View File

@ -73,10 +73,6 @@ class AuthController extends Controller
public function actions()
{
return [
'captcha' => [ // @deprecated since 1.18.0
'class' => CaptchaAction::class,
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
'external' => [
'class' => AuthAction::class,
'successCallback' => [$this, 'onAuthSuccess'],

View File

@ -82,7 +82,7 @@ class Invite extends ActiveRecord
[['email'], 'required'],
[['email'], 'unique', 'except' => self::SCENARIO_INVITE_BY_LINK_FORM],
[['email'], 'email'],
[['captcha'], Yii::$app->params['captcha']['validatorClass'], 'on' => [self::SCENARIO_INVITE, self::SCENARIO_INVITE_BY_LINK_FORM]],
[['captcha'], Yii::$app->captcha->getValidatorClass(), 'on' => [self::SCENARIO_INVITE, self::SCENARIO_INVITE_BY_LINK_FORM]],
];
}

View File

@ -24,7 +24,7 @@ class AccountRecoverPassword extends Model
return [
['email', 'required'],
['email', 'email'],
['captcha', Yii::$app->params['captcha']['validatorClass']],
['captcha', Yii::$app->captcha->getValidatorClass()],
['email', 'verifyEmail'],
];
}

View File

@ -5,6 +5,7 @@ use humhub\modules\user\models\forms\Login;
use humhub\modules\user\models\Invite;
use humhub\modules\user\widgets\AuthChoice;
use humhub\widgets\form\ActiveForm;
use humhub\widgets\form\CaptchaField;
use humhub\widgets\SiteLogo;
$this->pageTitle = Yii::t('UserModule.auth', 'Login');
@ -107,7 +108,7 @@ $this->pageTitle = Yii::t('UserModule.auth', 'Login');
<?= $form->field($invite, 'email')->input('email', ['id' => 'register-email', 'placeholder' => $invite->getAttributeLabel('email'), 'aria-label' => $invite->getAttributeLabel('email')])->label(false); ?>
<?php if ($invite->showCaptureInRegisterForm()) : ?>
<div id="registration-form-captcha" style="display: none;">
<?= $form->field($invite, 'captcha')->widget(Yii::$app->params['captcha']['inputClass'])->label(false) ?>
<?= $form->field($invite, 'captcha')->widget(CaptchaField::class)->label(false) ?>
</div>
<?php endif; ?>
<hr>

View File

@ -5,6 +5,7 @@ use humhub\modules\user\models\forms\Login;
use humhub\modules\user\models\Invite;
use humhub\modules\user\widgets\AuthChoice;
use humhub\widgets\form\ActiveForm;
use humhub\widgets\form\CaptchaField;
use humhub\widgets\modal\Modal;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;
@ -122,7 +123,7 @@ use yii\helpers\Url;
<?= $form->field($invite, 'email')->input('email', ['id' => 'register-email', 'placeholder' => $invite->getAttributeLabel('email')]); ?>
<?php if ($invite->showCaptureInRegisterForm()) : ?>
<?= $form->field($invite, 'captcha')->widget(Yii::$app->params['captcha']['inputClass'])->label(false) ?>
<?= $form->field($invite, 'captcha')->widget(CaptchaField::class)->label(false) ?>
<?php endif; ?>
<hr>

View File

@ -4,6 +4,7 @@ use humhub\helpers\Html;
use humhub\modules\user\models\forms\AccountRecoverPassword;
use humhub\widgets\bootstrap\Button;
use humhub\widgets\form\ActiveForm;
use humhub\widgets\form\CaptchaField;
use humhub\widgets\SiteLogo;
use yii\helpers\Url;
@ -31,7 +32,7 @@ $this->pageTitle = Yii::t('UserModule.auth', 'Password recovery');
<?= $form->field($model, 'email')->textInput(['class' => 'form-control', 'id' => 'email_txt', 'placeholder' => Yii::t('UserModule.auth', 'Your email')])->label(false) ?>
<div class="mb-3">
<?= $form->field($model, 'captcha')->widget(Yii::$app->params['captcha']['inputClass'])->label(false);
<?= $form->field($model, 'captcha')->widget(CaptchaField::class)->label(false);
?>
</div>

View File

@ -2,6 +2,7 @@
use humhub\helpers\Html;
use humhub\modules\user\models\forms\AccountRecoverPassword;
use humhub\widgets\form\CaptchaField;
use humhub\widgets\modal\Modal;
use yii\helpers\Url;
@ -22,7 +23,7 @@ use yii\helpers\Url;
</div>
<div class="mb-3">
<?= $form->field($model, 'captcha')->widget(Yii::$app->params['captcha']['inputClass'])->label(false) ?>
<?= $form->field($model, 'captcha')->widget(CaptchaField::class)->label(false) ?>
</div>
<hr>

View File

@ -4,6 +4,7 @@ use humhub\helpers\Html;
use humhub\modules\user\models\Invite;
use humhub\modules\user\widgets\AuthChoice;
use humhub\widgets\form\ActiveForm;
use humhub\widgets\form\CaptchaField;
use humhub\widgets\SiteLogo;
/**
@ -42,7 +43,7 @@ $this->pageTitle = Yii::t('UserModule.auth', 'Create Account');
<?= $form->field($invite, 'email')->input('email', ['id' => 'register-email', 'placeholder' => $invite->getAttributeLabel('email'), 'aria-label' => $invite->getAttributeLabel('email')])->label(false); ?>
<?php if ($invite->showCaptureInRegisterForm()) : ?>
<div id="registration-form-captcha" style="display: none;">
<?= $form->field($invite, 'captcha')->widget(Yii::$app->params['captcha']['inputClass'])->label(false) ?>
<?= $form->field($invite, 'captcha')->widget(CaptchaField::class)->label(false) ?>
</div>
<?php endif; ?>
<hr>

View File

@ -0,0 +1,21 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2025 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\widgets\form;
use humhub\components\Widget;
use Yii;
class CaptchaField extends Widget
{
public static function widget($config = [])
{
return Yii::$app->captcha->createInputWidget($config);
}
}