SharingService.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\DAV\Sharing;
  8. abstract class SharingService {
  9. protected string $resourceType = '';
  10. public function __construct(protected SharingMapper $mapper) {
  11. }
  12. public function getResourceType(): string {
  13. return $this->resourceType;
  14. }
  15. public function shareWith(int $resourceId, string $principal, int $access): void {
  16. // remove the share if it already exists
  17. $this->mapper->deleteShare($resourceId, $this->getResourceType(), $principal);
  18. $this->mapper->share($resourceId, $this->getResourceType(), $access, $principal);
  19. }
  20. public function unshare(int $resourceId, string $principal): void {
  21. $this->mapper->unshare($resourceId, $this->getResourceType(), $principal);
  22. }
  23. public function deleteShare(int $resourceId, string $principal): void {
  24. $this->mapper->deleteShare($resourceId, $this->getResourceType(), $principal);
  25. }
  26. public function deleteAllShares(int $resourceId): void {
  27. $this->mapper->deleteAllShares($resourceId, $this->getResourceType());
  28. }
  29. public function deleteAllSharesByUser(string $principaluri): void {
  30. $this->mapper->deleteAllSharesByUser($principaluri, $this->getResourceType());
  31. }
  32. public function getShares(int $resourceId): array {
  33. return $this->mapper->getSharesForId($resourceId, $this->getResourceType());
  34. }
  35. public function getSharesForIds(array $resourceIds): array {
  36. return $this->mapper->getSharesForIds($resourceIds, $this->getResourceType());
  37. }
  38. /**
  39. * @param array $oldShares
  40. * @return bool
  41. */
  42. public function hasGroupShare(array $oldShares): bool {
  43. return !empty(array_filter($oldShares, function (array $share) {
  44. return $share['{http://owncloud.org/ns}group-share'] === true;
  45. }));
  46. }
  47. }