diff options
Diffstat (limited to 'lib/private/Collaboration/Resources')
-rw-r--r-- | lib/private/Collaboration/Resources/Collection.php | 82 | ||||
-rw-r--r-- | lib/private/Collaboration/Resources/Listener.php | 66 | ||||
-rw-r--r-- | lib/private/Collaboration/Resources/Manager.php | 99 | ||||
-rw-r--r-- | lib/private/Collaboration/Resources/ProviderManager.php | 46 | ||||
-rw-r--r-- | lib/private/Collaboration/Resources/Resource.php | 70 |
5 files changed, 81 insertions, 282 deletions
diff --git a/lib/private/Collaboration/Resources/Collection.php b/lib/private/Collaboration/Resources/Collection.php index 5bdc1489fb8..2481a3e9a09 100644 --- a/lib/private/Collaboration/Resources/Collection.php +++ b/lib/private/Collaboration/Resources/Collection.php @@ -3,29 +3,9 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2018 Joas Schilling <coding@schilljs.com> - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Joas Schilling <coding@schilljs.com> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * 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 - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ - namespace OC\Collaboration\Resources; use Doctrine\DBAL\Exception\ConstraintViolationException; @@ -38,47 +18,21 @@ use OCP\IDBConnection; use OCP\IUser; class Collection implements ICollection { - - /** @var IManager|Manager */ - protected $manager; - - /** @var IDBConnection */ - protected $connection; - - /** @var int */ - protected $id; - - /** @var string */ - protected $name; - - /** @var IUser|null */ - protected $userForAccess; - - /** @var bool|null */ - protected $access; - /** @var IResource[] */ - protected $resources; + protected array $resources = []; public function __construct( - IManager $manager, - IDBConnection $connection, - int $id, - string $name, - ?IUser $userForAccess = null, - ?bool $access = null + /** @var Manager $manager */ + protected IManager $manager, + protected IDBConnection $connection, + protected int $id, + protected string $name, + protected ?IUser $userForAccess = null, + protected ?bool $access = null, ) { - $this->manager = $manager; - $this->connection = $connection; - $this->id = $id; - $this->name = $name; - $this->userForAccess = $userForAccess; - $this->access = $access; - $this->resources = []; } /** - * @return int * @since 16.0.0 */ public function getId(): int { @@ -86,7 +40,6 @@ class Collection implements ICollection { } /** - * @return string * @since 16.0.0 */ public function getName(): string { @@ -94,7 +47,6 @@ class Collection implements ICollection { } /** - * @param string $name * @since 16.0.0 */ public function setName(string $name): void { @@ -102,7 +54,7 @@ class Collection implements ICollection { $query->update(Manager::TABLE_COLLECTIONS) ->set('name', $query->createNamedParameter($name)) ->where($query->expr()->eq('id', $query->createNamedParameter($this->getId(), IQueryBuilder::PARAM_INT))); - $query->execute(); + $query->executeStatement(); $this->name = $name; } @@ -122,7 +74,6 @@ class Collection implements ICollection { /** * Adds a resource to a collection * - * @param IResource $resource * @throws ResourceException when the resource is already part of the collection * @since 16.0.0 */ @@ -155,7 +106,6 @@ class Collection implements ICollection { /** * Removes a resource from a collection * - * @param IResource $resource * @since 16.0.0 */ public function removeResource(IResource $resource): void { @@ -168,7 +118,7 @@ class Collection implements ICollection { ->where($query->expr()->eq('collection_id', $query->createNamedParameter($this->id, IQueryBuilder::PARAM_INT))) ->andWhere($query->expr()->eq('resource_type', $query->createNamedParameter($resource->getType()))) ->andWhere($query->expr()->eq('resource_id', $query->createNamedParameter($resource->getId()))); - $query->execute(); + $query->executeStatement(); if (empty($this->resources)) { $this->removeCollection(); @@ -180,8 +130,6 @@ class Collection implements ICollection { /** * Can a user/guest access the collection * - * @param IUser|null $user - * @return bool * @since 16.0.0 */ public function canAccess(?IUser $user): bool { @@ -216,15 +164,15 @@ class Collection implements ICollection { } protected function isSameResource(IResource $resource1, IResource $resource2): bool { - return $resource1->getType() === $resource2->getType() && - $resource1->getId() === $resource2->getId(); + return $resource1->getType() === $resource2->getType() + && $resource1->getId() === $resource2->getId(); } protected function removeCollection(): void { $query = $this->connection->getQueryBuilder(); $query->delete(Manager::TABLE_COLLECTIONS) ->where($query->expr()->eq('id', $query->createNamedParameter($this->id, IQueryBuilder::PARAM_INT))); - $query->execute(); + $query->executeStatement(); $this->manager->invalidateAccessCacheForCollection($this); $this->id = 0; diff --git a/lib/private/Collaboration/Resources/Listener.php b/lib/private/Collaboration/Resources/Listener.php index ffb5001e801..dfdde24d78e 100644 --- a/lib/private/Collaboration/Resources/Listener.php +++ b/lib/private/Collaboration/Resources/Listener.php @@ -3,63 +3,47 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2019 Joas Schilling <coding@schilljs.com> - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Joas Schilling <coding@schilljs.com> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * 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 - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ - namespace OC\Collaboration\Resources; use OCP\Collaboration\Resources\IManager; -use OCP\IGroup; -use OCP\IUser; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\EventDispatcher\GenericEvent; +use OCP\EventDispatcher\IEventDispatcher; +use OCP\Group\Events\BeforeGroupDeletedEvent; +use OCP\Group\Events\UserAddedEvent; +use OCP\Group\Events\UserRemovedEvent; +use OCP\User\Events\UserDeletedEvent; class Listener { - public static function register(EventDispatcherInterface $dispatcher): void { - $listener = function (GenericEvent $event) { - /** @var IUser $user */ - $user = $event->getArgument('user'); + public static function register(IEventDispatcher $eventDispatcher): void { + $eventDispatcher->addListener(UserAddedEvent::class, function (UserAddedEvent $event) { + $user = $event->getUser(); + /** @var IManager $resourceManager */ + $resourceManager = \OCP\Server::get(IManager::class); + + $resourceManager->invalidateAccessCacheForUser($user); + }); + $eventDispatcher->addListener(UserRemovedEvent::class, function (UserRemovedEvent $event) { + $user = $event->getUser(); /** @var IManager $resourceManager */ - $resourceManager = \OC::$server->query(IManager::class); + $resourceManager = \OCP\Server::get(IManager::class); $resourceManager->invalidateAccessCacheForUser($user); - }; - $dispatcher->addListener(IGroup::class . '::postAddUser', $listener); - $dispatcher->addListener(IGroup::class . '::postRemoveUser', $listener); + }); - $dispatcher->addListener(IUser::class . '::postDelete', function (GenericEvent $event) { - /** @var IUser $user */ - $user = $event->getSubject(); + $eventDispatcher->addListener(UserDeletedEvent::class, function (UserDeletedEvent $event) { + $user = $event->getUser(); /** @var IManager $resourceManager */ - $resourceManager = \OC::$server->query(IManager::class); + $resourceManager = \OCP\Server::get(IManager::class); $resourceManager->invalidateAccessCacheForUser($user); }); - $dispatcher->addListener(IGroup::class . '::preDelete', function (GenericEvent $event) { - /** @var IGroup $group */ - $group = $event->getSubject(); + $eventDispatcher->addListener(BeforeGroupDeletedEvent::class, function (BeforeGroupDeletedEvent $event) { + $group = $event->getGroup(); /** @var IManager $resourceManager */ - $resourceManager = \OC::$server->query(IManager::class); + $resourceManager = \OCP\Server::get(IManager::class); foreach ($group->getUsers() as $user) { $resourceManager->invalidateAccessCacheForUser($user); diff --git a/lib/private/Collaboration/Resources/Manager.php b/lib/private/Collaboration/Resources/Manager.php index 97213c45669..8d1e4b13287 100644 --- a/lib/private/Collaboration/Resources/Manager.php +++ b/lib/private/Collaboration/Resources/Manager.php @@ -3,29 +3,9 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2018 Joas Schilling <coding@schilljs.com> - * - * @author Daniel Kesselberg <mail@danielkesselberg.de> - * @author Joas Schilling <coding@schilljs.com> - * @author Julius Härtl <jus@bitgrid.net> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * 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 - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ - namespace OC\Collaboration\Resources; use Doctrine\DBAL\Exception\UniqueConstraintViolationException; @@ -38,34 +18,25 @@ use OCP\Collaboration\Resources\IResource; use OCP\Collaboration\Resources\ResourceException; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; -use OCP\ILogger; use OCP\IUser; +use Psr\Log\LoggerInterface; class Manager implements IManager { public const TABLE_COLLECTIONS = 'collres_collections'; public const TABLE_RESOURCES = 'collres_resources'; public const TABLE_ACCESS_CACHE = 'collres_accesscache'; - /** @var IDBConnection */ - protected $connection; - /** @var IProviderManager */ - protected $providerManager; - /** @var ILogger */ - protected $logger; - /** @var string[] */ - protected $providers = []; - + protected array $providers = []; - public function __construct(IDBConnection $connection, IProviderManager $providerManager, ILogger $logger) { - $this->connection = $connection; - $this->providerManager = $providerManager; - $this->logger = $logger; + public function __construct( + protected IDBConnection $connection, + protected IProviderManager $providerManager, + protected LoggerInterface $logger, + ) { } /** - * @param int $id - * @return ICollection * @throws CollectionException when the collection could not be found * @since 16.0.0 */ @@ -82,13 +53,10 @@ class Manager implements IManager { throw new CollectionException('Collection not found'); } - return new Collection($this, $this->connection, (int) $row['id'], (string) $row['name']); + return new Collection($this, $this->connection, (int)$row['id'], (string)$row['name']); } /** - * @param int $id - * @param IUser|null $user - * @return ICollection * @throws CollectionException when the collection could not be found * @since 16.0.0 */ @@ -114,25 +82,21 @@ class Manager implements IManager { throw new CollectionException('Collection not found'); } - $access = $row['access'] === null ? null : (bool) $row['access']; + $access = $row['access'] === null ? null : (bool)$row['access']; if ($user instanceof IUser) { - return new Collection($this, $this->connection, (int) $row['id'], (string) $row['name'], $user, $access); + return new Collection($this, $this->connection, (int)$row['id'], (string)$row['name'], $user, $access); } - return new Collection($this, $this->connection, (int) $row['id'], (string) $row['name'], $user, $access); + return new Collection($this, $this->connection, (int)$row['id'], (string)$row['name'], $user, $access); } /** - * @param IUser $user - * @param string $filter - * @param int $limit - * @param int $start * @return ICollection[] * @since 16.0.0 */ public function searchCollections(IUser $user, string $filter, int $limit = 50, int $start = 0): array { $query = $this->connection->getQueryBuilder(); - $userId = $user instanceof IUser ? $user->getUID() : ''; + $userId = $user->getUID(); $query->select('c.*', 'a.access') ->from(self::TABLE_COLLECTIONS, 'c') @@ -149,7 +113,7 @@ class Manager implements IManager { ->setFirstResult($start); if ($filter !== '') { - $query->where($query->expr()->iLike('c.name', $query->createNamedParameter('%' . $this->connection->escapeLikeParameter($filter) . '%'))); + $query->andWhere($query->expr()->iLike('c.name', $query->createNamedParameter('%' . $this->connection->escapeLikeParameter($filter) . '%'))); } $result = $query->execute(); @@ -158,7 +122,7 @@ class Manager implements IManager { $foundResults = 0; while ($row = $result->fetch()) { $foundResults++; - $access = $row['access'] === null ? null : (bool) $row['access']; + $access = $row['access'] === null ? null : (bool)$row['access']; $collection = new Collection($this, $this->connection, (int)$row['id'], (string)$row['name'], $user, $access); if ($collection->canAccess($user)) { $collections[] = $collection; @@ -174,8 +138,6 @@ class Manager implements IManager { } /** - * @param string $name - * @return ICollection * @since 16.0.0 */ public function newCollection(string $name): ICollection { @@ -190,9 +152,6 @@ class Manager implements IManager { } /** - * @param string $type - * @param string $id - * @return IResource * @since 16.0.0 */ public function createResource(string $type, string $id): IResource { @@ -200,10 +159,6 @@ class Manager implements IManager { } /** - * @param string $type - * @param string $id - * @param IUser|null $user - * @return IResource * @throws ResourceException * @since 16.0.0 */ @@ -231,7 +186,7 @@ class Manager implements IManager { throw new ResourceException('Resource not found'); } - $access = $row['access'] === null ? null : (bool) $row['access']; + $access = $row['access'] === null ? null : (bool)$row['access']; if ($user instanceof IUser) { return new Resource($this, $this->connection, $type, $id, $user, $access); } @@ -240,8 +195,6 @@ class Manager implements IManager { } /** - * @param ICollection $collection - * @param IUser|null $user * @return IResource[] * @since 16.0.0 */ @@ -264,7 +217,7 @@ class Manager implements IManager { $resources = []; $result = $query->execute(); while ($row = $result->fetch()) { - $access = $row['access'] === null ? null : (bool) $row['access']; + $access = $row['access'] === null ? null : (bool)$row['access']; $resources[] = new Resource($this, $this->connection, $row['resource_type'], $row['resource_id'], $user, $access); } $result->closeCursor(); @@ -275,8 +228,6 @@ class Manager implements IManager { /** * Get the rich object data of a resource * - * @param IResource $resource - * @return array * @since 16.0.0 */ public function getResourceRichObject(IResource $resource): array { @@ -295,9 +246,6 @@ class Manager implements IManager { /** * Can a user/guest access the collection * - * @param IResource $resource - * @param IUser|null $user - * @return bool * @since 16.0.0 */ public function canAccessResource(IResource $resource, ?IUser $user): bool { @@ -326,9 +274,6 @@ class Manager implements IManager { /** * Can a user/guest access the collection * - * @param ICollection $collection - * @param IUser|null $user - * @return bool * @since 16.0.0 */ public function canAccessCollection(ICollection $collection, ?IUser $user): bool { @@ -366,7 +311,7 @@ class Manager implements IManager { $hasAccess = null; $result = $query->execute(); if ($row = $result->fetch()) { - $hasAccess = (bool) $row['access']; + $hasAccess = (bool)$row['access']; } $result->closeCursor(); @@ -386,7 +331,7 @@ class Manager implements IManager { $hasAccess = null; $result = $query->execute(); if ($row = $result->fetch()) { - $hasAccess = (bool) $row['access']; + $hasAccess = (bool)$row['access']; } $result->closeCursor(); @@ -506,9 +451,6 @@ class Manager implements IManager { $query->execute(); } - /** - * @param string $provider - */ public function registerResourceProvider(string $provider): void { $this->logger->debug('\OC\Collaboration\Resources\Manager::registerResourceProvider is deprecated', ['provider' => $provider]); $this->providerManager->registerResourceProvider($provider); @@ -517,7 +459,6 @@ class Manager implements IManager { /** * Get the resource type of the provider * - * @return string * @since 16.0.0 */ public function getType(): string { diff --git a/lib/private/Collaboration/Resources/ProviderManager.php b/lib/private/Collaboration/Resources/ProviderManager.php index 095ffdcd8ee..0ce4ae7155a 100644 --- a/lib/private/Collaboration/Resources/ProviderManager.php +++ b/lib/private/Collaboration/Resources/ProviderManager.php @@ -3,52 +3,28 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2019 Daniel Kesselberg <mail@danielkesselberg.de> - * - * @author Daniel Kesselberg <mail@danielkesselberg.de> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * 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 - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ - namespace OC\Collaboration\Resources; use OCP\AppFramework\QueryException; use OCP\Collaboration\Resources\IProvider; use OCP\Collaboration\Resources\IProviderManager; -use OCP\ILogger; use OCP\IServerContainer; +use Psr\Log\LoggerInterface; class ProviderManager implements IProviderManager { - /** @var string[] */ - protected $providers = []; + protected array $providers = []; /** @var IProvider[] */ - protected $providerInstances = []; - - /** @var IServerContainer */ - protected $serverContainer; - - /** @var ILogger */ - protected $logger; + protected array $providerInstances = []; - public function __construct(IServerContainer $serverContainer, ILogger $logger) { - $this->serverContainer = $serverContainer; - $this->logger = $logger; + public function __construct( + protected IServerContainer $serverContainer, + protected LoggerInterface $logger, + ) { } public function getResourceProviders(): array { @@ -57,8 +33,8 @@ class ProviderManager implements IProviderManager { try { $this->providerInstances[] = $this->serverContainer->query($provider); } catch (QueryException $e) { - $this->logger->logException($e, [ - 'message' => "Could not query resource provider $provider: " . $e->getMessage() + $this->logger->error("Could not query resource provider $provider: " . $e->getMessage(), [ + 'exception' => $e, ]); } } diff --git a/lib/private/Collaboration/Resources/Resource.php b/lib/private/Collaboration/Resources/Resource.php index 702bc2faf46..19da3da7e7d 100644 --- a/lib/private/Collaboration/Resources/Resource.php +++ b/lib/private/Collaboration/Resources/Resource.php @@ -3,28 +3,9 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2018 Joas Schilling <coding@schilljs.com> - * - * @author Joas Schilling <coding@schilljs.com> - * @author Julius Härtl <jus@bitgrid.net> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * 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 - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ - namespace OC\Collaboration\Resources; use OCP\Collaboration\Resources\ICollection; @@ -34,46 +15,19 @@ use OCP\IDBConnection; use OCP\IUser; class Resource implements IResource { - - /** @var IManager */ - protected $manager; - - /** @var IDBConnection */ - protected $connection; - - /** @var string */ - protected $type; - - /** @var string */ - protected $id; - - /** @var IUser|null */ - protected $userForAccess; - - /** @var bool|null */ - protected $access; - - /** @var array|null */ - protected $data; + protected ?array $data = null; public function __construct( - IManager $manager, - IDBConnection $connection, - string $type, - string $id, - ?IUser $userForAccess = null, - ?bool $access = null + protected IManager $manager, + protected IDBConnection $connection, + protected string $type, + protected string $id, + protected ?IUser $userForAccess = null, + protected ?bool $access = null, ) { - $this->manager = $manager; - $this->connection = $connection; - $this->type = $type; - $this->id = $id; - $this->userForAccess = $userForAccess; - $this->access = $access; } /** - * @return string * @since 16.0.0 */ public function getType(): string { @@ -81,7 +35,6 @@ class Resource implements IResource { } /** - * @return string * @since 16.0.0 */ public function getId(): string { @@ -89,7 +42,6 @@ class Resource implements IResource { } /** - * @return array * @since 16.0.0 */ public function getRichObject(): array { @@ -103,8 +55,6 @@ class Resource implements IResource { /** * Can a user/guest access the resource * - * @param IUser|null $user - * @return bool * @since 16.0.0 */ public function canAccess(?IUser $user): bool { @@ -154,7 +104,7 @@ class Resource implements IResource { $result = $query->execute(); while ($row = $result->fetch()) { - $collections[] = $this->manager->getCollection((int) $row['collection_id']); + $collections[] = $this->manager->getCollection((int)$row['collection_id']); } $result->closeCursor(); |