diff options
-rw-r--r-- | lib/private/encryption/manager.php | 53 | ||||
-rw-r--r-- | lib/private/encryption/util.php | 51 | ||||
-rw-r--r-- | tests/lib/encryption/utiltest.php | 45 |
3 files changed, 106 insertions, 43 deletions
diff --git a/lib/private/encryption/manager.php b/lib/private/encryption/manager.php index ecbb86993ec..eeddea26b1a 100644 --- a/lib/private/encryption/manager.php +++ b/lib/private/encryption/manager.php @@ -208,49 +208,16 @@ class Manager implements IManager { return $this->config->getAppValue('core', 'default_encryption_module'); } + /** + * Add storage wrapper + */ public static function setupStorage() { - \OC\Files\Filesystem::addStorageWrapper('oc_encryption', function ($mountPoint, $storage, IMountPoint $mount) { - $parameters = [ - 'storage' => $storage, - 'mountPoint' => $mountPoint, - 'mount' => $mount]; - - if (!($storage instanceof Shared)) { - $manager = \OC::$server->getEncryptionManager(); - $util = new Util( - new View(), - \OC::$server->getUserManager(), - \OC::$server->getGroupManager(), - \OC::$server->getConfig() - ); - $user = \OC::$server->getUserSession()->getUser(); - $logger = \OC::$server->getLogger(); - $mountManager = Filesystem::getMountManager(); - $uid = $user ? $user->getUID() : null; - $fileHelper = \OC::$server->getEncryptionFilesHelper(); - $keyStorage = \OC::$server->getEncryptionKeyStorage(); - $update = new Update( - new View(), - $util, - Filesystem::getMountManager(), - $manager, - $fileHelper, - $uid - ); - return new Encryption( - $parameters, - $manager, - $util, - $logger, - $fileHelper, - $uid, - $keyStorage, - $update, - $mountManager - ); - } else { - return $storage; - } - }, 2); + $util = new Util( + new View(), + \OC::$server->getUserManager(), + \OC::$server->getGroupManager(), + \OC::$server->getConfig() + ); + \OC\Files\Filesystem::addStorageWrapper('oc_encryption', array($util, 'wrapStorage'), 2); } } diff --git a/lib/private/encryption/util.php b/lib/private/encryption/util.php index b77672d2f6b..80499249561 100644 --- a/lib/private/encryption/util.php +++ b/lib/private/encryption/util.php @@ -26,8 +26,11 @@ use OC\Encryption\Exceptions\EncryptionHeaderKeyExistsException; use OC\Encryption\Exceptions\EncryptionHeaderToLargeException; use OC\Encryption\Exceptions\ModuleDoesNotExistsException; use OC\Files\Filesystem; +use OC\Files\Storage\Wrapper\Encryption; use OC\Files\View; use OCP\Encryption\IEncryptionModule; +use OCP\Files\Mount\IMountPoint; +use OCP\Files\Storage; use OCP\IConfig; class Util { @@ -386,4 +389,52 @@ class Util { return ($enabled === '1') ? true : false; } + /** + * Wraps the given storage when it is not a shared storage + * + * @param string $mountPoint + * @param Storage $storage + * @param IMountPoint $mount + * @return Encryption|Storage + */ + public function wrapStorage($mountPoint, Storage $storage, IMountPoint $mount) { + $parameters = [ + 'storage' => $storage, + 'mountPoint' => $mountPoint, + 'mount' => $mount]; + + if (!$storage->instanceOfStorage('OC\Files\Storage\Shared') + && !$storage->instanceOfStorage('OCA\Files_Sharing\External\Storage') + && !$storage->instanceOfStorage('OC\Files\Storage\OwnCloud')) { + + $manager = \OC::$server->getEncryptionManager(); + $user = \OC::$server->getUserSession()->getUser(); + $logger = \OC::$server->getLogger(); + $mountManager = Filesystem::getMountManager(); + $uid = $user ? $user->getUID() : null; + $fileHelper = \OC::$server->getEncryptionFilesHelper(); + $keyStorage = \OC::$server->getEncryptionKeyStorage(); + $update = new Update( + new View(), + $this, + Filesystem::getMountManager(), + $manager, + $fileHelper, + $uid + ); + return new Encryption( + $parameters, + $manager, + $this, + $logger, + $fileHelper, + $uid, + $keyStorage, + $update, + $mountManager + ); + } else { + return $storage; + } + } } diff --git a/tests/lib/encryption/utiltest.php b/tests/lib/encryption/utiltest.php index d3a4e211daa..d5f5ce4c2e9 100644 --- a/tests/lib/encryption/utiltest.php +++ b/tests/lib/encryption/utiltest.php @@ -194,4 +194,49 @@ class UtilTest extends TestCase { ); } + /** + * @dataProvider provideWrapStorage + */ + public function testWrapStorage($expectedWrapped, $wrappedStorages) { + $storage = $this->getMockBuilder('OC\Files\Storage\Storage') + ->disableOriginalConstructor() + ->getMock(); + + foreach ($wrappedStorages as $wrapper) { + $storage->expects($this->any()) + ->method('instanceOfStorage') + ->willReturnMap([ + [$wrapper, true], + ]); + } + + $mount = $this->getMockBuilder('OCP\Files\Mount\IMountPoint') + ->disableOriginalConstructor() + ->getMock(); + + $returnedStorage = $this->util->wrapStorage('mountPoint', $storage, $mount); + + $this->assertEquals( + $expectedWrapped, + $returnedStorage->instanceOfStorage('OC\Files\Storage\Wrapper\Encryption'), + 'Asserted that the storage is (not) wrapped with encryption' + ); + } + + public function provideWrapStorage() { + return [ + // Wrap when not wrapped or not wrapped with storage + [true, []], + [true, ['OCA\Files_Trashbin\Storage']], + + // Do not wrap shared storages + [false, ['OC\Files\Storage\Shared']], + [false, ['OCA\Files_Sharing\External\Storage']], + [false, ['OC\Files\Storage\OwnCloud']], + [false, ['OC\Files\Storage\Shared', 'OCA\Files_Sharing\External\Storage']], + [false, ['OC\Files\Storage\Shared', 'OC\Files\Storage\OwnCloud']], + [false, ['OCA\Files_Sharing\External\Storage', 'OC\Files\Storage\OwnCloud']], + [false, ['OC\Files\Storage\Shared', 'OCA\Files_Sharing\External\Storage', 'OC\Files\Storage\OwnCloud']], + ]; + } } |