SharingService.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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(
  11. protected SharingMapper $mapper,
  12. ) {
  13. }
  14. public function getResourceType(): string {
  15. return $this->resourceType;
  16. }
  17. public function shareWith(int $resourceId, string $principal, int $access): void {
  18. // remove the share if it already exists
  19. $this->mapper->deleteShare($resourceId, $this->getResourceType(), $principal);
  20. $this->mapper->share($resourceId, $this->getResourceType(), $access, $principal);
  21. }
  22. public function unshare(int $resourceId, string $principal): void {
  23. $this->mapper->unshare($resourceId, $this->getResourceType(), $principal);
  24. }
  25. public function deleteShare(int $resourceId, string $principal): void {
  26. $this->mapper->deleteShare($resourceId, $this->getResourceType(), $principal);
  27. }
  28. public function deleteAllShares(int $resourceId): void {
  29. $this->mapper->deleteAllShares($resourceId, $this->getResourceType());
  30. }
  31. public function deleteAllSharesByUser(string $principaluri): void {
  32. $this->mapper->deleteAllSharesByUser($principaluri, $this->getResourceType());
  33. }
  34. public function getShares(int $resourceId): array {
  35. return $this->mapper->getSharesForId($resourceId, $this->getResourceType());
  36. }
  37. public function getUnshares(int $resourceId): array {
  38. return $this->mapper->getUnsharesForId($resourceId, $this->getResourceType());
  39. }
  40. public function getSharesForIds(array $resourceIds): array {
  41. return $this->mapper->getSharesForIds($resourceIds, $this->getResourceType());
  42. }
  43. /**
  44. * @param array $oldShares
  45. * @return bool
  46. */
  47. public function hasGroupShare(array $oldShares): bool {
  48. return !empty(array_filter($oldShares, function (array $share) {
  49. return $share['{http://owncloud.org/ns}group-share'] === true;
  50. }));
  51. }
  52. }