Skip to content

Commit

Permalink
Merge pull request #162 from creative-commoners/pulls/4.0/disable-emails
Browse files Browse the repository at this point in the history
FIX MFA notifications can now be disabled via configuration
  • Loading branch information
robbieaverill committed Jun 6, 2019
2 parents 8dfc9ca + 16ab143 commit 1f8f86e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/Service/Notification.php
Expand Up @@ -5,16 +5,18 @@
use Exception;
use Psr\Log\LoggerInterface;
use SilverStripe\Control\Email\Email;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Extensible;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Security\Member;

/**
* Ecapsulates setting up an Email in order to allow for Dependency Injection and to avoid introducing a hard
* Encapsulates setting up an Email in order to allow for dependency injection and to avoid introducing a hard
* coupling to the SilverStripe core Email class in code that consumes this class.
*/
class Notification
{
use Configurable;
use Injectable;
use Extensible;

Expand All @@ -26,6 +28,14 @@ class Notification
'Logger' => '%$' . LoggerInterface::class . '.mfa',
];

/**
* Whether sending emails is enabled for MFA changes
*
* @config
* @var bool
*/
private static $enabled = true;

/**
* @var LoggerInterface
*/
Expand All @@ -52,6 +62,10 @@ public function setLogger(LoggerInterface $logger): self
*/
public function send(Member $member, string $template, array $data = []): bool
{
if (!$this->config()->get('enabled')) {
return false;
}

// Catch exceptions with setting the "to" address and sending the email.
try {
$email = Email::create()
Expand Down
17 changes: 17 additions & 0 deletions tests/php/Service/NotificationTest.php
@@ -0,0 +1,17 @@
<?php

namespace SilverStripe\MFA\Service\Tests;

use SilverStripe\Dev\SapphireTest;
use SilverStripe\MFA\Service\Notification;
use SilverStripe\Security\Member;

class NotificationTest extends SapphireTest
{
public function testCanBeDisabled()
{
Notification::config()->set('enabled', false);
$result = Notification::create()->send($this->createMock(Member::class), 'foo');
$this->assertFalse($result);
}
}

0 comments on commit 1f8f86e

Please sign in to comment.