Skip to content

Commit

Permalink
core: handle administrators activation
Browse files Browse the repository at this point in the history
  • Loading branch information
danigargar committed Feb 9, 2024
1 parent 294f11d commit 0b1e381
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 13 deletions.
4 changes: 4 additions & 0 deletions app/config/packages/security.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ security:
pattern: ^/token/refresh
stateless: true

activate_admin:
pattern: ^/activate_admin/\d+$
security: false

api:
pattern: ^/.+
stateless: true
Expand Down
9 changes: 9 additions & 0 deletions app/config/routes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
28 changes: 28 additions & 0 deletions app/src/App/Controller/Administrator/Activate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Controller\Administrator;

use Demo\Application\Service\Administrator\ActivateAdministrator;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;

class Activate
{
public function __construct(
private ActivateAdministrator $activateAdministrator
) {
}

public function __invoke(Request $request): Response
{
$administratorId = (int) $request->get('id');

try {
$this->activateAdministrator->execute($administratorId);
} catch(\DomainException $e) {
return new Response($e->getMessage(), $e->getCode());
}

return new Response('Administrator activated', 200);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Demo\Application\Service\Administrator;

use Demo\Domain\Model\Administrator\AdministratorDto;
use Demo\Domain\Model\Administrator\AdministratorRepository;
use Ivoz\Core\Domain\Service\EntityTools;

class ActivateAdministrator
{
public function __construct(
private AdministratorRepository $administratorRepository,
private EntityTools $entityTools
) {
}

public function execute(int $administratorId): void
{
$administrator = $this->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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class AdministratorLifecycleServiceCollection implements LifecycleServiceCollect

/** @var array<array-key, array> $bindedBaseServices */
public static $bindedBaseServices = [
"post_persist" =>
"on_commit" =>
[
\Demo\Domain\Service\Administrator\SendActivationEmail::class => 200,
],
Expand Down
17 changes: 5 additions & 12 deletions app/src/Demo/Domain/Service/Administrator/SendActivationEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

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 $activationEmail,
private LoggerInterface $logger
private SendActivationEmailInterface $sendActivationEmail,
private LoggerInterface $logger
) {
}

Expand All @@ -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
];
}

Expand All @@ -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);
}
}

0 comments on commit 0b1e381

Please sign in to comment.