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
|
|
|
|
|
|
|
|
|
use api\components\ReCaptcha\Validator as ReCaptchaValidator;
|
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-01-15 14:51:27 +05:30
|
|
|
|
use common\models\EmailActivation;
|
2016-09-21 13:43:43 +05:30
|
|
|
|
use common\models\UsernameHistory;
|
2016-05-13 14:33:00 +05:30
|
|
|
|
use common\validators\LanguageValidator;
|
2016-05-12 03:43:19 +05:30
|
|
|
|
use common\validators\PasswordValidate;
|
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-04-15 09:36:21 +05:30
|
|
|
|
use yii\base\InvalidConfigException;
|
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-03-20 04:55:26 +05:30
|
|
|
|
['email', 'validateEmail', 'skipOnEmpty' => false],
|
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-05-12 03:43:19 +05:30
|
|
|
|
['password', PasswordValidate::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
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-20 04:55:26 +05:30
|
|
|
|
public function validateEmail() {
|
|
|
|
|
$account = new Account();
|
|
|
|
|
$account->email = $this->email;
|
|
|
|
|
if (!$account->validate(['email'])) {
|
|
|
|
|
$this->addErrors($account->getErrors());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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-10-27 04:04:51 +05:30
|
|
|
|
if (!$account->save(false)) {
|
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');
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-13 04:49:00 +05:30
|
|
|
|
$this->sendMail($emailActivation, $account);
|
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-03-13 04:49:00 +05:30
|
|
|
|
// TODO: подумать, чтобы вынести этот метод в какую-то отдельную конструкцию, т.к. используется и внутри NewAccountActivationForm
|
|
|
|
|
public function sendMail(EmailActivation $emailActivation, Account $account) {
|
|
|
|
|
/** @var \yii\swiftmailer\Mailer $mailer */
|
|
|
|
|
$mailer = Yii::$app->mailer;
|
2016-04-15 09:36:21 +05:30
|
|
|
|
$fromEmail = Yii::$app->params['fromEmail'];
|
|
|
|
|
|
|
|
|
|
if (!$fromEmail) {
|
|
|
|
|
throw new InvalidConfigException('Please specify fromEmail app in app params');
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-02 15:22:17 +05:30
|
|
|
|
$htmlBody = Yii::$app->emailRenderer->getTemplate('register')
|
2016-09-13 17:20:22 +05:30
|
|
|
|
->setLocale($account->lang)
|
|
|
|
|
->setParams([
|
|
|
|
|
'username' => $account->username,
|
2016-10-04 04:15:50 +05:30
|
|
|
|
'code' => $emailActivation->key,
|
2016-09-13 17:20:22 +05:30
|
|
|
|
'link' => Yii::$app->request->getHostInfo() . '/activation/' . $emailActivation->key,
|
2016-03-13 04:49:00 +05:30
|
|
|
|
])
|
2016-09-13 17:20:22 +05:30
|
|
|
|
->render();
|
|
|
|
|
|
|
|
|
|
/** @var \yii\swiftmailer\Message $message */
|
|
|
|
|
$message = $mailer->compose()
|
|
|
|
|
->setHtmlBody($htmlBody)
|
2016-03-13 04:49:00 +05:30
|
|
|
|
->setTo([$account->email => $account->username])
|
2016-04-15 09:36:21 +05:30
|
|
|
|
->setFrom([$fromEmail => 'Ely.by Accounts'])
|
2016-03-13 04:49:00 +05:30
|
|
|
|
->setSubject('Ely.by Account registration');
|
|
|
|
|
|
|
|
|
|
if (!$message->send()) {
|
|
|
|
|
throw new ErrorException('Unable send email with activation code.');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|