mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
#274: упразднена базовая модель KeyConfirmationForm, внедрён обновлённый валидатор EmailActivationKeyValidator
This commit is contained in:
@ -2,56 +2,53 @@
|
||||
namespace api\models\authentication;
|
||||
|
||||
use api\models\AccountIdentity;
|
||||
use api\models\base\KeyConfirmationForm;
|
||||
use api\models\base\ApiForm;
|
||||
use api\models\profile\ChangeUsernameForm;
|
||||
use api\validators\EmailActivationKeyValidator;
|
||||
use common\models\Account;
|
||||
use common\models\EmailActivation;
|
||||
use Yii;
|
||||
use yii\base\ErrorException;
|
||||
|
||||
class ConfirmEmailForm extends KeyConfirmationForm {
|
||||
class ConfirmEmailForm extends ApiForm {
|
||||
|
||||
public $key;
|
||||
|
||||
public function rules() {
|
||||
return [
|
||||
['key', EmailActivationKeyValidator::class, 'type' => EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \api\components\User\LoginResult|bool
|
||||
* @throws ErrorException
|
||||
*/
|
||||
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.');
|
||||
}
|
||||
|
||||
$changeUsernameForm = new ChangeUsernameForm();
|
||||
$changeUsernameForm->createEventTask($account->id, $account->username, null);
|
||||
|
||||
$transaction->commit();
|
||||
} catch (ErrorException $e) {
|
||||
$transaction->rollBack();
|
||||
if (YII_DEBUG) {
|
||||
throw $e;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
/** @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.');
|
||||
}
|
||||
|
||||
/** @var \api\components\User\Component $component */
|
||||
$component = Yii::$app->user;
|
||||
if (!$account->save()) {
|
||||
throw new ErrorException('Unable activate user account.');
|
||||
}
|
||||
|
||||
return $component->login(new AccountIdentity($account->attributes), true);
|
||||
$changeUsernameForm = new ChangeUsernameForm();
|
||||
$changeUsernameForm->createEventTask($account->id, $account->username, null);
|
||||
|
||||
$transaction->commit();
|
||||
|
||||
return Yii::$app->user->login(new AccountIdentity($account->attributes), true);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user