Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/sharing/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@
'OCA\\Sharing\\Controller\\ApiV1Controller' => $baseDir . '/../lib/Controller/ApiV1Controller.php',
'OCA\\Sharing\\Middleware\\ShareApiEnabledMiddleware' => $baseDir . '/../lib/Middleware/ShareApiEnabledMiddleware.php',
'OCA\\Sharing\\Migration\\Version1000Date20250929161325' => $baseDir . '/../lib/Migration/Version1000Date20250929161325.php',
'OCA\\Sharing\\Migration\\Version1000Date20260731171922' => $baseDir . '/../lib/Migration/Version1000Date20260731171922.php',
'OCA\\Sharing\\ResponseDefinitions' => $baseDir . '/../lib/ResponseDefinitions.php',
);
1 change: 1 addition & 0 deletions apps/sharing/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class ComposerStaticInitSharing
'OCA\\Sharing\\Controller\\ApiV1Controller' => __DIR__ . '/..' . '/../lib/Controller/ApiV1Controller.php',
'OCA\\Sharing\\Middleware\\ShareApiEnabledMiddleware' => __DIR__ . '/..' . '/../lib/Middleware/ShareApiEnabledMiddleware.php',
'OCA\\Sharing\\Migration\\Version1000Date20250929161325' => __DIR__ . '/..' . '/../lib/Migration/Version1000Date20250929161325.php',
'OCA\\Sharing\\Migration\\Version1000Date20260731171922' => __DIR__ . '/..' . '/../lib/Migration/Version1000Date20260731171922.php',
'OCA\\Sharing\\ResponseDefinitions' => __DIR__ . '/..' . '/../lib/ResponseDefinitions.php',
);

Expand Down
30 changes: 21 additions & 9 deletions apps/sharing/lib/Migration/Version1000Date20250929161325.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,17 @@ final class Version1000Date20250929161325 extends SimpleMigrationStep {
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
$schema = $schemaClosure();

// TODO: Add mapping table for class names
// TODO: Check indexes

$mappingTable = $schema->createTable('sharing_classmap');
$mappingTable->addColumn('class_id', Types::INTEGER, [
'autoincrement' => true,
'notnull' => true,
]);
$mappingTable->addColumn('class_name', Types::STRING, ['length' => 64]);
$mappingTable->setPrimaryKey(['class_id']);
$mappingTable->addUniqueIndex(['class_name']);

$shareTable = $schema->createTable('sharing_share');
$shareTable->addColumn('id', Types::BIGINT);
$shareTable->addColumn('owner_user_id', Types::STRING, ['length' => 64]);
Expand All @@ -39,38 +47,42 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt

$sourcesTable = $schema->createTable('sharing_share_sources');
$sourcesTable->addColumn('share_id', Types::BIGINT);
$sourcesTable->addColumn('source_class', Types::STRING, ['length' => 64]);
$sourcesTable->addColumn('source_class_id', Types::INTEGER);
$sourcesTable->addColumn('source_value', Types::STRING, ['length' => 255]);
$sourcesTable->setPrimaryKey(['share_id', 'source_class', 'source_value']);
$sourcesTable->setPrimaryKey(['share_id', 'source_class_id', 'source_value']);
$sourcesTable->addForeignKeyConstraint($shareTable->getName(), ['share_id'], ['id'], ['onDelete' => 'CASCADE']);
$sourcesTable->addForeignKeyConstraint($mappingTable->getName(), ['source_class_id'], ['class_id']);

// TODO: Add possibility to mask permissions for recipients. For reshares the user may only mask permissions for their child recipients, not their self recipients
$recipientsTable = $schema->createTable('sharing_share_recipients');
$recipientsTable->addColumn('share_id', Types::BIGINT);
$recipientsTable->addColumn('recipient_class', Types::STRING, ['length' => 64]);
$recipientsTable->addColumn('recipient_class_id', Types::INTEGER);
$recipientsTable->addColumn('recipient_value', Types::STRING, ['length' => 255]);
$recipientsTable->addColumn('recipient_instance', Types::STRING, ['length' => 128, 'notnull' => false]);
$recipientsTable->addColumn('recipient_secret', Types::STRING, ['length' => 32]);
$recipientsTable->addColumn('initiator_user_id', Types::STRING, ['length' => 64]);
$recipientsTable->addColumn('initiator_instance', Types::STRING, ['length' => 128, 'notnull' => false]);
$recipientsTable->setPrimaryKey(['share_id', 'recipient_class', 'recipient_value']);
$recipientsTable->setPrimaryKey(['share_id', 'recipient_class_id', 'recipient_value']);
$recipientsTable->addForeignKeyConstraint($shareTable->getName(), ['share_id'], ['id'], ['onDelete' => 'CASCADE']);
// TODO: Maybe needs composite index with share_id
$recipientsTable->addUniqueIndex(['recipient_secret']);
$recipientsTable->addForeignKeyConstraint($mappingTable->getName(), ['recipient_class_id'], ['class_id']);

$propertiesTable = $schema->createTable('sharing_share_properties');
$propertiesTable->addColumn('share_id', Types::BIGINT);
$propertiesTable->addColumn('property_class', Types::STRING, ['length' => 64]);
$propertiesTable->addColumn('property_class_id', Types::INTEGER);
$propertiesTable->addColumn('property_value', Types::STRING, ['length' => 1000, 'notnull' => false]);
$propertiesTable->setPrimaryKey(['share_id', 'property_class']);
$propertiesTable->setPrimaryKey(['share_id', 'property_class_id']);
$propertiesTable->addForeignKeyConstraint($shareTable->getName(), ['share_id'], ['id'], ['onDelete' => 'CASCADE']);
$propertiesTable->addForeignKeyConstraint($mappingTable->getName(), ['property_class_id'], ['class_id']);

$permissionsTable = $schema->createTable('sharing_share_permissions');
$permissionsTable->addColumn('share_id', Types::BIGINT);
$permissionsTable->addColumn('permission_class', Types::STRING, ['length' => 64]);
$permissionsTable->addColumn('permission_class_id', Types::INTEGER);
$permissionsTable->addColumn('permission_enabled', Types::BOOLEAN);
$permissionsTable->setPrimaryKey(['share_id', 'permission_class']);
$permissionsTable->setPrimaryKey(['share_id', 'permission_class_id']);
$permissionsTable->addForeignKeyConstraint($shareTable->getName(), ['share_id'], ['id'], ['onDelete' => 'CASCADE']);
$permissionsTable->addForeignKeyConstraint($mappingTable->getName(), ['permission_class_id'], ['class_id']);

return $schema;
}
Expand Down
105 changes: 105 additions & 0 deletions apps/sharing/lib/Migration/Version1000Date20260731171922.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

declare(strict_types=1);

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

namespace OCA\Sharing\Migration;

use Closure;
use Doctrine\DBAL\Schema\SchemaException;
use OCP\DB\ISchemaWrapper;
use OCP\DB\Types;
use OCP\Migration\Attributes\AddColumn;
use OCP\Migration\Attributes\AddIndex;
use OCP\Migration\Attributes\CreateTable;
use OCP\Migration\Attributes\DropColumn;
use OCP\Migration\Attributes\DropIndex;
use OCP\Migration\Attributes\IndexType;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;
use Override;

#[CreateTable(table: 'sharing_classmap')]
#[DropColumn(table: 'sharing_share_sources', name: 'source_class')]
#[DropColumn(table: 'sharing_share_recipients', name: 'recipient_class')]
#[DropColumn(table: 'sharing_share_properties', name: 'property_class')]
#[DropColumn(table: 'sharing_share_permissions', name: 'permission_class')]
#[AddColumn(table: 'sharing_share_sources', name: 'source_class_id')]
#[AddColumn(table: 'sharing_share_recipients', name: 'recipient_class_id')]
#[AddColumn(table: 'sharing_share_properties', name: 'property_class_id')]
#[AddColumn(table: 'sharing_share_permissions', name: 'permission_class_id')]
#[DropIndex(table: 'sharing_share_sources', type: IndexType::PRIMARY)]
#[DropIndex(table: 'sharing_share_recipients', type: IndexType::PRIMARY)]
#[DropIndex(table: 'sharing_share_properties', type: IndexType::PRIMARY)]
#[DropIndex(table: 'sharing_share_permissions', type: IndexType::PRIMARY)]
#[AddIndex(table: 'sharing_share_sources', type: IndexType::PRIMARY)]
#[AddIndex(table: 'sharing_share_recipients', type: IndexType::PRIMARY)]
#[AddIndex(table: 'sharing_share_properties', type: IndexType::PRIMARY)]
#[AddIndex(table: 'sharing_share_permissions', type: IndexType::PRIMARY)]
final class Version1000Date20260731171922 extends SimpleMigrationStep {
Comment thread
icewind1991 marked this conversation as resolved.
/**
* @param Closure():ISchemaWrapper $schemaClosure
* @throws SchemaException
*/
#[Override]
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
$schema = $schemaClosure();

if (!$schema->hasTable('sharing_classmap')) {
$table = $schema->createTable('sharing_classmap');
$table->addColumn('class_id', Types::INTEGER, [
'autoincrement' => true,
'notnull' => true,
]);
$table->addColumn('class_name', Types::STRING, ['length' => 64]);
$table->setPrimaryKey(['class_id']);
$table->addUniqueIndex(['class_name']);
}

$sourcesTable = $schema->getTable('sharing_share_sources');
if ($sourcesTable->hasColumn('source_class')) {
$sourcesTable->dropColumn('source_class');
$sourcesTable->addColumn('source_class_id', Types::INTEGER);
$sourcesTable->dropPrimaryKey();
$sourcesTable->setPrimaryKey(['share_id', 'source_class_id', 'source_value']);
$sourcesTable->addForeignKeyConstraint('sharing_classmap', ['source_class_id'], ['class_id']);
}

$recipientsTable = $schema->getTable('sharing_share_recipients');
if ($recipientsTable->hasColumn('recipient_class')) {
$recipientsTable->dropColumn('recipient_class');
$recipientsTable->addColumn('recipient_class_id', Types::INTEGER);
$recipientsTable->dropPrimaryKey();
$recipientsTable->setPrimaryKey(['share_id', 'recipient_class_id', 'recipient_value']);
$recipientsTable->addForeignKeyConstraint('sharing_classmap', ['recipient_class_id'], ['class_id']);
}

$propertiesTable = $schema->getTable('sharing_share_properties');
if ($propertiesTable->hasColumn('property_class')) {
$propertiesTable->dropColumn('property_class');
$propertiesTable->addColumn('property_class_id', Types::INTEGER);
$propertiesTable->dropPrimaryKey();
$propertiesTable->setPrimaryKey(['share_id', 'property_class_id']);
$propertiesTable->addForeignKeyConstraint('sharing_classmap', ['property_class_id'], ['class_id']);
}

$permissionsTable = $schema->getTable('sharing_share_permissions');
if ($permissionsTable->hasColumn('permission_class')) {
$permissionsTable->dropColumn('permission_class');
$permissionsTable->addColumn('permission_class_id', Types::INTEGER);
$permissionsTable->dropPrimaryKey();
$permissionsTable->setPrimaryKey(['share_id', 'permission_class_id']);
$permissionsTable->addForeignKeyConstraint('sharing_classmap', ['permission_class_id'], ['class_id']);
}

return $schema;
}

#[Override]
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
}
}
2 changes: 2 additions & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
'NCU\\Security\\Signature\\ISignatureManager' => $baseDir . '/lib/unstable/Security/Signature/ISignatureManager.php',
'NCU\\Security\\Signature\\ISignedRequest' => $baseDir . '/lib/unstable/Security/Signature/ISignedRequest.php',
'NCU\\Security\\Signature\\Model\\Signatory' => $baseDir . '/lib/unstable/Security/Signature/Model/Signatory.php',
'NCU\\Sharing\\Event\\SharesDefaultSetEvent' => $baseDir . '/lib/unstable/Sharing/Event/SharesDefaultSetEvent.php',
'NCU\\Sharing\\Exception\\AShareException' => $baseDir . '/lib/unstable/Sharing/Exception/AShareException.php',
'NCU\\Sharing\\Exception\\ShareInvalidException' => $baseDir . '/lib/unstable/Sharing/Exception/ShareInvalidException.php',
'NCU\\Sharing\\Exception\\ShareNotFoundException' => $baseDir . '/lib/unstable/Sharing/Exception/ShareNotFoundException.php',
Expand Down Expand Up @@ -2306,6 +2307,7 @@
'OC\\Share20\\UserDeletedListener' => $baseDir . '/lib/private/Share20/UserDeletedListener.php',
'OC\\Share20\\UserRemovedListener' => $baseDir . '/lib/private/Share20/UserRemovedListener.php',
'OC\\Share\\Constants' => $baseDir . '/lib/private/Share/Constants.php',
'OC\\Sharing\\ClassMapper' => $baseDir . '/lib/private/Sharing/ClassMapper.php',
'OC\\Sharing\\ISharingLegacyBackend' => $baseDir . '/lib/private/Sharing/ISharingLegacyBackend.php',
'OC\\Sharing\\SharingBackend' => $baseDir . '/lib/private/Sharing/SharingBackend.php',
'OC\\Sharing\\SharingManager' => $baseDir . '/lib/private/Sharing/SharingManager.php',
Expand Down
2 changes: 2 additions & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'NCU\\Security\\Signature\\ISignatureManager' => __DIR__ . '/../../..' . '/lib/unstable/Security/Signature/ISignatureManager.php',
'NCU\\Security\\Signature\\ISignedRequest' => __DIR__ . '/../../..' . '/lib/unstable/Security/Signature/ISignedRequest.php',
'NCU\\Security\\Signature\\Model\\Signatory' => __DIR__ . '/../../..' . '/lib/unstable/Security/Signature/Model/Signatory.php',
'NCU\\Sharing\\Event\\SharesDefaultSetEvent' => __DIR__ . '/../../..' . '/lib/unstable/Sharing/Event/SharesDefaultSetEvent.php',
'NCU\\Sharing\\Exception\\AShareException' => __DIR__ . '/../../..' . '/lib/unstable/Sharing/Exception/AShareException.php',
'NCU\\Sharing\\Exception\\ShareInvalidException' => __DIR__ . '/../../..' . '/lib/unstable/Sharing/Exception/ShareInvalidException.php',
'NCU\\Sharing\\Exception\\ShareNotFoundException' => __DIR__ . '/../../..' . '/lib/unstable/Sharing/Exception/ShareNotFoundException.php',
Expand Down Expand Up @@ -2347,6 +2348,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Share20\\UserDeletedListener' => __DIR__ . '/../../..' . '/lib/private/Share20/UserDeletedListener.php',
'OC\\Share20\\UserRemovedListener' => __DIR__ . '/../../..' . '/lib/private/Share20/UserRemovedListener.php',
'OC\\Share\\Constants' => __DIR__ . '/../../..' . '/lib/private/Share/Constants.php',
'OC\\Sharing\\ClassMapper' => __DIR__ . '/../../..' . '/lib/private/Sharing/ClassMapper.php',
'OC\\Sharing\\ISharingLegacyBackend' => __DIR__ . '/../../..' . '/lib/private/Sharing/ISharingLegacyBackend.php',
'OC\\Sharing\\SharingBackend' => __DIR__ . '/../../..' . '/lib/private/Sharing/SharingBackend.php',
'OC\\Sharing\\SharingManager' => __DIR__ . '/../../..' . '/lib/private/Sharing/SharingManager.php',
Expand Down
1 change: 1 addition & 0 deletions lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,7 @@ function () use ($c) {

$this->registerAlias(\NCU\Sharing\ISharingRegistry::class, SharingRegistry::class);
$this->registerAlias(\NCU\Sharing\ISharingManager::class, SharingManager::class);
$this->registerAlias(\NCU\Sharing\ISharingBackend::class, \OC\Sharing\SharingBackend::class);
Comment thread
CarlSchwan marked this conversation as resolved.

$this->connectDispatcher();
}
Expand Down
156 changes: 156 additions & 0 deletions lib/private/Sharing/ClassMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OC\Sharing;

use OCP\DB\Exception;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;

final class ClassMapper {
/** @var array<int, class-string> $map */
private array $map = [];

/** @var array<class-string, int> */
private array $reverseMap = [];

private bool $loaded = false;

public function __construct(
private readonly IDBConnection $connection,
) {
}

/**
* @param array{class_id: int|string, class_name: class-string} $row
*/
private function insertRow(array $row): void {
$id = (int)$row['class_id'];
$class = $row['class_name'];
$this->map[$id] = $class;
$this->reverseMap[$class] = $id;
}

private function loadFromDb(): void {
if ($this->loaded) {
return;
}

$query = $this->connection->getTypedQueryBuilder();
$query->selectColumns('class_id', 'class_name')
->from('sharing_classmap');
$rows = $query->executeQuery()->fetchAll();

foreach ($rows as $row) {
/** @var array{class_id: int|string, class_name: class-string} $row */
$this->insertRow($row);
}

$this->loaded = true;
}

/**
* @param class-string $className
*/
private function loadFromDbByName(string $className): ?int {
$query = $this->connection->getTypedQueryBuilder();
$query->selectColumns('class_id', 'class_name')
->from('sharing_classmap')
->where($query->expr()->eq('class_name', $query->createNamedParameter($className)));
$row = $query->executeQuery()->fetchAssociative();

if ($row !== false) {
/** @var array{class_id: int|string, class_name: class-string} $row */
$this->insertRow($row);
return (int)$row['class_id'];
}

return null;
}

/**
* @return class-string|null
*/
private function loadFromDbById(int $id): ?string {
$query = $this->connection->getTypedQueryBuilder();
$query->selectColumns('class_id', 'class_name')
->from('sharing_classmap')
->where($query->expr()->eq('class_id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
$row = $query->executeQuery()->fetchAssociative();

if ($row !== false) {
/** @var array{class_id: int|string, class_name: class-string} $row */
$this->insertRow($row);
return $row['class_name'];
}

return null;
}

/**
* @param class-string $className
*/
private function insert(string $className): int {
$id = $this->loadFromDbByName($className);
if ($id !== null) {
return $id;
}

$query = $this->connection->getTypedQueryBuilder();
$query->insert('sharing_classmap')
->values([
'class_name' => $query->createNamedParameter($className)
]);
try {
$query->executeStatement();
$id = $query->getLastInsertId();
$this->map[$id] = $className;
$this->reverseMap[$className] = $id;
return $id;
} catch (Exception $exception) {
// handle concurrent inserts
if ($exception->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
$id = $this->loadFromDbByName($className);
if ($id === null) {
throw new \Exception(sprintf("Failed to insert '%s' into sharing_classmap, duplicate on insert but can't fetch it either", $className), $exception->getCode(), $exception);
}

return $id;
}

throw $exception;
}
}

/**
* @param class-string $class
*/
public function getClassId(string $class): int {
$this->loadFromDb();

return $this->reverseMap[$class] ?? $this->insert($class);
}

/**
* @return class-string
*/
public function getClassName(int $id): string {
$this->loadFromDb();
if (isset($this->map[$id])) {
return $this->map[$id];
}

$class = $this->loadFromDbById($id);
if ($class) {
return $class;
}

throw new \Exception(sprintf("Unknown mapped class '%d'", $id));
}

}
Loading
Loading