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

Do not allow passwords to be expired for non-local users #31

Merged
merged 1 commit into from
Jul 10, 2018
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
11 changes: 9 additions & 2 deletions lib/Command/ExpirePassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ protected function configure() {
protected function execute(InputInterface $input, OutputInterface $output) {
$uid = $input->getArgument('uid');

$exists = $this->userManager->userExists($uid);
if($exists === false) {
/** @var $user \OCP\IUser */
$user = $this->userManager->get($uid);

if ($user === null) {
$output->writeln("<error>Unknown user: $uid</error>");
/**
* return EX_NOUSER from /usr/include/sysexits.h
Expand All @@ -88,6 +90,11 @@ protected function execute(InputInterface $input, OutputInterface $output) {
return 67;
}

if (!$user->canChangePassword()) {
$output->writeln("<error>The user's backend doesn't support password changes. The password cannot be expired for user: $uid</error>");
return 1;
}

$date = new \DateTime($input->getArgument('expiredate'));
$date->setTimezone(new \DateTimeZone('UTC'));
$value = $date->format('Y-m-d\TH:i:s\Z'); // ISO8601 with Zulu = UTC
Expand Down
36 changes: 32 additions & 4 deletions tests/Command/ExpirePasswordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

use OCA\PasswordPolicy\Command\ExpirePassword;
use OCP\IConfig;
use OCP\IUser;
use OCP\IUserManager;
use Symfony\Component\Console\Tester\CommandTester;
use Test\TestCase;
Expand Down Expand Up @@ -52,9 +53,9 @@ public function setUp() {
public function testExpirePasswordUserNotExisting() {
$this->userManager
->expects(self::once())
->method('userExists')
->method('get')
->with('not-existing-uid')
->willReturn(false);
->willReturn(null);

$this->commandTester->execute([
'uid' => 'not-existing-uid',
Expand All @@ -65,11 +66,17 @@ public function testExpirePasswordUserNotExisting() {
}

public function testExpirePassword() {
$user = $this->createMock(IUser::class);
$user
->expects($this->once())
->method('canChangePassword')
->willReturn(true);

$this->userManager
->expects($this->once())
->method('userExists')
->method('get')
->with('existing-uid')
->willReturn(true);
->willReturn($user);
$this->config
->expects($this->once())
->method('setUserValue')
Expand All @@ -88,4 +95,25 @@ public function testExpirePassword() {
self::assertContains('The password for existing-uid is set to expire on 2018-06-28 10:13:00 UTC.', $output);
}

public function testCannotExpirePassword() {
$user = $this->createMock(IUser::class);
$user
->expects($this->once())
->method('canChangePassword')
->willReturn(false);

$this->userManager
->expects($this->once())
->method('get')
->with('existing-uid')
->willReturn($user);

$this->commandTester->execute([
'uid' => 'existing-uid',
'expiredate' => '2018-06-28 10:13 UTC'
]);
$output = $this->commandTester->getDisplay();
self::assertContains("The user's backend doesn't support password changes. The password cannot be expired for user: existing-uid", $output);
}

}