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-01-15 14:51:27 +05:30
|
|
|
use common\components\UserFriendlyRandomKey;
|
2018-04-18 02:17:25 +05:30
|
|
|
use common\helpers\Error as E;
|
2016-01-15 14:51:27 +05:30
|
|
|
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;
|
2017-11-26 07:14:41 +05:30
|
|
|
use common\tasks\SendRegistrationEmail;
|
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
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function validatePasswordAndRePasswordMatch($attribute): void {
|
2016-01-15 14:51:27 +05:30
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function signup() {
|
2016-10-27 04:04:51 +05:30
|
|
|
if (!$this->validate() && !$this->canContinue($this->getFirstErrors())) {
|
2024-12-02 15:40:55 +05:30
|
|
|
return;
|
2016-01-15 14:51:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
$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-11-26 07:14:41 +05:30
|
|
|
Yii::$app->queue->push(SendRegistrationEmail::createFromConfirmation($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
|
|
|
/**
|
2019-07-15 04:29:56 +05:30
|
|
|
* The method checks whether the username or E-mail specified during registration
|
|
|
|
* can be occupied. It happens that users enter the wrong E-mail or username,
|
|
|
|
* then notice it and try to re-register. We'll not interfere with them
|
|
|
|
* and simply delete existing not-finished-registration account,
|
|
|
|
* allowing them to take it.
|
2016-10-27 04:04:51 +05:30
|
|
|
*
|
2019-07-15 04:29:56 +05:30
|
|
|
* @param array $errors an array where the key is a field and the value is
|
|
|
|
* the first error from our standard error dictionary
|
2016-10-27 04:04:51 +05:30
|
|
|
* @return bool
|
|
|
|
*/
|
2018-04-18 02:17:25 +05:30
|
|
|
protected function canContinue(array $errors): bool {
|
2016-10-27 04:04:51 +05:30
|
|
|
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
|
|
|
}
|