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.

OldGroupMembershipShares.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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;
  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. * @suppress SqlInjectionChecker
  61. */
  62. public function run(IOutput $output) {
  63. $deletedEntries = 0;
  64. $query = $this->connection->getQueryBuilder();
  65. $query->select('s1.id')->selectAlias('s1.share_with', 'user')->selectAlias('s2.share_with', 'group')
  66. ->from('share', 's1')
  67. ->where($query->expr()->isNotNull('s1.parent'))
  68. // \OC\Share\Constant::$shareTypeGroupUserUnique === 2
  69. ->andWhere($query->expr()->eq('s1.share_type', $query->expr()->literal(2)))
  70. ->andWhere($query->expr()->isNotNull('s2.id'))
  71. ->andWhere($query->expr()->eq('s2.share_type', $query->expr()->literal(Share::SHARE_TYPE_GROUP)))
  72. ->leftJoin('s1', 'share', 's2', $query->expr()->eq('s1.parent', 's2.id'));
  73. $deleteQuery = $this->connection->getQueryBuilder();
  74. $deleteQuery->delete('share')
  75. ->where($query->expr()->eq('id', $deleteQuery->createParameter('share')));
  76. $result = $query->execute();
  77. while ($row = $result->fetch()) {
  78. if (!$this->isMember($row['group'], $row['user'])) {
  79. $deletedEntries += $deleteQuery->setParameter('share', (int) $row['id'])
  80. ->execute();
  81. }
  82. }
  83. $result->closeCursor();
  84. if ($deletedEntries) {
  85. $output->info('Removed ' . $deletedEntries . ' shares where user is not a member of the group anymore');
  86. }
  87. }
  88. /**
  89. * @param string $gid
  90. * @param string $uid
  91. * @return bool
  92. */
  93. protected function isMember($gid, $uid) {
  94. if (isset($this->memberships[$gid][$uid])) {
  95. return $this->memberships[$gid][$uid];
  96. }
  97. $isMember = $this->groupManager->isInGroup($uid, $gid);
  98. if (!isset($this->memberships[$gid])) {
  99. $this->memberships[$gid] = [];
  100. }
  101. $this->memberships[$gid][$uid] = $isMember;
  102. return $isMember;
  103. }
  104. }