SharingMapper.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. use OCP\DB\QueryBuilder\IQueryBuilder;
  9. use OCP\IDBConnection;
  10. class SharingMapper {
  11. public function __construct(
  12. private IDBConnection $db,
  13. ) {
  14. }
  15. protected function getSharesForIdByAccess(int $resourceId, string $resourceType, bool $sharesWithAccess): array {
  16. $query = $this->db->getQueryBuilder();
  17. $query->select(['principaluri', 'access'])
  18. ->from('dav_shares')
  19. ->where($query->expr()->eq('resourceid', $query->createNamedParameter($resourceId, IQueryBuilder::PARAM_INT)))
  20. ->andWhere($query->expr()->eq('type', $query->createNamedParameter($resourceType, IQueryBuilder::PARAM_STR)))
  21. ->groupBy(['principaluri', 'access']);
  22. if ($sharesWithAccess) {
  23. $query->andWhere($query->expr()->neq('access', $query->createNamedParameter(Backend::ACCESS_UNSHARED, IQueryBuilder::PARAM_INT)));
  24. } else {
  25. $query->andWhere($query->expr()->eq('access', $query->createNamedParameter(Backend::ACCESS_UNSHARED, IQueryBuilder::PARAM_INT)));
  26. }
  27. $result = $query->executeQuery();
  28. $rows = $result->fetchAll();
  29. $result->closeCursor();
  30. return $rows;
  31. }
  32. public function getSharesForId(int $resourceId, string $resourceType): array {
  33. return $this->getSharesForIdByAccess($resourceId, $resourceType, true);
  34. }
  35. public function getUnsharesForId(int $resourceId, string $resourceType): array {
  36. return $this->getSharesForIdByAccess($resourceId, $resourceType, false);
  37. }
  38. public function getSharesForIds(array $resourceIds, string $resourceType): array {
  39. $query = $this->db->getQueryBuilder();
  40. $result = $query->select(['resourceid', 'principaluri', 'access'])
  41. ->from('dav_shares')
  42. ->where($query->expr()->in('resourceid', $query->createNamedParameter($resourceIds, IQueryBuilder::PARAM_INT_ARRAY)))
  43. ->andWhere($query->expr()->eq('type', $query->createNamedParameter($resourceType)))
  44. ->andWhere($query->expr()->neq('access', $query->createNamedParameter(Backend::ACCESS_UNSHARED, IQueryBuilder::PARAM_INT)))
  45. ->groupBy(['principaluri', 'access', 'resourceid'])
  46. ->executeQuery();
  47. $rows = $result->fetchAll();
  48. $result->closeCursor();
  49. return $rows;
  50. }
  51. public function unshare(int $resourceId, string $resourceType, string $principal): void {
  52. $query = $this->db->getQueryBuilder();
  53. $query->insert('dav_shares')
  54. ->values([
  55. 'principaluri' => $query->createNamedParameter($principal),
  56. 'type' => $query->createNamedParameter($resourceType),
  57. 'access' => $query->createNamedParameter(Backend::ACCESS_UNSHARED),
  58. 'resourceid' => $query->createNamedParameter($resourceId)
  59. ]);
  60. $query->executeStatement();
  61. }
  62. public function share(int $resourceId, string $resourceType, int $access, string $principal): void {
  63. $query = $this->db->getQueryBuilder();
  64. $query->insert('dav_shares')
  65. ->values([
  66. 'principaluri' => $query->createNamedParameter($principal),
  67. 'type' => $query->createNamedParameter($resourceType),
  68. 'access' => $query->createNamedParameter($access),
  69. 'resourceid' => $query->createNamedParameter($resourceId)
  70. ]);
  71. $query->executeStatement();
  72. }
  73. public function deleteShare(int $resourceId, string $resourceType, string $principal): void {
  74. $query = $this->db->getQueryBuilder();
  75. $query->delete('dav_shares');
  76. $query->where(
  77. $query->expr()->eq('resourceid', $query->createNamedParameter($resourceId, IQueryBuilder::PARAM_INT)),
  78. $query->expr()->eq('type', $query->createNamedParameter($resourceType)),
  79. $query->expr()->eq('principaluri', $query->createNamedParameter($principal))
  80. );
  81. $query->executeStatement();
  82. }
  83. public function deleteAllShares(int $resourceId, string $resourceType): void {
  84. $query = $this->db->getQueryBuilder();
  85. $query->delete('dav_shares')
  86. ->where($query->expr()->eq('resourceid', $query->createNamedParameter($resourceId)))
  87. ->andWhere($query->expr()->eq('type', $query->createNamedParameter($resourceType)))
  88. ->executeStatement();
  89. }
  90. public function deleteAllSharesByUser(string $principaluri, string $resourceType): void {
  91. $query = $this->db->getQueryBuilder();
  92. $query->delete('dav_shares')
  93. ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principaluri)))
  94. ->andWhere($query->expr()->eq('type', $query->createNamedParameter($resourceType)))
  95. ->executeStatement();
  96. }
  97. }