#2592 üye kayıt formuna capthca eklenmesi

This commit is contained in:
Diatrex 2020-11-05 17:13:53 +03:00
parent 9b52d3428f
commit baff84bf27
11 changed files with 108 additions and 5 deletions

View File

@ -20,6 +20,8 @@
'url':form.options.url
})|raw }}
<input type="hidden" name="recaptcha_token" id="recaptcha_token">
{% include 'visiosoft.theme.base::addons/anomaly/users-module/partials/register-form' %}
<div class="col-12">
@ -76,6 +78,12 @@
</div>
</div>
{% set reCAPTCHASiteKey = setting_value('visiosoft.module.profile::google_captcha_site_key') %}
<script>
let reCAPTCHASiteKey = "{{ reCAPTCHASiteKey }}"
</script>
{{ asset_add('scripts.js', asset_download('https://www.google.com/recaptcha/api.js?render=' ~ reCAPTCHASiteKey, 60*60*24)) }}
{{ asset_add("scripts.js", "visiosoft.theme.base::js/register.js") }}
{{ asset_add("scripts.js", "visiosoft.module.profile::assets/js/captcha.js") }}
{% endblock %}

View File

@ -10,6 +10,7 @@
]
},
"require": {
"maatwebsite/excel": "*"
"maatwebsite/excel": "*",
"google/recaptcha": "1.2.*"
}
}

View File

@ -0,0 +1,6 @@
grecaptcha.ready(function() {
grecaptcha.execute(reCAPTCHASiteKey)
.then(function(token) {
document.getElementById("recaptcha_token").value = token;
});
});

View File

@ -4,13 +4,18 @@ return [
'monitoring' => [
'stacked' => false,
'tabs' => [
'general_setting' => [
'title' => 'visiosoft.module.profile::section.general_setting',
'fields' => [
'show_my_ads', 'upload_avatar', 'show_tax_office'
],
],
'recaptcha' => [
'title' => 'visiosoft.module.profile::section.recaptcha',
'fields' => [
'google_captcha_site_key', 'google_captcha_secret_key', 'score_threshold'
],
],
],
],
];

View File

@ -13,11 +13,25 @@ return [
'default_value' => 1
],
],
'show_tax_office' => [
'type' => 'anomaly.field_type.boolean',
'config' => [
'default_value' => true,
],
],
'google_captcha_site_key' => [
'type' => 'anomaly.field_type.text',
],
'google_captcha_secret_key' => [
'type' => 'anomaly.field_type.text',
],
"score_threshold" => [
"type" => "anomaly.field_type.decimal",
"config" => [
"default_value" => 0.5,
"decimals" => 1,
"min" => 0.0,
"max" => 1.0,
]
],
];

View File

@ -38,4 +38,8 @@ return [
// Mail
'update_email_mail_subject' => 'Email Updated!',
'update_email_mail_message' => 'Your Email Has Been Updated!',
// Register
'recaptcha_field_is_required' => 'reCaptcha field is required.',
'failed_to_validate_captcha' => 'Failed to validate captcha.',
];

View File

@ -8,4 +8,5 @@ return [
'title' => 'Adress',
],
'general_setting' => 'General Settings',
'recaptcha' => 'reCAPTCHA',
];

View File

@ -7,8 +7,17 @@ return [
'upload_avatar' => [
'name' => 'Avatar Upload',
],
'show_tax_office' => [
'name' => 'Show Tax Office Field',
],
'google_captcha_site_key' => [
'name' => 'Google Captcha Site Key',
],
'google_captcha_secret_key' => [
'name' => 'Google Captcha Secret Key',
],
'score_threshold' => [
'name' => 'Score Threshold',
'instructions' => 'A value between 0 and 1. The higher the threshold the more strict ReCaptcha is in trying to determine if a user is a bot or not.',
],
];

View File

@ -4,6 +4,7 @@ use Anomaly\Streams\Platform\Ui\Form\FormBuilder;
use Visiosoft\ProfileModule\Profile\Register2\Command\SetOptions;
use Anomaly\UsersModule\User\UserModel;
use Visiosoft\ProfileModule\Profile\Validation\ValidateRegister;
use Visiosoft\ProfileModule\Rules\ReCaptchaRule;
/**
* Class RegisterFormBuilder
@ -37,6 +38,22 @@ class Register2FormBuilder extends FormBuilder
* @var array
*/
protected $fields = [
'recaptcha_token' => [
'required' => true,
'type' => 'anomaly.field_type.text',
'config' => [
"max" => 0,
],
'rules' => [
'valid_recaptcha'
],
'validators' => [
'valid_recaptcha' => [
'message' => false,
'handler' => ReCaptchaRule::class
]
]
],
'username' => [
'required' => true,
],

View File

@ -62,7 +62,8 @@ class Register2FormHandler
$fields['accept_terms'],
$fields['accept_protection_law'],
$fields['accept_privacy_terms'],
$fields['receive_sms_emails']
$fields['receive_sms_emails'],
$fields['recaptcha_token']
);
$register = $users->create($fields);

View File

@ -0,0 +1,37 @@
<?php namespace Visiosoft\ProfileModule\Rules;
use Anomaly\Streams\Platform\Message\MessageBag;
use ReCaptcha\ReCaptcha;
class ReCaptchaRule
{
private $message;
public function __construct(MessageBag $message)
{
$this->message = $message;
}
public function handle($attribute, $value)
{
if (empty($value)) {
$this->message->error(trans('visiosoft.module.profile::message.recaptcha_field_is_required'));
return false;
}
$recaptcha = new ReCaptcha(setting_value('visiosoft.module.profile::google_captcha_secret_key'));
$resp = $recaptcha->setExpectedHostname(request()->server('SERVER_NAME'))
->setScoreThreshold(setting_value('visiosoft.module.profile::score_threshold'))
->verify($value, request()->server('REMOTE_ADDR'));
if (!$resp->isSuccess()) {
$this->message->error('visiosoft.module.profile::message.failed_to_validate_captcha');
return false;
}
return true;
}
}