You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.3 KiB

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