2016-01-21 02:44:29 +05:30
|
|
|
<?php
|
2018-07-08 20:50:19 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-05-14 05:17:17 +05:30
|
|
|
namespace api\models\authentication;
|
2016-01-21 02:44:29 +05:30
|
|
|
|
2017-11-19 21:02:51 +05:30
|
|
|
use api\aop\annotations\CollectModelMetrics;
|
2016-12-23 03:52:51 +05:30
|
|
|
use api\models\base\ApiForm;
|
|
|
|
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;
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function rules(): array {
|
2016-12-23 03:52:51 +05:30
|
|
|
return [
|
|
|
|
['key', EmailActivationKeyValidator::class, 'type' => EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-11-19 21:02:51 +05:30
|
|
|
* @CollectModelMetrics(prefix="signup.confirmEmail")
|
2017-09-19 22:36:16 +05:30
|
|
|
* @return \api\components\User\AuthenticationResult|bool
|
2016-12-23 03:52:51 +05:30
|
|
|
* @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
|
|
|
$transaction->commit();
|
2016-05-30 05:14:17 +05:30
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
return Yii::$app->user->createJwtAuthenticationToken($account, true);
|
2016-01-21 02:44:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|