RepairUnmergedShares.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <?php
  2. /**
  3. * @author Vincent Petry <pvince81@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2016, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace OC\Repair;
  22. use OCP\Migration\IOutput;
  23. use OCP\Migration\IRepairStep;
  24. use OC\Share\Constants;
  25. use OCP\DB\QueryBuilder\IQueryBuilder;
  26. use OCP\IConfig;
  27. use OCP\IDBConnection;
  28. use OCP\IUserManager;
  29. use OCP\IUser;
  30. use OCP\IGroupManager;
  31. use OC\Share20\DefaultShareProvider;
  32. /**
  33. * Repairs shares for which the received folder was not properly deduplicated.
  34. *
  35. * An unmerged share can for example happen when sharing a folder with the same
  36. * user through multiple ways, like several groups and also directly, additionally
  37. * to group shares. Since 9.0.0 these would create duplicate entries "folder (2)",
  38. * one for every share. This repair step rearranges them so they only appear as a single
  39. * folder.
  40. */
  41. class RepairUnmergedShares implements IRepairStep {
  42. /** @var \OCP\IConfig */
  43. protected $config;
  44. /** @var \OCP\IDBConnection */
  45. protected $connection;
  46. /** @var IUserManager */
  47. protected $userManager;
  48. /** @var IGroupManager */
  49. protected $groupManager;
  50. /** @var IQueryBuilder */
  51. private $queryGetSharesWithUsers;
  52. /** @var IQueryBuilder */
  53. private $queryUpdateSharePermissionsAndTarget;
  54. /** @var IQueryBuilder */
  55. private $queryUpdateShareInBatch;
  56. /**
  57. * @param \OCP\IConfig $config
  58. * @param \OCP\IDBConnection $connection
  59. */
  60. public function __construct(
  61. IConfig $config,
  62. IDBConnection $connection,
  63. IUserManager $userManager,
  64. IGroupManager $groupManager
  65. ) {
  66. $this->connection = $connection;
  67. $this->config = $config;
  68. $this->userManager = $userManager;
  69. $this->groupManager = $groupManager;
  70. }
  71. public function getName() {
  72. return 'Repair unmerged shares';
  73. }
  74. /**
  75. * Builds prepared queries for reuse
  76. */
  77. private function buildPreparedQueries() {
  78. /**
  79. * Retrieve shares for a given user/group and share type
  80. */
  81. $query = $this->connection->getQueryBuilder();
  82. $query
  83. ->select('item_source', 'id', 'file_target', 'permissions', 'parent', 'share_type', 'stime')
  84. ->from('share')
  85. ->where($query->expr()->eq('share_type', $query->createParameter('shareType')))
  86. ->andWhere($query->expr()->in('share_with', $query->createParameter('shareWiths')))
  87. ->andWhere($query->expr()->in('item_type', $query->createParameter('itemTypes')))
  88. ->orderBy('item_source', 'ASC')
  89. ->addOrderBy('stime', 'ASC');
  90. $this->queryGetSharesWithUsers = $query;
  91. /**
  92. * Updates the file_target to the given value for all given share ids.
  93. *
  94. * This updates several shares in bulk which is faster than individually.
  95. */
  96. $query = $this->connection->getQueryBuilder();
  97. $query->update('share')
  98. ->set('file_target', $query->createParameter('file_target'))
  99. ->where($query->expr()->in('id', $query->createParameter('ids')));
  100. $this->queryUpdateShareInBatch = $query;
  101. /**
  102. * Updates the share permissions and target path of a single share.
  103. */
  104. $query = $this->connection->getQueryBuilder();
  105. $query->update('share')
  106. ->set('permissions', $query->createParameter('permissions'))
  107. ->set('file_target', $query->createParameter('file_target'))
  108. ->where($query->expr()->eq('id', $query->createParameter('shareid')));
  109. $this->queryUpdateSharePermissionsAndTarget = $query;
  110. }
  111. private function getSharesWithUser($shareType, $shareWiths) {
  112. $groupedShares = [];
  113. $query = $this->queryGetSharesWithUsers;
  114. $query->setParameter('shareWiths', $shareWiths, IQueryBuilder::PARAM_STR_ARRAY);
  115. $query->setParameter('shareType', $shareType);
  116. $query->setParameter('itemTypes', ['file', 'folder'], IQueryBuilder::PARAM_STR_ARRAY);
  117. $shares = $query->execute()->fetchAll();
  118. // group by item_source
  119. foreach ($shares as $share) {
  120. if (!isset($groupedShares[$share['item_source']])) {
  121. $groupedShares[$share['item_source']] = [];
  122. }
  123. $groupedShares[$share['item_source']][] = $share;
  124. }
  125. return $groupedShares;
  126. }
  127. private function isPotentialDuplicateName($name) {
  128. return (preg_match('/\(\d+\)(\.[^\.]+)?$/', $name) === 1);
  129. }
  130. /**
  131. * Decide on the best target name based on all group shares and subshares,
  132. * goal is to increase the likeliness that the chosen name matches what
  133. * the user is expecting.
  134. *
  135. * For this, we discard the entries with parenthesis "(2)".
  136. * In case the user also renamed the duplicates to a legitimate name, this logic
  137. * will still pick the most recent one as it's the one the user is most likely to
  138. * remember renaming.
  139. *
  140. * If no suitable subshare is found, use the least recent group share instead.
  141. *
  142. * @param array $groupShares group share entries
  143. * @param array $subShares sub share entries
  144. *
  145. * @return string chosen target name
  146. */
  147. private function findBestTargetName($groupShares, $subShares) {
  148. $pickedShare = null;
  149. // sort by stime, this also properly sorts the direct user share if any
  150. @usort($subShares, function($a, $b) {
  151. return ((int)$a['stime'] - (int)$b['stime']);
  152. });
  153. foreach ($subShares as $subShare) {
  154. // skip entries that have parenthesis with numbers
  155. if ($this->isPotentialDuplicateName($subShare['file_target'])) {
  156. continue;
  157. }
  158. // pick any share found that would match, the last being the most recent
  159. $pickedShare = $subShare;
  160. }
  161. // no suitable subshare found
  162. if ($pickedShare === null) {
  163. // use least recent group share target instead
  164. $pickedShare = $groupShares[0];
  165. }
  166. return $pickedShare['file_target'];
  167. }
  168. /**
  169. * Fix the given received share represented by the set of group shares
  170. * and matching sub shares
  171. *
  172. * @param array $groupShares group share entries
  173. * @param array $subShares sub share entries
  174. *
  175. * @return boolean false if the share was not repaired, true if it was
  176. */
  177. private function fixThisShare($groupShares, $subShares) {
  178. if (empty($subShares)) {
  179. return false;
  180. }
  181. $groupSharesById = [];
  182. foreach ($groupShares as $groupShare) {
  183. $groupSharesById[$groupShare['id']] = $groupShare;
  184. }
  185. if ($this->isThisShareValid($groupSharesById, $subShares)) {
  186. return false;
  187. }
  188. $targetPath = $this->findBestTargetName($groupShares, $subShares);
  189. // check whether the user opted out completely of all subshares
  190. $optedOut = true;
  191. foreach ($subShares as $subShare) {
  192. if ((int)$subShare['permissions'] !== 0) {
  193. $optedOut = false;
  194. break;
  195. }
  196. }
  197. $shareIds = [];
  198. foreach ($subShares as $subShare) {
  199. // only if the user deleted some subshares but not all, adjust the permissions of that subshare
  200. if (!$optedOut && (int)$subShare['permissions'] === 0 && (int)$subShare['share_type'] === DefaultShareProvider::SHARE_TYPE_USERGROUP) {
  201. // set permissions from parent group share
  202. $permissions = $groupSharesById[$subShare['parent']]['permissions'];
  203. // fix permissions and target directly
  204. $query = $this->queryUpdateSharePermissionsAndTarget;
  205. $query->setParameter('shareid', $subShare['id']);
  206. $query->setParameter('file_target', $targetPath);
  207. $query->setParameter('permissions', $permissions);
  208. $query->execute();
  209. } else {
  210. // gather share ids for bulk target update
  211. if ($subShare['file_target'] !== $targetPath) {
  212. $shareIds[] = (int)$subShare['id'];
  213. }
  214. }
  215. }
  216. if (!empty($shareIds)) {
  217. $query = $this->queryUpdateShareInBatch;
  218. $query->setParameter('ids', $shareIds, IQueryBuilder::PARAM_INT_ARRAY);
  219. $query->setParameter('file_target', $targetPath);
  220. $query->execute();
  221. }
  222. return true;
  223. }
  224. /**
  225. * Checks whether the number of group shares is balanced with the child subshares.
  226. * If all group shares have exactly one subshare, and the target of every subshare
  227. * is the same, then the share is valid.
  228. * If however there is a group share entry that has no matching subshare, it means
  229. * we're in the bogus situation and the whole share must be repaired
  230. *
  231. * @param array $groupSharesById
  232. * @param array $subShares
  233. *
  234. * @return true if the share is valid, false if it needs repair
  235. */
  236. private function isThisShareValid($groupSharesById, $subShares) {
  237. $foundTargets = [];
  238. // every group share needs to have exactly one matching subshare
  239. foreach ($subShares as $subShare) {
  240. $foundTargets[$subShare['file_target']] = true;
  241. if (count($foundTargets) > 1) {
  242. // not all the same target path value => invalid
  243. return false;
  244. }
  245. if (isset($groupSharesById[$subShare['parent']])) {
  246. // remove it from the list as we found it
  247. unset($groupSharesById[$subShare['parent']]);
  248. }
  249. }
  250. // if we found one subshare per group entry, the set will be empty.
  251. // If not empty, it means that one of the group shares did not have
  252. // a matching subshare entry.
  253. return empty($groupSharesById);
  254. }
  255. /**
  256. * Detect unmerged received shares and merge them properly
  257. */
  258. private function fixUnmergedShares(IOutput $out, IUser $user) {
  259. $groups = $this->groupManager->getUserGroupIds($user);
  260. if (empty($groups)) {
  261. // user is in no groups, so can't have received group shares
  262. return;
  263. }
  264. // get all subshares grouped by item source
  265. $subSharesByItemSource = $this->getSharesWithUser(DefaultShareProvider::SHARE_TYPE_USERGROUP, [$user->getUID()]);
  266. // because sometimes one wants to give the user more permissions than the group share
  267. $userSharesByItemSource = $this->getSharesWithUser(Constants::SHARE_TYPE_USER, [$user->getUID()]);
  268. if (empty($subSharesByItemSource) && empty($userSharesByItemSource)) {
  269. // nothing to repair for this user, no need to do extra queries
  270. return;
  271. }
  272. $groupSharesByItemSource = $this->getSharesWithUser(Constants::SHARE_TYPE_GROUP, $groups);
  273. if (empty($groupSharesByItemSource) && empty($userSharesByItemSource)) {
  274. // nothing to repair for this user
  275. return;
  276. }
  277. foreach ($groupSharesByItemSource as $itemSource => $groupShares) {
  278. $subShares = [];
  279. if (isset($subSharesByItemSource[$itemSource])) {
  280. $subShares = $subSharesByItemSource[$itemSource];
  281. }
  282. if (isset($userSharesByItemSource[$itemSource])) {
  283. // add it to the subshares to get a similar treatment
  284. $subShares = array_merge($subShares, $userSharesByItemSource[$itemSource]);
  285. }
  286. $this->fixThisShare($groupShares, $subShares);
  287. }
  288. }
  289. public function run(IOutput $output) {
  290. $ocVersionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0');
  291. if (version_compare($ocVersionFromBeforeUpdate, '9.1.0.16', '<')) {
  292. // this situation was only possible between 9.0.0 and 9.0.3 included
  293. $function = function(IUser $user) use ($output) {
  294. $this->fixUnmergedShares($output, $user);
  295. $output->advance();
  296. };
  297. $this->buildPreparedQueries();
  298. $output->startProgress($this->userManager->countUsers());
  299. $this->userManager->callForAllUsers($function);
  300. $output->finishProgress();
  301. }
  302. }
  303. }