Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable10] Protect group share concurrent updates with upsert #34769

Merged
merged 1 commit into from
Apr 5, 2019
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
73 changes: 25 additions & 48 deletions lib/private/Share20/DefaultShareProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -395,59 +395,36 @@ public function updateForRecipient(\OCP\Share\IShare $share, $recipient) {
throw new ProviderException('Recipient not in receiving group');
}

// Check if there is a usergroup share
$qb = $this->dbConn->getQueryBuilder();
$stmt = $qb->select('id')
->from('share')
->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($recipient)))
->andWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
->andWhere($qb->expr()->orX(
$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
))
->setMaxResults(1)
->execute();

$data = $stmt->fetch();
$stmt->closeCursor();
$node = $share->getNode();

$shareAttributes = $this->formatShareAttributes(
$share->getAttributes()
);

if ($data === false) {
// No usergroup share yet. Create one.
$qb = $this->dbConn->getQueryBuilder();
$qb->insert('share')
->values([
'share_type' => $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP),
'share_with' => $qb->createNamedParameter($recipient),
'uid_owner' => $qb->createNamedParameter($share->getShareOwner()),
'uid_initiator' => $qb->createNamedParameter($share->getSharedBy()),
'parent' => $qb->createNamedParameter($share->getId()),
'item_type' => $qb->createNamedParameter($share->getNode() instanceof File ? 'file' : 'folder'),
'item_source' => $qb->createNamedParameter($share->getNode()->getId()),
'file_source' => $qb->createNamedParameter($share->getNode()->getId()),
'file_target' => $qb->createNamedParameter($share->getTarget()),
'permissions' => $qb->createNamedParameter($share->getPermissions()),
'attributes' => $qb->createNamedParameter($shareAttributes),
'stime' => $qb->createNamedParameter($share->getShareTime()->getTimestamp()),
'accepted' => $qb->createNamedParameter($share->getState()),
])->execute();
} else {
// Already a usergroup share. Update it.
$qb = $this->dbConn->getQueryBuilder();
$qb->update('share')
->set('accepted', $qb->createNamedParameter($share->getState()))
->set('file_target', $qb->createNamedParameter($share->getTarget()))
// make sure to reset the permissions to the one of the parent share,
// as legacy entries with zero permissions might still exist
->set('permissions', $qb->createNamedParameter($share->getPermissions()))
->set('attributes', $qb->createNamedParameter($shareAttributes))
->where($qb->expr()->eq('id', $qb->createNamedParameter($data['id'])))
->execute();
}
// Check if there is a usergroup share
$this->dbConn->upsert(
'*PREFIX*share',
[
'share_type' => self::SHARE_TYPE_USERGROUP,
'share_with' => $recipient,
'parent' => $share->getId(),
'uid_owner' => $share->getShareOwner(),
'uid_initiator' => $share->getSharedBy(),
'item_type' => $node instanceof File ? 'file' : 'folder',
'item_source' => $node->getId(),
'file_source' => $node->getId(),
'file_target' => $share->getTarget(),
'attributes' => $shareAttributes,
'permissions' => $share->getPermissions(),
'stime' => $share->getShareTime()->getTimestamp(),
'accepted' => $share->getState(),
],
[
'share_type',
'share_with',
'parent'
]
);
} else {
throw new ProviderException('Can\'t update share of recipient for share type ' . $share->getShareType());
}
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/Share20/DefaultShareProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2678,7 +2678,7 @@ public function testUpdateShareState($sourceState, $targetState) {
$folder = $this->createMock(Folder::class);
$folder->method('getId')->willReturn(42);

$this->rootFolder->method('getUserFolder')->with('user1')->will($this->returnSelf());
$this->rootFolder->method('getUserFolder')->with($this->logicalOr('user1', 'user2'))->will($this->returnSelf());
$this->rootFolder->method('getById')->willReturn([$folder]);

$share = $this->provider->getShareById($id, 'user0');
Expand Down