@@ -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() { |
@@ -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; | |||
} | |||
/** |
@@ -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); |
@@ -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(); |
@@ -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); |
@@ -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()); | |||
} | |||
// /** |