folder-share: redirect to local if owned

pull/465/head
Varun Patil 2023-03-09 18:47:59 -08:00
parent 0b62dc4aee
commit bd85a16f02
1 changed files with 67 additions and 2 deletions

View File

@ -2,18 +2,21 @@
namespace OCA\Memories\Controller; namespace OCA\Memories\Controller;
use OCA\Memories\AppInfo\Application;
use OCP\App\IAppManager; use OCP\App\IAppManager;
use OCP\AppFramework\AuthPublicShareController; use OCP\AppFramework\AuthPublicShareController;
use OCP\AppFramework\Http\Template\PublicTemplateResponse; use OCP\AppFramework\Http\Template\PublicTemplateResponse;
use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState; use OCP\AppFramework\Services\IInitialState;
use OCP\EventDispatcher\IEventDispatcher; use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
use OCP\IConfig; use OCP\IConfig;
use OCP\IRequest; use OCP\IRequest;
use OCP\ISession; use OCP\ISession;
use OCP\IURLGenerator; use OCP\IURLGenerator;
use OCP\IUserManager; use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Share\IManager as IShareManager; use OCP\Share\IManager as IShareManager;
use OCP\Share\IShare; use OCP\Share\IShare;
use OCP\Util; use OCP\Util;
@ -23,6 +26,8 @@ class PublicController extends AuthPublicShareController
protected $appName; protected $appName;
protected IEventDispatcher $eventDispatcher; protected IEventDispatcher $eventDispatcher;
protected IInitialState $initialState; protected IInitialState $initialState;
protected IUserSession $userSession;
protected IRootFolder $rootFolder;
protected IShareManager $shareManager; protected IShareManager $shareManager;
protected IUserManager $userManager; protected IUserManager $userManager;
protected IAppManager $appManager; protected IAppManager $appManager;
@ -37,6 +42,8 @@ class PublicController extends AuthPublicShareController
IURLGenerator $urlGenerator, IURLGenerator $urlGenerator,
IEventDispatcher $eventDispatcher, IEventDispatcher $eventDispatcher,
IInitialState $initialState, IInitialState $initialState,
IUserSession $userSession,
IRootFolder $rootFolder,
IShareManager $shareManager, IShareManager $shareManager,
IUserManager $userManager, IUserManager $userManager,
IAppManager $appManager, IAppManager $appManager,
@ -45,6 +52,8 @@ class PublicController extends AuthPublicShareController
parent::__construct($AppName, $request, $session, $urlGenerator); parent::__construct($AppName, $request, $session, $urlGenerator);
$this->eventDispatcher = $eventDispatcher; $this->eventDispatcher = $eventDispatcher;
$this->initialState = $initialState; $this->initialState = $initialState;
$this->userSession = $userSession;
$this->rootFolder = $rootFolder;
$this->shareManager = $shareManager; $this->shareManager = $shareManager;
$this->userManager = $userManager; $this->userManager = $userManager;
$this->appManager = $appManager; $this->appManager = $appManager;
@ -61,6 +70,8 @@ class PublicController extends AuthPublicShareController
*/ */
public function showAuthenticate(): TemplateResponse public function showAuthenticate(): TemplateResponse
{ {
$this->redirectIfOwned($this->share);
$templateParameters = ['share' => $this->share]; $templateParameters = ['share' => $this->share];
return new TemplateResponse('core', 'publicshareauth', $templateParameters, 'guest'); return new TemplateResponse('core', 'publicshareauth', $templateParameters, 'guest');
@ -84,8 +95,6 @@ class PublicController extends AuthPublicShareController
*/ */
public function showShare(): TemplateResponse public function showShare(): TemplateResponse
{ {
\OC_User::setIncognitoMode(true);
// Check whether share exists // Check whether share exists
try { try {
$share = $this->shareManager->getShareByToken($this->getToken()); $share = $this->shareManager->getShareByToken($this->getToken());
@ -97,6 +106,12 @@ class PublicController extends AuthPublicShareController
throw new NotFoundException(); throw new NotFoundException();
} }
// Redirect to main app if user owns this share
$this->redirectIfOwned($share);
// Set incognito mode
\OC_User::setIncognitoMode(true);
// Scripts // Scripts
Util::addScript($this->appName, 'memories-main'); Util::addScript($this->appName, 'memories-main');
PageController::provideCommonInitialState($this->initialState); PageController::provideCommonInitialState($this->initialState);
@ -166,4 +181,54 @@ class PublicController extends AuthPublicShareController
{ {
return null !== $this->share->getPassword(); return null !== $this->share->getPassword();
} }
protected function redirectIfOwned(IShare $share)
{
$user = $this->userSession->getUser();
if (!$user) {
return null;
}
/** @var \OCP\Files\Node */
$node = null;
/** @var \OCP\Files\Folder */
$userFolder = null;
// Check if the user has this folder in their root
try {
$userFolder = $this->rootFolder->getUserFolder($user->getUID());
$nodes = $userFolder->getById($share->getNodeId());
if (0 === \count($nodes)) {
return null;
}
$node = $nodes[0];
} catch (NotFoundException $e) {
return null;
}
// Remove user folder path from start of node path
$relPath = substr($node->getPath(), \strlen($userFolder->getPath()));
// Get the user's folders path
$foldersPath = $this->config->getUserValue($user->getUID(), Application::APPNAME, 'foldersPath', '/');
// Check if relPath starts with foldersPath
if (0 !== strpos($relPath, $foldersPath)) {
return null;
}
// Remove foldersPath from start of relPath
$relPath = substr($relPath, \strlen($foldersPath));
// Redirect to the local path
$url = $this->urlGenerator->linkToRouteAbsolute('memories.Page.folder', ['path' => $relPath]);
// Cannot send a redirect response here because the return
// type is a template response for the base class
header('HTTP/1.1 302 Found');
header('Location: '.$url);
exit;
}
} }