From 110a400b05c8fb0741f5e9a9dd97844b9ce43dd7 Mon Sep 17 00:00:00 2001 From: Dani Garcia Date: Tue, 6 Feb 2024 16:39:44 +0100 Subject: [PATCH] core: handle administrators activation --- app/config/packages/security.yaml | 4 +++ app/config/routes.yaml | 9 ++++++ .../App/Controller/Administrator/Activate.php | 28 +++++++++++++++++ .../Administrator/SendActivationEmail.php | 9 ++---- .../Administrator/ActivateAdministrator.php | 30 +++++++++++++++++++ ...dministratorLifecycleServiceCollection.php | 2 +- .../Administrator/SendActivationEmail.php | 13 ++------ 7 files changed, 77 insertions(+), 18 deletions(-) create mode 100644 app/src/App/Controller/Administrator/Activate.php create mode 100644 app/src/Demo/Application/Service/Administrator/ActivateAdministrator.php diff --git a/app/config/packages/security.yaml b/app/config/packages/security.yaml index a422501..34d4efc 100755 --- a/app/config/packages/security.yaml +++ b/app/config/packages/security.yaml @@ -43,6 +43,10 @@ security: pattern: ^/token/refresh stateless: true + activate_admin: + pattern: ^/activate_admin/\d+$ + security: false + api: pattern: ^/.+ stateless: true diff --git a/app/config/routes.yaml b/app/config/routes.yaml index 063a943..3a84f30 100755 --- a/app/config/routes.yaml +++ b/app/config/routes.yaml @@ -11,8 +11,17 @@ api_admin_login: post_send_administrator_activation_email: path: '/administrators/{id}/send_activation_email' methods: ['POST'] + requirements: + id: '\d+' defaults: _controller: App\Controller\Administrator\SendActivationEmail _api_item_operation_name: 'send_activacion_email' _api_receive: false +get_activate_administrator: + path: '/activate_admin/{id}' + methods: ['GET'] + requirements: + id: '\d+' + defaults: + _controller: App\Controller\Administrator\Activate diff --git a/app/src/App/Controller/Administrator/Activate.php b/app/src/App/Controller/Administrator/Activate.php new file mode 100644 index 0000000..deb145a --- /dev/null +++ b/app/src/App/Controller/Administrator/Activate.php @@ -0,0 +1,28 @@ +get('id'); + + try { + $this->activateAdministrator->execute($administratorId); + } catch(\DomainException $e) { + return new Response($e->getMessage(), $e->getCode()); + } + + return new Response('Administrator activated', 200); + } +} diff --git a/app/src/App/Controller/Administrator/SendActivationEmail.php b/app/src/App/Controller/Administrator/SendActivationEmail.php index af398b2..b39044c 100644 --- a/app/src/App/Controller/Administrator/SendActivationEmail.php +++ b/app/src/App/Controller/Administrator/SendActivationEmail.php @@ -27,13 +27,8 @@ public function __invoke(Request $request): Response throw new \DomainException("Resource not found", 404); } - try { - $this->sendActivationEmail->execute($administrator); - $response->setStatusCode(200); - } catch (\Exception $e) { - $response->setContent($e->getMessage()); - $response->setStatusCode($e->getCode()); - } + $this->sendActivationEmail->execute($administrator); + $response->setStatusCode(200); return $response; } diff --git a/app/src/Demo/Application/Service/Administrator/ActivateAdministrator.php b/app/src/Demo/Application/Service/Administrator/ActivateAdministrator.php new file mode 100644 index 0000000..1f0e706 --- /dev/null +++ b/app/src/Demo/Application/Service/Administrator/ActivateAdministrator.php @@ -0,0 +1,30 @@ +administratorRepository->find($administratorId); + + if ($administrator === null) { + throw new \DomainException('Administrator not found.', 404); + } + + /** @var AdministratorDto $administratorDto */ + $administratorDto = $this->entityTools->entityToDto($administrator); + $administratorDto->setActive(1); + $this->entityTools->persistDto($administratorDto, $administrator); + } +} diff --git a/app/src/Demo/Domain/Service/Administrator/AdministratorLifecycleServiceCollection.php b/app/src/Demo/Domain/Service/Administrator/AdministratorLifecycleServiceCollection.php index c583cde..69cbcd8 100644 --- a/app/src/Demo/Domain/Service/Administrator/AdministratorLifecycleServiceCollection.php +++ b/app/src/Demo/Domain/Service/Administrator/AdministratorLifecycleServiceCollection.php @@ -15,7 +15,7 @@ class AdministratorLifecycleServiceCollection implements LifecycleServiceCollect /** @var array $bindedBaseServices */ public static $bindedBaseServices = [ - "post_persist" => + "on_commit" => [ \Demo\Domain\Service\Administrator\SendActivationEmail::class => 200, ], diff --git a/app/src/Demo/Domain/Service/Administrator/SendActivationEmail.php b/app/src/Demo/Domain/Service/Administrator/SendActivationEmail.php index e26c625..0ea63f3 100644 --- a/app/src/Demo/Domain/Service/Administrator/SendActivationEmail.php +++ b/app/src/Demo/Domain/Service/Administrator/SendActivationEmail.php @@ -7,7 +7,7 @@ class SendActivationEmail implements AdministratorLifecycleEventHandlerInterface { - public const POST_PERSIST_PRIORITY = self::PRIORITY_NORMAL; + public const ON_COMMIT_PRIORITY = self::PRIORITY_NORMAL; public function __construct( private SendActivationEmailInterface $sendActivationEmail, @@ -21,7 +21,7 @@ public function __construct( public static function getSubscribedEvents(): array { return [ - self::EVENT_POST_PERSIST => self::POST_PERSIST_PRIORITY + self::EVENT_ON_COMMIT => self::ON_COMMIT_PRIORITY ]; } @@ -40,13 +40,6 @@ public function execute(AdministratorInterface $administrator): void return; } - $id = $administrator->getId(); - if ($id === null) { - throw new \DomainException('Administrator not found'); - } - - $this->activationEmail->execute($administrator); - - $this->logger->info("Administrator created with Id {$id}"); + $this->sendActivationEmail->execute($administrator); } }