2016-01-15 14:51:27 +05:30
|
|
|
|
<?php
|
2016-05-14 05:17:17 +05:30
|
|
|
|
namespace api\models\authentication;
|
2016-01-15 14:51:27 +05:30
|
|
|
|
|
2017-11-19 21:02:51 +05:30
|
|
|
|
use api\aop\annotations\CollectModelMetrics;
|
2016-01-15 14:51:27 +05:30
|
|
|
|
use api\components\ReCaptcha\Validator as ReCaptchaValidator;
|
2017-04-24 21:52:24 +05:30
|
|
|
|
use common\emails\EmailHelper;
|
2016-03-20 05:03:49 +05:30
|
|
|
|
use api\models\base\ApiForm;
|
2016-06-17 02:02:23 +05:30
|
|
|
|
use common\helpers\Error as E;
|
2016-01-15 14:51:27 +05:30
|
|
|
|
use common\components\UserFriendlyRandomKey;
|
|
|
|
|
use common\models\Account;
|
2016-05-11 01:10:06 +05:30
|
|
|
|
use common\models\confirmations\RegistrationConfirmation;
|
2016-09-21 13:43:43 +05:30
|
|
|
|
use common\models\UsernameHistory;
|
2016-11-02 02:27:42 +05:30
|
|
|
|
use common\validators\EmailValidator;
|
2016-05-13 14:33:00 +05:30
|
|
|
|
use common\validators\LanguageValidator;
|
2016-11-02 02:29:43 +05:30
|
|
|
|
use common\validators\PasswordValidator;
|
2016-11-02 01:57:38 +05:30
|
|
|
|
use common\validators\UsernameValidator;
|
2016-07-17 21:12:37 +05:30
|
|
|
|
use Exception;
|
2016-02-28 17:38:00 +05:30
|
|
|
|
use Ramsey\Uuid\Uuid;
|
2016-01-15 14:51:27 +05:30
|
|
|
|
use Yii;
|
|
|
|
|
use yii\base\ErrorException;
|
2016-10-27 04:04:51 +05:30
|
|
|
|
use yii\helpers\ArrayHelper;
|
2016-08-06 21:22:03 +05:30
|
|
|
|
use const common\LATEST_RULES_VERSION;
|
2016-01-15 14:51:27 +05:30
|
|
|
|
|
2016-03-20 05:03:49 +05:30
|
|
|
|
class RegistrationForm extends ApiForm {
|
2016-01-15 14:51:27 +05:30
|
|
|
|
|
2016-08-03 18:27:41 +05:30
|
|
|
|
public $captcha;
|
|
|
|
|
|
2016-01-15 14:51:27 +05:30
|
|
|
|
public $username;
|
2016-08-03 18:27:41 +05:30
|
|
|
|
|
2016-01-15 14:51:27 +05:30
|
|
|
|
public $email;
|
2016-08-03 18:27:41 +05:30
|
|
|
|
|
2016-01-15 14:51:27 +05:30
|
|
|
|
public $password;
|
2016-08-03 18:27:41 +05:30
|
|
|
|
|
2016-01-15 14:51:27 +05:30
|
|
|
|
public $rePassword;
|
2016-08-03 18:27:41 +05:30
|
|
|
|
|
2016-01-15 14:51:27 +05:30
|
|
|
|
public $rulesAgreement;
|
2016-08-03 18:27:41 +05:30
|
|
|
|
|
2016-05-13 14:33:00 +05:30
|
|
|
|
public $lang;
|
2016-01-15 14:51:27 +05:30
|
|
|
|
|
|
|
|
|
public function rules() {
|
|
|
|
|
return [
|
2016-08-03 18:27:41 +05:30
|
|
|
|
['captcha', ReCaptchaValidator::class],
|
2016-06-17 02:02:23 +05:30
|
|
|
|
['rulesAgreement', 'required', 'message' => E::RULES_AGREEMENT_REQUIRED],
|
2016-01-15 14:51:27 +05:30
|
|
|
|
|
2016-11-02 01:57:38 +05:30
|
|
|
|
['username', UsernameValidator::class],
|
2016-11-02 02:27:42 +05:30
|
|
|
|
['email', EmailValidator::class],
|
2016-01-15 14:51:27 +05:30
|
|
|
|
|
2016-06-17 02:02:23 +05:30
|
|
|
|
['password', 'required', 'message' => E::PASSWORD_REQUIRED],
|
|
|
|
|
['rePassword', 'required', 'message' => E::RE_PASSWORD_REQUIRED],
|
2016-11-02 02:29:43 +05:30
|
|
|
|
['password', PasswordValidator::class],
|
2016-01-15 14:51:27 +05:30
|
|
|
|
['rePassword', 'validatePasswordAndRePasswordMatch'],
|
2016-05-13 14:33:00 +05:30
|
|
|
|
|
|
|
|
|
['lang', LanguageValidator::class],
|
2016-10-27 04:04:51 +05:30
|
|
|
|
['lang', 'default', 'value' => 'en'],
|
2016-01-15 14:51:27 +05:30
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function validatePasswordAndRePasswordMatch($attribute) {
|
|
|
|
|
if (!$this->hasErrors()) {
|
|
|
|
|
if ($this->password !== $this->rePassword) {
|
2016-06-17 02:02:23 +05:30
|
|
|
|
$this->addError($attribute, E::RE_PASSWORD_DOES_NOT_MATCH);
|
2016-01-15 14:51:27 +05:30
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-11-19 21:02:51 +05:30
|
|
|
|
* @CollectModelMetrics(prefix="signup.register")
|
2016-01-15 14:51:27 +05:30
|
|
|
|
* @return Account|null the saved model or null if saving fails
|
2016-07-17 21:12:37 +05:30
|
|
|
|
* @throws Exception
|
2016-01-15 14:51:27 +05:30
|
|
|
|
*/
|
|
|
|
|
public function signup() {
|
2016-10-27 04:04:51 +05:30
|
|
|
|
if (!$this->validate() && !$this->canContinue($this->getFirstErrors())) {
|
2016-01-15 14:51:27 +05:30
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$transaction = Yii::$app->db->beginTransaction();
|
|
|
|
|
try {
|
|
|
|
|
$account = new Account();
|
2016-02-28 17:38:00 +05:30
|
|
|
|
$account->uuid = Uuid::uuid4();
|
2016-01-15 14:51:27 +05:30
|
|
|
|
$account->email = $this->email;
|
|
|
|
|
$account->username = $this->username;
|
|
|
|
|
$account->password = $this->password;
|
2016-05-13 14:33:00 +05:30
|
|
|
|
$account->lang = $this->lang;
|
2016-01-15 14:51:27 +05:30
|
|
|
|
$account->status = Account::STATUS_REGISTERED;
|
2016-08-06 21:22:03 +05:30
|
|
|
|
$account->rules_agreement_version = LATEST_RULES_VERSION;
|
2016-08-18 05:25:52 +05:30
|
|
|
|
$account->setRegistrationIp(Yii::$app->request->getUserIP());
|
2016-11-02 02:32:58 +05:30
|
|
|
|
if (!$account->save()) {
|
2016-01-15 14:51:27 +05:30
|
|
|
|
throw new ErrorException('Account not created.');
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-11 01:10:06 +05:30
|
|
|
|
$emailActivation = new RegistrationConfirmation();
|
2016-01-15 14:51:27 +05:30
|
|
|
|
$emailActivation->account_id = $account->id;
|
|
|
|
|
$emailActivation->key = UserFriendlyRandomKey::make();
|
|
|
|
|
|
|
|
|
|
if (!$emailActivation->save()) {
|
|
|
|
|
throw new ErrorException('Unable save email-activation model.');
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 13:43:43 +05:30
|
|
|
|
$usernamesHistory = new UsernameHistory();
|
|
|
|
|
$usernamesHistory->account_id = $account->id;
|
|
|
|
|
$usernamesHistory->username = $account->username;
|
|
|
|
|
$usernamesHistory->applied_in = $account->created_at;
|
|
|
|
|
if (!$usernamesHistory->save()) {
|
|
|
|
|
throw new ErrorException('Cannot save username history record');
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-21 04:11:43 +05:30
|
|
|
|
EmailHelper::registration($emailActivation);
|
2016-01-15 14:51:27 +05:30
|
|
|
|
|
|
|
|
|
$transaction->commit();
|
2016-07-17 21:12:37 +05:30
|
|
|
|
} catch (Exception $e) {
|
2016-01-15 14:51:27 +05:30
|
|
|
|
$transaction->rollBack();
|
|
|
|
|
throw $e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $account;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-27 04:04:51 +05:30
|
|
|
|
/**
|
|
|
|
|
* Метод проверяет, можно ли занять указанный при регистрации ник или e-mail. Так случается,
|
|
|
|
|
* что пользователи вводят неправильный e-mail или ник, после замечают это и пытаются вновь
|
|
|
|
|
* выпонить регистрацию. Мы не будем им мешать и просто удаляем существующие недозарегистрированные
|
|
|
|
|
* аккаунты, позволяя им зарегистрироваться.
|
|
|
|
|
*
|
|
|
|
|
* @param array $errors массив, где ключ - это поле, а значение - первая ошибка из нашего
|
|
|
|
|
* стандартного словаря ошибок
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
protected function canContinue(array $errors) : bool {
|
|
|
|
|
if (ArrayHelper::getValue($errors, 'username') === E::USERNAME_NOT_AVAILABLE) {
|
|
|
|
|
$duplicatedUsername = Account::findOne([
|
|
|
|
|
'username' => $this->username,
|
|
|
|
|
'status' => Account::STATUS_REGISTERED,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if ($duplicatedUsername !== null) {
|
|
|
|
|
$duplicatedUsername->delete();
|
|
|
|
|
unset($errors['username']);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ArrayHelper::getValue($errors, 'email') === E::EMAIL_NOT_AVAILABLE) {
|
|
|
|
|
$duplicatedEmail = Account::findOne([
|
|
|
|
|
'email' => $this->email,
|
|
|
|
|
'status' => Account::STATUS_REGISTERED,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if ($duplicatedEmail !== null) {
|
|
|
|
|
$duplicatedEmail->delete();
|
|
|
|
|
unset($errors['email']);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return empty($errors);
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-15 14:51:27 +05:30
|
|
|
|
}
|