Skip to content
Open
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
6 changes: 6 additions & 0 deletions core/Command/User/ResetPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
namespace OC\Core\Command\User;

use OC\Core\Command\Base;
use OC\Core\Events\BeforePasswordResetEvent;
use OC\Core\Events\PasswordResetEvent;
use OCP\App\IAppManager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IUser;
use OCP\IUserManager;
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
Expand All @@ -25,6 +28,7 @@ class ResetPassword extends Base {
public function __construct(
protected IUserManager $userManager,
private IAppManager $appManager,
private IEventDispatcher $eventDispatcher,
) {
parent::__construct();
}
Expand Down Expand Up @@ -118,13 +122,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

try {
$this->eventDispatcher->dispatchTyped(new BeforePasswordResetEvent($user, $password));
$success = $user->setPassword($password);
} catch (\Exception $e) {
$output->writeln('<error>' . $e->getMessage() . '</error>');
return 1;
}

if ($success) {
$this->eventDispatcher->dispatchTyped(new PasswordResetEvent($user, $password));
$output->writeln('<info>Successfully reset password for ' . $username . '</info>');
} else {
$output->writeln('<error>Error while resetting password!</error>');
Expand Down
117 changes: 117 additions & 0 deletions tests/Core/Command/User/ResetPasswordTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

declare(strict_types=1);

namespace Tests\Core\Command\User;

use OC\Core\Command\User\ResetPassword;
use OC\Core\Events\BeforePasswordResetEvent;
use OC\Core\Events\PasswordResetEvent;
use OCP\App\IAppManager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IUser;
use OCP\IUserManager;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Test\TestCase;

class ResetPasswordTest extends TestCase {
#[\PHPUnit\Framework\Attributes\DataProvider('passwordResetResults')]
public function testPasswordResetEvents(
bool $passwordChangeSucceeds,
int $expectedExitCode,
array $expectedSequence,
string $expectedOutput,
): void {
$sequence = [];

$user = $this->createMock(IUser::class);
$user->expects(self::once())
->method('setPassword')
->with('NewPassword')
->willReturnCallback(static function () use (&$sequence, $passwordChangeSucceeds): bool {
$sequence[] = 'setPassword';
return $passwordChangeSucceeds;
});

$userManager = $this->createMock(IUserManager::class);
$userManager->expects(self::once())
->method('get')
->with('alice')
->willReturn($user);

$eventDispatcher = $this->createMock(IEventDispatcher::class);
$eventDispatcher->expects(self::exactly(count($expectedSequence) - 1))
->method('dispatchTyped')
->willReturnCallback(static function (object $event) use (&$sequence, $user): void {
if ($event instanceof BeforePasswordResetEvent) {
self::assertSame($user, $event->getUser());
self::assertSame('NewPassword', $event->getPassword());
$sequence[] = 'beforeReset';
return;
}

self::assertInstanceOf(PasswordResetEvent::class, $event);
self::assertSame($user, $event->getUser());
self::assertSame('NewPassword', $event->getPassword());
$sequence[] = 'afterReset';
});

$input = $this->createMock(InputInterface::class);
$input->method('getArgument')
->with('user')
->willReturn('alice');
$input->method('getOption')
->with('password-from-env')
->willReturn(true);

$output = $this->createMock(OutputInterface::class);
$output->expects(self::once())
->method('writeln')
->with($expectedOutput);

$command = new ResetPassword(
$userManager,
$this->createStub(IAppManager::class),
$eventDispatcher,
);

$previousPassword = getenv('NC_PASS');
putenv('NC_PASS=NewPassword');

try {
$exitCode = self::invokePrivate($command, 'execute', [$input, $output]);
} finally {
if ($previousPassword === false) {
putenv('NC_PASS');
} else {
putenv('NC_PASS=' . $previousPassword);
}
}

self::assertSame($expectedExitCode, $exitCode);
self::assertSame($expectedSequence, $sequence);
}

public static function passwordResetResults(): array {
return [
'success dispatches both reset events around the password change' => [
true,
0,
['beforeReset', 'setPassword', 'afterReset'],
'<info>Successfully reset password for alice</info>',
],
'failure does not dispatch the after-reset event' => [
false,
1,
['beforeReset', 'setPassword'],
'<error>Error while resetting password!</error>',
],
];
}
}
Loading