Skip to content
Prev Previous commit
Next Next commit
update shares directly in db
Signed-off-by: Robin Appelman <[email protected]>
  • Loading branch information
icewind1991 authored and backportbot-nextcloud[bot] committed Nov 8, 2022
commit b694313929602e9b7fef500ec3f994173ff92596
30 changes: 17 additions & 13 deletions core/Command/Maintenance/RepairShareOwnership.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,13 @@

class RepairShareOwnership extends Command {
private IDBConnection $dbConnection;
private IManager $shareManager;
private IUserManager $userManager;

public function __construct(
IDBConnection $dbConnection,
IManager $shareManager,
IUserManager $userManager
) {
$this->dbConnection = $dbConnection;
$this->shareManager = $shareManager;
$this->userManager = $userManager;
parent::__construct();
}
Expand All @@ -65,6 +62,7 @@ protected function configure() {
protected function execute(InputInterface $input, OutputInterface $output): int {
$dryRun = $input->getOption('dry-run');
$userId = $input->getArgument('user');
$this->dbConnection->beginTransaction();
if ($userId) {
$user = $this->userManager->get($userId);
if (!$user) {
Expand All @@ -86,13 +84,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
foreach ($found as $item) {
$output->writeln($item);
}
$this->dbConnection->commit();

return 0;
}

protected function repairWrongShareOwnershipForUser(IUser $user, bool $dryRun = true): array {
$qb = $this->dbConnection->getQueryBuilder();
$brokenShare = $qb
->select('s.id', 'm.user_id', 's.uid_owner', 's.uid_initiator', 's.share_with', 's.share_type', 's.file_target')
->select('s.id', 'm.user_id', 's.uid_owner', 's.uid_initiator', 's.share_with', 's.file_target')
->from('share', 's')
->join('s', 'filecache', 'f', $qb->expr()->eq('s.item_source', $qb->expr()->castColumn('f.fileid', IQueryBuilder::PARAM_STR)))
->join('s', 'mounts', 'm', $qb->expr()->eq('f.storage', 'm.storage_id'))
Expand All @@ -104,9 +104,14 @@ protected function repairWrongShareOwnershipForUser(IUser $user, bool $dryRun =

$found = [];

$update = $this->dbConnection->getQueryBuilder();
$update->update('share')
->set('uid_owner', $update->createParameter('share_owner'))
->set('uid_initiator', $update->createParameter('share_initiator'))
->where($update->expr()->eq('id', $update->createParameter('share_id')));

foreach ($brokenShare as $queryResult) {
$shareId = (int) $queryResult['id'];
$shareType = (int) $queryResult['share_type'];
$fileTarget = $queryResult['file_target'];
$initiator = $queryResult['uid_initiator'];
$receiver = $queryResult['share_with'];
Expand All @@ -119,15 +124,14 @@ protected function repairWrongShareOwnershipForUser(IUser $user, bool $dryRun =
continue;
}

$provider = $this->shareManager->getProviderForType($shareType);
$share = $provider->getShareById($shareId);

if ($share->getShareOwner() === $share->getSharedBy()) {
$share->setSharedBy($mountOwner);
$update->setParameter('share_id', $shareId, IQueryBuilder::PARAM_INT);
$update->setParameter('share_owner', $mountOwner);
if ($initiator === $owner) {
$update->setParameter('share_initiator', $mountOwner);
} else {
$update->setParameter('share_initiator', $initiator);
}
$share->setShareOwner($mountOwner);

$this->shareManager->updateShare($share);
$update->executeStatement();
}

return $found;
Expand Down