Skip to content
Merged
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
54 changes: 38 additions & 16 deletions apps/sharing/lib/SharingBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use OCP\Sharing\Source\IShareSourceType;
use OCP\Sharing\Source\ShareSource;
use OCP\Snowflake\ISnowflakeGenerator;
use RuntimeException;

// TODO: Add mapping table for class names in sources, recipients, permissions and properties

Expand All @@ -59,7 +60,7 @@ public function __construct(
}

#[\Override]
public function createShare(IUser $owner): string {
public function createShare(ShareUser $owner): string {
$id = $this->snowflakeGenerator->nextId();
$lastUpdated = $this->manager->generateTimestamp();

Expand All @@ -68,7 +69,8 @@ public function createShare(IUser $owner): string {
->insert('sharing_share')
->values([
'id' => $qb->createNamedParameter($id),
'owner_user_id' => $qb->createNamedParameter($owner->getUID()),
'owner_user_id' => $qb->createNamedParameter($owner->userId),
'owner_instance' => $qb->createNamedParameter($owner->instance),
'last_updated' => $qb->createNamedParameter($lastUpdated),
'state' => $qb->createNamedParameter(ShareState::Draft->value),
])
Expand All @@ -78,13 +80,18 @@ public function createShare(IUser $owner): string {
}

#[\Override]
public function onOwnerDeleted(IUser $owner): void {
public function onOwnerDeleted(ShareUser $owner): void {
$qb = $this->connection->getQueryBuilder();
$qb
->delete('sharing_share')
->where($qb->expr()->eq('owner_user_id', $qb->createNamedParameter($owner->getUID())))
->andWhere($qb->expr()->isNull('owner_instance'))
->executeStatement();
->where($qb->expr()->eq('owner_user_id', $qb->createNamedParameter($owner->userId)));
if ($owner->instance === null) {
$qb->andWhere($qb->expr()->isNull('owner_instance'));
} else {
$qb->andWhere($qb->expr()->eq('owner_instance', $qb->createNamedParameter($owner->instance)));
}

$qb->executeStatement();
}

#[\Override]
Expand Down Expand Up @@ -164,7 +171,11 @@ public function onSourceDeleted(ShareSource $source): array {
}

#[\Override]
public function addShareRecipient(string $id, IUser $initiator, ShareRecipient $recipient): void {
public function addShareRecipient(string $id, ShareRecipient $recipient): void {
if (!$recipient->initiator instanceof ShareUser) {
throw new RuntimeException('The initiator must not be null.');
}

try {
$qb = $this->connection->getQueryBuilder();

Expand All @@ -174,7 +185,8 @@ public function addShareRecipient(string $id, IUser $initiator, ShareRecipient $
'recipient_value' => $qb->createNamedParameter($recipient->value),
'recipient_instance' => $qb->createNamedParameter($recipient->instance),
'recipient_secret' => $qb->createNamedParameter($this->manager->generateSecret()),
'initiator_user_id' => $qb->createNamedParameter($initiator->getUID(), IQueryBuilder::PARAM_STR),
'initiator_user_id' => $qb->createNamedParameter($recipient->initiator->userId),
'initiator_instance' => $qb->createNamedParameter($recipient->initiator->instance),
];

$qb
Expand Down Expand Up @@ -248,14 +260,19 @@ public function onRecipientDeleted(ShareRecipient $recipient): array {
}

#[\Override]
public function onInitiatorDeleted(IUser $initiator): array {
public function onInitiatorDeleted(ShareUser $initiator): array {
$qb = $this->connection->getQueryBuilder();
$result = $qb
$qb
->selectDistinct('share_id')
->from('sharing_share_recipients')
->andWhere($qb->expr()->isNull('initiator_instance'))
->andWhere($qb->expr()->eq('initiator_user_id', $qb->createNamedParameter($initiator->getUID())))
->executeQuery();
->where($qb->expr()->eq('initiator_user_id', $qb->createNamedParameter($initiator->userId)));
if ($initiator->instance === null) {
$qb->andWhere($qb->expr()->isNull('initiator_instance'));
} else {
$qb->andWhere($qb->expr()->eq('initiator_instance', $qb->createNamedParameter($initiator->instance)));
}

$result = $qb->executeQuery();

/** @var list<string|int> $ids */
$ids = $result->fetchFirstColumn();
Expand All @@ -274,9 +291,14 @@ public function onInitiatorDeleted(IUser $initiator): array {
->set('initiator_user_id', $qb->createNamedParameter($owner->userId))
->set('initiator_instance', $qb->createNamedParameter($owner->instance))
->where($qb->expr()->eq('share_id', $qb->createNamedParameter($id)))
->andWhere($qb->expr()->isNull('initiator_instance'))
->andWhere($qb->expr()->eq('initiator_user_id', $qb->createNamedParameter($initiator->getUID())))
->executeStatement();
->andWhere($qb->expr()->eq('initiator_user_id', $qb->createNamedParameter($initiator->userId)));
if ($initiator->instance === null) {
$qb->andWhere($qb->expr()->isNull('initiator_instance'));
} else {
$qb->andWhere($qb->expr()->eq('initiator_instance', $qb->createNamedParameter($initiator->instance)));
}

$qb->executeStatement();
}

return $ids;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public function isRequired(Share $share): bool {
if ($this->hasRemoteRecipient($share) && $this->legacyManager->shareApiRemoteDefaultExpireDateEnforced()) {
return true;
}

return $this->hasLocalNonTokenAndEmailRecipient($share) && $this->legacyManager->shareApiInternalDefaultExpireDateEnforced();
}

Expand Down
22 changes: 16 additions & 6 deletions lib/private/Sharing/SharingManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,14 @@ public function createShare(ShareAccessContext $accessContext): string {
$this->assertInTransaction();

$backend = $this->getBackend(null);
$id = $backend->createShare($currentUser);
$id = $backend->createShare(new ShareUser($currentUser->getUID(), null));
$this->backendCache->set($id, $backend::class);

return $id;
}

#[\Override]
public function onOwnerDeleted(ShareAccessContext $accessContext, IUser $owner): void {
public function onOwnerDeleted(ShareAccessContext $accessContext, ShareUser $owner): void {
if (!$accessContext->overrideChecks) {
throw new RuntimeException('Only possible if checks are overridden.');
}
Expand Down Expand Up @@ -301,7 +301,15 @@ public function addShareRecipient(ShareAccessContext $accessContext, string $id,
$this->validateInteraction($accessContext, $owner, $share->sources, $share->getEnabledPermissions(), $recipients);
}

$backend->addShareRecipient($id, $currentUser, $recipient);
$recipient = new ShareRecipient(
$recipient->class,
$recipient->value,
$recipient->instance,
$recipient->secret,
new ShareUser($currentUser->getUID(), null),
);

$backend->addShareRecipient($id, $recipient);
}

#[\Override]
Expand Down Expand Up @@ -353,7 +361,7 @@ public function onRecipientDeleted(ShareAccessContext $accessContext, ShareRecip
}

#[\Override]
public function onInitiatorDeleted(ShareAccessContext $accessContext, IUser $initiator): void {
public function onInitiatorDeleted(ShareAccessContext $accessContext, ShareUser $initiator): void {
if (!$accessContext->overrideChecks) {
throw new RuntimeException('Only possible if checks are overridden.');
}
Expand Down Expand Up @@ -518,10 +526,12 @@ public function getShares(ShareAccessContext $accessContext, ?string $filterSour

#[\Override]
public function handle(Event $event): void {
$shareUser = new ShareUser($event->getUser()->getUID(), null);

try {
$this->dbConnection->beginTransaction();
$this->onOwnerDeleted(new ShareAccessContext(overrideChecks: true), $event->getUser());
$this->onInitiatorDeleted(new ShareAccessContext(overrideChecks: true), $event->getUser());
$this->onOwnerDeleted(new ShareAccessContext(overrideChecks: true), $shareUser);
$this->onInitiatorDeleted(new ShareAccessContext(overrideChecks: true), $shareUser);
$this->dbConnection->commit();
} catch (Exception $exception) {
$this->dbConnection->rollBack();
Expand Down
9 changes: 4 additions & 5 deletions lib/public/Sharing/ISharingBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace OCP\Sharing;

use OCP\AppFramework\Attribute\Implementable;
use OCP\IUser;
use OCP\Sharing\Exception\ShareInvalidException;
use OCP\Sharing\Exception\ShareNotFoundException;
use OCP\Sharing\Permission\ISharePermissionPreset;
Expand All @@ -31,14 +30,14 @@ interface ISharingBackend {
*
* @since 35.0.0
*/
public function createShare(IUser $owner): string;
public function createShare(ShareUser $owner): string;

/**
* Perform all updates when the owner was deleted.
*
* @since 35.0.0
*/
public function onOwnerDeleted(IUser $owner): void;
public function onOwnerDeleted(ShareUser $owner): void;

/**
* Update the state of a share.
Expand Down Expand Up @@ -80,7 +79,7 @@ public function onSourceDeleted(ShareSource $source): array;
* @throws ShareNotFoundException
* @since 35.0.0
*/
public function addShareRecipient(string $id, IUser $initiator, ShareRecipient $recipient): void;
public function addShareRecipient(string $id, ShareRecipient $recipient): void;

/**
* Remove an existing recipient from a share.
Expand All @@ -104,7 +103,7 @@ public function onRecipientDeleted(ShareRecipient $recipient): array;
* @return list<string>
* @since 35.0.0
*/
public function onInitiatorDeleted(IUser $initiator): array;
public function onInitiatorDeleted(ShareUser $initiator): array;

/**
* Update the secret of a recipient.
Expand Down
5 changes: 2 additions & 3 deletions lib/public/Sharing/ISharingManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace OCP\Sharing;

use OCP\AppFramework\Attribute\Consumable;
use OCP\IUser;
use OCP\Sharing\Exception\ShareInvalidException;
use OCP\Sharing\Exception\ShareNotFoundException;
use OCP\Sharing\Exception\ShareOperationForbiddenException;
Expand Down Expand Up @@ -68,7 +67,7 @@ public function createShare(ShareAccessContext $accessContext): string;
*
* @since 35.0.0
*/
public function onOwnerDeleted(ShareAccessContext $accessContext, IUser $owner): void;
public function onOwnerDeleted(ShareAccessContext $accessContext, ShareUser $owner): void;

/**
* Update the state of a share.
Expand Down Expand Up @@ -137,7 +136,7 @@ public function onRecipientDeleted(ShareAccessContext $accessContext, ShareRecip
*
* @since 35.0.0
*/
public function onInitiatorDeleted(ShareAccessContext $accessContext, IUser $initiator): void;
public function onInitiatorDeleted(ShareAccessContext $accessContext, ShareUser $initiator): void;

/**
* Update the secret of a recipient.
Expand Down
Loading