Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent undefined offset 0 in findByUserIdOrMail #16308

Merged
merged 1 commit into from
Jul 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions core/Controller/LostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
use OCP\Mail\IMailer;
use OCP\Security\ICrypto;
use OCP\Security\ISecureRandom;
use function array_filter;
use function count;
use function reset;

/**
* Class LostController
Expand Down Expand Up @@ -389,12 +392,12 @@ protected function findUserByIdOrMail($input) {
return $user;
}

$users = \array_filter($this->userManager->getByEmail($input), function (IUser $user) {
$users = array_filter($this->userManager->getByEmail($input), function (IUser $user) {
return $user->isEnabled();
});

if (\count($users) === 1) {
return $users[0];
if (count($users) === 1) {
return reset($users);
}

throw $userNotFound;
Expand Down
51 changes: 27 additions & 24 deletions tests/Core/Controller/LostControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -821,28 +821,38 @@ public function testTwoUsersWithSameEmail() {
$this->assertEquals($expectedResponse, $response);
}

public function testTwoUsersWithSameEmailOneDisabled() {

/**
* @return array
*/
public function dataTwoUserswithSameEmailOneDisabled(): array {
return [
['user1' => true, 'user2' => false],
['user1' => false, 'user2' => true]
];
}

/**
* @dataProvider dataTwoUserswithSameEmailOneDisabled
* @param bool $userEnabled1
* @param bool $userEnabled2
*/
public function testTwoUsersWithSameEmailOneDisabled(bool $userEnabled1, bool $userEnabled2): void {
$user1 = $this->createMock(IUser::class);
$user1->expects($this->any())
->method('getEMailAddress')
$user1->method('getEMailAddress')
->willReturn('test@example.com');
$user1->expects($this->any())
->method('getUID')
$user1->method('getUID')
->willReturn('User1');
$user1->expects($this->any())
->method('isEnabled')
->willReturn(true);
$user1->method('isEnabled')
->willReturn($userEnabled1);

$user2 = $this->createMock(IUser::class);
$user2->expects($this->any())
->method('getEMailAddress')
$user2->method('getEMailAddress')
->willReturn('test@example.com');
$user2->expects($this->any())
->method('getUID')
$user2->method('getUID')
->willReturn('User2');
$user2->expects($this->any())
->method('isEnabled')
->willReturn(false);
$user2->method('isEnabled')
->willReturn($userEnabled2);

$this->userManager
->method('get')
Expand All @@ -852,14 +862,7 @@ public function testTwoUsersWithSameEmailOneDisabled() {
->method('getByEmail')
->willReturn([$user1, $user2]);

// request password reset for test@example.com
$response = $this->lostController->email('test@example.com');

$expectedResponse = new JSONResponse([
'status' => 'success'
]);
$expectedResponse->throttle();

$this->assertEquals($expectedResponse, $response);
$result = self::invokePrivate($this->lostController, 'findUserByIdOrMail', ['test@example.com']);
$this->assertInstanceOf(IUser::class, $result);
}
}