Skip to content

Commit be69ea9

Browse files
Merge pull request #41569 from nextcloud/bugfix/noid/dont-notify-disabled-users-again
fix(2fa-backupcodes): Don't remember disabled and deleted users over …
2 parents e38ebeb + b80dd1f commit be69ea9

File tree

4 files changed

+98
-6
lines changed

4 files changed

+98
-6
lines changed

apps/twofactor_backupcodes/lib/BackgroundJob/CheckBackupCodes.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ public function __construct(ITimeFactory $timeFactory, IUserManager $userManager
5858

5959
protected function run($argument) {
6060
$this->userManager->callForSeenUsers(function (IUser $user) {
61+
if (!$user->isEnabled()) {
62+
return;
63+
}
64+
6165
$providers = $this->registry->getProviderStates($user);
6266
$isTwoFactorAuthenticated = $this->twofactorManager->isTwoFactorAuthenticated($user);
6367

apps/twofactor_backupcodes/lib/BackgroundJob/RememberBackupCodesJob.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ protected function run($argument) {
7070

7171
if ($user === null) {
7272
// We can't run with an invalid user
73+
$this->jobList->remove(self::class, $argument);
7374
return;
7475
}
7576

@@ -98,6 +99,11 @@ protected function run($argument) {
9899
->setSubject('create_backupcodes');
99100
$this->notificationManager->markProcessed($notification);
100101

102+
if (!$user->isEnabled()) {
103+
// Don't recreate a notification for a user that can not read it
104+
$this->jobList->remove(self::class, $argument);
105+
return;
106+
}
101107
$notification->setDateTime($date);
102108
$this->notificationManager->notify($notification);
103109
}

apps/twofactor_backupcodes/tests/Unit/BackgroundJob/CheckBackupCodeTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ protected function setUp(): void {
8282
}
8383

8484
public function testRunAlreadyGenerated() {
85+
$this->user->method('isEnabled')
86+
->willReturn(true);
87+
8588
$this->registry->method('getProviderStates')
8689
->with($this->user)
8790
->willReturn(['backup_codes' => true]);
@@ -97,6 +100,8 @@ public function testRunAlreadyGenerated() {
97100
public function testRun() {
98101
$this->user->method('getUID')
99102
->willReturn('myUID');
103+
$this->user->method('isEnabled')
104+
->willReturn(true);
100105

101106
$this->registry->expects($this->once())
102107
->method('getProviderStates')
@@ -117,7 +122,26 @@ public function testRun() {
117122
$this->invokePrivate($this->checkBackupCodes, 'run', [[]]);
118123
}
119124

125+
public function testRunDisabledUser() {
126+
$this->user->method('getUID')
127+
->willReturn('myUID');
128+
$this->user->method('isEnabled')
129+
->willReturn(false);
130+
131+
$this->registry->expects($this->never())
132+
->method('getProviderStates')
133+
->with($this->user);
134+
135+
$this->jobList->expects($this->never())
136+
->method('add');
137+
138+
$this->invokePrivate($this->checkBackupCodes, 'run', [[]]);
139+
}
140+
120141
public function testRunNoProviders() {
142+
$this->user->method('isEnabled')
143+
->willReturn(true);
144+
121145
$this->registry->expects($this->once())
122146
->method('getProviderStates')
123147
->with($this->user)

apps/twofactor_backupcodes/tests/Unit/BackgroundJob/RememberBackupCodesJobTest.php

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use OCP\IUserManager;
3434
use OCP\Notification\IManager;
3535
use OCP\Notification\INotification;
36+
use OCP\Server;
3637
use Test\TestCase;
3738

3839
class RememberBackupCodesJobTest extends TestCase {
@@ -82,16 +83,25 @@ public function testInvalidUID() {
8283

8384
$this->notificationManager->expects($this->never())
8485
->method($this->anything());
86+
$this->jobList->expects($this->once())
87+
->method('remove')
88+
->with(
89+
RememberBackupCodesJob::class,
90+
['uid' => 'invalidUID']
91+
);
8592
$this->jobList->expects($this->never())
86-
->method($this->anything());
93+
->method('add');
8794

88-
$this->invokePrivate($this->job, 'run', [['uid' => 'invalidUID']]);
95+
self::invokePrivate($this->job, 'run', [['uid' => 'invalidUID']]);
8996
}
9097

9198
public function testBackupCodesGenerated() {
9299
$user = $this->createMock(IUser::class);
93100
$user->method('getUID')
94101
->willReturn('validUID');
102+
$user->method('isEnabled')
103+
->willReturn(true);
104+
95105
$this->userManager->method('get')
96106
->with('validUID')
97107
->willReturn($user);
@@ -112,7 +122,7 @@ public function testBackupCodesGenerated() {
112122
$this->notificationManager->expects($this->never())
113123
->method($this->anything());
114124

115-
$this->invokePrivate($this->job, 'run', [['uid' => 'validUID']]);
125+
self::invokePrivate($this->job, 'run', [['uid' => 'validUID']]);
116126
}
117127

118128
public function testNoActiveProvider() {
@@ -140,13 +150,15 @@ public function testNoActiveProvider() {
140150
$this->notificationManager->expects($this->never())
141151
->method($this->anything());
142152

143-
$this->invokePrivate($this->job, 'run', [['uid' => 'validUID']]);
153+
self::invokePrivate($this->job, 'run', [['uid' => 'validUID']]);
144154
}
145155

146156
public function testNotificationSend() {
147157
$user = $this->createMock(IUser::class);
148158
$user->method('getUID')
149159
->willReturn('validUID');
160+
$user->method('isEnabled')
161+
->willReturn(true);
150162
$this->userManager->method('get')
151163
->with('validUID')
152164
->willReturn($user);
@@ -165,7 +177,7 @@ public function testNotificationSend() {
165177
$date->setTimestamp($this->time->getTime());
166178

167179
$this->notificationManager->method('createNotification')
168-
->willReturn(\OC::$server->query(IManager::class)->createNotification());
180+
->willReturn(Server::get(IManager::class)->createNotification());
169181

170182
$this->notificationManager->expects($this->once())
171183
->method('notify')
@@ -178,6 +190,52 @@ public function testNotificationSend() {
178190
$n->getSubject() === 'create_backupcodes';
179191
}));
180192

181-
$this->invokePrivate($this->job, 'run', [['uid' => 'validUID']]);
193+
self::invokePrivate($this->job, 'run', [['uid' => 'validUID']]);
194+
}
195+
196+
public function testNotificationNotSendForDisabledUser() {
197+
$user = $this->createMock(IUser::class);
198+
$user->method('getUID')
199+
->willReturn('validUID');
200+
$user->method('isEnabled')
201+
->willReturn(false);
202+
$this->userManager->method('get')
203+
->with('validUID')
204+
->willReturn($user);
205+
206+
$this->registry->method('getProviderStates')
207+
->with($user)
208+
->willReturn([
209+
'backup_codes' => false,
210+
'foo' => true,
211+
]);
212+
213+
$this->jobList->expects($this->once())
214+
->method('remove')
215+
->with(
216+
RememberBackupCodesJob::class,
217+
['uid' => 'validUID']
218+
);
219+
220+
$date = new \DateTime();
221+
$date->setTimestamp($this->time->getTime());
222+
223+
$this->notificationManager->method('createNotification')
224+
->willReturn(Server::get(IManager::class)->createNotification());
225+
226+
$this->notificationManager->expects($this->once())
227+
->method('markProcessed')
228+
->with($this->callback(function (INotification $n) {
229+
return $n->getApp() === 'twofactor_backupcodes' &&
230+
$n->getUser() === 'validUID' &&
231+
$n->getObjectType() === 'create' &&
232+
$n->getObjectId() === 'codes' &&
233+
$n->getSubject() === 'create_backupcodes';
234+
}));
235+
236+
$this->notificationManager->expects($this->never())
237+
->method('notify');
238+
239+
self::invokePrivate($this->job, 'run', [['uid' => 'validUID']]);
182240
}
183241
}

0 commit comments

Comments
 (0)