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

Allow restoring of deleted user shares #10132

Closed
wants to merge 4 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ private function formatShare(IShare $share): array {
* @NoAdminRequired
*/
public function index(): DataResponse {
$shares = $this->shareManager->getDeletedSharedWith($this->userId, \OCP\Share::SHARE_TYPE_GROUP, null, -1, 0);
$shares = $this->shareManager->getDeletedSharedWith($this->userId, \OCP\Share::SHARE_TYPE_USER, null, -1, 0);
$shares = array_merge($shares, $this->shareManager->getDeletedSharedWith($this->userId, \OCP\Share::SHARE_TYPE_GROUP, null, -1, 0));

$shares = array_map(function (IShare $share) {
return $this->formatShare($share);
Expand Down
73 changes: 45 additions & 28 deletions lib/private/Share20/DefaultShareProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ class DefaultShareProvider implements IShareProvider {
// Special share type for user modified group shares
const SHARE_TYPE_USERGROUP = 2;

// Acceptend types
const SHARE_ACCEPTED = 0;
const SHARE_PENDING = 1;
const SHARE_DELETED = 2;

/** @var IDBConnection */
private $dbConn;

Expand Down Expand Up @@ -404,44 +409,51 @@ public function deleteFromSelf(\OCP\Share\IShare $share, $recipient) {
throw new ProviderException('Recipient does not match');
}

// We can just delete user and link shares
$this->delete($share);
// Mark the share as deleted
$qb = $this->dbConn->getQueryBuilder();
$qb->update('share')
->set('accepted', $qb->createNamedParameter(self::SHARE_DELETED))
->execute();
} else {
throw new ProviderException('Invalid shareType');
}
}

/**
* @inheritdoc
*
* For now this only works for group shares
* If this gets implemented for normal shares we have to extend it
*/
public function restore(IShare $share, string $recipient): IShare {
$qb = $this->dbConn->getQueryBuilder();
$qb->select('permissions')
->from('share')
->where(
$qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))
);
$cursor = $qb->execute();
$data = $cursor->fetch();
$cursor->closeCursor();

$originalPermission = $data['permissions'];
if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
$qb = $this->dbConn->getQueryBuilder();
$qb->update('share')
->set('accepted', $qb->createNamedParameter(self::SHARE_ACCEPTED))
->execute();
} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
$qb = $this->dbConn->getQueryBuilder();
$qb->select('permissions')
->from('share')
->where(
$qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))
);
$cursor = $qb->execute();
$data = $cursor->fetch();
$cursor->closeCursor();

$qb = $this->dbConn->getQueryBuilder();
$qb->update('share')
->set('permissions', $qb->createNamedParameter($originalPermission))
->where(
$qb->expr()->eq('parent', $qb->createNamedParameter($share->getParent()))
)->andWhere(
$qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))
)->andWhere(
$qb->expr()->eq('share_with', $qb->createNamedParameter($recipient))
);
$originalPermission = $data['permissions'];

$qb->execute();
$qb = $this->dbConn->getQueryBuilder();
$qb->update('share')
->set('permissions', $qb->createNamedParameter($originalPermission))
->where(
$qb->expr()->eq('parent', $qb->createNamedParameter($share->getParent()))
)->andWhere(
$qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))
)->andWhere(
$qb->expr()->eq('share_with', $qb->createNamedParameter($recipient))
);

$qb->execute();
}

return $this->getShareById($share->getId(), $recipient);
}
Expand Down Expand Up @@ -873,10 +885,15 @@ private function createShare($data) {
$share = new Share($this->rootFolder, $this->userManager);
$share->setId((int)$data['id'])
->setShareType((int)$data['share_type'])
->setPermissions((int)$data['permissions'])
->setTarget($data['file_target'])
->setMailSend((bool)$data['mail_send']);

if ((int)$data['accepted'] === self::SHARE_DELETED) {
$share->setPermissions(0);
} else {
$share->setPermissions((int)$data['permissions']);
}

$shareTime = new \DateTime();
$shareTime->setTimestamp((int)$data['stime']);
$share->setShareTime($shareTime);
Expand Down