mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Cleanup User Component, update tests
This commit is contained in:
@ -1,4 +1,6 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace api\models\authentication;
|
||||
|
||||
use api\aop\annotations\CollectModelMetrics;
|
||||
@ -7,8 +9,8 @@ use api\validators\EmailActivationKeyValidator;
|
||||
use common\helpers\Error as E;
|
||||
use common\models\EmailActivation;
|
||||
use common\validators\PasswordValidator;
|
||||
use Webmozart\Assert\Assert;
|
||||
use Yii;
|
||||
use yii\base\ErrorException;
|
||||
|
||||
class RecoverPasswordForm extends ApiForm {
|
||||
|
||||
@ -18,7 +20,7 @@ class RecoverPasswordForm extends ApiForm {
|
||||
|
||||
public $newRePassword;
|
||||
|
||||
public function rules() {
|
||||
public function rules(): array {
|
||||
return [
|
||||
['key', EmailActivationKeyValidator::class, 'type' => EmailActivation::TYPE_FORGOT_PASSWORD_KEY],
|
||||
['newPassword', 'required', 'message' => E::NEW_PASSWORD_REQUIRED],
|
||||
@ -28,18 +30,16 @@ class RecoverPasswordForm extends ApiForm {
|
||||
];
|
||||
}
|
||||
|
||||
public function validatePasswordAndRePasswordMatch($attribute) {
|
||||
if (!$this->hasErrors()) {
|
||||
if ($this->newPassword !== $this->newRePassword) {
|
||||
$this->addError($attribute, E::NEW_RE_PASSWORD_DOES_NOT_MATCH);
|
||||
}
|
||||
public function validatePasswordAndRePasswordMatch(string $attribute): void {
|
||||
if (!$this->hasErrors() && $this->newPassword !== $this->newRePassword) {
|
||||
$this->addError($attribute, E::NEW_RE_PASSWORD_DOES_NOT_MATCH);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @CollectModelMetrics(prefix="authentication.recoverPassword")
|
||||
* @return \api\components\User\AuthenticationResult|bool
|
||||
* @throws ErrorException
|
||||
* @throws \Throwable
|
||||
*/
|
||||
public function recoverPassword() {
|
||||
if (!$this->validate()) {
|
||||
@ -52,17 +52,16 @@ class RecoverPasswordForm extends ApiForm {
|
||||
$confirmModel = $this->key;
|
||||
$account = $confirmModel->account;
|
||||
$account->password = $this->newPassword;
|
||||
if (!$confirmModel->delete()) {
|
||||
throw new ErrorException('Unable remove activation key.');
|
||||
}
|
||||
Assert::notSame($confirmModel->delete(), false, 'Unable remove activation key.');
|
||||
|
||||
if (!$account->save(false)) {
|
||||
throw new ErrorException('Unable activate user account.');
|
||||
}
|
||||
Assert::true($account->save(), 'Unable activate user account.');
|
||||
|
||||
$token = Yii::$app->user->createJwtAuthenticationToken($account);
|
||||
$jwt = Yii::$app->user->serializeToken($token);
|
||||
|
||||
$transaction->commit();
|
||||
|
||||
return Yii::$app->user->createJwtAuthenticationToken($account, false);
|
||||
return new \api\components\User\AuthenticationResult($account, $jwt, null);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user