SharingService.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2024 Anna Larch <anna.larch@gmx.net>
  5. *
  6. * @author Anna Larch <anna.larch@gmx.net>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. namespace OCA\DAV\DAV\Sharing;
  22. abstract class SharingService {
  23. protected string $resourceType = '';
  24. public function __construct(protected SharingMapper $mapper) {
  25. }
  26. public function getResourceType(): string {
  27. return $this->resourceType;
  28. }
  29. public function shareWith(int $resourceId, string $principal, int $access): void {
  30. // remove the share if it already exists
  31. $this->mapper->deleteShare($resourceId, $this->getResourceType(), $principal);
  32. $this->mapper->share($resourceId, $this->getResourceType(), $access, $principal);
  33. }
  34. public function unshare(int $resourceId, string $principal): void {
  35. $this->mapper->unshare($resourceId, $this->getResourceType(), $principal);
  36. }
  37. public function deleteShare(int $resourceId, string $principal): void {
  38. $this->mapper->deleteShare($resourceId, $this->getResourceType(), $principal);
  39. }
  40. public function deleteAllShares(int $resourceId): void {
  41. $this->mapper->deleteAllShares($resourceId, $this->getResourceType());
  42. }
  43. public function deleteAllSharesByUser(string $principaluri): void {
  44. $this->mapper->deleteAllSharesByUser($principaluri, $this->getResourceType());
  45. }
  46. public function getShares(int $resourceId): array {
  47. return $this->mapper->getSharesForId($resourceId, $this->getResourceType());
  48. }
  49. public function getSharesForIds(array $resourceIds): array {
  50. return $this->mapper->getSharesForIds($resourceIds, $this->getResourceType());
  51. }
  52. /**
  53. * @param array $oldShares
  54. * @return bool
  55. */
  56. public function hasGroupShare(array $oldShares): bool {
  57. return !empty(array_filter($oldShares, function (array $share) {
  58. return $share['{http://owncloud.org/ns}group-share'] === true;
  59. }));
  60. }
  61. }