2016-01-21 02:44:29 +05:30
|
|
|
<?php
|
2016-05-14 05:17:17 +05:30
|
|
|
namespace api\models\authentication;
|
2016-01-21 02:44:29 +05:30
|
|
|
|
2016-05-30 05:14:17 +05:30
|
|
|
use api\models\AccountIdentity;
|
2016-03-20 05:03:49 +05:30
|
|
|
use api\models\base\KeyConfirmationForm;
|
2016-10-27 02:36:59 +05:30
|
|
|
use api\models\profile\ChangeUsernameForm;
|
2016-01-21 02:44:29 +05:30
|
|
|
use common\models\Account;
|
|
|
|
use common\models\EmailActivation;
|
|
|
|
use Yii;
|
|
|
|
use yii\base\ErrorException;
|
|
|
|
|
2016-03-20 05:03:49 +05:30
|
|
|
class ConfirmEmailForm extends KeyConfirmationForm {
|
2016-01-21 02:44:29 +05:30
|
|
|
|
|
|
|
public function confirm() {
|
|
|
|
if (!$this->validate()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$confirmModel = $this->getActivationCodeModel();
|
|
|
|
if ($confirmModel->type != EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION) {
|
|
|
|
$confirmModel->delete();
|
|
|
|
// TODO: вот где-то здесь нужно ещё попутно сгенерировать соответствующую ошибку
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$transaction = Yii::$app->db->beginTransaction();
|
|
|
|
try {
|
|
|
|
$account = $confirmModel->account;
|
|
|
|
$account->status = Account::STATUS_ACTIVE;
|
|
|
|
if (!$confirmModel->delete()) {
|
|
|
|
throw new ErrorException('Unable remove activation key.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$account->save()) {
|
|
|
|
throw new ErrorException('Unable activate user account.');
|
|
|
|
}
|
|
|
|
|
2016-10-27 02:36:59 +05:30
|
|
|
$changeUsernameForm = new ChangeUsernameForm();
|
|
|
|
$changeUsernameForm->createEventTask($account->id, $account->username, null);
|
|
|
|
|
2016-01-21 02:44:29 +05:30
|
|
|
$transaction->commit();
|
|
|
|
} catch (ErrorException $e) {
|
|
|
|
$transaction->rollBack();
|
2016-02-27 04:07:55 +05:30
|
|
|
if (YII_DEBUG) {
|
|
|
|
throw $e;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-21 02:44:29 +05:30
|
|
|
}
|
|
|
|
|
2016-05-30 05:14:17 +05:30
|
|
|
/** @var \api\components\User\Component $component */
|
|
|
|
$component = Yii::$app->user;
|
|
|
|
|
|
|
|
return $component->login(new AccountIdentity($account->attributes), true);
|
2016-01-21 02:44:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|