Skip to content

Commit b9514ee

Browse files
OXDEV-9927 Add email notifier
1 parent b145151 commit b9514ee

9 files changed

Lines changed: 147 additions & 1 deletion

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/**
4+
* Copyright © OXID eSales AG. All rights reserved.
5+
* See LICENSE file for license details.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Provider;
11+
12+
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Provider\Factory\EmailFactoryInterface;
13+
14+
class EmailAdapter
15+
{
16+
public function __construct(
17+
readonly private EmailFactoryInterface $emailFactory,
18+
) {
19+
}
20+
21+
public function getName()
22+
{
23+
return 'email';
24+
}
25+
26+
public function notify($recipient, $code): void
27+
{
28+
$emailModel = $this->emailFactory->create();
29+
$emailModel->sendEmail(
30+
$recipient,
31+
'Your verification code',
32+
"Your verification code is: {$code}"
33+
);
34+
}
35+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/**
4+
* Copyright © OXID eSales AG. All rights reserved.
5+
* See LICENSE file for license details.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Provider\Factory;
11+
12+
use OxidEsales\Eshop\Core\Email;
13+
14+
class EmailFactory implements EmailFactoryInterface
15+
{
16+
public function create(): Email
17+
{
18+
return oxNew(Email::class);
19+
}
20+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
/**
4+
* Copyright © OXID eSales AG. All rights reserved.
5+
* See LICENSE file for license details.
6+
*/
7+
8+
namespace OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Provider\Factory;
9+
10+
use OxidEsales\Eshop\Core\Email;
11+
12+
interface EmailFactoryInterface
13+
{
14+
public function create(): Email;
15+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
services:
2+
_defaults:
3+
autowire: true
4+
public: false
5+
6+
OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Provider\Factory\EmailFactoryInterface:
7+
class: OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Provider\Factory\EmailFactory
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
imports:
2+
- { resource: Factory/services.yaml }
3+
4+
services:
5+
_defaults:
6+
autowire: true
7+
public: false
8+
9+
OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Provider\EmailAdapter:
10+
tags: [ 'security.twofa.tag.notify.provider' ]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
imports:
22
- { resource: Factory/services.yaml }
3+
- { resource: Provider/services.yaml }
34
- { resource: Repository/services.yaml }
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/**
4+
* Copyright © OXID eSales AG. All rights reserved.
5+
* See LICENSE file for license details.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service;
11+
12+
use Exception;
13+
14+
class NotifierService implements NotifierServiceInterface
15+
{
16+
private array $collectedNotifiers;
17+
18+
public function __construct(
19+
private readonly iterable $notifiers,
20+
) {
21+
$this->collectedNotifiers = iterator_to_array($this->notifiers);
22+
}
23+
24+
public function notify(
25+
string $type,
26+
string $recipient,
27+
string $code
28+
): void {
29+
$notifierFound = array_filter(
30+
$this->collectedNotifiers,
31+
fn ($notifier) => $notifier->getName() === $type
32+
);
33+
34+
if (!$notifierFound) {
35+
throw new Exception();
36+
}
37+
38+
$notifier = reset($this->collectedNotifiers);
39+
$notifier->notify($recipient, $code);
40+
}
41+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
/**
4+
* Copyright © OXID eSales AG. All rights reserved.
5+
* See LICENSE file for license details.
6+
*/
7+
8+
namespace OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service;
9+
10+
interface NotifierServiceInterface
11+
{
12+
13+
}

src/Authentication/TwoFactorAuth/Service/services.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ services:
88

99
OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\ModuleSettingsServiceInterface:
1010
class: OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\ModuleSettingsService
11-
public: true
1211

1312
OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\AuthorizeServiceInterface:
1413
class: OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\AuthorizeService
14+
15+
OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\NotifierServiceInterface:
16+
class: OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\NotifierService
1517
public: true
18+
arguments:
19+
$notifiers: !tagged 'security.twofa.tag.notify.provider'

0 commit comments

Comments
 (0)