|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\Files_Sharing\Command; |
| 11 | + |
| 12 | +use OC\Core\Command\Base; |
| 13 | +use OCP\DB\QueryBuilder\IQueryBuilder; |
| 14 | +use OCP\IDBConnection; |
| 15 | +use OCP\Share\IShare; |
| 16 | +use Symfony\Component\Console\Input\InputInterface; |
| 17 | +use Symfony\Component\Console\Input\InputOption; |
| 18 | +use Symfony\Component\Console\Output\OutputInterface; |
| 19 | + |
| 20 | +/** |
| 21 | + * Fixes USERGROUP subshares that were created without `accepted = STATUS_ACCEPTED` |
| 22 | + * by the rename bug affecting ownCloud-migrated group shares. |
| 23 | + * |
| 24 | + * When an ownCloud-migrated group share (which has no per-user USERGROUP subshare) |
| 25 | + * is renamed for the first time, a new USERGROUP row is inserted without an |
| 26 | + * `accepted` value. The column defaults to 0 (STATUS_PENDING), causing |
| 27 | + * MountProvider to skip the share — the file disappears for the recipient. |
| 28 | + * |
| 29 | + * A USERGROUP subshare with permissions = 0 was explicitly declined by the user |
| 30 | + * and must not be touched. |
| 31 | + */ |
| 32 | +class FixOwncloudGroupShares extends Base { |
| 33 | + public function __construct( |
| 34 | + private IDBConnection $connection, |
| 35 | + ) { |
| 36 | + parent::__construct(); |
| 37 | + } |
| 38 | + |
| 39 | + protected function configure(): void { |
| 40 | + $this |
| 41 | + ->setName('sharing:fix-owncloud-group-shares') |
| 42 | + ->setDescription('Fix group share subshares left with accepted = STATUS_PENDING after renaming on an ownCloud-migrated instance') |
| 43 | + ->addOption( |
| 44 | + 'dry-run', |
| 45 | + null, |
| 46 | + InputOption::VALUE_NONE, |
| 47 | + 'Show how many shares would be fixed without making any changes', |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + public function execute(InputInterface $input, OutputInterface $output): int { |
| 52 | + $dryRun = $input->getOption('dry-run'); |
| 53 | + |
| 54 | + $qb = $this->connection->getQueryBuilder(); |
| 55 | + $count = (int)$qb->select($qb->func()->count('id')) |
| 56 | + ->from('share') |
| 57 | + ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_USERGROUP, IQueryBuilder::PARAM_INT))) |
| 58 | + ->andWhere($qb->expr()->eq('accepted', $qb->createNamedParameter(IShare::STATUS_PENDING, IQueryBuilder::PARAM_INT))) |
| 59 | + ->andWhere($qb->expr()->neq('permissions', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT))) |
| 60 | + ->executeQuery() |
| 61 | + ->fetchOne(); |
| 62 | + |
| 63 | + if ($count === 0) { |
| 64 | + $output->writeln('No affected group share subshares found.'); |
| 65 | + return self::SUCCESS; |
| 66 | + } |
| 67 | + |
| 68 | + if ($dryRun) { |
| 69 | + $output->writeln("Would fix <info>$count</info> group share subshare(s) (dry-run, no changes made)."); |
| 70 | + return self::SUCCESS; |
| 71 | + } |
| 72 | + |
| 73 | + $qb = $this->connection->getQueryBuilder(); |
| 74 | + $qb->update('share') |
| 75 | + ->set('accepted', $qb->createNamedParameter(IShare::STATUS_ACCEPTED, IQueryBuilder::PARAM_INT)) |
| 76 | + ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_USERGROUP, IQueryBuilder::PARAM_INT))) |
| 77 | + ->andWhere($qb->expr()->eq('accepted', $qb->createNamedParameter(IShare::STATUS_PENDING, IQueryBuilder::PARAM_INT))) |
| 78 | + ->andWhere($qb->expr()->neq('permissions', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT))) |
| 79 | + ->executeStatement(); |
| 80 | + |
| 81 | + $output->writeln("Fixed <info>$count</info> group share subshare(s)."); |
| 82 | + return self::SUCCESS; |
| 83 | + } |
| 84 | +} |
0 commit comments