mirror of
				https://github.com/elyby/accounts.git
				synced 2025-05-31 14:11:46 +05:30 
			
		
		
		
	Refactor all JWT-related components Replace RS256 with ES256 as a preferred JWT algorithm
		
			
				
	
	
		
			67 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			67 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
declare(strict_types=1);
 | 
						|
 | 
						|
namespace api\models\authentication;
 | 
						|
 | 
						|
use api\aop\annotations\CollectModelMetrics;
 | 
						|
use api\components\Tokens\TokensFactory;
 | 
						|
use api\models\base\ApiForm;
 | 
						|
use api\validators\EmailActivationKeyValidator;
 | 
						|
use common\helpers\Error as E;
 | 
						|
use common\models\EmailActivation;
 | 
						|
use common\validators\PasswordValidator;
 | 
						|
use Webmozart\Assert\Assert;
 | 
						|
use Yii;
 | 
						|
 | 
						|
class RecoverPasswordForm extends ApiForm {
 | 
						|
 | 
						|
    public $key;
 | 
						|
 | 
						|
    public $newPassword;
 | 
						|
 | 
						|
    public $newRePassword;
 | 
						|
 | 
						|
    public function rules(): array {
 | 
						|
        return [
 | 
						|
            ['key', EmailActivationKeyValidator::class, 'type' => EmailActivation::TYPE_FORGOT_PASSWORD_KEY],
 | 
						|
            ['newPassword', 'required', 'message' => E::NEW_PASSWORD_REQUIRED],
 | 
						|
            ['newRePassword', 'required', 'message' => E::NEW_RE_PASSWORD_REQUIRED],
 | 
						|
            ['newPassword', PasswordValidator::class],
 | 
						|
            ['newRePassword', 'validatePasswordAndRePasswordMatch'],
 | 
						|
        ];
 | 
						|
    }
 | 
						|
 | 
						|
    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")
 | 
						|
     */
 | 
						|
    public function recoverPassword(): ?AuthenticationResult {
 | 
						|
        if (!$this->validate()) {
 | 
						|
            return null;
 | 
						|
        }
 | 
						|
 | 
						|
        $transaction = Yii::$app->db->beginTransaction();
 | 
						|
 | 
						|
        /** @var \common\models\confirmations\ForgotPassword $confirmModel */
 | 
						|
        $confirmModel = $this->key;
 | 
						|
        $account = $confirmModel->account;
 | 
						|
        $account->password = $this->newPassword;
 | 
						|
        /** @noinspection PhpUnhandledExceptionInspection */
 | 
						|
        Assert::notSame($confirmModel->delete(), false, 'Unable remove activation key.');
 | 
						|
 | 
						|
        Assert::true($account->save(), 'Unable activate user account.');
 | 
						|
 | 
						|
        $token = TokensFactory::createForAccount($account);
 | 
						|
 | 
						|
        $transaction->commit();
 | 
						|
 | 
						|
        return new AuthenticationResult($token);
 | 
						|
    }
 | 
						|
 | 
						|
}
 |