2018-06-19 10:20:35 +03:00
|
|
|
<?php
|
2019-12-03 21:57:53 +03:00
|
|
|
|
2018-06-19 10:20:35 +03:00
|
|
|
declare(strict_types=1);
|
2019-12-03 21:57:53 +03:00
|
|
|
|
2018-06-19 10:20:35 +03:00
|
|
|
/**
|
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
*
|
|
|
|
* @author Daniel Calviño Sánchez <danxuliu@gmail.com>
|
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2018-06-19 10:20:35 +03:00
|
|
|
* @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-06-19 10:20:35 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Files_Sharing\Controller;
|
|
|
|
|
2018-07-15 22:51:14 +03:00
|
|
|
use OCP\App\IAppManager;
|
2018-06-19 10:20:35 +03:00
|
|
|
use OCP\AppFramework\Http\DataResponse;
|
|
|
|
use OCP\AppFramework\OCS\OCSException;
|
|
|
|
use OCP\AppFramework\OCS\OCSNotFoundException;
|
|
|
|
use OCP\AppFramework\OCSController;
|
2018-07-15 22:51:14 +03:00
|
|
|
use OCP\AppFramework\QueryException;
|
2018-06-20 20:14:50 +03:00
|
|
|
use OCP\Files\IRootFolder;
|
2018-06-26 10:32:29 +03:00
|
|
|
use OCP\Files\NotFoundException;
|
2018-06-20 20:14:50 +03:00
|
|
|
use OCP\IGroupManager;
|
2018-06-19 10:20:35 +03:00
|
|
|
use OCP\IRequest;
|
2018-07-15 22:51:14 +03:00
|
|
|
use OCP\IServerContainer;
|
2018-06-19 10:20:35 +03:00
|
|
|
use OCP\IUserManager;
|
|
|
|
use OCP\Share\Exceptions\GenericShareException;
|
|
|
|
use OCP\Share\Exceptions\ShareNotFound;
|
|
|
|
use OCP\Share\IManager as ShareManager;
|
|
|
|
use OCP\Share\IShare;
|
|
|
|
|
|
|
|
class DeletedShareAPIController extends OCSController {
|
|
|
|
|
|
|
|
/** @var ShareManager */
|
|
|
|
private $shareManager;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $userId;
|
|
|
|
|
|
|
|
/** @var IUserManager */
|
|
|
|
private $userManager;
|
|
|
|
|
2018-06-20 20:14:50 +03:00
|
|
|
/** @var IGroupManager */
|
|
|
|
private $groupManager;
|
|
|
|
|
|
|
|
/** @var IRootFolder */
|
|
|
|
private $rootFolder;
|
|
|
|
|
2018-07-15 22:51:14 +03:00
|
|
|
/** @var IAppManager */
|
|
|
|
private $appManager;
|
|
|
|
|
|
|
|
/** @var IServerContainer */
|
|
|
|
private $serverContainer;
|
|
|
|
|
2018-06-19 10:20:35 +03:00
|
|
|
public function __construct(string $appName,
|
|
|
|
IRequest $request,
|
|
|
|
ShareManager $shareManager,
|
|
|
|
string $UserId,
|
2018-06-20 20:14:50 +03:00
|
|
|
IUserManager $userManager,
|
|
|
|
IGroupManager $groupManager,
|
2018-07-15 22:51:14 +03:00
|
|
|
IRootFolder $rootFolder,
|
|
|
|
IAppManager $appManager,
|
|
|
|
IServerContainer $serverContainer) {
|
2018-06-19 10:20:35 +03:00
|
|
|
parent::__construct($appName, $request);
|
|
|
|
|
|
|
|
$this->shareManager = $shareManager;
|
|
|
|
$this->userId = $UserId;
|
|
|
|
$this->userManager = $userManager;
|
2018-06-20 20:14:50 +03:00
|
|
|
$this->groupManager = $groupManager;
|
|
|
|
$this->rootFolder = $rootFolder;
|
2018-07-15 22:51:14 +03:00
|
|
|
$this->appManager = $appManager;
|
|
|
|
$this->serverContainer = $serverContainer;
|
2018-06-19 10:20:35 +03:00
|
|
|
}
|
|
|
|
|
2018-07-24 12:57:52 +03:00
|
|
|
/**
|
|
|
|
* @suppress PhanUndeclaredClassMethod
|
|
|
|
*/
|
2018-06-19 10:20:35 +03:00
|
|
|
private function formatShare(IShare $share): array {
|
2018-06-20 20:14:50 +03:00
|
|
|
|
|
|
|
$result = [
|
2018-06-19 10:20:35 +03:00
|
|
|
'id' => $share->getFullId(),
|
2018-06-20 20:14:50 +03:00
|
|
|
'share_type' => $share->getShareType(),
|
|
|
|
'uid_owner' => $share->getSharedBy(),
|
|
|
|
'displayname_owner' => $this->userManager->get($share->getSharedBy())->getDisplayName(),
|
2018-06-20 21:32:19 +03:00
|
|
|
'permissions' => 0,
|
2018-06-20 20:14:50 +03:00
|
|
|
'stime' => $share->getShareTime()->getTimestamp(),
|
|
|
|
'parent' => null,
|
|
|
|
'expiration' => null,
|
|
|
|
'token' => null,
|
|
|
|
'uid_file_owner' => $share->getShareOwner(),
|
|
|
|
'displayname_file_owner' => $this->userManager->get($share->getShareOwner())->getDisplayName(),
|
2018-06-19 10:20:35 +03:00
|
|
|
'path' => $share->getTarget(),
|
|
|
|
];
|
2018-06-20 20:14:50 +03:00
|
|
|
$userFolder = $this->rootFolder->getUserFolder($share->getSharedBy());
|
|
|
|
$nodes = $userFolder->getById($share->getNodeId());
|
|
|
|
if (empty($nodes)) {
|
|
|
|
// fallback to guessing the path
|
|
|
|
$node = $userFolder->get($share->getTarget());
|
|
|
|
if ($node === null || $share->getTarget() === '') {
|
|
|
|
throw new NotFoundException();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$node = $nodes[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
$result['path'] = $userFolder->getRelativePath($node->getPath());
|
2020-04-09 15:04:56 +03:00
|
|
|
if ($node instanceof \OCP\Files\Folder) {
|
2018-06-20 20:14:50 +03:00
|
|
|
$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();
|
|
|
|
|
|
|
|
$expiration = $share->getExpirationDate();
|
|
|
|
if ($expiration !== null) {
|
|
|
|
$result['expiration'] = $expiration->format('Y-m-d 00:00:00');
|
|
|
|
}
|
|
|
|
|
2018-07-15 22:51:14 +03:00
|
|
|
if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
|
|
|
|
$group = $this->groupManager->get($share->getSharedWith());
|
|
|
|
$result['share_with'] = $share->getSharedWith();
|
|
|
|
$result['share_with_displayname'] = $group !== null ? $group->getDisplayName() : $share->getSharedWith();
|
|
|
|
} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_ROOM) {
|
|
|
|
$result['share_with'] = $share->getSharedWith();
|
|
|
|
$result['share_with_displayname'] = '';
|
|
|
|
|
|
|
|
try {
|
|
|
|
$result = array_merge($result, $this->getRoomShareHelper()->formatShare($share));
|
|
|
|
} catch (QueryException $e) {
|
|
|
|
}
|
|
|
|
}
|
2018-06-20 20:14:50 +03:00
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
2018-06-19 10:20:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*/
|
|
|
|
public function index(): DataResponse {
|
2018-07-15 22:51:14 +03:00
|
|
|
$groupShares = $this->shareManager->getDeletedSharedWith($this->userId, \OCP\Share::SHARE_TYPE_GROUP, null, -1, 0);
|
|
|
|
$roomShares = $this->shareManager->getDeletedSharedWith($this->userId, \OCP\Share::SHARE_TYPE_ROOM, null, -1, 0);
|
|
|
|
|
|
|
|
$shares = array_merge($groupShares, $roomShares);
|
2018-06-19 10:20:35 +03:00
|
|
|
|
|
|
|
$shares = array_map(function (IShare $share) {
|
|
|
|
return $this->formatShare($share);
|
|
|
|
}, $shares);
|
|
|
|
|
|
|
|
return new DataResponse($shares);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @throws OCSException
|
|
|
|
*/
|
|
|
|
public function undelete(string $id): DataResponse {
|
|
|
|
try {
|
|
|
|
$share = $this->shareManager->getShareById($id, $this->userId);
|
|
|
|
} catch (ShareNotFound $e) {
|
|
|
|
throw new OCSNotFoundException('Share not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($share->getPermissions() !== 0) {
|
|
|
|
throw new OCSNotFoundException('No deleted share found');
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->shareManager->restoreShare($share, $this->userId);
|
|
|
|
} catch (GenericShareException $e) {
|
|
|
|
throw new OCSException('Something went wrong');
|
|
|
|
}
|
|
|
|
|
|
|
|
return new DataResponse([]);
|
|
|
|
}
|
2018-07-15 22:51:14 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2019-09-05 16:32:58 +03:00
|
|
|
* @return \OCA\Talk\Share\Helper\DeletedShareAPIController
|
2018-07-15 22:51:14 +03:00
|
|
|
* @throws QueryException
|
|
|
|
*/
|
|
|
|
private function getRoomShareHelper() {
|
|
|
|
if (!$this->appManager->isEnabledForUser('spreed')) {
|
|
|
|
throw new QueryException();
|
|
|
|
}
|
|
|
|
|
2019-09-05 16:32:58 +03:00
|
|
|
return $this->serverContainer->query('\OCA\Talk\Share\Helper\DeletedShareAPIController');
|
2018-07-15 22:51:14 +03:00
|
|
|
}
|
2018-06-19 10:20:35 +03:00
|
|
|
}
|