mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Тестовые данные теперь интегрируются через аспектную библиотеку
This commit is contained in:
@@ -8,6 +8,7 @@ use Go\Core\AspectKernel as BaseAspectKernel;
|
|||||||
class AspectKernel extends BaseAspectKernel {
|
class AspectKernel extends BaseAspectKernel {
|
||||||
|
|
||||||
protected function configureAop(AspectContainer $container): void {
|
protected function configureAop(AspectContainer $container): void {
|
||||||
|
$container->registerAspect(new aspects\MockDataAspect());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
177
api/aop/aspects/MockDataAspect.php
Normal file
177
api/aop/aspects/MockDataAspect.php
Normal file
@@ -0,0 +1,177 @@
|
|||||||
|
<?php
|
||||||
|
namespace api\aop\aspects;
|
||||||
|
|
||||||
|
use Go\Aop\Aspect;
|
||||||
|
use Go\Aop\Intercept\MethodInvocation;
|
||||||
|
use Go\Lang\Annotation\Around;
|
||||||
|
use Yii;
|
||||||
|
use yii\web\Request;
|
||||||
|
|
||||||
|
class MockDataAspect implements Aspect {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param MethodInvocation $invocation Invocation
|
||||||
|
* @Around("execution(public api\controllers\SignupController->actionIndex(*))")
|
||||||
|
*/
|
||||||
|
public function beforeSignup(MethodInvocation $invocation) {
|
||||||
|
$email = $this->getRequest()->post('email');
|
||||||
|
if ($email === 'let-me-register@ely.by') {
|
||||||
|
return ['success' => true];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $invocation->proceed();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param MethodInvocation $invocation
|
||||||
|
* @Around("execution(public api\controllers\SignupController->actionRepeatMessage(*))")
|
||||||
|
*/
|
||||||
|
public function beforeRepeatMessage(MethodInvocation $invocation) {
|
||||||
|
$email = $this->getRequest()->post('email');
|
||||||
|
if ($email === 'let-me-register@ely.by' || $email === 'let-me-repeat@ely.by') {
|
||||||
|
return ['success' => true];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $invocation->proceed();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param MethodInvocation $invocation
|
||||||
|
* @Around("execution(public api\controllers\SignupController->actionConfirm(*))")
|
||||||
|
*/
|
||||||
|
public function beforeSignupConfirm(MethodInvocation $invocation) {
|
||||||
|
$email = $this->getRequest()->post('key');
|
||||||
|
if ($email === 'LETMEIN') {
|
||||||
|
return [
|
||||||
|
'success' => true,
|
||||||
|
'access_token' => 'dummy_token',
|
||||||
|
'expires_in' => time() + 60,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $invocation->proceed();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param MethodInvocation $invocation
|
||||||
|
* @Around("execution(public api\controllers\AuthenticationController->actionForgotPassword(*))")
|
||||||
|
*/
|
||||||
|
public function beforeForgotPassword(MethodInvocation $invocation) {
|
||||||
|
$login = $this->getRequest()->post('login');
|
||||||
|
if ($login === 'let-me-recover@ely.by') {
|
||||||
|
return [
|
||||||
|
'success' => true,
|
||||||
|
'data' => [
|
||||||
|
'canRepeatIn' => time() + 60,
|
||||||
|
'repeatFrequency' => 60,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $invocation->proceed();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param MethodInvocation $invocation
|
||||||
|
* @Around("execution(public api\controllers\AuthenticationController->actionRecoverPassword(*))")
|
||||||
|
*/
|
||||||
|
public function beforeRecoverPassword(MethodInvocation $invocation) {
|
||||||
|
$key = $this->getRequest()->post('key');
|
||||||
|
if ($key === 'LETMEIN') {
|
||||||
|
return [
|
||||||
|
'success' => true,
|
||||||
|
'access_token' => 'dummy_token',
|
||||||
|
'expires_in' => time() + 60,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $invocation->proceed();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param MethodInvocation $invocation
|
||||||
|
* @Around("execution(public api\modules\accounts\controllers\DefaultController->actionGet(*))")
|
||||||
|
*/
|
||||||
|
public function beforeAccountGet(MethodInvocation $invocation) {
|
||||||
|
$httpAuth = $this->getRequest()->getHeaders()->get('authorization');
|
||||||
|
if ($httpAuth === 'Bearer dummy_token') {
|
||||||
|
return [
|
||||||
|
'id' => 1,
|
||||||
|
'uuid' => 'f63cd5e1-680f-4c2d-baa2-cc7bb174b71a',
|
||||||
|
'username' => 'dummy',
|
||||||
|
'isOtpEnabled' => false,
|
||||||
|
'registeredAt' => time(),
|
||||||
|
'lang' => 'en',
|
||||||
|
'elyProfileLink' => 'http://ely.by/u1',
|
||||||
|
'email' => 'let-me-register@ely.by',
|
||||||
|
'isActive' => true,
|
||||||
|
'passwordChangedAt' => time(),
|
||||||
|
'hasMojangUsernameCollision' => false,
|
||||||
|
'shouldAcceptRules' => false,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $invocation->proceed();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param MethodInvocation $invocation
|
||||||
|
* @Around("execution(public api\modules\accounts\actions\EmailVerificationAction->run(*))")
|
||||||
|
*/
|
||||||
|
public function beforeAccountEmailVerification(MethodInvocation $invocation) {
|
||||||
|
$httpAuth = $this->getRequest()->getHeaders()->get('authorization');
|
||||||
|
if ($httpAuth === 'Bearer dummy_token') {
|
||||||
|
$password = $this->getRequest()->post('password');
|
||||||
|
if (empty($password)) {
|
||||||
|
return [
|
||||||
|
'success' => false,
|
||||||
|
'errors' => [
|
||||||
|
'password' => 'error.password_required',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'success' => true,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $invocation->proceed();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param MethodInvocation $invocation
|
||||||
|
* @Around("execution(public api\modules\accounts\actions\NewEmailVerificationAction->run(*))")
|
||||||
|
*/
|
||||||
|
public function beforeAccountNewEmailVerification(MethodInvocation $invocation) {
|
||||||
|
$key = $this->getRequest()->post('key');
|
||||||
|
if ($key === 'LETMEIN') {
|
||||||
|
return [
|
||||||
|
'success' => true,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $invocation->proceed();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param MethodInvocation $invocation
|
||||||
|
* @Around("execution(public api\modules\accounts\actions\ChangeEmailAction->run(*))")
|
||||||
|
*/
|
||||||
|
public function beforeAccountChangeEmail(MethodInvocation $invocation) {
|
||||||
|
$key = $this->getRequest()->post('key');
|
||||||
|
if ($key === 'LETMEIN') {
|
||||||
|
return [
|
||||||
|
'success' => true,
|
||||||
|
'email' => 'brand-new-email@ely.by',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $invocation->proceed();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getRequest(): Request {
|
||||||
|
return Yii::$app->getRequest();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -1,174 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace api\components;
|
|
||||||
|
|
||||||
use Closure;
|
|
||||||
use Yii;
|
|
||||||
use yii\base\ActionEvent;
|
|
||||||
use yii\helpers\Json;
|
|
||||||
use yii\web\Request;
|
|
||||||
|
|
||||||
class TestData {
|
|
||||||
|
|
||||||
private const MAP = [
|
|
||||||
'signup/index' => 'beforeSignup',
|
|
||||||
'signup/repeat-message' => 'beforeRepeatMessage',
|
|
||||||
'signup/confirm' => 'beforeSignupConfirm',
|
|
||||||
'authentication/forgot-password' => 'beforeForgotPassword',
|
|
||||||
'authentication/recover-password' => 'beforeRecoverPassword',
|
|
||||||
'default/get' => 'beforeAccountGet',
|
|
||||||
'default/email-verification' => 'beforeAccountEmailVerification',
|
|
||||||
'default/new-email-verification' => 'beforeAccountNewEmailVerification',
|
|
||||||
'default/email' => 'beforeAccountChangeEmail',
|
|
||||||
];
|
|
||||||
|
|
||||||
public static function getInstance(): callable {
|
|
||||||
return Closure::fromCallable([new static(), 'beforeAction']);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function beforeAction(ActionEvent $event): void {
|
|
||||||
$id = $event->action->controller->id . '/' . $event->action->id;
|
|
||||||
if (!isset(self::MAP[$id])) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$handler = self::MAP[$id];
|
|
||||||
$request = Yii::$app->request;
|
|
||||||
$response = Yii::$app->response;
|
|
||||||
$result = $this->$handler($request, $response);
|
|
||||||
if ($result === null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$response->content = Json::encode($result);
|
|
||||||
|
|
||||||
// Prevent request execution
|
|
||||||
$event->isValid = false;
|
|
||||||
$event->handled = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function beforeSignup(Request $request): ?array {
|
|
||||||
$email = $request->post('email');
|
|
||||||
if ($email === 'let-me-register@ely.by') {
|
|
||||||
return ['success' => true];
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function beforeRepeatMessage(Request $request): ?array {
|
|
||||||
$email = $request->post('email');
|
|
||||||
if ($email === 'let-me-register@ely.by' || $email === 'let-me-repeat@ely.by') {
|
|
||||||
return ['success' => true];
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function beforeSignupConfirm(Request $request): ?array {
|
|
||||||
$email = $request->post('key');
|
|
||||||
if ($email === 'LETMEIN') {
|
|
||||||
return [
|
|
||||||
'success' => true,
|
|
||||||
'access_token' => 'dummy_token',
|
|
||||||
'expires_in' => time() + 60,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function beforeForgotPassword(Request $request): ?array {
|
|
||||||
$login = $request->post('login');
|
|
||||||
if ($login === 'let-me-recover@ely.by') {
|
|
||||||
return [
|
|
||||||
'success' => true,
|
|
||||||
'data' => [
|
|
||||||
'canRepeatIn' => time() + 60,
|
|
||||||
'repeatFrequency' => 60,
|
|
||||||
],
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function beforeRecoverPassword(Request $request): ?array {
|
|
||||||
$key = $request->post('key');
|
|
||||||
if ($key === 'LETMEIN') {
|
|
||||||
return [
|
|
||||||
'success' => true,
|
|
||||||
'access_token' => 'dummy_token',
|
|
||||||
'expires_in' => time() + 60,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function beforeAccountGet(Request $request): ?array {
|
|
||||||
$httpAuth = $request->getHeaders()->get('authorization');
|
|
||||||
if ($httpAuth === 'Bearer dummy_token') {
|
|
||||||
return [
|
|
||||||
'id' => 1,
|
|
||||||
'uuid' => 'f63cd5e1-680f-4c2d-baa2-cc7bb174b71a',
|
|
||||||
'username' => 'dummy',
|
|
||||||
'isOtpEnabled' => false,
|
|
||||||
'registeredAt' => time(),
|
|
||||||
'lang' => 'en',
|
|
||||||
'elyProfileLink' => 'http://ely.by/u1',
|
|
||||||
'email' => 'let-me-register@ely.by',
|
|
||||||
'isActive' => true,
|
|
||||||
'passwordChangedAt' => time(),
|
|
||||||
'hasMojangUsernameCollision' => false,
|
|
||||||
'shouldAcceptRules' => false,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function beforeAccountEmailVerification(Request $request): ?array {
|
|
||||||
$httpAuth = $request->getHeaders()->get('authorization');
|
|
||||||
if ($httpAuth === 'Bearer dummy_token') {
|
|
||||||
$password = $request->post('password');
|
|
||||||
if (empty($password)) {
|
|
||||||
return [
|
|
||||||
'success' => false,
|
|
||||||
'errors' => [
|
|
||||||
'password' => 'error.password_required',
|
|
||||||
],
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
return [
|
|
||||||
'success' => true,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function beforeAccountNewEmailVerification(Request $request): ?array {
|
|
||||||
$key = $request->post('key');
|
|
||||||
if ($key === 'LETMEIN') {
|
|
||||||
return [
|
|
||||||
'success' => true,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function beforeAccountChangeEmail(Request $request): ?array {
|
|
||||||
$key = $request->post('key');
|
|
||||||
if ($key === 'LETMEIN') {
|
|
||||||
return [
|
|
||||||
'success' => true,
|
|
||||||
'email' => 'brand-new-email@ely.by',
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@@ -87,5 +87,4 @@ return [
|
|||||||
'internal' => api\modules\internal\Module::class,
|
'internal' => api\modules\internal\Module::class,
|
||||||
'accounts' => api\modules\accounts\Module::class,
|
'accounts' => api\modules\accounts\Module::class,
|
||||||
],
|
],
|
||||||
'on beforeAction' => api\components\TestData::getInstance(),
|
|
||||||
];
|
];
|
||||||
|
@@ -9,7 +9,8 @@ use yii\web\NotFoundHttpException;
|
|||||||
|
|
||||||
abstract class BaseAccountAction extends Action {
|
abstract class BaseAccountAction extends Action {
|
||||||
|
|
||||||
final public function run(int $id): array {
|
// TODO: вернуть final модификатор метода после того, как в GoAOP добавят поддержку аспектов для final методов
|
||||||
|
public function run(int $id): array {
|
||||||
$className = $this->getFormClassName();
|
$className = $this->getFormClassName();
|
||||||
/** @var AccountActionForm $model */
|
/** @var AccountActionForm $model */
|
||||||
$model = new $className($this->findAccount($id));
|
$model = new $className($this->findAccount($id));
|
||||||
|
Reference in New Issue
Block a user