2016-01-15 14:51:27 +05:30
|
|
|
<?php
|
|
|
|
namespace api\models;
|
|
|
|
|
|
|
|
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;
|
|
|
|
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-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-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
|
|
|
|
|
|
|
public $username;
|
|
|
|
public $email;
|
|
|
|
public $password;
|
|
|
|
public $rePassword;
|
|
|
|
public $rulesAgreement;
|
|
|
|
|
|
|
|
public function rules() {
|
|
|
|
return [
|
|
|
|
[[], ReCaptchaValidator::class, 'message' => 'error.captcha_invalid', 'when' => !YII_ENV_TEST],
|
2016-03-20 04:55:26 +05:30
|
|
|
['rulesAgreement', 'required', 'message' => 'error.you_must_accept_rules'],
|
2016-01-15 14:51:27 +05:30
|
|
|
|
2016-03-20 04:55:26 +05:30
|
|
|
['username', 'validateUsername', 'skipOnEmpty' => false],
|
|
|
|
['email', 'validateEmail', 'skipOnEmpty' => false],
|
2016-01-15 14:51:27 +05:30
|
|
|
|
|
|
|
['password', 'required', 'message' => 'error.password_required'],
|
|
|
|
['rePassword', 'required', 'message' => 'error.rePassword_required'],
|
|
|
|
['password', 'string', 'min' => 8, 'tooShort' => 'error.password_too_short'],
|
|
|
|
['rePassword', 'validatePasswordAndRePasswordMatch'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-03-20 04:55:26 +05:30
|
|
|
public function validateUsername() {
|
|
|
|
$account = new Account();
|
|
|
|
$account->username = $this->username;
|
|
|
|
if (!$account->validate(['username'])) {
|
|
|
|
$this->addErrors($account->getErrors());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
$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;
|
|
|
|
if (!$account->save()) {
|
|
|
|
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-03-13 04:49:00 +05:30
|
|
|
$this->sendMail($emailActivation, $account);
|
2016-01-15 14:51:27 +05:30
|
|
|
|
|
|
|
$transaction->commit();
|
|
|
|
} catch (ErrorException $e) {
|
|
|
|
$transaction->rollBack();
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
|
2016-04-24 00:14:10 +05:30
|
|
|
$changeUsernameForm = new ChangeUsernameForm();
|
|
|
|
$changeUsernameForm->createTask($account->id, $account->username, null);
|
|
|
|
|
2016-01-15 14:51:27 +05:30
|
|
|
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-03-13 04:49:00 +05:30
|
|
|
/** @var \yii\swiftmailer\Message $message */
|
|
|
|
$message = $mailer->compose([
|
|
|
|
'html' => '@app/mails/registration-confirmation-html',
|
|
|
|
'text' => '@app/mails/registration-confirmation-text',
|
|
|
|
], [
|
|
|
|
'key' => $emailActivation->key,
|
|
|
|
])
|
|
|
|
->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-01-15 14:51:27 +05:30
|
|
|
}
|