summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBjoern Schiessle <schiessle@owncloud.com>2015-04-14 16:48:39 +0200
committerBjoern Schiessle <schiessle@owncloud.com>2015-04-17 10:31:33 +0200
commit7d4b1b52d08fd4bc8e9f65ca2ccc747de2fc429a (patch)
treec40489a85da1be3eb66041a2ca2719f38c0298f3
parentf32d97750c33942db53a56d1deceacb2ed3e779b (diff)
downloadnextcloud-server-7d4b1b52d08fd4bc8e9f65ca2ccc747de2fc429a.tar.gz
nextcloud-server-7d4b1b52d08fd4bc8e9f65ca2ccc747de2fc429a.zip
always create a new instance of the encryption module
-rw-r--r--apps/encryption/appinfo/application.php18
-rw-r--r--apps/encryption/lib/crypto/encryption.php3
-rw-r--r--lib/private/encryption/manager.php42
-rw-r--r--lib/public/encryption/imanager.php14
-rw-r--r--settings/admin.php8
-rw-r--r--tests/lib/encryption/managertest.php41
6 files changed, 66 insertions, 60 deletions
diff --git a/apps/encryption/appinfo/application.php b/apps/encryption/appinfo/application.php
index dbeb1171bd8..5d0fe2c9184 100644
--- a/apps/encryption/appinfo/application.php
+++ b/apps/encryption/appinfo/application.php
@@ -27,6 +27,7 @@ namespace OCA\Encryption\AppInfo;
use OC\Files\Filesystem;
use OC\Files\View;
use OCA\Encryption\Crypto\Crypt;
+use OCA\Encryption\Crypto\Encryption;
use OCA\Encryption\HookManager;
use OCA\Encryption\Hooks\UserHooks;
use OCA\Encryption\KeyManager;
@@ -90,14 +91,17 @@ class Application extends \OCP\AppFramework\App {
public function registerEncryptionModule() {
$container = $this->getContainer();
- $container->registerService('EncryptionModule', function (IAppContainer $c) {
- return new \OCA\Encryption\Crypto\Encryption(
- $c->query('Crypt'),
- $c->query('KeyManager'),
- $c->query('Util'));
+
+ $this->encryptionManager->registerEncryptionModule(
+ Encryption::ID,
+ Encryption::DISPLAY_NAME,
+ function() use ($container) {
+ return new Encryption(
+ $container->query('Crypt'),
+ $container->query('KeyManager'),
+ $container->query('Util')
+ );
});
- $module = $container->query('EncryptionModule');
- $this->encryptionManager->registerEncryptionModule($module);
}
public function registerServices() {
diff --git a/apps/encryption/lib/crypto/encryption.php b/apps/encryption/lib/crypto/encryption.php
index fd5a84c9734..8498b4223e1 100644
--- a/apps/encryption/lib/crypto/encryption.php
+++ b/apps/encryption/lib/crypto/encryption.php
@@ -32,6 +32,7 @@ use OCA\Encryption\KeyManager;
class Encryption implements IEncryptionModule {
const ID = 'OC_DEFAULT_MODULE';
+ const DISPLAY_NAME = 'ownCloud Default Encryption';
/**
* @var Crypt
@@ -90,7 +91,7 @@ class Encryption implements IEncryptionModule {
* @return string
*/
public function getDisplayName() {
- return 'ownCloud Default Encryption';
+ return self::DISPLAY_NAME;
}
/**
diff --git a/lib/private/encryption/manager.php b/lib/private/encryption/manager.php
index 89abad4934a..e01054c92a4 100644
--- a/lib/private/encryption/manager.php
+++ b/lib/private/encryption/manager.php
@@ -87,17 +87,17 @@ class Manager implements IManager {
}
/**
- * Registers an encryption module
+ * Registers an callback function which must return an encryption module instance
*
- * @param IEncryptionModule $module
+ * @param string $id
+ * @param string $displayName
+ * @param callable $callback
* @throws Exceptions\ModuleAlreadyExistsException
*/
- public function registerEncryptionModule(IEncryptionModule $module) {
- $id = $module->getId();
- $name = $module->getDisplayName();
+ public function registerEncryptionModule($id, $displayName, callable $callback) {
if (isset($this->encryptionModules[$id])) {
- throw new Exceptions\ModuleAlreadyExistsException($id, $name);
+ throw new Exceptions\ModuleAlreadyExistsException($id, $displayName);
}
$defaultEncryptionModuleId = $this->getDefaultEncryptionModuleId();
@@ -106,22 +106,26 @@ class Manager implements IManager {
$this->setDefaultEncryptionModule($id);
}
- $this->encryptionModules[$id] = $module;
+ $this->encryptionModules[$id] = array(
+ 'id' => $id,
+ 'displayName' => 'displayName',
+ 'callback' => $callback
+ );
}
/**
* Unregisters an encryption module
*
- * @param IEncryptionModule $module
+ * @param string $moduleId
*/
- public function unregisterEncryptionModule(IEncryptionModule $module) {
- unset($this->encryptionModules[$module->getId()]);
+ public function unregisterEncryptionModule($moduleId) {
+ unset($this->encryptionModules[$moduleId]);
}
/**
* get a list of all encryption modules
*
- * @return IEncryptionModule[]
+ * @return array [id => ['id' => $id, 'displayName' => $displayName, 'callback' => callback]]
*/
public function getEncryptionModules() {
return $this->encryptionModules;
@@ -137,21 +141,13 @@ class Manager implements IManager {
public function getEncryptionModule($moduleId = '') {
if (!empty($moduleId)) {
if (isset($this->encryptionModules[$moduleId])) {
- return $this->encryptionModules[$moduleId];
+ return call_user_func($this->encryptionModules[$moduleId]['callback']);
} else {
$message = "Module with id: $moduleId does not exists.";
throw new Exceptions\ModuleDoesNotExistsException($message);
}
- } else { // get default module and return this
- // For now we simply return the first module until we have a way
- // to enable multiple modules and define a default module
- $module = reset($this->encryptionModules);
- if ($module) {
- return $module;
- } else {
- $message = 'No encryption module registered';
- throw new Exceptions\ModuleDoesNotExistsException($message);
- }
+ } else {
+ return $this->getDefaultEncryptionModule();
}
}
@@ -165,7 +161,7 @@ class Manager implements IManager {
$defaultModuleId = $this->getDefaultEncryptionModuleId();
if (!empty($defaultModuleId)) {
if (isset($this->encryptionModules[$defaultModuleId])) {
- return $this->encryptionModules[$defaultModuleId];
+ return call_user_func($this->encryptionModules[$defaultModuleId]['callback']);
} else {
$message = 'Default encryption module not loaded';
throw new Exceptions\ModuleDoesNotExistsException($message);
diff --git a/lib/public/encryption/imanager.php b/lib/public/encryption/imanager.php
index badc91c9023..3a370710781 100644
--- a/lib/public/encryption/imanager.php
+++ b/lib/public/encryption/imanager.php
@@ -40,26 +40,28 @@ interface IManager {
function isEnabled();
/**
- * Registers an encryption module
+ * Registers an callback function which must return an encryption module instance
*
- * @param IEncryptionModule $module
+ * @param string $id
+ * @param string $displayName
+ * @param callable $callback
* @throws ModuleAlreadyExistsException
* @since 8.1.0
*/
- function registerEncryptionModule(IEncryptionModule $module);
+ function registerEncryptionModule($id, $displayName, callable $callback);
/**
* Unregisters an encryption module
*
- * @param IEncryptionModule $module
+ * @param string $moduleId
* @since 8.1.0
*/
- function unregisterEncryptionModule(IEncryptionModule $module);
+ function unregisterEncryptionModule($moduleId);
/**
* get a list of all encryption modules
*
- * @return array
+ * @return array [id => ['id' => $id, 'displayName' => $displayName, 'callback' => callback]]
* @since 8.1.0
*/
function getEncryptionModules();
diff --git a/settings/admin.php b/settings/admin.php
index 5cfe2654f66..08b114b6d9e 100644
--- a/settings/admin.php
+++ b/settings/admin.php
@@ -96,10 +96,10 @@ try {
}
$encModulues = array();
foreach ($encryptionModules as $module) {
- $encModulues[$module->getId()]['displayName'] = $module->getDisplayName();
- $encModulues[$module->getId()]['default'] = false;
- if ($defaultEncryptionModule && $module->getId() === $defaultEncryptionModuleId) {
- $encModulues[$module->getId()]['default'] = true;
+ $encModulues[$module['id']]['displayName'] = $module['displayName'];
+ $encModulues[$module['id']]['default'] = false;
+ if ($defaultEncryptionModule && $module['id'] === $defaultEncryptionModuleId) {
+ $encModulues[$module['id']]['default'] = true;
}
}
$template->assign('encryptionModules', $encModulues);
diff --git a/tests/lib/encryption/managertest.php b/tests/lib/encryption/managertest.php
index f123f438311..13f5d47b083 100644
--- a/tests/lib/encryption/managertest.php
+++ b/tests/lib/encryption/managertest.php
@@ -36,9 +36,9 @@ class ManagerTest extends TestCase {
public function testManagerIsDisabledIfDisabledButModules() {
$this->config->expects($this->any())->method('getAppValue')->willReturn(false);
$em = $this->getMock('\OCP\Encryption\IEncryptionModule');
- $em->expects($this->any())->method('getId')->willReturn(0);
+ $em->expects($this->any())->method('getId')->willReturn('id');
$em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
- $this->manager->registerEncryptionModule($em);
+ $this->manager->registerEncryptionModule('id', 'TestDummyModule0', function() use ($em) {return $em;});
$this->assertFalse($this->manager->isEnabled());
}
@@ -50,29 +50,32 @@ class ManagerTest extends TestCase {
/**
* @expectedException \OC\Encryption\Exceptions\ModuleAlreadyExistsException
- * @expectedExceptionMessage Id "0" already used by encryption module "TestDummyModule0"
+ * @expectedExceptionMessage Id "id" already used by encryption module "TestDummyModule0"
*/
public function testModuleRegistration() {
$this->config->expects($this->any())->method('getAppValue')->willReturn('yes');
$em = $this->getMock('\OCP\Encryption\IEncryptionModule');
- $em->expects($this->any())->method('getId')->willReturn(0);
+ $em->expects($this->any())->method('getId')->willReturn('id');
$em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
- $this->manager->registerEncryptionModule($em);
+
+ $this->manager->registerEncryptionModule('id', 'TestDummyModule0', function () use ($em) { return $em;});
$this->assertSame(1, count($this->manager->getEncryptionModules()));
- $this->manager->registerEncryptionModule($em);
+ $this->manager->registerEncryptionModule('id', 'TestDummyModule0', function () use ($em) { return $em;});
}
public function testModuleUnRegistration() {
$this->config->expects($this->any())->method('getAppValue')->willReturn(true);
$em = $this->getMock('\OCP\Encryption\IEncryptionModule');
- $em->expects($this->any())->method('getId')->willReturn(0);
+ $em->expects($this->any())->method('getId')->willReturn('id');
$em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
- $this->manager->registerEncryptionModule($em);
+ $this->manager->registerEncryptionModule('id', 'TestDummyModule0', function () use ($em) { return $em;});
$this->assertSame(1,
count($this->manager->getEncryptionModules())
);
- $this->manager->unregisterEncryptionModule($em);
+
+ $this->manager->unregisterEncryptionModule('id');
$this->assertEmpty($this->manager->getEncryptionModules());
+
}
/**
@@ -82,9 +85,9 @@ class ManagerTest extends TestCase {
public function testGetEncryptionModuleUnknown() {
$this->config->expects($this->any())->method('getAppValue')->willReturn(true);
$em = $this->getMock('\OCP\Encryption\IEncryptionModule');
- $em->expects($this->any())->method('getId')->willReturn(0);
+ $em->expects($this->any())->method('getId')->willReturn('id');
$em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
- $this->manager->registerEncryptionModule($em);
+ $this->manager->registerEncryptionModule('id', 'TestDummyModule0', function () use ($em) { return $em;});
$this->assertSame(1, count($this->manager->getEncryptionModules()));
$this->manager->getEncryptionModule('unknown');
}
@@ -92,23 +95,23 @@ class ManagerTest extends TestCase {
public function testGetEncryptionModule() {
$this->config->expects($this->any())->method('getAppValue')->willReturn(true);
$em = $this->getMock('\OCP\Encryption\IEncryptionModule');
- $em->expects($this->any())->method('getId')->willReturn(0);
+ $em->expects($this->any())->method('getId')->willReturn('id');
$em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
- $this->manager->registerEncryptionModule($em);
+ $this->manager->registerEncryptionModule('id', 'TestDummyModule0', function () use ($em) { return $em;});
$this->assertSame(1, count($this->manager->getEncryptionModules()));
- $en0 = $this->manager->getEncryptionModule(0);
- $this->assertEquals(0, $en0->getId());
+ $en0 = $this->manager->getEncryptionModule('id');
+ $this->assertEquals('id', $en0->getId());
}
public function testGetDefaultEncryptionModule() {
$this->config->expects($this->any())->method('getAppValue')->willReturn(true);
$em = $this->getMock('\OCP\Encryption\IEncryptionModule');
- $em->expects($this->any())->method('getId')->willReturn(0);
+ $em->expects($this->any())->method('getId')->willReturn('id');
$em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
- $this->manager->registerEncryptionModule($em);
+ $this->manager->registerEncryptionModule('id', 'TestDummyModule0', function () use ($em) { return $em;});
$this->assertSame(1, count($this->manager->getEncryptionModules()));
- $en0 = $this->manager->getEncryptionModule(0);
- $this->assertEquals(0, $en0->getId());
+ $en0 = $this->manager->getEncryptionModule('id');
+ $this->assertEquals('id', $en0->getId());
}
// /**