2016-01-15 14:51:27 +05:30
|
|
|
<?php
|
|
|
|
namespace api\models;
|
|
|
|
|
|
|
|
use api\components\ReCaptcha\Validator as ReCaptchaValidator;
|
|
|
|
use common\components\UserFriendlyRandomKey;
|
|
|
|
use common\models\Account;
|
|
|
|
use common\models\EmailActivation;
|
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-01-21 02:44:29 +05:30
|
|
|
class RegistrationForm extends BaseApiForm {
|
2016-01-15 14:51:27 +05:30
|
|
|
|
|
|
|
public $username;
|
|
|
|
public $email;
|
|
|
|
public $password;
|
|
|
|
public $rePassword;
|
|
|
|
public $rulesAgreement;
|
|
|
|
|
|
|
|
public function rules() {
|
|
|
|
return [
|
2016-02-14 23:20:10 +05:30
|
|
|
['rulesAgreement', 'required', 'message' => 'error.you_must_accept_rules'],
|
2016-01-15 14:51:27 +05:30
|
|
|
[[], 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();
|
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;
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|