SharingMapper.php 3.8 KB

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