mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Отрефакторены тесты
Удалено тестовое окружение acceptance Удалена часть потенциально ненужных тестов Добавлена логика для формы регистрации Добавлена таблица для хранения ключей активации по E-mail Добавлены тесты для формы регистрации Реорганизован роутинг Добавлен компонент для ReCaptcha2
This commit is contained in:
16
api/components/ReCaptcha/Component.php
Normal file
16
api/components/ReCaptcha/Component.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
namespace api\components\ReCaptcha;
|
||||
|
||||
use yii\base\InvalidConfigException;
|
||||
|
||||
class Component extends \yii\base\Component {
|
||||
|
||||
public $secret;
|
||||
|
||||
public function init() {
|
||||
if ($this->secret === NULL) {
|
||||
throw new InvalidConfigException('');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
65
api/components/ReCaptcha/Validator.php
Normal file
65
api/components/ReCaptcha/Validator.php
Normal file
@ -0,0 +1,65 @@
|
||||
<?php
|
||||
namespace api\components\ReCaptcha;
|
||||
|
||||
|
||||
use Yii;
|
||||
use yii\base\Exception;
|
||||
use yii\base\InvalidConfigException;
|
||||
|
||||
class Validator extends \yii\validators\Validator {
|
||||
|
||||
const SITE_VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify';
|
||||
const CAPTCHA_RESPONSE_FIELD = 'g-recaptcha-response';
|
||||
|
||||
public $skipOnEmpty = false;
|
||||
|
||||
/**
|
||||
* @return Component
|
||||
*/
|
||||
protected function getComponent() {
|
||||
return Yii::$app->reCaptcha;
|
||||
}
|
||||
|
||||
public function init() {
|
||||
parent::init();
|
||||
if ($this->getComponent() === null) {
|
||||
throw new InvalidConfigException('Required "reCaptcha" component as instance of ' . Component::class . '.');
|
||||
}
|
||||
|
||||
if ($this->message === null) {
|
||||
$this->message = Yii::t('yii', 'The verification code is incorrect.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function validateValue($value) {
|
||||
$value = Yii::$app->request->post(self::CAPTCHA_RESPONSE_FIELD);
|
||||
if (empty($value)) {
|
||||
return [$this->message, []];
|
||||
}
|
||||
|
||||
$requestParams = [
|
||||
'secret' => $this->getComponent()->secret,
|
||||
'response' => $value,
|
||||
'remoteip' => Yii::$app->request->userIP,
|
||||
];
|
||||
|
||||
$requestUrl = self::SITE_VERIFY_URL . '?' . http_build_query($requestParams);
|
||||
$response = $this->getResponse($requestUrl);
|
||||
|
||||
if (!isset($response['success'])) {
|
||||
throw new Exception('Invalid recaptcha verify response.');
|
||||
}
|
||||
|
||||
return $response['success'] ? null : [$this->message, []];
|
||||
}
|
||||
|
||||
protected function getResponse($request) {
|
||||
$response = file_get_contents($request);
|
||||
|
||||
return json_decode($response, true);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user