remove remote address book if the admin removes the server from the trusted servers list

This commit is contained in:
Bjoern Schiessle 2016-02-26 16:59:53 +01:00
parent eccd7cf654
commit 7189c72c33
9 changed files with 97 additions and 18 deletions

View File

@ -28,6 +28,18 @@ $app->registerHooks();
return $app->getSyncService(); return $app->getSyncService();
}); });
$eventDispatcher = \OC::$server->getEventDispatcher();
$eventDispatcher->addListener('OCP\Federation\TrustedServerEvent::remove',
function(\Symfony\Component\EventDispatcher\GenericEvent $event) use ($app) {
/** @var \OCA\DAV\CardDAV\CardDavBackend $cardDavBackend */
$cardDavBackend = $app->getContainer()->query('CardDavBackend');
$addressBookUri = $event->getSubject();
$addressBook = $cardDavBackend->getAddressBooksByUri('principals/system/system', $addressBookUri);
$cardDavBackend->deleteAddressBook($addressBook['id']);
}
);
$cm = \OC::$server->getContactsManager(); $cm = \OC::$server->getContactsManager();
$cm->register(function() use ($cm, $app) { $cm->register(function() use ($cm, $app) {
$userId = \OC::$server->getUserSession()->getUser()->getUID(); $userId = \OC::$server->getUserSession()->getUser()->getUID();

View File

@ -75,13 +75,15 @@ class Application extends \OCP\AppFramework\App {
}); });
$container->registerService('TrustedServers', function(IAppContainer $c) { $container->registerService('TrustedServers', function(IAppContainer $c) {
$server = $c->getServer();
return new TrustedServers( return new TrustedServers(
$c->query('DbHandler'), $c->query('DbHandler'),
\OC::$server->getHTTPClientService(), $server->getHTTPClientService(),
\OC::$server->getLogger(), $server->getLogger(),
\OC::$server->getJobList(), $server->getJobList(),
\OC::$server->getSecureRandom(), $server->getSecureRandom(),
\OC::$server->getConfig() $server->getConfig(),
$server->getEventDispatcher()
); );
}); });
@ -94,6 +96,7 @@ class Application extends \OCP\AppFramework\App {
$c->query('TrustedServers') $c->query('TrustedServers')
); );
}); });
} }
private function registerMiddleware() { private function registerMiddleware() {

View File

@ -96,7 +96,8 @@ class GetSharedSecret extends QueuedJob{
$this->logger, $this->logger,
$this->jobList, $this->jobList,
\OC::$server->getSecureRandom(), \OC::$server->getSecureRandom(),
\OC::$server->getConfig() \OC::$server->getConfig(),
\OC::$server->getEventDispatcher()
); );
} }
} }

View File

@ -95,7 +95,8 @@ class RequestSharedSecret extends QueuedJob {
$this->logger, $this->logger,
$this->jobList, $this->jobList,
\OC::$server->getSecureRandom(), \OC::$server->getSecureRandom(),
\OC::$server->getConfig() \OC::$server->getConfig(),
\OC::$server->getEventDispatcher()
); );
} }
} }

View File

@ -105,6 +105,28 @@ class DbHandler {
$query->execute(); $query->execute();
} }
/**
* get trusted server with given ID
*
* @param int $id
* @return array
* @throws \Exception
*/
public function getServerById($id) {
$query = $this->connection->getQueryBuilder();
$query->select('*')->from($this->dbTable)
->where($query->expr()->eq('id', $query->createParameter('id')))
->setParameter('id', $id);
$query->execute();
$result = $query->execute()->fetchAll();
if (empty($result)) {
throw new \Exception('No Server found with ID: ' . $id);
}
return $result[0];
}
/** /**
* get all trusted servers * get all trusted servers
* *

View File

@ -30,6 +30,8 @@ use OCP\Http\Client\IClientService;
use OCP\IConfig; use OCP\IConfig;
use OCP\ILogger; use OCP\ILogger;
use OCP\Security\ISecureRandom; use OCP\Security\ISecureRandom;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
class TrustedServers { class TrustedServers {
@ -58,6 +60,9 @@ class TrustedServers {
/** @var IConfig */ /** @var IConfig */
private $config; private $config;
/** @var EventDispatcherInterface */
private $dispatcher;
/** /**
* @param DbHandler $dbHandler * @param DbHandler $dbHandler
* @param IClientService $httpClientService * @param IClientService $httpClientService
@ -65,6 +70,7 @@ class TrustedServers {
* @param IJobList $jobList * @param IJobList $jobList
* @param ISecureRandom $secureRandom * @param ISecureRandom $secureRandom
* @param IConfig $config * @param IConfig $config
* @param EventDispatcherInterface $dispatcher
*/ */
public function __construct( public function __construct(
DbHandler $dbHandler, DbHandler $dbHandler,
@ -72,7 +78,8 @@ class TrustedServers {
ILogger $logger, ILogger $logger,
IJobList $jobList, IJobList $jobList,
ISecureRandom $secureRandom, ISecureRandom $secureRandom,
IConfig $config IConfig $config,
EventDispatcherInterface $dispatcher
) { ) {
$this->dbHandler = $dbHandler; $this->dbHandler = $dbHandler;
$this->httpClientService = $httpClientService; $this->httpClientService = $httpClientService;
@ -80,6 +87,7 @@ class TrustedServers {
$this->jobList = $jobList; $this->jobList = $jobList;
$this->secureRandom = $secureRandom; $this->secureRandom = $secureRandom;
$this->config = $config; $this->config = $config;
$this->dispatcher = $dispatcher;
} }
/** /**
@ -154,7 +162,10 @@ class TrustedServers {
* @param int $id * @param int $id
*/ */
public function removeServer($id) { public function removeServer($id) {
$server = $this->dbHandler->getServerById($id);
$this->dbHandler->removeServer($id); $this->dbHandler->removeServer($id);
$event = new GenericEvent($server['url_hash']);
$this->dispatcher->dispatch('OCP\Federation\TrustedServerEvent::remove', $event);
} }
/** /**

View File

@ -34,7 +34,8 @@ $trustedServers = new \OCA\Federation\TrustedServers(
\OC::$server->getLogger(), \OC::$server->getLogger(),
\OC::$server->getJobList(), \OC::$server->getJobList(),
\OC::$server->getSecureRandom(), \OC::$server->getSecureRandom(),
\OC::$server->getConfig() \OC::$server->getConfig(),
\OC::$server->getEventDispatcher()
); );
$template->assign('trustedServers', $trustedServers->getServers()); $template->assign('trustedServers', $trustedServers->getServers());

View File

@ -115,6 +115,15 @@ class DbHandlerTest extends TestCase {
$this->assertSame($id1, (int)$result[0]['id']); $this->assertSame($id1, (int)$result[0]['id']);
} }
public function testGetServerById() {
$this->dbHandler->addServer('server1');
$id = $this->dbHandler->addServer('server2');
$result = $this->dbHandler->getServerById($id);
$this->assertSame('server2', $result['url']);
}
public function testGetAll() { public function testGetAll() {
$id1 = $this->dbHandler->addServer('server1'); $id1 = $this->dbHandler->addServer('server1');
$id2 = $this->dbHandler->addServer('server2'); $id2 = $this->dbHandler->addServer('server2');

View File

@ -23,7 +23,6 @@
namespace OCA\Federation\Tests\lib; namespace OCA\Federation\Tests\lib;
use OC\HintException;
use OCA\Federation\DbHandler; use OCA\Federation\DbHandler;
use OCA\Federation\TrustedServers; use OCA\Federation\TrustedServers;
use OCP\BackgroundJob\IJobList; use OCP\BackgroundJob\IJobList;
@ -33,6 +32,7 @@ use OCP\Http\Client\IResponse;
use OCP\IConfig; use OCP\IConfig;
use OCP\ILogger; use OCP\ILogger;
use OCP\Security\ISecureRandom; use OCP\Security\ISecureRandom;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Test\TestCase; use Test\TestCase;
class TrustedServersTest extends TestCase { class TrustedServersTest extends TestCase {
@ -64,11 +64,16 @@ class TrustedServersTest extends TestCase {
/** @var \PHPUnit_Framework_MockObject_MockObject | IConfig */ /** @var \PHPUnit_Framework_MockObject_MockObject | IConfig */
private $config; private $config;
/** @var \PHPUnit_Framework_MockObject_MockObject | EventDispatcherInterface */
private $dispatcher;
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
$this->dbHandler = $this->getMockBuilder('\OCA\Federation\DbHandler') $this->dbHandler = $this->getMockBuilder('\OCA\Federation\DbHandler')
->disableOriginalConstructor()->getMock(); ->disableOriginalConstructor()->getMock();
$this->dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')
->disableOriginalConstructor()->getMock();
$this->httpClientService = $this->getMock('OCP\Http\Client\IClientService'); $this->httpClientService = $this->getMock('OCP\Http\Client\IClientService');
$this->httpClient = $this->getMock('OCP\Http\Client\IClient'); $this->httpClient = $this->getMock('OCP\Http\Client\IClient');
$this->response = $this->getMock('OCP\Http\Client\IResponse'); $this->response = $this->getMock('OCP\Http\Client\IResponse');
@ -83,7 +88,8 @@ class TrustedServersTest extends TestCase {
$this->logger, $this->logger,
$this->jobList, $this->jobList,
$this->secureRandom, $this->secureRandom,
$this->config $this->config,
$this->dispatcher
); );
} }
@ -103,7 +109,8 @@ class TrustedServersTest extends TestCase {
$this->logger, $this->logger,
$this->jobList, $this->jobList,
$this->secureRandom, $this->secureRandom,
$this->config $this->config,
$this->dispatcher
] ]
) )
->setMethods(['normalizeUrl', 'updateProtocol']) ->setMethods(['normalizeUrl', 'updateProtocol'])
@ -191,7 +198,18 @@ class TrustedServersTest extends TestCase {
public function testRemoveServer() { public function testRemoveServer() {
$id = 42; $id = 42;
$server = ['url_hash' => 'url_hash'];
$this->dbHandler->expects($this->once())->method('removeServer')->with($id); $this->dbHandler->expects($this->once())->method('removeServer')->with($id);
$this->dbHandler->expects($this->once())->method('getServerById')->with($id)
->willReturn($server);
$this->dispatcher->expects($this->once())->method('dispatch')
->willReturnCallback(
function($eventId, $event) {
$this->assertSame($eventId, 'OCP\Federation\TrustedServerEvent::remove');
$this->assertInstanceOf('Symfony\Component\EventDispatcher\GenericEvent', $event);
$this->assertSame('url_hash', $event->getSubject());
}
);
$this->trustedServers->removeServer($id); $this->trustedServers->removeServer($id);
} }
@ -247,7 +265,8 @@ class TrustedServersTest extends TestCase {
$this->logger, $this->logger,
$this->jobList, $this->jobList,
$this->secureRandom, $this->secureRandom,
$this->config $this->config,
$this->dispatcher
] ]
) )
->setMethods(['checkOwnCloudVersion']) ->setMethods(['checkOwnCloudVersion'])