Skip to content

Commit

Permalink
fix duplicate name on fedcircle
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <[email protected]>
  • Loading branch information
ArtificialOwl committed Nov 6, 2024
1 parent 3419018 commit 40c68a2
Show file tree
Hide file tree
Showing 13 changed files with 379 additions and 182 deletions.
4 changes: 2 additions & 2 deletions lib/Db/CoreQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1564,7 +1564,7 @@ public function limitToShareOwner(
*
* @throws RequestBuilderException
*/
public function leftJoinMountpoint(string $aliasMount, string $aliasMountMemberships = '') {
public function leftJoinMountpoint(string $aliasMount, IFederatedUser $federatedUser, string $aliasMountMemberships = '') {
$expr = $this->expr();

$aliasMountpoint = $this->generateAlias($aliasMount, self::MOUNTPOINT);
Expand All @@ -1576,7 +1576,7 @@ public function leftJoinMountpoint(string $aliasMount, string $aliasMountMembers
$aliasMountMemberships, CoreRequestBuilder::TABLE_MOUNTPOINT, $aliasMountpoint,
$expr->andX(
$expr->eq($aliasMountpoint . '.mount_id', $aliasMount . '.mount_id'),
$expr->eq($aliasMountpoint . '.single_id', $aliasMountMemberships . '.single_id')
$expr->eq($aliasMountpoint . '.single_id', $this->createNamedParameter($federatedUser->getSingleId()))
)
);

Expand Down
52 changes: 52 additions & 0 deletions lib/Db/MountPointRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Circles\Db;

use OCA\Circles\Exceptions\MountNotFoundException;
use OCA\Circles\Model\Mountpoint;
use OCA\Circles\Tools\Traits\TStringTools;

class MountPointRequest extends MountPointRequestBuilder {
use TStringTools;

/**
* @param Mountpoint $mountpoint
*/
public function insert(Mountpoint $mountpoint): void {
$qb = $this->getMountPointInsertSql();

$hash = ($mountpoint->getMountPoint() === '-') ? '' : md5($mountpoint->getMountPoint());
$qb->setValue('mountpoint', $qb->createNamedParameter($mountpoint->getMountPoint()))
->setValue('mountpoint_hash', $qb->createNamedParameter($hash))
->setValue('mount_id', $qb->createNamedParameter($mountpoint->getMountId()))
->setValue('single_id', $qb->createNamedParameter($mountpoint->getSingleId()));
$qb->executeStatement();
}

/**
* @param Mountpoint $mountpoint
* @throws MountNotFoundException
*/
public function update(Mountpoint $mountpoint): void {
$qb = $this->getMountPointUpdateSql();

$hash = ($mountpoint->getMountPoint() === '-') ? '' : md5($mountpoint->getMountPoint());

$qb->set('mountpoint', $qb->createNamedParameter($mountpoint->getMountPoint()))
->set('mountpoint_hash', $qb->createNamedParameter($hash));
$qb->limit('mount_id', $mountpoint->getMountId());
$qb->limitToSingleId($mountpoint->getSingleId());
$nb = $qb->executeStatement();

if ($nb === 0) {
throw new MountNotFoundException('Mount not found');
}
}
}
66 changes: 66 additions & 0 deletions lib/Db/MountPointRequestBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);


/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Circles\Db;

use OCA\Circles\Exceptions\MountNotFoundException;
use OCA\Circles\Model\Mount;
use OCA\Circles\Model\Mountpoint;
use OCA\Circles\Tools\Exceptions\RowNotFoundException;

class MountPointRequestBuilder extends CoreRequestBuilder {
protected function getMountPointInsertSql(): CoreQueryBuilder {
$qb = $this->getQueryBuilder();
$qb->insert(self::TABLE_MOUNTPOINT);

return $qb;
}

protected function getMountPointUpdateSql(): CoreQueryBuilder {
$qb = $this->getQueryBuilder();
$qb->update(self::TABLE_MOUNTPOINT);

return $qb;
}

protected function getMountPointSelectSql(string $alias = CoreQueryBuilder::MOUNTPOINT): CoreQueryBuilder {
$qb = $this->getQueryBuilder();
$qb->generateSelect(self::TABLE_MOUNTPOINT, self::$tables[self::TABLE_MOUNTPOINT], $alias);

return $qb;
}

protected function getMountPointDeleteSql(): CoreQueryBuilder {
$qb = $this->getQueryBuilder();
$qb->delete(self::TABLE_MOUNTPOINT);

return $qb;
}

public function getItemFromRequest(CoreQueryBuilder $qb): MountPoint {
/** @var MountPoint $mountpoint */
try {
$mountpoint = $qb->asItem(MountPoint::class);
} catch (RowNotFoundException $e) {
throw new MountNotFoundException('Mount not found');
}

return $mountpoint;
}

/**
* @param CoreQueryBuilder $qb
*
* @return Mount[]
*/
public function getItemsFromRequest(CoreQueryBuilder $qb): array {
/** @var MountPoint[] $result */
return $qb->asItems(MountPoint::class);
}
}
6 changes: 3 additions & 3 deletions lib/Db/MountRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public function save(Mount $mount): void {
->setValue('single_id', $qb->createNamedParameter($mount->getOwner()->getSingleId()))
->setValue('token', $qb->createNamedParameter($mount->getToken()))
->setValue('parent', $qb->createNamedParameter($mount->getParent()))
->setValue('mountpoint', $qb->createNamedParameter($mount->getMountPoint()))
->setValue('mountpoint_hash', $qb->createNamedParameter(md5($mount->getMountPoint())));
->setValue('mountpoint', $qb->createNamedParameter($mount->getOriginalMountPoint()))
->setValue('mountpoint_hash', $qb->createNamedParameter(md5($mount->getOriginalMountPoint())));

$qb->execute();
}
Expand All @@ -63,7 +63,7 @@ public function getForUser(IFederatedUser $federatedUser): array {
$qb = $this->getMountSelectSql();
$qb->setOptions([CoreQueryBuilder::MOUNT], ['getData' => true]);
$qb->leftJoinMember(CoreQueryBuilder::MOUNT);
$qb->leftJoinMountpoint(CoreQueryBuilder::MOUNT);
$qb->leftJoinMountpoint(CoreQueryBuilder::MOUNT, $federatedUser);
$qb->limitToInitiator(CoreQueryBuilder::MOUNT, $federatedUser, 'circle_id');

return $this->getItemsFromRequest($qb);
Expand Down
14 changes: 14 additions & 0 deletions lib/Exceptions/MountPointNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);


/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Circles\Exceptions;

class MountPointNotFoundException extends FederatedItemNotFoundException {
}
42 changes: 42 additions & 0 deletions lib/Migration/Version0031Date20241105133904.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);


/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/


namespace OCA\Circles\Migration;

use Closure;
use Doctrine\DBAL\Schema\SchemaException;
use OCP\DB\ISchemaWrapper;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;
use Psr\Log\LoggerInterface;

class Version0031Date20241105133904 extends SimpleMigrationStep {
public function __construct(
private LoggerInterface $logger,
) {
}

public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();

try {
$table = $schema->getTable('circles_mountpoint');
if (!$table->hasIndex('dname')) {
$table->addUniqueIndex(['single_id', 'mountpoint_hash'], 'mp_sid_hash');
}
} catch (SchemaException $e) {
$this->logger->warning('Could not find circles_mountpoint', ['exception' => $e]);
}

return $schema;
}
}
14 changes: 12 additions & 2 deletions lib/Model/ModelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use OCA\Circles\Exceptions\FileCacheNotFoundException;
use OCA\Circles\Exceptions\MemberNotFoundException;
use OCA\Circles\Exceptions\MembershipNotFoundException;
use OCA\Circles\Exceptions\MountPointNotFoundException;
use OCA\Circles\Exceptions\OwnerNotFoundException;
use OCA\Circles\Exceptions\RemoteInstanceException;
use OCA\Circles\Exceptions\RemoteNotFoundException;
Expand Down Expand Up @@ -462,7 +463,7 @@ private function importIntoMount(
$member = new Member();
$member->importFromDatabase($data, $prefix);
$mount->setOwner($member);
} catch (MemberNotFoundException $e) {
} catch (MemberNotFoundException) {
}
break;

Expand All @@ -471,7 +472,16 @@ private function importIntoMount(
$initiator = new Member();
$initiator->importFromDatabase($data, $prefix);
$mount->setInitiator($initiator);
} catch (MemberNotFoundException $e) {
} catch (MemberNotFoundException) {
}
break;

case CoreQueryBuilder::MOUNTPOINT:
try {
$mountPoint = new Mountpoint();
$mountPoint->importFromDatabase($data, $prefix);
$mount->setAlternateMountPoint($mountPoint);
} catch (MountPointNotFoundException) {
}
break;
}
Expand Down
Loading

0 comments on commit 40c68a2

Please sign in to comment.