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-12-23 03:52:51 +05:30
|
|
|
use api\models\base\ApiForm;
|
2016-10-27 02:36:59 +05:30
|
|
|
use api\models\profile\ChangeUsernameForm;
|
2016-12-23 03:52:51 +05:30
|
|
|
use api\validators\EmailActivationKeyValidator;
|
2016-01-21 02:44:29 +05:30
|
|
|
use common\models\Account;
|
|
|
|
use common\models\EmailActivation;
|
|
|
|
use Yii;
|
|
|
|
use yii\base\ErrorException;
|
|
|
|
|
2016-12-23 03:52:51 +05:30
|
|
|
class ConfirmEmailForm extends ApiForm {
|
2016-01-21 02:44:29 +05:30
|
|
|
|
2016-12-23 03:52:51 +05:30
|
|
|
public $key;
|
|
|
|
|
|
|
|
public function rules() {
|
|
|
|
return [
|
|
|
|
['key', EmailActivationKeyValidator::class, 'type' => EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \api\components\User\LoginResult|bool
|
|
|
|
* @throws ErrorException
|
|
|
|
*/
|
2016-01-21 02:44:29 +05:30
|
|
|
public function confirm() {
|
|
|
|
if (!$this->validate()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-23 03:52:51 +05:30
|
|
|
$transaction = Yii::$app->db->beginTransaction();
|
|
|
|
|
|
|
|
/** @var \common\models\confirmations\RegistrationConfirmation $confirmModel */
|
|
|
|
$confirmModel = $this->key;
|
|
|
|
$account = $confirmModel->account;
|
|
|
|
$account->status = Account::STATUS_ACTIVE;
|
|
|
|
if (!$confirmModel->delete()) {
|
|
|
|
throw new ErrorException('Unable remove activation key.');
|
2016-01-21 02:44:29 +05:30
|
|
|
}
|
|
|
|
|
2016-12-23 03:52:51 +05:30
|
|
|
if (!$account->save()) {
|
|
|
|
throw new ErrorException('Unable activate user account.');
|
2016-01-21 02:44:29 +05:30
|
|
|
}
|
|
|
|
|
2016-12-23 03:52:51 +05:30
|
|
|
$changeUsernameForm = new ChangeUsernameForm();
|
|
|
|
$changeUsernameForm->createEventTask($account->id, $account->username, null);
|
|
|
|
|
|
|
|
$transaction->commit();
|
2016-05-30 05:14:17 +05:30
|
|
|
|
2016-12-23 03:52:51 +05:30
|
|
|
return Yii::$app->user->login(new AccountIdentity($account->attributes), true);
|
2016-01-21 02:44:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|