Отрефакторены тесты

Удалено тестовое окружение acceptance
Удалена часть потенциально ненужных тестов
Добавлена логика для формы регистрации
Добавлена таблица для хранения ключей активации по E-mail
Добавлены тесты для формы регистрации
Реорганизован роутинг
Добавлен компонент для ReCaptcha2
This commit is contained in:
ErickSkrauch
2016-01-15 12:21:27 +03:00
parent 45c31dfbbe
commit 44aaea2c08
56 changed files with 1075 additions and 972 deletions

View 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('');
}
}
}

View 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);
}
}

View File

@@ -36,9 +36,9 @@ return [
'showScriptName' => false,
'rules' => [],
],
],
'modules' => [
'login' => 'api\modules\login\Module',
'reCaptcha' => [
'class' => 'api\components\ReCaptcha\Component',
],
],
'params' => $params,
];

View File

@@ -1,8 +1,7 @@
<?php
namespace api\modules\login\controllers;
namespace api\controllers;
use api\controllers\Controller;
use api\modules\login\models\AuthenticationForm;
use api\models\LoginForm;
use Yii;
use yii\filters\AccessControl;
@@ -12,10 +11,10 @@ class AuthenticationController extends Controller {
return array_merge(parent::behaviors(), [
'access' => [
'class' => AccessControl::className(),
'only' => ['login-info'],
'only' => ['login'],
'rules' => [
[
'actions' => ['login-info'],
'actions' => ['login', 'register'],
'allow' => true,
'roles' => ['?'],
],
@@ -26,17 +25,18 @@ class AuthenticationController extends Controller {
public function verbs() {
return [
'loginInfo' => ['post'],
'login' => ['post'],
'register' => ['post'],
];
}
public function actionLoginInfo() {
$model = new AuthenticationForm();
public function actionLogin() {
$model = new LoginForm();
$model->load(Yii::$app->request->post());
if (!$model->login()) {
return [
'success' => false,
'errors' => $model->getErrors(),
'errors' => $this->normalizeModelErrors($model->getErrors()),
];
}

View File

@@ -1,8 +1,10 @@
<?php
namespace api\controllers;
use api\traits\ApiNormalize;
class Controller extends \yii\rest\Controller {
use ApiNormalize;
public $enableCsrfValidation = true;

View File

@@ -0,0 +1,41 @@
<?php
namespace api\controllers;
use api\models\RegistrationForm;
use Yii;
use yii\filters\AccessControl;
class SignupController extends Controller {
public function behaviors() {
return array_merge(parent::behaviors(), [
'access' => [
'class' => AccessControl::className(),
'only' => ['register'],
'rules' => [
[
'actions' => ['register'],
'allow' => true,
'roles' => ['?'],
],
],
],
]);
}
public function actionRegister() {
$model = new RegistrationForm();
$model->load(Yii::$app->request->post());
if (!$model->signup()) {
return [
'success' => false,
'errors' => $this->normalizeModelErrors($model->getErrors()),
];
}
return [
'success' => true,
];
}
}

View File

@@ -2,7 +2,6 @@
namespace api\controllers;
use api\models\ContactForm;
use api\models\LoginForm;
use api\models\PasswordResetRequestForm;
use api\models\ResetPasswordForm;
use api\models\SignupForm;
@@ -11,12 +10,11 @@ use yii\base\InvalidParamException;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\web\BadRequestHttpException;
use yii\web\Controller;
/**
* Site controller
*/
class SiteController extends Controller
class SiteController extends \yii\web\Controller
{
/**
* @inheritdoc
@@ -75,27 +73,6 @@ class SiteController extends Controller
return $this->render('index');
}
/**
* Logs in a user.
*
* @return mixed
*/
public function actionLogin()
{
if (!\Yii::$app->user->isGuest) {
return $this->goHome();
}
$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
return $this->goBack();
} else {
return $this->render('login', [
'model' => $model,
]);
}
}
/**
* Logs out the current user.
*

View File

@@ -0,0 +1,8 @@
<?php
/**
* @var string $key
*/
?>
<p>Текст письма</p>
<p>Код: <?= $key ?></p>

View File

@@ -0,0 +1,9 @@
<?php
/**
* @var string $key
*/
?>
В общем по результату работы сложного PHP скрипта вы успешно зарегистрированы.
Код активации <?= $key ?>

View File

@@ -5,46 +5,45 @@ use common\models\Account;
use Yii;
use yii\base\Model;
/**
* Login form
*/
class LoginForm extends Model
{
public $username;
class LoginForm extends Model {
public $login;
public $password;
public $rememberMe = true;
private $_user;
private $_account;
public function formName() {
return '';
}
/**
* @inheritdoc
*/
public function rules()
{
public function rules() {
return [
// username and password are both required
[['username', 'password'], 'required'],
// rememberMe must be a boolean value
['rememberMe', 'boolean'],
// password is validated by validatePassword()
['login', 'required', 'message' => 'error.login_required'],
['login', 'validateLogin'],
['password', 'required', 'when' => function(self $model) {
return !$model->hasErrors();
}, 'message' => 'error.password_required'],
['password', 'validatePassword'],
['rememberMe', 'boolean'],
];
}
/**
* Validates the password.
* This method serves as the inline validation for password.
*
* @param string $attribute the attribute currently being validated
* @param array $params the additional name-value pairs given in the rule
*/
public function validatePassword($attribute, $params)
{
public function validateLogin($attribute) {
if (!$this->hasErrors()) {
$user = $this->getUser();
if (!$user || !$user->validatePassword($this->password)) {
$this->addError($attribute, 'Incorrect username or password.');
if (!$this->getAccount()) {
$this->addError($attribute, 'error.' . $attribute . '_not_exist');
}
}
}
public function validatePassword($attribute) {
if (!$this->hasErrors()) {
$account = $this->getAccount();
if (!$account || !$account->validatePassword($this->password)) {
$this->addError($attribute, 'error.' . $attribute . '_incorrect');
}
}
}
@@ -54,26 +53,24 @@ class LoginForm extends Model
*
* @return boolean whether the user is logged in successfully
*/
public function login()
{
if ($this->validate()) {
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
} else {
public function login() {
if (!$this->validate()) {
return false;
}
return Yii::$app->user->login($this->getAccount(), $this->rememberMe ? 3600 * 24 * 30 : 0);
}
/**
* Finds user by [[username]]
*
* @return Account|null
*/
protected function getUser()
{
if ($this->_user === null) {
$this->_user = Account::findByEmail($this->username);
protected function getAccount() {
if ($this->_account === NULL) {
$attribute = strpos($this->login, '@') ? 'email' : 'username';
$this->_account = Account::findOne([$attribute => $this->login]);
}
return $this->_user;
return $this->_account;
}
}

View File

@@ -0,0 +1,111 @@
<?php
namespace api\models;
use api\components\ReCaptcha\Validator as ReCaptchaValidator;
use common\components\UserFriendlyRandomKey;
use common\models\Account;
use common\models\EmailActivation;
use Yii;
use yii\base\ErrorException;
use yii\base\Model;
class RegistrationForm extends Model {
public $username;
public $email;
public $password;
public $rePassword;
public $rulesAgreement;
public function formName() {
return '';
}
public function rules() {
return [
['rulesAgreement', 'required', 'message' => 'error.you_must_accept_rules'],
[[], ReCaptchaValidator::class, 'message' => 'error.captcha_invalid', 'when' => !YII_ENV_TEST],
['username', 'filter', 'filter' => 'trim'],
['username', 'required', 'message' => 'error.username_required'],
['username', 'string', 'min' => 3, 'max' => 21,
'tooShort' => 'error.username_too_short',
'tooLong' => 'error.username_too_long',
],
['username', 'match', 'pattern' => '/^[\p{L}\d-_\.!?#$%^&*()\[\]:;]+$/u'],
['username', 'unique', 'targetClass' => Account::class, 'message' => 'error.username_not_available'],
['email', 'filter', 'filter' => 'trim'],
['email', 'required', 'message' => 'error.email_required'],
['email', 'string', 'max' => 255, 'tooLong' => 'error.email_too_long'],
['email', 'email', 'checkDNS' => true, 'enableIDN' => true, 'message' => 'error.email_invalid'],
['email', 'unique', 'targetClass' => Account::class, 'message' => 'error.email_not_available'],
['password', 'required', 'message' => 'error.password_required'],
['rePassword', 'required', 'message' => 'error.rePassword_required'],
['password', 'string', 'min' => 8, 'tooShort' => 'error.password_too_short'],
['rePassword', 'validatePasswordAndRePasswordMatch'],
];
}
public function validatePasswordAndRePasswordMatch($attribute) {
if (!$this->hasErrors()) {
if ($this->password !== $this->rePassword) {
$this->addError($attribute, "error.rePassword_does_not_match");
}
}
}
/**
* @return Account|null the saved model or null if saving fails
*/
public function signup() {
if (!$this->validate()) {
return null;
}
$transaction = Yii::$app->db->beginTransaction();
try {
$account = new Account();
$account->email = $this->email;
$account->username = $this->username;
$account->password = $this->password;
$account->status = Account::STATUS_REGISTERED;
$account->generateAuthKey();
if (!$account->save()) {
throw new ErrorException('Account not created.');
}
$emailActivation = new EmailActivation();
$emailActivation->account_id = $account->id;
$emailActivation->type = EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION;
$emailActivation->key = UserFriendlyRandomKey::make();
if (!$emailActivation->save()) {
throw new ErrorException('Unable save email-activation model.');
}
/** @var \yii\swiftmailer\Mailer $mailer */
$mailer = Yii::$app->mailer;
/** @var \yii\swiftmailer\Message $message */
$message = $mailer->compose([
'html' => '@app/mails/registration-confirmation-html',
'text' => '@app/mails/registration-confirmation-text',
], [
'key' => $emailActivation->key,
])->setFrom(['account@ely.by' => 'Ely.by']);
if (!$message->send()) {
throw new ErrorException('Unable send email with activation code.');
}
$transaction->commit();
} catch (ErrorException $e) {
$transaction->rollBack();
throw $e;
}
return $account;
}
}

View File

@@ -1,9 +0,0 @@
<?php
namespace api\modules\login;
class Module extends \yii\base\Module {
public $id = 'login';
}

View File

@@ -1,12 +0,0 @@
<?php
namespace api\modules\login\controllers;
use api\controllers\Controller;
class DefaultController extends Controller {
public function actionIndex() {
return ['hello' => 'world'];
}
}

View File

@@ -1,72 +0,0 @@
<?php
namespace api\modules\login\models;
use common\models\Account;
use Yii;
use yii\base\Model;
class AuthenticationForm extends Model {
public $login;
public $password;
public $rememberMe = true;
private $_account;
public function rules() {
return [
['login', 'required', 'message' => 'error.login_required'],
['login', 'validateLogin'],
['password', 'required', 'when' => function(self $model) {
return !$model->hasErrors();
}, 'message' => 'error.password_required'],
['password', 'validatePassword'],
['rememberMe', 'boolean'],
];
}
public function validateLogin($attribute) {
if (!$this->hasErrors()) {
if (!$this->getAccount()) {
$this->addError($attribute, 'error.' . $attribute . '_not_exist');
}
}
}
public function validatePassword($attribute) {
if (!$this->hasErrors()) {
$account = $this->getAccount();
if (!$account || !$account->validatePassword($this->password)) {
$this->addError($attribute, 'error.' . $attribute . '_incorrect');
}
}
}
/**
* Logs in a user using the provided username and password.
*
* @return boolean whether the user is logged in successfully
*/
public function login() {
if (!$this->validate()) {
return false;
}
return Yii::$app->user->login($this->getAccount(), $this->rememberMe ? 3600 * 24 * 30 : 0);
}
/**
* @return Account|null
*/
protected function getAccount() {
if ($this->_account === NULL) {
$attribute = strpos($this->login, '@') ? 'email' : 'username';
$this->_account = Account::findOne([$attribute => $this->login]);
}
return $this->_account;
}
}

View File

@@ -0,0 +1,26 @@
<?php
namespace api\traits;
trait ApiNormalize {
/**
* Метод убирает все ошибки для поля, кроме первой и возвращает значения в формате
* [
* 'field1' => 'first_error_of_field1',
* 'field2' => 'first_error_of_field2',
* ]
*
* @param array $errors
* @return array
*/
public function normalizeModelErrors(array $errors) {
$normalized = [];
foreach($errors as $attribute => $attrErrors) {
$normalized[$attribute] = $attrErrors[0];
}
return $normalized;
}
}

View File

@@ -1,39 +0,0 @@
<?php
/* @var $this yii\web\View */
/* @var $form yii\bootstrap\ActiveForm */
/* @var $model \api\models\LoginForm */
use yii\bootstrap\ActiveForm;
use yii\helpers\Html;
$this->title = 'Login';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="site-login">
<h1><?= Html::encode($this->title) ?></h1>
<p>Please fill out the following fields to login:</p>
<div class="row">
<div class="col-lg-5">
<?php $form = ActiveForm::begin(['id' => 'login-form']); ?>
<?= $form->field($model, 'username') ?>
<?= $form->field($model, 'password')->passwordInput() ?>
<?= $form->field($model, 'rememberMe')->checkbox() ?>
<div style="color:#999;margin:1em 0">
If you forgot your password you can <?= Html::a('reset it', ['site/request-password-reset']) ?>.
</div>
<div class="form-group">
<?= Html::submitButton('Login', ['class' => 'btn btn-primary', 'name' => 'login-button']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
</div>
</div>