Skip to content

Commit 8fa19a0

Browse files
authored
Merge pull request #9647 from nextcloud/artonge/feat/public_share_template_provider
Document providing a public share template
2 parents 90def73 + ebd28bf commit 8fa19a0

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

developer_manual/basics/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ Basic concepts
1616
logging
1717
setting
1818
storage/index
19+
public_share_template
1920
testing
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
=======
2+
Public share template
3+
=======
4+
5+
.. sectionauthor:: Louis Chmn <louis@chmn.me>
6+
7+
It is possible to overrite the default public share view. This is possible by implementing the ``IPublicShareTemplateProvider`` interface.
8+
9+
.. code-block:: php
10+
11+
<?php
12+
// lib/AppInfo/Application.php
13+
namespace OCA\MyApp;
14+
15+
class Application extends App implements IBootstrap {
16+
...
17+
18+
public function register(IRegistrationContext $context): void {
19+
...
20+
21+
$context->registerPublicShareTemplateProvider(MyPublicShareTemplateProvider::class);
22+
}
23+
}
24+
25+
.. code-block:: php
26+
27+
<?php
28+
// lib/providers/MyPublicShareTemplateProvider.php
29+
namespace OCA\MyApp;
30+
31+
use OCA\MyApp\AppInfo\Application;
32+
use OCP\AppFramework\Http\ContentSecurityPolicy;
33+
use OCP\AppFramework\Http\Template\PublicTemplateResponse;
34+
use OCP\AppFramework\Http\TemplateResponse;
35+
use OCP\Defaults;
36+
use OCP\IL10N;
37+
use OCP\IURLGenerator;
38+
use OCP\IUserManager;
39+
use OCP\Share\IShare;
40+
use OCP\Share\IPublicShareTemplateProvider;
41+
use OCP\AppFramework\Services\IInitialState;
42+
use OCP\Util;
43+
44+
class E2EEPublicShareTemplateProvider implements IPublicShareTemplateProvider {
45+
public function __construct(
46+
private IUserManager $userManager,
47+
private IUrlGenerator $urlGenerator,
48+
private IL10N $l10n,
49+
private Defaults $defaults,
50+
private IInitialState $initialState,
51+
) {
52+
}
53+
54+
public function shouldRespond(IShare $share): bool {
55+
$node = $share->getNode();
56+
return ...; // Whether your provider should be used or not.
57+
}
58+
59+
public function renderPage(IShare $share, string $token, string $path): TemplateResponse {
60+
$shareNode = $share->getNode();
61+
$owner = $this->userManager->get($share->getShareOwner());
62+
63+
$this->initialState->provideInitialState('fileId', $shareNode->getId());
64+
...; // More initial state that you might need in your view.
65+
66+
// OpenGraph Support: http://ogp.me/
67+
Util::addHeader('meta', ['property' => "og:title", 'content' => $this->l10n->t("Encrypted share")]);
68+
Util::addHeader('meta', ['property' => "og:description", 'content' => $this->defaults->getName() . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : '')]);
69+
Util::addHeader('meta', ['property' => "og:site_name", 'content' => $this->defaults->getName()]);
70+
Util::addHeader('meta', ['property' => "og:url", 'content' => $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $token])]);
71+
Util::addHeader('meta', ['property' => "og:type", 'content' => "object"]);
72+
73+
$csp->addAllowedFrameDomain('\'self\'');
74+
$response->setContentSecurityPolicy($csp);
75+
76+
$response = new PublicTemplateResponse(Application::APP_ID, 'myCustomTemplateFileName', []);
77+
$response->setHeaderTitle($this->l10n->t("My custom title"));
78+
79+
$csp = new ContentSecurityPolicy();
80+
81+
return $response;
82+
}
83+
}

0 commit comments

Comments
 (0)