diff options
-rw-r--r-- | apps/dav/appinfo/info.xml | 3 | ||||
-rw-r--r-- | apps/dav/composer/composer/ClassLoader.php | 15 | ||||
-rw-r--r-- | apps/dav/composer/composer/autoload_classmap.php | 1 | ||||
-rw-r--r-- | apps/dav/composer/composer/autoload_static.php | 10 | ||||
-rw-r--r-- | apps/dav/lib/CalDAV/Calendar.php | 2 | ||||
-rw-r--r-- | apps/dav/lib/CardDAV/AddressBook.php | 2 | ||||
-rw-r--r-- | apps/dav/lib/Command/RemoveInvalidShares.php | 81 | ||||
-rw-r--r-- | apps/dav/lib/Connector/Sabre/Principal.php | 7 | ||||
-rw-r--r-- | apps/dav/lib/DAV/Sharing/Backend.php | 12 | ||||
-rw-r--r-- | apps/dav/lib/Server.php | 48 | ||||
-rw-r--r-- | apps/dav/tests/unit/CalDAV/AbstractCalDavBackend.php | 9 | ||||
-rw-r--r-- | apps/dav/tests/unit/CardDAV/CardDavBackendTest.php | 9 | ||||
-rw-r--r-- | apps/dav/tests/unit/Command/RemoveInvalidSharesTest.php | 71 | ||||
-rw-r--r-- | apps/dav/tests/unit/ServerTest.php | 20 |
14 files changed, 252 insertions, 38 deletions
diff --git a/apps/dav/appinfo/info.xml b/apps/dav/appinfo/info.xml index 25a0d542e85..54afaf32a00 100644 --- a/apps/dav/appinfo/info.xml +++ b/apps/dav/appinfo/info.xml @@ -4,8 +4,8 @@ <name>WebDAV</name> <description>WebDAV endpoint</description> <licence>AGPL</licence> + <version>1.4.7</version> <author>owncloud.org</author> - <version>1.4.6</version> <default_enable/> <types> <filesystem/> @@ -29,6 +29,7 @@ <command>OCA\DAV\Command\CreateCalendar</command> <command>OCA\DAV\Command\SyncBirthdayCalendar</command> <command>OCA\DAV\Command\SyncSystemAddressBook</command> + <command>OCA\DAV\Command\RemoveInvalidShares</command> </commands> <activity> diff --git a/apps/dav/composer/composer/ClassLoader.php b/apps/dav/composer/composer/ClassLoader.php index c6f6d2322bb..dc02dfb114f 100644 --- a/apps/dav/composer/composer/ClassLoader.php +++ b/apps/dav/composer/composer/ClassLoader.php @@ -43,8 +43,7 @@ namespace Composer\Autoload; class ClassLoader { // PSR-4 - private $firstCharsPsr4 = array(); - private $prefixLengthsPsr4 = array(); // For BC with legacy static maps + private $prefixLengthsPsr4 = array(); private $prefixDirsPsr4 = array(); private $fallbackDirsPsr4 = array(); @@ -171,10 +170,11 @@ class ClassLoader } } elseif (!isset($this->prefixDirsPsr4[$prefix])) { // Register directories for a new namespace. - if ('\\' !== substr($prefix, -1)) { + $length = strlen($prefix); + if ('\\' !== $prefix[$length - 1]) { throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); } - $this->firstCharsPsr4[$prefix[0]] = true; + $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; $this->prefixDirsPsr4[$prefix] = (array) $paths; } elseif ($prepend) { // Prepend directories for an already registered namespace. @@ -221,10 +221,11 @@ class ClassLoader if (!$prefix) { $this->fallbackDirsPsr4 = (array) $paths; } else { - if ('\\' !== substr($prefix, -1)) { + $length = strlen($prefix); + if ('\\' !== $prefix[$length - 1]) { throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); } - $this->firstCharsPsr4[$prefix[0]] = true; + $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; $this->prefixDirsPsr4[$prefix] = (array) $paths; } } @@ -372,7 +373,7 @@ class ClassLoader $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext; $first = $class[0]; - if (isset($this->firstCharsPsr4[$first]) || isset($this->prefixLengthsPsr4[$first])) { + if (isset($this->prefixLengthsPsr4[$first])) { $subPath = $class; while (false !== $lastPos = strrpos($subPath, '\\')) { $subPath = substr($subPath, 0, $lastPos); diff --git a/apps/dav/composer/composer/autoload_classmap.php b/apps/dav/composer/composer/autoload_classmap.php index 0bf6b25751e..d15268f8c33 100644 --- a/apps/dav/composer/composer/autoload_classmap.php +++ b/apps/dav/composer/composer/autoload_classmap.php @@ -64,6 +64,7 @@ return array( 'OCA\\DAV\\CardDAV\\Xml\\Groups' => $baseDir . '/../lib/CardDAV/Xml/Groups.php', 'OCA\\DAV\\Command\\CreateAddressBook' => $baseDir . '/../lib/Command/CreateAddressBook.php', 'OCA\\DAV\\Command\\CreateCalendar' => $baseDir . '/../lib/Command/CreateCalendar.php', + 'OCA\\DAV\\Command\\RemoveInvalidShares' => $baseDir . '/../lib/Command/RemoveInvalidShares.php', 'OCA\\DAV\\Command\\SyncBirthdayCalendar' => $baseDir . '/../lib/Command/SyncBirthdayCalendar.php', 'OCA\\DAV\\Command\\SyncSystemAddressBook' => $baseDir . '/../lib/Command/SyncSystemAddressBook.php', 'OCA\\DAV\\Comments\\CommentNode' => $baseDir . '/../lib/Comments/CommentNode.php', diff --git a/apps/dav/composer/composer/autoload_static.php b/apps/dav/composer/composer/autoload_static.php index 41fea367ee5..00a930ff6f7 100644 --- a/apps/dav/composer/composer/autoload_static.php +++ b/apps/dav/composer/composer/autoload_static.php @@ -6,8 +6,11 @@ namespace Composer\Autoload; class ComposerStaticInitDAV { - public static $firstCharsPsr4 = array ( - 'O' => true, + public static $prefixLengthsPsr4 = array ( + 'O' => + array ( + 'OCA\\DAV\\' => 8, + ), ); public static $prefixDirsPsr4 = array ( @@ -76,6 +79,7 @@ class ComposerStaticInitDAV 'OCA\\DAV\\CardDAV\\Xml\\Groups' => __DIR__ . '/..' . '/../lib/CardDAV/Xml/Groups.php', 'OCA\\DAV\\Command\\CreateAddressBook' => __DIR__ . '/..' . '/../lib/Command/CreateAddressBook.php', 'OCA\\DAV\\Command\\CreateCalendar' => __DIR__ . '/..' . '/../lib/Command/CreateCalendar.php', + 'OCA\\DAV\\Command\\RemoveInvalidShares' => __DIR__ . '/..' . '/../lib/Command/RemoveInvalidShares.php', 'OCA\\DAV\\Command\\SyncBirthdayCalendar' => __DIR__ . '/..' . '/../lib/Command/SyncBirthdayCalendar.php', 'OCA\\DAV\\Command\\SyncSystemAddressBook' => __DIR__ . '/..' . '/../lib/Command/SyncSystemAddressBook.php', 'OCA\\DAV\\Comments\\CommentNode' => __DIR__ . '/..' . '/../lib/Comments/CommentNode.php', @@ -166,7 +170,7 @@ class ComposerStaticInitDAV public static function getInitializer(ClassLoader $loader) { return \Closure::bind(function () use ($loader) { - $loader->firstCharsPsr4 = ComposerStaticInitDAV::$firstCharsPsr4; + $loader->prefixLengthsPsr4 = ComposerStaticInitDAV::$prefixLengthsPsr4; $loader->prefixDirsPsr4 = ComposerStaticInitDAV::$prefixDirsPsr4; $loader->classMap = ComposerStaticInitDAV::$classMap; diff --git a/apps/dav/lib/CalDAV/Calendar.php b/apps/dav/lib/CalDAV/Calendar.php index 02808ab5662..a07bbe93218 100644 --- a/apps/dav/lib/CalDAV/Calendar.php +++ b/apps/dav/lib/CalDAV/Calendar.php @@ -203,7 +203,7 @@ class Calendar extends \Sabre\CalDAV\Calendar implements IShareable { } $this->caldavBackend->updateShares($this, [], [ - 'href' => $principal + $principal ]); return; } diff --git a/apps/dav/lib/CardDAV/AddressBook.php b/apps/dav/lib/CardDAV/AddressBook.php index a034f8b9426..71202319874 100644 --- a/apps/dav/lib/CardDAV/AddressBook.php +++ b/apps/dav/lib/CardDAV/AddressBook.php @@ -181,7 +181,7 @@ class AddressBook extends \Sabre\CardDAV\AddressBook implements IShareable { } $this->carddavBackend->updateShares($this, [], [ - 'href' => $principal + $principal ]); return; } diff --git a/apps/dav/lib/Command/RemoveInvalidShares.php b/apps/dav/lib/Command/RemoveInvalidShares.php new file mode 100644 index 00000000000..a42396cf5b5 --- /dev/null +++ b/apps/dav/lib/Command/RemoveInvalidShares.php @@ -0,0 +1,81 @@ +<?php +/** + * @author Thomas Müller <thomas.mueller@tmit.eu> + * + * @copyright Copyright (c) 2018, ownCloud GmbH + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OCA\DAV\Command; + +use OCA\DAV\Connector\Sabre\Principal; +use OCP\IDBConnection; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +/** + * Class RemoveInvalidShares - removes shared calendars and addressbook which + * have no matching principal. Happened because of a bug in the calendar app. + */ +class RemoveInvalidShares extends Command { + + /** @var IDBConnection */ + private $connection; + /** @var Principal */ + private $principalBackend; + + public function __construct(IDBConnection $connection, + Principal $principalBackend) { + parent::__construct(); + + $this->connection = $connection; + $this->principalBackend = $principalBackend; + } + + protected function configure() { + $this + ->setName('dav:remove-invalid-shares') + ->setDescription('Remove invalid dav shares'); + } + + protected function execute(InputInterface $input, OutputInterface $output) { + $query = $this->connection->getQueryBuilder(); + $result = $query->selectDistinct('principaluri') + ->from('dav_shares') + ->execute(); + + while($row = $result->fetch()) { + $principaluri = $row['principaluri']; + $p = $this->principalBackend->getPrincipalByPath($principaluri); + if ($p === null) { + $this->deleteSharesForPrincipal($principaluri); + } + } + + $result->closeCursor(); + } + + /** + * @param string $principaluri + */ + private function deleteSharesForPrincipal($principaluri) { + $delete = $this->connection->getQueryBuilder(); + $delete->delete('dav_shares') + ->where($delete->expr()->eq('principaluri', $delete->createNamedParameter($principaluri))); + $delete->execute(); + } +} diff --git a/apps/dav/lib/Connector/Sabre/Principal.php b/apps/dav/lib/Connector/Sabre/Principal.php index 06fb10f7e2c..250d67aafd1 100644 --- a/apps/dav/lib/Connector/Sabre/Principal.php +++ b/apps/dav/lib/Connector/Sabre/Principal.php @@ -325,6 +325,13 @@ class Principal implements BackendInterface { return $this->principalPrefix . '/' . $user->getUID(); } } + if (substr($uri, 0, 10) === 'principal:') { + $principal = substr($uri, 10); + $principal = $this->getPrincipalByPath($principal); + if ($principal !== null) { + return $principal['uri']; + } + } return null; } diff --git a/apps/dav/lib/DAV/Sharing/Backend.php b/apps/dav/lib/DAV/Sharing/Backend.php index 141c54899ad..030a3de3443 100644 --- a/apps/dav/lib/DAV/Sharing/Backend.php +++ b/apps/dav/lib/DAV/Sharing/Backend.php @@ -67,12 +67,18 @@ class Backend { * @param string[] $add * @param string[] $remove */ - public function updateShares($shareable, $add, $remove) { + public function updateShares(IShareable $shareable, $add, $remove) { foreach($add as $element) { - $this->shareWith($shareable, $element); + $principal = $this->principalBackend->findByUri($element['href'], ''); + if ($principal !== '') { + $this->shareWith($shareable, $element); + } } foreach($remove as $element) { - $this->unshare($shareable, $element); + $principal = $this->principalBackend->findByUri($element, ''); + if ($principal !== '') { + $this->unshare($shareable, $element); + } } } diff --git a/apps/dav/lib/Server.php b/apps/dav/lib/Server.php index a34ba94f21f..150b3dd78c1 100644 --- a/apps/dav/lib/Server.php +++ b/apps/dav/lib/Server.php @@ -73,14 +73,13 @@ class Server { private $baseUri; /** @var Connector\Sabre\Server */ - private $server; + public $server; public function __construct(IRequest $request, $baseUri) { $this->request = $request; $this->baseUri = $baseUri; $logger = \OC::$server->getLogger(); $dispatcher = \OC::$server->getEventDispatcher(); - $sendInvitations = \OC::$server->getConfig()->getAppValue('dav', 'sendInvitations', 'yes') === 'yes'; $root = new RootCollection(); $this->server = new \OCA\DAV\Connector\Sabre\Server(new CachingTree($root)); @@ -139,24 +138,29 @@ class Server { $this->server->addPlugin($acl); // calendar plugins - $this->server->addPlugin(new \OCA\DAV\CalDAV\Plugin()); - $this->server->addPlugin(new \Sabre\CalDAV\ICSExportPlugin()); - $this->server->addPlugin(new \OCA\DAV\CalDAV\Schedule\Plugin()); - if ($sendInvitations) { - $this->server->addPlugin(\OC::$server->query(\OCA\DAV\CalDAV\Schedule\IMipPlugin::class)); + if ($this->requestIsForSubtree(['calendars', 'principals'])) { + $this->server->addPlugin(new \OCA\DAV\CalDAV\Plugin()); + $this->server->addPlugin(new \Sabre\CalDAV\ICSExportPlugin()); + $this->server->addPlugin(new \OCA\DAV\CalDAV\Schedule\Plugin()); + if (\OC::$server->getConfig()->getAppValue('dav', 'sendInvitations', 'yes') === 'yes') { + $this->server->addPlugin(\OC::$server->query(\OCA\DAV\CalDAV\Schedule\IMipPlugin::class)); + } + $this->server->addPlugin(new \Sabre\CalDAV\Subscriptions\Plugin()); + $this->server->addPlugin(new \Sabre\CalDAV\Notifications\Plugin()); + $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest())); + $this->server->addPlugin(new \OCA\DAV\CalDAV\Publishing\PublishPlugin( + \OC::$server->getConfig(), + \OC::$server->getURLGenerator() + )); } - $this->server->addPlugin(new \Sabre\CalDAV\Subscriptions\Plugin()); - $this->server->addPlugin(new \Sabre\CalDAV\Notifications\Plugin()); - $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest())); - $this->server->addPlugin(new \OCA\DAV\CalDAV\Publishing\PublishPlugin( - \OC::$server->getConfig(), - \OC::$server->getURLGenerator() - )); // addressbook plugins - $this->server->addPlugin(new \OCA\DAV\CardDAV\Plugin()); - $this->server->addPlugin(new VCFExportPlugin()); - $this->server->addPlugin(new ImageExportPlugin(new PhotoCache(\OC::$server->getAppDataDir('dav-photocache')))); + if ($this->requestIsForSubtree(['addressbooks', 'principals'])) { + $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest())); + $this->server->addPlugin(new \OCA\DAV\CardDAV\Plugin()); + $this->server->addPlugin(new VCFExportPlugin()); + $this->server->addPlugin(new ImageExportPlugin(new PhotoCache(\OC::$server->getAppDataDir('dav-photocache')))); + } // system tags plugins $this->server->addPlugin(new SystemTagPlugin( @@ -282,4 +286,14 @@ class Server { public function exec() { $this->server->exec(); } + + private function requestIsForSubtree($subTrees) { + foreach ($subTrees as $subTree) { + $subTree = trim($subTree, ' /'); + if (strpos($this->server->getRequestUri(), $subTree.'/') === 0) { + return true; + } + } + return false; + } } diff --git a/apps/dav/tests/unit/CalDAV/AbstractCalDavBackend.php b/apps/dav/tests/unit/CalDAV/AbstractCalDavBackend.php index 2a01bd425c0..f30f4aa10f9 100644 --- a/apps/dav/tests/unit/CalDAV/AbstractCalDavBackend.php +++ b/apps/dav/tests/unit/CalDAV/AbstractCalDavBackend.php @@ -30,7 +30,9 @@ use OCA\DAV\Connector\Sabre\Principal; use OCP\IGroupManager; use OCP\ILogger; use OCP\IUserManager; +use OCP\IUserSession; use OCP\Security\ISecureRandom; +use OCP\Share\IManager as ShareManager; use Sabre\CalDAV\Xml\Property\SupportedCalendarComponentSet; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Test\TestCase; @@ -73,7 +75,12 @@ abstract class AbstractCalDavBackend extends TestCase { $this->groupManager = $this->createMock(IGroupManager::class); $this->dispatcher = $this->createMock(EventDispatcherInterface::class); $this->principal = $this->getMockBuilder(Principal::class) - ->disableOriginalConstructor() + ->setConstructorArgs([ + $this->userManager, + $this->groupManager, + $this->createMock(ShareManager::class), + $this->createMock(IUserSession::class), + ]) ->setMethods(['getPrincipalByPath', 'getGroupMembership']) ->getMock(); $this->principal->expects($this->any())->method('getPrincipalByPath') diff --git a/apps/dav/tests/unit/CardDAV/CardDavBackendTest.php b/apps/dav/tests/unit/CardDAV/CardDavBackendTest.php index 63e090873bb..920e5a4ec1f 100644 --- a/apps/dav/tests/unit/CardDAV/CardDavBackendTest.php +++ b/apps/dav/tests/unit/CardDAV/CardDavBackendTest.php @@ -40,6 +40,8 @@ use OCP\IDBConnection; use OCP\IGroupManager; use OCP\IL10N; use OCP\IUserManager; +use OCP\IUserSession; +use OCP\Share\IManager as ShareManager; use Sabre\DAV\PropPatch; use Sabre\VObject\Component\VCard; use Sabre\VObject\Property\Text; @@ -90,7 +92,12 @@ class CardDavBackendTest extends TestCase { $this->userManager = $this->createMock(IUserManager::class); $this->groupManager = $this->createMock(IGroupManager::class); $this->principal = $this->getMockBuilder(Principal::class) - ->disableOriginalConstructor() + ->setConstructorArgs([ + $this->userManager, + $this->groupManager, + $this->createMock(ShareManager::class), + $this->createMock(IUserSession::class), + ]) ->setMethods(['getPrincipalByPath', 'getGroupMembership']) ->getMock(); $this->principal->method('getPrincipalByPath') diff --git a/apps/dav/tests/unit/Command/RemoveInvalidSharesTest.php b/apps/dav/tests/unit/Command/RemoveInvalidSharesTest.php new file mode 100644 index 00000000000..2574e4d0aec --- /dev/null +++ b/apps/dav/tests/unit/Command/RemoveInvalidSharesTest.php @@ -0,0 +1,71 @@ +<?php +/** + * @author Thomas Müller <thomas.mueller@tmit.eu> + * + * @copyright Copyright (c) 2018, ownCloud GmbH + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + + +namespace OCA\DAV\Tests\Unit\Command; + + +use OCA\DAV\Connector\Sabre\Principal; +use OCA\DAV\Command\RemoveInvalidShares; +use OCP\Migration\IOutput; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Test\TestCase; + +/** + * Class RemoveInvalidSharesTest + * + * @package OCA\DAV\Tests\Unit\Repair + * @group DB + */ +class RemoveInvalidSharesTest extends TestCase { + + public function setUp() { + parent::setUp(); + $db = \OC::$server->getDatabaseConnection(); + + $db->insertIfNotExist('*PREFIX*dav_shares', [ + 'principaluri' => 'principal:unknown', + 'type' => 'calendar', + 'access' => 2, + 'resourceid' => 666, + ]); + } + + public function test() { + $db = \OC::$server->getDatabaseConnection(); + /** @var Principal | \PHPUnit_Framework_MockObject_MockObject $principal */ + $principal = $this->createMock(Principal::class); + + /** @var IOutput | \PHPUnit_Framework_MockObject_MockObject $output */ + $output = $this->createMock(IOutput::class); + + $repair = new RemoveInvalidShares($db, $principal); + $this->invokePrivate($repair, 'run', [$this->createMock(InputInterface::class), $this->createMock(OutputInterface::class)]); + + $query = $db->getQueryBuilder(); + $result = $query->select('*')->from('dav_shares') + ->where($query->expr()->eq('principaluri', $query->createNamedParameter('principal:unknown')))->execute(); + $data = $result->fetchAll(); + $result->closeCursor(); + $this->assertEquals(0, count($data)); + } +} diff --git a/apps/dav/tests/unit/ServerTest.php b/apps/dav/tests/unit/ServerTest.php index d502b24adcf..74939d43a14 100644 --- a/apps/dav/tests/unit/ServerTest.php +++ b/apps/dav/tests/unit/ServerTest.php @@ -38,10 +38,24 @@ use OCA\DAV\AppInfo\PluginManager; */ class ServerTest extends \Test\TestCase { - public function test() { - /** @var IRequest $r */ + /** + * @dataProvider providesUris + */ + public function test($uri, $plugins) { + /** @var IRequest | \PHPUnit_Framework_MockObject_MockObject $r */ $r = $this->createMock(IRequest::class); + $r->expects($this->any())->method('getRequestUri')->willReturn($uri); $s = new Server($r, '/'); - $this->assertInstanceOf('OCA\DAV\Server', $s); + $this->assertNotNull($s->server); + foreach ($plugins as $plugin) { + $this->assertNotNull($s->server->getPlugin($plugin)); + } + } + public function providesUris() { + return [ + 'principals' => ['principals/users/admin', ['caldav', 'oc-resource-sharing', 'carddav']], + 'calendars' => ['calendars/admin', ['caldav', 'oc-resource-sharing']], + 'addressbooks' => ['addressbooks/admin', ['carddav', 'oc-resource-sharing']], + ]; } } |