diff options
author | Roeland Jago Douma <roeland@famdouma.nl> | 2019-03-05 22:21:37 +0300 |
---|---|---|
committer | Backportbot <backportbot-noreply@rullzer.com> | 2019-03-06 12:17:38 +0300 |
commit | 390917a9e5bace3d4e8f709b30bb52aef80744cd (patch) | |
tree | 0088a816a361c2f0065fffee7ba38aeb83e4f355 /apps/twofactor_backupcodes | |
parent | f42309f356b2b851aa0127b55681fe106734132c (diff) |
Do not send notification if no active 2fa
If the job is still present we should also not fire it off if there is
not a single active 2FA provider.
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
Diffstat (limited to 'apps/twofactor_backupcodes')
-rw-r--r-- | apps/twofactor_backupcodes/lib/BackgroundJob/RememberBackupCodesJob.php | 10 | ||||
-rw-r--r-- | apps/twofactor_backupcodes/tests/Unit/BackgroundJob/RememberBackupCodesJobTest.php | 31 |
2 files changed, 39 insertions, 2 deletions
diff --git a/apps/twofactor_backupcodes/lib/BackgroundJob/RememberBackupCodesJob.php b/apps/twofactor_backupcodes/lib/BackgroundJob/RememberBackupCodesJob.php index 1f227061feb..18a171543a5 100644 --- a/apps/twofactor_backupcodes/lib/BackgroundJob/RememberBackupCodesJob.php +++ b/apps/twofactor_backupcodes/lib/BackgroundJob/RememberBackupCodesJob.php @@ -72,7 +72,15 @@ class RememberBackupCodesJob extends TimedJob { } $providers = $this->registry->getProviderStates($user); - if (isset($providers['backup_codes']) && $providers['backup_codes'] === true) { + $state2fa = array_reduce($providers, function(bool $carry, bool $state) { + return $carry || $state; + }, false); + + /* + * If no provider is active or if the backup codes are already generate + * we can remove the job + */ + if ($state2fa === false || (isset($providers['backup_codes']) && $providers['backup_codes'] === true)) { // Backup codes already generated lets remove this job $this->jobList->remove(self::class, $argument); return; diff --git a/apps/twofactor_backupcodes/tests/Unit/BackgroundJob/RememberBackupCodesJobTest.php b/apps/twofactor_backupcodes/tests/Unit/BackgroundJob/RememberBackupCodesJobTest.php index 0e23e032bd8..fe68da8ebf5 100644 --- a/apps/twofactor_backupcodes/tests/Unit/BackgroundJob/RememberBackupCodesJobTest.php +++ b/apps/twofactor_backupcodes/tests/Unit/BackgroundJob/RememberBackupCodesJobTest.php @@ -114,6 +114,34 @@ class RememberBackupCodesJobTest extends TestCase { $this->invokePrivate($this->job, 'run', [['uid' => 'validUID']]); } + public function testNoActiveProvider() { + $user = $this->createMock(IUser::class); + $user->method('getUID') + ->willReturn('validUID'); + $this->userManager->method('get') + ->with('validUID') + ->willReturn($user); + + $this->registry->method('getProviderStates') + ->with($user) + ->willReturn([ + 'backup_codes' => false, + 'foo' => false, + ]); + + $this->jobList->expects($this->once()) + ->method('remove') + ->with( + RememberBackupCodesJob::class, + ['uid' => 'validUID'] + ); + + $this->notificationManager->expects($this->never()) + ->method($this->anything()); + + $this->invokePrivate($this->job, 'run', [['uid' => 'validUID']]); + } + public function testNotificationSend() { $user = $this->createMock(IUser::class); $user->method('getUID') @@ -125,7 +153,8 @@ class RememberBackupCodesJobTest extends TestCase { $this->registry->method('getProviderStates') ->with($user) ->willReturn([ - 'backup_codes' => false + 'backup_codes' => false, + 'foo' => true, ]); $this->jobList->expects($this->never()) |