Merge pull request #18983 from owncloud/fix_18926

check for the right user if we can change his password
This commit is contained in:
Thomas Müller 2015-09-18 11:59:57 +02:00
commit b59c42e5df
3 changed files with 71 additions and 5 deletions

View File

@ -84,6 +84,7 @@ class Application extends \OCP\AppFramework\App {
$hookManager->registerHook([ $hookManager->registerHook([
new UserHooks($container->query('KeyManager'), new UserHooks($container->query('KeyManager'),
$server->getUserManager(),
$server->getLogger(), $server->getLogger(),
$container->query('UserSetup'), $container->query('UserSetup'),
$server->getUserSession(), $server->getUserSession(),

View File

@ -24,6 +24,7 @@
namespace OCA\Encryption\Hooks; namespace OCA\Encryption\Hooks;
use OCP\IUserManager;
use OCP\Util as OCUtil; use OCP\Util as OCUtil;
use OCA\Encryption\Hooks\Contracts\IHook; use OCA\Encryption\Hooks\Contracts\IHook;
use OCA\Encryption\KeyManager; use OCA\Encryption\KeyManager;
@ -41,6 +42,10 @@ class UserHooks implements IHook {
* @var KeyManager * @var KeyManager
*/ */
private $keyManager; private $keyManager;
/**
* @var IUserManager
*/
private $userManager;
/** /**
* @var ILogger * @var ILogger
*/ */
@ -74,6 +79,7 @@ class UserHooks implements IHook {
* UserHooks constructor. * UserHooks constructor.
* *
* @param KeyManager $keyManager * @param KeyManager $keyManager
* @param IUserManager $userManager
* @param ILogger $logger * @param ILogger $logger
* @param Setup $userSetup * @param Setup $userSetup
* @param IUserSession $user * @param IUserSession $user
@ -83,6 +89,7 @@ class UserHooks implements IHook {
* @param Recovery $recovery * @param Recovery $recovery
*/ */
public function __construct(KeyManager $keyManager, public function __construct(KeyManager $keyManager,
IUserManager $userManager,
ILogger $logger, ILogger $logger,
Setup $userSetup, Setup $userSetup,
IUserSession $user, IUserSession $user,
@ -92,6 +99,7 @@ class UserHooks implements IHook {
Recovery $recovery) { Recovery $recovery) {
$this->keyManager = $keyManager; $this->keyManager = $keyManager;
$this->userManager = $userManager;
$this->logger = $logger; $this->logger = $logger;
$this->userSetup = $userSetup; $this->userSetup = $userSetup;
$this->user = $user; $this->user = $user;
@ -196,7 +204,7 @@ class UserHooks implements IHook {
public function preSetPassphrase($params) { public function preSetPassphrase($params) {
if (App::isEnabled('encryption')) { if (App::isEnabled('encryption')) {
$user = $this->user->getUser(); $user = $this->userManager->get($params['uid']);
if ($user && !$user->canChangePassword()) { if ($user && !$user->canChangePassword()) {
$this->setPassphrase($params); $this->setPassphrase($params);

View File

@ -47,6 +47,11 @@ class UserHooksTest extends TestCase {
* @var \PHPUnit_Framework_MockObject_MockObject * @var \PHPUnit_Framework_MockObject_MockObject
*/ */
private $keyManagerMock; private $keyManagerMock;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $userManagerMock;
/** /**
* @var \PHPUnit_Framework_MockObject_MockObject * @var \PHPUnit_Framework_MockObject_MockObject
*/ */
@ -101,11 +106,58 @@ class UserHooksTest extends TestCase {
$this->assertNull($this->instance->postDeleteUser($this->params)); $this->assertNull($this->instance->postDeleteUser($this->params));
} }
public function testPreSetPassphrase() { /**
$this->userSessionMock->expects($this->once()) * @dataProvider dataTestPreSetPassphrase
->method('canChangePassword'); */
public function testPreSetPassphrase($canChange) {
$this->assertNull($this->instance->preSetPassphrase($this->params)); /** @var UserHooks | \PHPUnit_Framework_MockObject_MockObject $instance */
$instance = $this->getMockBuilder('OCA\Encryption\Hooks\UserHooks')
->setConstructorArgs(
[
$this->keyManagerMock,
$this->userManagerMock,
$this->loggerMock,
$this->userSetupMock,
$this->userSessionMock,
$this->utilMock,
$this->sessionMock,
$this->cryptMock,
$this->recoveryMock
]
)
->setMethods(['setPassphrase'])
->getMock();
$userMock = $this->getMock('OCP\IUser');
$this->userManagerMock->expects($this->once())
->method('get')
->with($this->params['uid'])
->willReturn($userMock);
$userMock->expects($this->once())
->method('canChangePassword')
->willReturn($canChange);
if ($canChange) {
// in this case the password will be changed in the post hook
$instance->expects($this->never())->method('setPassphrase');
} else {
// if user can't change the password we update the encryption
// key password already in the pre hook
$instance->expects($this->once())
->method('setPassphrase')
->with($this->params);
}
$instance->preSetPassphrase($this->params);
}
public function dataTestPreSetPassphrase() {
return [
[true],
[false]
];
} }
public function testSetPassphrase() { public function testSetPassphrase() {
@ -186,6 +238,7 @@ class UserHooksTest extends TestCase {
->willReturn(false); ->willReturn(false);
$userHooks = new UserHooks($this->keyManagerMock, $userHooks = new UserHooks($this->keyManagerMock,
$this->userManagerMock,
$this->loggerMock, $this->loggerMock,
$this->userSetupMock, $this->userSetupMock,
$userSessionMock, $userSessionMock,
@ -216,6 +269,9 @@ class UserHooksTest extends TestCase {
$this->keyManagerMock = $this->getMockBuilder('OCA\Encryption\KeyManager') $this->keyManagerMock = $this->getMockBuilder('OCA\Encryption\KeyManager')
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$this->userManagerMock = $this->getMockBuilder('OCP\IUserManager')
->disableOriginalConstructor()
->getMock();
$this->userSetupMock = $this->getMockBuilder('OCA\Encryption\Users\Setup') $this->userSetupMock = $this->getMockBuilder('OCA\Encryption\Users\Setup')
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
@ -258,6 +314,7 @@ class UserHooksTest extends TestCase {
$this->recoveryMock = $recoveryMock; $this->recoveryMock = $recoveryMock;
$this->utilMock = $utilMock; $this->utilMock = $utilMock;
$this->instance = new UserHooks($this->keyManagerMock, $this->instance = new UserHooks($this->keyManagerMock,
$this->userManagerMock,
$this->loggerMock, $this->loggerMock,
$this->userSetupMock, $this->userSetupMock,
$this->userSessionMock, $this->userSessionMock,