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

fix can change password check in case of encryption is enabled #13172

Merged
merged 1 commit into from
Jan 4, 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
68 changes: 48 additions & 20 deletions settings/Controller/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

use OC\Accounts\AccountManager;
use OC\AppFramework\Http;
use OC\Encryption\Exceptions\ModuleDoesNotExistsException;
use OC\ForbiddenException;
use OC\Security\IdentityProof\Manager;
use OCA\User_LDAP\User_Proxy;
Expand Down Expand Up @@ -128,9 +129,9 @@ public function __construct(string $appName,
/**
* @NoCSRFRequired
* @NoAdminRequired
*
*
* Display users list template
*
*
* @return TemplateResponse
*/
public function usersListByGroup() {
Expand All @@ -140,17 +141,17 @@ public function usersListByGroup() {
/**
* @NoCSRFRequired
* @NoAdminRequired
*
*
* Display users list template
*
*
* @return TemplateResponse
*/
public function usersList() {
$user = $this->userSession->getUser();
$uid = $user->getUID();

\OC::$server->getNavigationManager()->setActiveEntry('core_users');

/* SORT OPTION: SORT_USERCOUNT or SORT_GROUPNAME */
$sortGroupsBy = \OC\Group\MetaData::SORT_USERCOUNT;
$isLDAPUsed = false;
Expand All @@ -166,22 +167,17 @@ public function usersList() {
}
}
}

/* ENCRYPTION CONFIG */
$isEncryptionEnabled = $this->encryptionManager->isEnabled();
$useMasterKey = $this->config->getAppValue('encryption', 'useMasterKey', true);
// If masterKey enabled, then you can change password. This is to avoid data loss!
$canChangePassword = ($isEncryptionEnabled && $useMasterKey) || $useMasterKey;


/* GROUPS */

$canChangePassword = $this->canAdminChangeUserPasswords();

/* GROUPS */
$groupsInfo = new \OC\Group\MetaData(
$uid,
$this->isAdmin,
$this->groupManager,
$this->userSession
);

$groupsInfo->setSorting($sortGroupsBy);
list($adminGroup, $groups) = $groupsInfo->get();

Expand All @@ -190,7 +186,7 @@ public function usersList() {
return $ldapFound || $backend instanceof User_Proxy;
});
}

if ($this->isAdmin) {
$disabledUsers = $isLDAPUsed ? -1 : $this->userManager->countDisabledUsers();
$userCount = $isLDAPUsed ? 0 : array_reduce($this->userManager->countUsers(), function($v, $w) {
Expand Down Expand Up @@ -221,7 +217,7 @@ public function usersList() {
'name' => 'Disabled users',
'usercount' => $disabledUsers
];

/* QUOTAS PRESETS */
$quotaPreset = $this->config->getAppValue('files', 'quota_preset', '1 GB, 5 GB, 10 GB');
$quotaPreset = explode(',', $quotaPreset);
Expand All @@ -230,12 +226,12 @@ public function usersList() {
}
$quotaPreset = array_diff($quotaPreset, array('default', 'none'));
$defaultQuota = $this->config->getAppValue('files', 'default_quota', 'none');

\OC::$server->getEventDispatcher()->dispatch('OC\Settings\Users::loadAdditionalScripts');

/* LANGUAGES */
$languages = $this->l10nFactory->getLanguages();

/* FINAL DATA */
$serverData = array();
// groups
Expand All @@ -254,6 +250,38 @@ public function usersList() {
return new TemplateResponse('settings', 'settings-vue', ['serverData' => $serverData]);
}

/**
* check if the admin can change the users password
*
* The admin can change the passwords if:
*
* - no encryption module is loaded and encryption is disabled
* - encryption module is loaded but it doesn't require per user keys
*
* The admin can not change the passwords if:
*
* - an encryption module is loaded and it uses per-user keys
* - encryption is enabled but no encryption modules are loaded
*
* @return bool
*/
protected function canAdminChangeUserPasswords() {
$isEncryptionEnabled = $this->encryptionManager->isEnabled();
try {
$noUserSpecificEncryptionKeys =!$this->encryptionManager->getEncryptionModule()->needDetailedAccessList();
$isEncryptionModuleLoaded = true;
} catch (ModuleDoesNotExistsException $e) {
$noUserSpecificEncryptionKeys = true;
$isEncryptionModuleLoaded = false;
}

$canChangePassword = ($isEncryptionEnabled && $isEncryptionModuleLoaded && $noUserSpecificEncryptionKeys)
|| (!$isEncryptionEnabled && !$isEncryptionModuleLoaded)
|| (!$isEncryptionEnabled && $isEncryptionModuleLoaded && $noUserSpecificEncryptionKeys);

return $canChangePassword;
}

/**
* @NoAdminRequired
* @NoSubadminRequired
Expand Down
48 changes: 47 additions & 1 deletion tests/Settings/Controller/UsersControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Tests\Settings\Controller;

use OC\Accounts\AccountManager;
use OC\Encryption\Exceptions\ModuleDoesNotExistsException;
use OC\Group\Group;
use OC\Group\Manager;
use OC\Settings\Controller\UsersController;
Expand Down Expand Up @@ -98,7 +99,7 @@ protected function setUp() {
$this->securityManager = $this->getMockBuilder(\OC\Security\IdentityProof\Manager::class)->disableOriginalConstructor()->getMock();
$this->jobList = $this->createMock(IJobList::class);
$this->encryptionManager = $this->createMock(IManager::class);

$this->l->method('t')
->will($this->returnCallback(function ($text, $parameters = []) {
return vsprintf($text, $parameters);
Expand Down Expand Up @@ -513,4 +514,49 @@ public function testGetVerificationCodeInvalidUser() {
$this->assertSame(Http::STATUS_BAD_REQUEST, $result->getStatus());
}

/**
* @dataProvider dataTestCanAdminChangeUserPasswords
*
* @param bool $encryptionEnabled
* @param bool $encryptionModuleLoaded
* @param bool $masterKeyEnabled
* @param bool $expected
*/
public function testCanAdminChangeUserPasswords($encryptionEnabled,
$encryptionModuleLoaded,
$masterKeyEnabled,
$expected) {
$controller = $this->getController();

$this->encryptionManager->expects($this->any())
->method('isEnabled')
->willReturn($encryptionEnabled);
$this->encryptionManager->expects($this->any())
->method('getEncryptionModule')
->willReturnCallback(function() use ($encryptionModuleLoaded) {
if ($encryptionModuleLoaded) return $this->encryptionModule;
else throw new ModuleDoesNotExistsException();
});
$this->encryptionModule->expects($this->any())
->method('needDetailedAccessList')
->willReturn(!$masterKeyEnabled);

$result = $this->invokePrivate($controller, 'canAdminChangeUserPasswords', []);
$this->assertSame($expected, $result);
}

public function dataTestCanAdminChangeUserPasswords() {
return [
// encryptionEnabled, encryptionModuleLoaded, masterKeyEnabled, expectedResult
[true, true, true, true],
[false, true, true, true],
[true, false, true, false],
[false, false, true, true],
[true, true, false, false],
[false, true, false, false],
[true, false, false, false],
[false, false, false, true],
];
}

}