SharingMapper.php 4.4 KB

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