-
-
Notifications
You must be signed in to change notification settings - Fork 4k
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
feat(files_sharing): allow not deleting expiring shares #48225
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,235 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
namespace OCA\Files_Sharing\Controller; | ||
|
||
use OCA\Files_Sharing\ResponseDefinitions; | ||
use OCP\App\IAppManager; | ||
use OCP\AppFramework\Http; | ||
use OCP\AppFramework\Http\Attribute\NoAdminRequired; | ||
use OCP\AppFramework\Http\DataResponse; | ||
use OCP\AppFramework\OCSController; | ||
use OCP\AppFramework\QueryException; | ||
use OCP\Files\IRootFolder; | ||
use OCP\Files\NotFoundException; | ||
use OCP\IGroupManager; | ||
use OCP\IRequest; | ||
use OCP\IServerContainer; | ||
use OCP\IUserManager; | ||
use OCP\Share\IManager as ShareManager; | ||
use OCP\Share\IShare; | ||
|
||
/** | ||
* @psalm-import-type Files_SharingDeletedShare from ResponseDefinitions | ||
*/ | ||
class ExpiredShareAPIController extends OCSController { | ||
|
||
/** @var ShareManager */ | ||
private $shareManager; | ||
|
||
/** @var string */ | ||
private $userId; | ||
|
||
/** @var IUserManager */ | ||
private $userManager; | ||
|
||
/** @var IGroupManager */ | ||
private $groupManager; | ||
|
||
/** @var IRootFolder */ | ||
private $rootFolder; | ||
|
||
/** @var IAppManager */ | ||
private $appManager; | ||
|
||
/** @var IServerContainer */ | ||
private $serverContainer; | ||
|
||
public function __construct(string $appName, | ||
IRequest $request, | ||
ShareManager $shareManager, | ||
string $UserId, | ||
IUserManager $userManager, | ||
IGroupManager $groupManager, | ||
IRootFolder $rootFolder, | ||
IAppManager $appManager, | ||
IServerContainer $serverContainer) { | ||
Check notice Code scanning / Psalm DeprecatedInterface Note
Interface OCP\IServerContainer is marked as deprecated
|
||
parent::__construct($appName, $request); | ||
|
||
$this->shareManager = $shareManager; | ||
$this->userId = $UserId; | ||
$this->userManager = $userManager; | ||
$this->groupManager = $groupManager; | ||
$this->rootFolder = $rootFolder; | ||
$this->appManager = $appManager; | ||
$this->serverContainer = $serverContainer; | ||
} | ||
|
||
/** | ||
* @suppress PhanUndeclaredClassMethod | ||
* | ||
* @return Files_SharingDeletedShare | ||
*/ | ||
private function formatShare(IShare $share): array { | ||
$result = [ | ||
'id' => $share->getFullId(), | ||
'share_type' => $share->getShareType(), | ||
'uid_owner' => $share->getSharedBy(), | ||
'displayname_owner' => $this->userManager->get($share->getSharedBy())->getDisplayName(), | ||
Check notice Code scanning / Psalm PossiblyNullReference Note
Cannot call method getDisplayName on possibly null value
|
||
'permissions' => 0, | ||
'stime' => $share->getShareTime()->getTimestamp(), | ||
'parent' => null, | ||
'expiration' => null, | ||
'token' => null, | ||
'uid_file_owner' => $share->getShareOwner(), | ||
'displayname_file_owner' => $this->userManager->get($share->getShareOwner())->getDisplayName(), | ||
Check notice Code scanning / Psalm PossiblyNullReference Note
Cannot call method getDisplayName on possibly null value
|
||
'path' => $share->getTarget(), | ||
]; | ||
$userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); | ||
$node = $userFolder->getFirstNodeById($share->getNodeId()); | ||
if (!$node) { | ||
// fallback to guessing the path | ||
$node = $userFolder->get($share->getTarget()); | ||
if ($node === null || $share->getTarget() === '') { | ||
Check notice Code scanning / Psalm DocblockTypeContradiction Note
OCP\Files\Node does not contain null
|
||
throw new NotFoundException(); | ||
} | ||
} | ||
|
||
$result['path'] = $userFolder->getRelativePath($node->getPath()); | ||
if ($node instanceof \OCP\Files\Folder) { | ||
$result['item_type'] = 'folder'; | ||
} else { | ||
$result['item_type'] = 'file'; | ||
} | ||
$result['mimetype'] = $node->getMimetype(); | ||
$result['storage_id'] = $node->getStorage()->getId(); | ||
$result['storage'] = $node->getStorage()->getCache()->getNumericStorageId(); | ||
$result['item_source'] = $node->getId(); | ||
$result['file_source'] = $node->getId(); | ||
$result['file_parent'] = $node->getParent()->getId(); | ||
$result['file_target'] = $share->getTarget(); | ||
$result['item_size'] = $node->getSize(); | ||
$result['item_mtime'] = $node->getMTime(); | ||
|
||
$expiration = $share->getExpirationDate(); | ||
if ($expiration !== null) { | ||
$result['expiration'] = $expiration->format('Y-m-d 00:00:00'); | ||
} | ||
|
||
if ($share->getShareType() === IShare::TYPE_GROUP) { | ||
$group = $this->groupManager->get($share->getSharedWith()); | ||
$result['share_with'] = $share->getSharedWith(); | ||
$result['share_with_displayname'] = $group !== null ? $group->getDisplayName() : $share->getSharedWith(); | ||
} elseif ($share->getShareType() === IShare::TYPE_ROOM) { | ||
$result['share_with'] = $share->getSharedWith(); | ||
$result['share_with_displayname'] = ''; | ||
|
||
try { | ||
$result = array_merge($result, $this->getRoomShareHelper()->formatShare($share)); | ||
} catch (QueryException $e) { | ||
} | ||
} elseif ($share->getShareType() === IShare::TYPE_DECK) { | ||
$result['share_with'] = $share->getSharedWith(); | ||
$result['share_with_displayname'] = ''; | ||
|
||
try { | ||
$result = array_merge($result, $this->getDeckShareHelper()->formatShare($share)); | ||
} catch (QueryException $e) { | ||
} | ||
} elseif ($share->getShareType() === IShare::TYPE_SCIENCEMESH) { | ||
$result['share_with'] = $share->getSharedWith(); | ||
$result['share_with_displayname'] = ''; | ||
|
||
try { | ||
$result = array_merge($result, $this->getSciencemeshShareHelper()->formatShare($share)); | ||
} catch (QueryException $e) { | ||
} | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* Get a list of all expired shares | ||
* | ||
* @return DataResponse<Http::STATUS_OK, Files_SharingDeletedShare[], array{}> | ||
* | ||
* 200: Deleted shares returned | ||
*/ | ||
#[NoAdminRequired] | ||
public function index(): DataResponse { | ||
$groupShares = $this->shareManager->getExpiredShares($this->userId, IShare::TYPE_GROUP, null, -1, 0); | ||
$roomShares = $this->shareManager->getExpiredShares($this->userId, IShare::TYPE_ROOM, null, -1, 0); | ||
$deckShares = $this->shareManager->getExpiredShares($this->userId, IShare::TYPE_DECK, null, -1, 0); | ||
$sciencemeshShares = $this->shareManager->getExpiredShares($this->userId, IShare::TYPE_SCIENCEMESH, null, -1, 0); | ||
$linkShares = $this->shareManager->getExpiredShares($this->userId, IShare::TYPE_LINK, null, -1, 0); | ||
$userShares = $this->shareManager->getExpiredShares($this->userId, IShare::TYPE_USER, null, -1, 0); | ||
$emailsShares = $this->shareManager->getExpiredShares($this->userId, IShare::TYPE_EMAIL, null, -1, 0); | ||
$circlesShares = $this->shareManager->getExpiredShares($this->userId, IShare::TYPE_CIRCLE, null, -1, 0); | ||
$remoteShares = $this->shareManager->getExpiredShares($this->userId, IShare::TYPE_REMOTE, null, -1, 0); | ||
|
||
$shares = array_merge($groupShares, $roomShares, $deckShares, $sciencemeshShares, $linkShares, $userShares, $emailsShares, $circlesShares, $remoteShares); | ||
|
||
$shares = array_map(function (IShare $share) { | ||
return $this->formatShare($share); | ||
}, $shares); | ||
|
||
return new DataResponse($shares); | ||
} | ||
|
||
/** | ||
* Returns the helper of DeletedShareAPIController for room shares. | ||
* | ||
* If the Talk application is not enabled or the helper is not available | ||
* a QueryException is thrown instead. | ||
* | ||
* @return \OCA\Talk\Share\Helper\DeletedShareAPIController | ||
* @throws QueryException | ||
*/ | ||
private function getRoomShareHelper() { | ||
if (!$this->appManager->isEnabledForUser('spreed')) { | ||
throw new QueryException(); | ||
Check notice Code scanning / Psalm DeprecatedClass Note
OCP\AppFramework\QueryException is marked deprecated
|
||
} | ||
|
||
return $this->serverContainer->get('\OCA\Talk\Share\Helper\DeletedShareAPIController'); | ||
} | ||
|
||
/** | ||
* Returns the helper of DeletedShareAPIHelper for deck shares. | ||
* | ||
* If the Deck application is not enabled or the helper is not available | ||
* a QueryException is thrown instead. | ||
* | ||
* @return \OCA\Deck\Sharing\ShareAPIHelper | ||
* @throws QueryException | ||
*/ | ||
private function getDeckShareHelper() { | ||
if (!$this->appManager->isEnabledForUser('deck')) { | ||
throw new QueryException(); | ||
Check notice Code scanning / Psalm DeprecatedClass Note
OCP\AppFramework\QueryException is marked deprecated
|
||
} | ||
|
||
return $this->serverContainer->get('\OCA\Deck\Sharing\ShareAPIHelper'); | ||
} | ||
|
||
/** | ||
* Returns the helper of DeletedShareAPIHelper for sciencemesh shares. | ||
* | ||
* If the sciencemesh application is not enabled or the helper is not available | ||
* a QueryException is thrown instead. | ||
* | ||
* @return \OCA\Deck\Sharing\ShareAPIHelper | ||
* @throws QueryException | ||
*/ | ||
private function getSciencemeshShareHelper() { | ||
if (!$this->appManager->isEnabledForUser('sciencemesh')) { | ||
throw new QueryException(); | ||
Check notice Code scanning / Psalm DeprecatedClass Note
OCP\AppFramework\QueryException is marked deprecated
|
||
} | ||
|
||
return $this->serverContainer->get('\OCA\ScienceMesh\Sharing\ShareAPIHelper'); | ||
} | ||
} |
Check notice
Code scanning / Psalm
DeprecatedInterface Note