OldGroupMembershipShares.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Thomas Müller <thomas.mueller@tmit.eu>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\Repair;
  24. use OCP\IDBConnection;
  25. use OCP\IGroupManager;
  26. use OCP\Migration\IOutput;
  27. use OCP\Migration\IRepairStep;
  28. use OCP\Share\IShare;
  29. class OldGroupMembershipShares implements IRepairStep {
  30. /** @var \OCP\IDBConnection */
  31. protected $connection;
  32. /** @var \OCP\IGroupManager */
  33. protected $groupManager;
  34. /**
  35. * @var array [gid => [uid => (bool)]]
  36. */
  37. protected $memberships;
  38. /**
  39. * @param IDBConnection $connection
  40. * @param IGroupManager $groupManager
  41. */
  42. public function __construct(IDBConnection $connection, IGroupManager $groupManager) {
  43. $this->connection = $connection;
  44. $this->groupManager = $groupManager;
  45. }
  46. /**
  47. * Returns the step's name
  48. *
  49. * @return string
  50. */
  51. public function getName() {
  52. return 'Remove shares of old group memberships';
  53. }
  54. /**
  55. * Run repair step.
  56. * Must throw exception on error.
  57. *
  58. * @throws \Exception in case of failure
  59. */
  60. public function run(IOutput $output) {
  61. $deletedEntries = 0;
  62. $query = $this->connection->getQueryBuilder();
  63. $query->select('s1.id')->selectAlias('s1.share_with', 'user')->selectAlias('s2.share_with', 'group')
  64. ->from('share', 's1')
  65. ->where($query->expr()->isNotNull('s1.parent'))
  66. // \OC\Share\Constant::$shareTypeGroupUserUnique === 2
  67. ->andWhere($query->expr()->eq('s1.share_type', $query->expr()->literal(2)))
  68. ->andWhere($query->expr()->isNotNull('s2.id'))
  69. ->andWhere($query->expr()->eq('s2.share_type', $query->expr()->literal(IShare::TYPE_GROUP)))
  70. ->leftJoin('s1', 'share', 's2', $query->expr()->eq('s1.parent', 's2.id'));
  71. $deleteQuery = $this->connection->getQueryBuilder();
  72. $deleteQuery->delete('share')
  73. ->where($query->expr()->eq('id', $deleteQuery->createParameter('share')));
  74. $result = $query->execute();
  75. while ($row = $result->fetch()) {
  76. if (!$this->isMember($row['group'], $row['user'])) {
  77. $deletedEntries += $deleteQuery->setParameter('share', (int) $row['id'])
  78. ->execute();
  79. }
  80. }
  81. $result->closeCursor();
  82. if ($deletedEntries) {
  83. $output->info('Removed ' . $deletedEntries . ' shares where user is not a member of the group anymore');
  84. }
  85. }
  86. /**
  87. * @param string $gid
  88. * @param string $uid
  89. * @return bool
  90. */
  91. protected function isMember($gid, $uid) {
  92. if (isset($this->memberships[$gid][$uid])) {
  93. return $this->memberships[$gid][$uid];
  94. }
  95. $isMember = $this->groupManager->isInGroup($uid, $gid);
  96. if (!isset($this->memberships[$gid])) {
  97. $this->memberships[$gid] = [];
  98. }
  99. $this->memberships[$gid][$uid] = $isMember;
  100. return $isMember;
  101. }
  102. }