]> source.dussan.org Git - nextcloud-server.git/commitdiff
fix(files_sharing): show proper share not found error message 46967/head
authorskjnldsv <skjnldsv@protonmail.com>
Thu, 1 Aug 2024 20:28:08 +0000 (22:28 +0200)
committerskjnldsv <skjnldsv@protonmail.com>
Tue, 6 Aug 2024 14:25:10 +0000 (16:25 +0200)
Signed-off-by: skjnldsv <skjnldsv@protonmail.com>
apps/files_sharing/lib/Controller/ShareController.php
apps/files_sharing/templates/sharenotfound.php [new file with mode: 0644]
lib/private/AppFramework/DependencyInjection/DIContainer.php
lib/private/AppFramework/Middleware/PublicShare/PublicShareMiddleware.php
tests/lib/AppFramework/Middleware/PublicShare/PublicShareMiddlewareTest.php

index eef2f5f32bc75118b6a1cd0a3ffd657aee01acaa..642065c99832bcf5bc8c143561aa023e9e0047d1 100644 (file)
@@ -294,11 +294,11 @@ class ShareController extends AuthPublicShareController {
                } catch (ShareNotFound $e) {
                        // The share does not exists, we do not emit an ShareLinkAccessedEvent
                        $this->emitAccessShareHook($this->getToken(), 404, 'Share not found');
-                       throw new NotFoundException();
+                       throw new NotFoundException($this->l10n->t('This share does not exist or is no longer available'));
                }
 
                if (!$this->validateShare($share)) {
-                       throw new NotFoundException();
+                       throw new NotFoundException($this->l10n->t('This share does not exist or is no longer available'));
                }
 
                $shareNode = $share->getNode();
@@ -309,7 +309,7 @@ class ShareController extends AuthPublicShareController {
                } catch (NotFoundException $e) {
                        $this->emitAccessShareHook($share, 404, 'Share not found');
                        $this->emitShareAccessEvent($share, ShareController::SHARE_ACCESS, 404, 'Share not found');
-                       throw new NotFoundException();
+                       throw new NotFoundException($this->l10n->t('This share does not exist or is no longer available'));
                }
 
                // We can't get the path of a file share
@@ -317,7 +317,7 @@ class ShareController extends AuthPublicShareController {
                        if ($shareNode instanceof \OCP\Files\File && $path !== '') {
                                $this->emitAccessShareHook($share, 404, 'Share not found');
                                $this->emitShareAccessEvent($share, self::SHARE_ACCESS, 404, 'Share not found');
-                               throw new NotFoundException();
+                               throw new NotFoundException($this->l10n->t('This share does not exist or is no longer available'));
                        }
                } catch (\Exception $e) {
                        $this->emitAccessShareHook($share, 404, 'Share not found');
diff --git a/apps/files_sharing/templates/sharenotfound.php b/apps/files_sharing/templates/sharenotfound.php
new file mode 100644 (file)
index 0000000..fe653d4
--- /dev/null
@@ -0,0 +1,23 @@
+<?php
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-only
+ */
+
+use OCP\IURLGenerator;
+use OCP\Server;
+
+$urlGenerator = Server::get(IURLGenerator::class);
+?>
+<div class="body-login-container update">
+       <div>
+               <svg xmlns="http://www.w3.org/2000/svg" height="70" viewBox="0 -960 960 960" width="70">
+                       <path fill="currentColor" d="m674-456-50-50 69-70-69-69 50-51 70 70 69-70 51 51-70 69 70 70-51 50-69-69-70 69Zm-290-24q-60 0-102-42t-42-102q0-60 42-102t102-42q60 0 102 42t42 102q0 60-42 102t-102 42ZM96-192v-92q0-26 12.5-47.5T143-366q55-32 116-49t125-17q64 0 125 17t116 49q22 13 34.5 34.5T672-284v92H96Z"/>
+               </svg>
+       </div>
+       <h2><?php p($l->t('Share not found')); ?></h2>
+       <p class="infogroup"><?php p($_['message'] ?: $l->t('This share does not exist or is no longer available')); ?></p>
+       <p><a class="button primary" href="<?php p($urlGenerator->linkTo('', 'index.php')) ?>">
+               <?php p($l->t('Back to %s', [$theme->getName()])); ?>
+       </a></p>
+</div>
index 81365900d9b1308e8c4a1bd916f3aad55a113bdd..208031b942fea9e4e5812bc51f6ff12603c6d3b6 100644 (file)
@@ -288,7 +288,7 @@ class DIContainer extends SimpleContainer implements IAppContainer {
                                new OC\AppFramework\Middleware\PublicShare\PublicShareMiddleware(
                                        $c->get(IRequest::class),
                                        $c->get(ISession::class),
-                                       $c->get(\OCP\IConfig::class),
+                                       $c->get(IConfig::class),
                                        $c->get(IThrottler::class)
                                )
                        );
index 41cba1aacd37e1701fb9ec4b0eea204a72aae54f..34291dfef100f9af630c5ff9e5b2cf44aa5b9626 100644 (file)
@@ -6,8 +6,10 @@
 namespace OC\AppFramework\Middleware\PublicShare;
 
 use OC\AppFramework\Middleware\PublicShare\Exceptions\NeedAuthenticationException;
+use OCA\Files_Sharing\AppInfo\Application;
 use OCP\AppFramework\AuthPublicShareController;
-use OCP\AppFramework\Http\NotFoundResponse;
+use OCP\AppFramework\Http;
+use OCP\AppFramework\Http\TemplateResponse;
 use OCP\AppFramework\Middleware;
 use OCP\AppFramework\PublicShareController;
 use OCP\Files\NotFoundException;
@@ -17,23 +19,13 @@ use OCP\ISession;
 use OCP\Security\Bruteforce\IThrottler;
 
 class PublicShareMiddleware extends Middleware {
-       /** @var IRequest */
-       private $request;
 
-       /** @var ISession */
-       private $session;
-
-       /** @var IConfig */
-       private $config;
-
-       /** @var IThrottler */
-       private $throttler;
-
-       public function __construct(IRequest $request, ISession $session, IConfig $config, IThrottler $throttler) {
-               $this->request = $request;
-               $this->session = $session;
-               $this->config = $config;
-               $this->throttler = $throttler;
+       public function __construct(
+               private IRequest $request,
+               private ISession $session,
+               private IConfig $config,
+               private IThrottler $throttler
+       ) {
        }
 
        public function beforeController($controller, $methodName) {
@@ -92,7 +84,9 @@ class PublicShareMiddleware extends Middleware {
                }
 
                if ($exception instanceof NotFoundException) {
-                       return new NotFoundResponse();
+                       return new TemplateResponse(Application::APP_ID, 'sharenotfound', [
+                               'message' => $exception->getMessage(),
+                       ], 'guest', Http::STATUS_NOT_FOUND);
                }
 
                if ($controller instanceof AuthPublicShareController && $exception instanceof NeedAuthenticationException) {
index 03cd253044de648cab0efd55f98561c2cabc1fb6..64a9efbfe60b41538873d91e7d6b645753096d6f 100644 (file)
@@ -10,8 +10,9 @@ use OC\AppFramework\Middleware\PublicShare\Exceptions\NeedAuthenticationExceptio
 use OC\AppFramework\Middleware\PublicShare\PublicShareMiddleware;
 use OCP\AppFramework\AuthPublicShareController;
 use OCP\AppFramework\Controller;
-use OCP\AppFramework\Http\NotFoundResponse;
+use OCP\AppFramework\Http;
 use OCP\AppFramework\Http\RedirectResponse;
+use OCP\AppFramework\Http\TemplateResponse;
 use OCP\AppFramework\PublicShareController;
 use OCP\Files\NotFoundException;
 use OCP\IConfig;
@@ -236,7 +237,8 @@ class PublicShareMiddlewareTest extends \Test\TestCase {
                $exception = new NotFoundException();
 
                $result = $this->middleware->afterException($controller, 'method', $exception);
-               $this->assertInstanceOf(NotFoundResponse::class, $result);
+               $this->assertInstanceOf(TemplateResponse::class, $result);
+               $this->assertEquals($result->getStatus(), Http::STATUS_NOT_FOUND);
        }
 
        public function testAfterExceptionPublicShareController() {