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

Re-enable sharing API for recent NC versions #214

Merged
merged 3 commits into from
Aug 12, 2024
Merged
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
391 changes: 229 additions & 162 deletions lib/Controller/ShareAPIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,169 +8,236 @@

use OCA\Files_Sharing\Controller\ShareAPIController as FilesSharingShareAPIController;
use OCP\AppFramework\Http\DataResponse;
use OCP\App\IAppManager;
use OCP\Files\IRootFolder;
use OCP\IConfig;
use OCP\IDateTimeZone;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\IPreview;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\Share\IManager;
use OCP\UserStatus\IManager as IUserStatusManager;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use OCP\IUserSession;
use OC_Util;
use OCP\IServerContainer;

class ShareAPIController extends FilesSharingShareAPIController
{
use AccessControl;

/**
* @NoAdminRequired
*
* @param string $path
* @param int $permissions
* @param string $shareWith
* @param string $sendPasswordByTalk
* @param string $attributes
*
* @throws NotFoundException
* @throws OCSBadRequestException
* @throws OCSException
* @throws OCSForbiddenException
* @throws OCSNotFoundException
* @throws InvalidPathException
*
* @suppress PhanUndeclaredClassMethod
*/
public function createShare(...$args
): DataResponse {
$response = parent::createShare(...$args);

return $this->checkOrigin($response);
}

/**
* The getShares function.
*
* @NoAdminRequired
*
* @param string $path
*
* - Get shares by the current user
* - Get shares by the current user and reshares (?reshares=true)
* - Get shares with the current user (?shared_with_me=true)
* - Get shares for a specific path (?path=...)
* - Get all shares in a folder (?subfiles=true&path=..)
*
* @throws NotFoundException
* @throws OCSBadRequestException
* @throws OCSNotFoundException
*/
public function getShares(
string $shared_with_me = 'false',
string $reshares = 'false',
string $subfiles = 'false',
string $path = '',
string $include_tags = 'false'
): DataResponse {
$response = parent::getShares(...func_get_args());

return $this->checkOrigin($response);
}

/**
* Get a specific share by id.
*
* @NoAdminRequired
*
* @throws OCSNotFoundException
*/
public function getShare(string $id, bool $include_tags = false): DataResponse
{
$response = parent::getShare(...func_get_args());

return $this->checkOrigin($response);
}

/**
* The getInheritedShares function.
* returns all shares relative to a file, including parent folders shares rights.
*
* @NoAdminRequired
*
* @param string $path
*
* - Get shares by the current user
* - Get shares by the current user and reshares (?reshares=true)
* - Get shares with the current user (?shared_with_me=true)
* - Get shares for a specific path (?path=...)
* - Get all shares in a folder (?subfiles=true&path=..)
*
* @throws InvalidPathException
* @throws NotFoundException
* @throws OCSNotFoundException
* @throws OCSBadRequestException
* @throws SharingRightsException
*/
public function getInheritedShares(string $path): DataResponse
{
$response = parent::getInheritedShares(...func_get_args());

return $this->checkOrigin($response);
}

/**
* @NoAdminRequired
*
* @throws InvalidPathException
* @throws NotFoundException
* @throws OCSNotFoundException
* @throws OCSBadRequestException
* @throws SharingRightsException
*/
public function pendingShares(): DataResponse
{
$response = parent::pendingShares(...func_get_args());

return $this->checkOrigin($response);
}

/**
* @NoAdminRequired
*
* @param int $permissions
* @param string $password
* @param string $sendPasswordByTalk
* @param string $publicUpload
* @param string $expireDate
* @param string $note
* @param string $label
* @param string $hideDownload
* @param string $attributes
*
* @throws LockedException
* @throws NotFoundException
* @throws OCSBadRequestException
* @throws OCSForbiddenException
* @throws OCSNotFoundException
*/
public function updateShare(
string $id,
int $permissions = null,
string $password = null,
string $sendPasswordByTalk = null,
string $publicUpload = null,
string $expireDate = null,
string $note = null,
string $label = null,
string $hideDownload = null,
string $attributes = null
): DataResponse {
$response = parent::updateShare(...func_get_args());

return $this->checkOrigin($response);
}

/**
* Delete a share.
*
* @NoAdminRequired
*
* @throws OCSNotFoundException
*/
public function deleteShare(string $id): DataResponse
{
$response = parent::deleteShare(...func_get_args());

return $this->checkOrigin($response);
}
use AccessControl;

public function __construct(
$AppName,
IRequest $request,
private IManager $shareManager,
private IGroupManager $groupManager,
private IUserManager $userManager,
private IRootFolder $rootFolder,
private IURLGenerator $urlGenerator,
private IL10N $l,
private IConfig $config,
private IAppManager $appManager,
private IServerContainer $serverContainerOld,
private ContainerInterface $serverContainer,
private IUserStatusManager $userStatusManager,
private IPreview $previewManager,
private IDateTimeZone $dateTimeZone,
private LoggerInterface $logger,
IUserSession $userSession,
) {
// In an options request, $user will be null, as there is no Auth header to get data from.
$user = $userSession->getUser();

// Enforce $uid to be a string under all circumstances, because Nextcloud's own ShareApiController
// will break if it is null, even if it is allowed by its constructor. Passing an empty string is fine.
$uid = $user ? $user->getUID() ?? '' : '';


// Call the constructor.
// The paremeter order is different between versions, this has to be accounted for.
// Version string is identical for 27.1.10.1 and 27.1.10.2.
$version = OC_Util::getVersionString();
if ($version == '27.1.10') {
parent::__construct($AppName, $request, $shareManager, $groupManager, $userManager, $rootFolder, $urlGenerator, $uid, $l, $config, $appManager, $serverContainerOld, $userStatusManager, $previewManager, $dateTimeZone);
} else {
parent::__construct($AppName, $request, $shareManager, $groupManager, $userManager, $rootFolder, $urlGenerator, $l, $config, $appManager, $serverContainer, $userStatusManager, $previewManager, $dateTimeZone, $logger, $uid);
}
}

/**
* @NoAdminRequired
*
* @param string $path
* @param int $permissions
* @param string $shareWith
* @param string $sendPasswordByTalk
* @param string $attributes
*
* @throws NotFoundException
* @throws OCSBadRequestException
* @throws OCSException
* @throws OCSForbiddenException
* @throws OCSNotFoundException
* @throws InvalidPathException
*
* @suppress PhanUndeclaredClassMethod
*/
public function createShare(
?string $path = null,
?int $permissions = null,
int $shareType = -1,
?string $shareWith = null,
string $publicUpload = 'false',
string $password = '',
?string $sendPasswordByTalk = null,
?string $expireDate = null,
string $note = '',
string $label = '',
?string $attributes = null
): DataResponse {
$response = parent::createShare(...func_get_args());

return $this->checkOrigin($response);
}


/**
* The getShares function.
*
* @NoAdminRequired
*
* @param string $path
*
* - Get shares by the current user
* - Get shares by the current user and reshares (?reshares=true)
* - Get shares with the current user (?shared_with_me=true)
* - Get shares for a specific path (?path=...)
* - Get all shares in a folder (?subfiles=true&path=..)
*
* @throws NotFoundException
* @throws OCSBadRequestException
* @throws OCSNotFoundException
*/
public function getShares(
string $shared_with_me = 'false',
string $reshares = 'false',
string $subfiles = 'false',
string $path = '',
string $include_tags = 'false'
): DataResponse {
$response = parent::getShares(...func_get_args());

return $this->checkOrigin($response);
}

/**
* Get a specific share by id.
*
* @NoAdminRequired
*
* @throws OCSNotFoundException
*/
public function getShare(string $id, bool $include_tags = false): DataResponse
{
$response = parent::getShare(...func_get_args());

return $this->checkOrigin($response);
}

/**
* The getInheritedShares function.
* returns all shares relative to a file, including parent folders shares rights.
*
* @NoAdminRequired
*
* @param string $path
*
* - Get shares by the current user
* - Get shares by the current user and reshares (?reshares=true)
* - Get shares with the current user (?shared_with_me=true)
* - Get shares for a specific path (?path=...)
* - Get all shares in a folder (?subfiles=true&path=..)
*
* @throws InvalidPathException
* @throws NotFoundException
* @throws OCSNotFoundException
* @throws OCSBadRequestException
* @throws SharingRightsException
*/
public function getInheritedShares(string $path): DataResponse
{
$response = parent::getInheritedShares(...func_get_args());

return $this->checkOrigin($response);
}

/**
* @NoAdminRequired
*
* @throws InvalidPathException
* @throws NotFoundException
* @throws OCSNotFoundException
* @throws OCSBadRequestException
* @throws SharingRightsException
*/
public function pendingShares(): DataResponse
{
$response = parent::pendingShares(...func_get_args());

return $this->checkOrigin($response);
}

/**
* @NoAdminRequired
*
* @param int $permissions
* @param string $password
* @param string $sendPasswordByTalk
* @param string $publicUpload
* @param string $expireDate
* @param string $note
* @param string $label
* @param string $hideDownload
* @param string $attributes
*
* @throws LockedException
* @throws NotFoundException
* @throws OCSBadRequestException
* @throws OCSForbiddenException
* @throws OCSNotFoundException
*/
public function updateShare(
string $id,
int $permissions = null,
string $password = null,
string $sendPasswordByTalk = null,
string $publicUpload = null,
string $expireDate = null,
string $note = null,
string $label = null,
string $hideDownload = null,
string $attributes = null
): DataResponse {
$response = parent::updateShare(...func_get_args());

return $this->checkOrigin($response);
}

/**
* Delete a share.
*
* @NoAdminRequired
*
* @throws OCSNotFoundException
*/
public function deleteShare(string $id): DataResponse
{
$response = parent::deleteShare(...func_get_args());

return $this->checkOrigin($response);
}
}