]> source.dussan.org Git - nextcloud-server.git/commitdiff
fix change password if no user is logged in, occ call
authorBjoern Schiessle <schiessle@owncloud.com>
Fri, 17 Apr 2015 08:52:40 +0000 (10:52 +0200)
committerBjoern Schiessle <schiessle@owncloud.com>
Fri, 17 Apr 2015 11:26:38 +0000 (13:26 +0200)
apps/encryption/hooks/userhooks.php
apps/encryption/tests/hooks/UserHooksTest.php

index e61d4f9c11987b6187746ae328519aad42bc4988..b09b8e7049a65296453c5c99b6e8d37343fbe8a9 100644 (file)
@@ -196,7 +196,9 @@ class UserHooks implements IHook {
        public function preSetPassphrase($params) {
                if (App::isEnabled('encryption')) {
 
-                       if (!$this->user->getUser()->canChangePassword()) {
+                       $user = $this->user->getUser();
+
+                       if ($user && !$user->canChangePassword()) {
                                $this->setPassphrase($params);
                        }
                }
@@ -212,8 +214,10 @@ class UserHooks implements IHook {
 
                // Get existing decrypted private key
                $privateKey = $this->session->getPrivateKey();
+               $user = $this->user->getUser();
 
-               if ($params['uid'] === $this->user->getUser()->getUID() && $privateKey) {
+               // current logged in user changes his own password
+               if ($user && $params['uid'] === $user->getUID() && $privateKey) {
 
                        // Encrypt private key with new user pwd as passphrase
                        $encryptedPrivateKey = $this->crypt->symmetricEncryptFileContent($privateKey,
@@ -230,7 +234,7 @@ class UserHooks implements IHook {
                        // NOTE: Session does not need to be updated as the
                        // private key has not changed, only the passphrase
                        // used to decrypt it has changed
-               } else { // admin changed the password for a different user, create new keys and reencrypt file keys
+               } else { // admin changed the password for a different user, create new keys and re-encrypt file keys
                        $user = $params['uid'];
                        $recoveryPassword = isset($params['recoveryPassword']) ? $params['recoveryPassword'] : null;
 
index 1d76e3ba1a2fbaca2c086cdbd888628299b0fdc9..bcfb33e86cb72357e9aa8eebf1cade31cea793c9 100644 (file)
@@ -143,6 +143,35 @@ class UserHooksTest extends TestCase {
                $this->assertNull($this->instance->setPassphrase($this->params));
        }
 
+       public function testSetPasswordNoUser() {
+               $this->sessionMock->expects($this->once())
+                       ->method('getPrivateKey')
+                       ->willReturn(true);
+
+               $userSessionMock = $this->getMockBuilder('OCP\IUserSession')
+                       ->disableOriginalConstructor()
+                       ->getMock();
+
+               $userSessionMock->expects($this->any())->method('getUser')->will($this->returnValue(null));
+
+               $this->recoveryMock->expects($this->once())
+                       ->method('isRecoveryEnabledForUser')
+                       ->with('testUser')
+                       ->willReturn(false);
+
+               $userHooks = new UserHooks($this->keyManagerMock,
+                       $this->loggerMock,
+                       $this->userSetupMock,
+                       $userSessionMock,
+                       $this->utilMock,
+                       $this->sessionMock,
+                       $this->cryptMock,
+                       $this->recoveryMock
+               );
+
+               $this->assertNull($userHooks->setPassphrase($this->params));
+       }
+
        public function testPostPasswordReset() {
                $this->keyManagerMock->expects($this->once())
                        ->method('replaceUserKeys')
@@ -157,7 +186,7 @@ class UserHooksTest extends TestCase {
 
        protected function setUp() {
                parent::setUp();
-               $loggerMock = $this->getMock('OCP\ILogger');
+               $this->loggerMock = $this->getMock('OCP\ILogger');
                $this->keyManagerMock = $this->getMockBuilder('OCA\Encryption\KeyManager')
                        ->disableOriginalConstructor()
                        ->getMock();
@@ -203,7 +232,7 @@ class UserHooksTest extends TestCase {
                $this->recoveryMock = $recoveryMock;
                $this->utilMock = $utilMock;
                $this->instance = new UserHooks($this->keyManagerMock,
-                       $loggerMock,
+                       $this->loggerMock,
                        $this->userSetupMock,
                        $this->userSessionMock,
                        $this->utilMock,