mirror of
				https://github.com/elyby/accounts.git
				synced 2025-05-31 14:11:46 +05:30 
			
		
		
		
	Добавлена аннотация для сбора метрик из моделей Yii2 и интегрирована в формы авторизации и регистрации
This commit is contained in:
		@@ -9,6 +9,7 @@ class AspectKernel extends BaseAspectKernel {
 | 
			
		||||
 | 
			
		||||
    protected function configureAop(AspectContainer $container): void {
 | 
			
		||||
        $container->registerAspect(new aspects\MockDataAspect());
 | 
			
		||||
        $container->registerAspect(new aspects\CollectMetricsAspect());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										19
									
								
								api/aop/annotations/CollectModelMetrics.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								api/aop/annotations/CollectModelMetrics.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,19 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace api\aop\annotations;
 | 
			
		||||
 | 
			
		||||
use Doctrine\Common\Annotations\Annotation;
 | 
			
		||||
use Doctrine\Common\Annotations\Annotation\Required;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @Annotation
 | 
			
		||||
 * @Target("METHOD")
 | 
			
		||||
 */
 | 
			
		||||
class CollectModelMetrics {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @Required()
 | 
			
		||||
     * @var string задаёт префикс для отправки метрик. Задаётся без ведущей и без завершающей точки.
 | 
			
		||||
     */
 | 
			
		||||
    public $prefix = '';
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										41
									
								
								api/aop/aspects/CollectMetricsAspect.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								api/aop/aspects/CollectMetricsAspect.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,41 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace api\aop\aspects;
 | 
			
		||||
 | 
			
		||||
use api\aop\annotations\CollectModelMetrics;
 | 
			
		||||
use Go\Aop\Aspect;
 | 
			
		||||
use Go\Aop\Intercept\MethodInvocation;
 | 
			
		||||
use Go\Lang\Annotation\Around;
 | 
			
		||||
use Yii;
 | 
			
		||||
 | 
			
		||||
class CollectMetricsAspect implements Aspect {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @param MethodInvocation $invocation Invocation
 | 
			
		||||
     * @Around("@execution(api\aop\annotations\CollectModelMetrics)")
 | 
			
		||||
     */
 | 
			
		||||
    public function sendMetrics(MethodInvocation $invocation) {
 | 
			
		||||
        /** @var CollectModelMetrics $annotation */
 | 
			
		||||
        $annotation = $invocation->getMethod()->getAnnotation(CollectModelMetrics::class);
 | 
			
		||||
        $prefix = trim($annotation->prefix, '.');
 | 
			
		||||
 | 
			
		||||
        Yii::$app->statsd->inc($prefix . '.attempt');
 | 
			
		||||
        $result = $invocation->proceed();
 | 
			
		||||
        if ($result !== false) {
 | 
			
		||||
            Yii::$app->statsd->inc($prefix . '.success');
 | 
			
		||||
            return $result;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /** @var \yii\base\Model $model */
 | 
			
		||||
        $model = $invocation->getThis();
 | 
			
		||||
        $errors = array_values($model->getFirstErrors());
 | 
			
		||||
        if (!isset($errors[0])) {
 | 
			
		||||
            Yii::error('Unsuccess result with empty errors list');
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        Yii::$app->statsd->inc($prefix . '.' . $errors[0]);
 | 
			
		||||
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -1,6 +1,7 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace api\models\authentication;
 | 
			
		||||
 | 
			
		||||
use api\aop\annotations\CollectModelMetrics;
 | 
			
		||||
use api\models\base\ApiForm;
 | 
			
		||||
use api\modules\accounts\models\ChangeUsernameForm;
 | 
			
		||||
use api\validators\EmailActivationKeyValidator;
 | 
			
		||||
@@ -20,6 +21,7 @@ class ConfirmEmailForm extends ApiForm {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @CollectModelMetrics(prefix="signup.confirmEmail")
 | 
			
		||||
     * @return \api\components\User\AuthenticationResult|bool
 | 
			
		||||
     * @throws ErrorException
 | 
			
		||||
     */
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,7 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace api\models\authentication;
 | 
			
		||||
 | 
			
		||||
use api\aop\annotations\CollectModelMetrics;
 | 
			
		||||
use api\components\ReCaptcha\Validator as ReCaptchaValidator;
 | 
			
		||||
use api\models\base\ApiForm;
 | 
			
		||||
use common\emails\EmailHelper;
 | 
			
		||||
@@ -55,6 +56,11 @@ class ForgotPasswordForm extends ApiForm {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @CollectModelMetrics(prefix="authentication.forgotPassword")
 | 
			
		||||
     * @return bool
 | 
			
		||||
     * @throws ErrorException
 | 
			
		||||
     */
 | 
			
		||||
    public function forgotPassword() {
 | 
			
		||||
        if (!$this->validate()) {
 | 
			
		||||
            return false;
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,7 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace api\models\authentication;
 | 
			
		||||
 | 
			
		||||
use api\aop\annotations\CollectModelMetrics;
 | 
			
		||||
use api\models\base\ApiForm;
 | 
			
		||||
use api\validators\TotpValidator;
 | 
			
		||||
use common\helpers\Error as E;
 | 
			
		||||
@@ -87,6 +88,7 @@ class LoginForm extends ApiForm {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @CollectModelMetrics(prefix="authentication.login")
 | 
			
		||||
     * @return \api\components\User\AuthenticationResult|bool
 | 
			
		||||
     */
 | 
			
		||||
    public function login() {
 | 
			
		||||
 
 | 
			
		||||
@@ -1,12 +1,18 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace api\models\authentication;
 | 
			
		||||
 | 
			
		||||
use api\aop\annotations\CollectModelMetrics;
 | 
			
		||||
use api\models\base\ApiForm;
 | 
			
		||||
use Yii;
 | 
			
		||||
 | 
			
		||||
class LogoutForm extends ApiForm {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @CollectModelMetrics(prefix="authentication.logout")
 | 
			
		||||
     * @return bool
 | 
			
		||||
     */
 | 
			
		||||
    public function logout() : bool {
 | 
			
		||||
        $component = \Yii::$app->user;
 | 
			
		||||
        $component = Yii::$app->user;
 | 
			
		||||
        $session = $component->getActiveSession();
 | 
			
		||||
        if ($session === null) {
 | 
			
		||||
            return true;
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,7 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace api\models\authentication;
 | 
			
		||||
 | 
			
		||||
use api\aop\annotations\CollectModelMetrics;
 | 
			
		||||
use api\models\base\ApiForm;
 | 
			
		||||
use api\validators\EmailActivationKeyValidator;
 | 
			
		||||
use common\helpers\Error as E;
 | 
			
		||||
@@ -36,6 +37,7 @@ class RecoverPasswordForm extends ApiForm {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @CollectModelMetrics(prefix="authentication.recoverPassword")
 | 
			
		||||
     * @return \api\components\User\AuthenticationResult|bool
 | 
			
		||||
     * @throws ErrorException
 | 
			
		||||
     */
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,7 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace api\models\authentication;
 | 
			
		||||
 | 
			
		||||
use api\aop\annotations\CollectModelMetrics;
 | 
			
		||||
use api\models\base\ApiForm;
 | 
			
		||||
use common\helpers\Error as E;
 | 
			
		||||
use common\models\AccountSession;
 | 
			
		||||
@@ -32,6 +33,7 @@ class RefreshTokenForm extends ApiForm {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @CollectModelMetrics(prefix="authentication.renew")
 | 
			
		||||
     * @return \api\components\User\AuthenticationResult|bool
 | 
			
		||||
     */
 | 
			
		||||
    public function renew() {
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,7 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace api\models\authentication;
 | 
			
		||||
 | 
			
		||||
use api\aop\annotations\CollectModelMetrics;
 | 
			
		||||
use api\components\ReCaptcha\Validator as ReCaptchaValidator;
 | 
			
		||||
use common\emails\EmailHelper;
 | 
			
		||||
use api\models\base\ApiForm;
 | 
			
		||||
@@ -63,6 +64,7 @@ class RegistrationForm extends ApiForm {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @CollectModelMetrics(prefix="signup.register")
 | 
			
		||||
     * @return Account|null the saved model or null if saving fails
 | 
			
		||||
     * @throws Exception
 | 
			
		||||
     */
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,7 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace api\models\authentication;
 | 
			
		||||
 | 
			
		||||
use api\aop\annotations\CollectModelMetrics;
 | 
			
		||||
use api\components\ReCaptcha\Validator as ReCaptchaValidator;
 | 
			
		||||
use common\emails\EmailHelper;
 | 
			
		||||
use api\models\base\ApiForm;
 | 
			
		||||
@@ -53,6 +54,11 @@ class RepeatAccountActivationForm extends ApiForm {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @CollectModelMetrics(prefix="signup.repeatEmail")
 | 
			
		||||
     * @return bool
 | 
			
		||||
     * @throws ErrorException
 | 
			
		||||
     */
 | 
			
		||||
    public function sendRepeatMessage() {
 | 
			
		||||
        if (!$this->validate()) {
 | 
			
		||||
            return false;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user