Skip to content

Commit

Permalink
[#80] Restore compatibility with Nextcloud 14+
Browse files Browse the repository at this point in the history
Signed-off-by: Marius David Wieschollek <git.public@mdns.eu>
  • Loading branch information
marius-wieschollek committed Jan 17, 2021
1 parent e0d24a0 commit 2fba69a
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 22 deletions.
3 changes: 2 additions & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
</dependencies>
<settings>
<admin>OCA\Unsplash\Settings\AdminSettings</admin>
<personal>\OCA\Unsplash\Settings\PersonalSettings</personal>
<personal>OCA\Unsplash\Settings\PersonalSettings</personal>
<personal-section>OCA\Unsplash\Settings\PersonalSection</personal-section>
</settings>
</info>
30 changes: 12 additions & 18 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use OCA\Unsplash\EventListener\AddContentSecurityPolicyEventListener;
use OCA\Unsplash\EventListener\BeforeTemplateRenderedEventListener;
use OCA\Unsplash\Services\LegacyInitialisationService;
use OCP\AppFramework\App;
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
use OCP\EventDispatcher\IEventDispatcher;
Expand All @@ -30,27 +31,20 @@ public function __construct(array $urlParams = []) {
$this->registerSystemEvents();
}

/**
* Register app functionality
*/
public function register() {
$this->registerPersonalSettings();
}

/**
*
*/
protected function registerSystemEvents() {
/* @var IEventDispatcher $eventDispatcher */
$dispatcher = $this->getContainer()->get(IEventDispatcher::class);
$dispatcher->addServiceListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedEventListener::class);
$dispatcher->addServiceListener(AddContentSecurityPolicyEvent::class, AddContentSecurityPolicyEventListener::class);
}

/**
* Add the personal settings page
*/
public function registerPersonalSettings() {
\OCP\App::registerPersonal('unsplash', 'templates/personal');
$container = $this->getContainer();
if(method_exists($container, 'get')) {
/* @var IEventDispatcher $eventDispatcher */
$dispatcher = $this->getContainer()->get(IEventDispatcher::class);
$dispatcher->addServiceListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedEventListener::class);
$dispatcher->addServiceListener(AddContentSecurityPolicyEvent::class, AddContentSecurityPolicyEventListener::class);
} else {
/** @var LegacyInitialisationService $service */
$service = $this->getContainer()->query(LegacyInitialisationService::class);
$service->initialize();
}
}
}
72 changes: 72 additions & 0 deletions lib/Services/LegacyInitialisationService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* This file is part of the Unpslash App
* and licensed under the AGPL.
*/

namespace OCA\Unsplash\Services;

use OC\Security\CSP\ContentSecurityPolicy;
use OCP\Security\IContentSecurityPolicyManager;
use OCP\Util;

/**
* Class LegacyInitialisationService
*
* @package OCA\Unsplash\Services
*/
class LegacyInitialisationService {

/**
* @var SettingsService
*/
protected $settingsService;

/**
* @var IContentSecurityPolicyManager
*/
protected $contentSecurityPolicyManager;

/**
* LegacyInitialisationService constructor.
*
* @param SettingsService $settingsService
* @param IContentSecurityPolicyManager $contentSecurityPolicyManager
*/
public function __construct(SettingsService $settingsService, IContentSecurityPolicyManager $contentSecurityPolicyManager) {
$this->settingsService = $settingsService;
$this->contentSecurityPolicyManager = $contentSecurityPolicyManager;
}

/**
*
*/
public function initialize() {
$this->registerStyleSheets();
$this->registerCsp();
}

/**
* Add the stylesheets
*/
protected function registerStyleSheets() {
if($this->settingsService->getUserStyleHeaderEnabled()) {
Util::addStyle('unsplash', 'header');
}
if($this->settingsService->getServerStyleLoginEnabled()) {
Util::addStyle('unsplash', 'login');
}
}

/**
* Allow Unsplash hosts in the csp
*/
protected function registerCsp() {
if($this->settingsService->getUserStyleHeaderEnabled() || $this->settingsService->getServerStyleLoginEnabled()) {
$policy = new ContentSecurityPolicy();
$policy->addAllowedImageDomain('https://source.unsplash.com');
$policy->addAllowedImageDomain('https://images.unsplash.com');
$this->contentSecurityPolicyManager->addDefaultPolicy($policy);
}
}
}
9 changes: 9 additions & 0 deletions lib/Services/SettingsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,13 @@ public function setServerStyleLoginEnabled(int $styleLogin = 1) {
$this->config->setAppValue($this->appName, self::STYLE_LOGIN, $styleLogin);
}

/**
* @return int
*/
public function getNextcloudVersion(): int {
$version = $this->config->getSystemValue('version', '0.0.0');
$parts = explode('.', $version, 2);

return intval($parts[0]);
}
}
3 changes: 2 additions & 1 deletion lib/Settings/AdminSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public function getForm(): TemplateResponse {
'saveSettingsUrl' => $this->urlGenerator->linkToRouteAbsolute('unsplash.admin_settings.set'),
'styleLogin' => $this->settings->getServerStyleLoginEnabled(),
'styleHeader' => $this->settings->getServerStyleHeaderEnabled(),
'styleDashboard' => $this->settings->getServerStyleDashboardEnabled()
'styleDashboard' => $this->settings->getServerStyleDashboardEnabled(),
'hasDashboard' => $this->settings->getNextcloudVersion() > 19
]);
}

Expand Down
92 changes: 92 additions & 0 deletions lib/Settings/PersonalSection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/**
* This file is part of the Unpslash App
* and licensed under the AGPL.
*/

namespace OCA\Unsplash\Settings;

use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\Settings\IIconSection;

/**
* Class PersonalSection
*
* @package OCA\Unsplash\Settings
*/
class PersonalSection implements IIconSection {

/**
* @var IURLGenerator
*/
protected $urlGenerator;

/**
* @var IL10N
*/
protected $localisation;

/**
* @var
*/
protected $appName;

/**
* AdminSection constructor.
*
* @param IL10N $localisation
* @param IURLGenerator $urlGenerator
* @param $appName
*/
public function __construct(IL10N $localisation, IURLGenerator $urlGenerator, $appName) {
$this->localisation = $localisation;
$this->urlGenerator = $urlGenerator;
$this->appName = $appName;
}

/**
* Returns the relative path to an 16*16 icon describing the section.
* e.g. '/core/img/places/files.svg'
*
* @returns string
* @since 12
*/
public function getIcon(): string {
return $this->urlGenerator->imagePath($this->appName, 'app-dark.svg');
}

/**
* Returns the ID of the section. It is supposed to be a lower case string,
* e.g. 'passwords'
*
* @returns string
* @since 9.1
*/
public function getID(): string {
return $this->appName;
}

/**
* Returns the translated name as it should be displayed, e.g. 'Splash'.
* Use the L10N service to translate it.
*
* @return string
* @since 9.1
*/
public function getName(): string {
return $this->localisation->t('Splash');
}

/**
* @return int whether the form should be rather on the top or bottom of
* the settings navigation. The sections are arranged in ascending order of
* the priority values. It is required to return a value between 0 and 99.
*
* E.g.: 70
* @since 9.1
*/
public function getPriority(): int {
return 50;
}
}
18 changes: 16 additions & 2 deletions lib/Settings/PersonalSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,24 @@ class PersonalSettings implements ISettings {
*/
protected $theming;

/**
* @var string
*/
protected $appName;

/**
* AdminSection constructor.
*
* @param IURLGenerator $urlGenerator
* @param SettingsService $settings
* @param OC_Defaults $theming
* @param $appName
*/
public function __construct(IURLGenerator $urlGenerator, SettingsService $settings, OC_Defaults $theming) {
public function __construct(IURLGenerator $urlGenerator, SettingsService $settings, OC_Defaults $theming, $appName) {
$this->urlGenerator = $urlGenerator;
$this->settings = $settings;
$this->theming = $theming;
$this->appName = $appName;
}

/**
Expand All @@ -58,14 +65,21 @@ public function getForm() {
'saveSettingsUrl' => $this->urlGenerator->linkToRouteAbsolute('unsplash.personal_settings.set'),
'styleHeader' => $this->settings->getUserStyleHeaderEnabled(),
'styleDashboard' => $this->settings->getUserStyleDashboardEnabled(),
'hasDashboard' => $this->settings->getNextcloudVersion() > 19,
'label' => $this->theming->getEntity()
], '');
}

/**
* @return string
*/
public function getSection() {
return 'additional';
return $this->appName;
}

/**
* @return int
*/
public function getPriority(): int {
return 75;
}
Expand Down
2 changes: 2 additions & 0 deletions templates/settings/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
<input id="unsplash-style-header" name="unsplash-style-header" data-setting="style/header" type="checkbox" <?=$_['styleHeader'] ? 'checked':''?> class="checkbox">
<label for="unsplash-style-header"><?php p($l->t('Set random image as header background')); ?></label>
</div>
<?php if($_['hasDashboard']): ?>
<div>
<input id="unsplash-style-dashboard" name="unsplash-style-dashboard" data-setting="style/dashboard" type="checkbox" <?=$_['styleDashboard'] ? 'checked':''?> class="checkbox">
<label for="unsplash-style-dashboard"><?php p($l->t('Set random image as dashboard background')); ?></label>
</div>
<?php endif; ?>
</form>
</div>
2 changes: 2 additions & 0 deletions templates/settings/personal.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
<input id="unsplash-style-header" name="unsplash-style-header" data-setting="style/header" type="checkbox" <?=$_['styleHeader'] ? 'checked':''?> class="checkbox">
<label for="unsplash-style-header"><?php p($l->t('Set random image as header background')); ?></label>
</div>
<?php if($_['hasDashboard']): ?>
<div>
<input id="unsplash-style-dashboard" name="unsplash-style-dashboard" data-setting="style/dashboard" type="checkbox" <?=$_['styleDashboard'] ? 'checked':''?> class="checkbox">
<label for="unsplash-style-dashboard"><?php p($l->t('Set random image as dashboard background')); ?></label>
</div>
<?php endif; ?>
</form>
</div>

0 comments on commit 2fba69a

Please sign in to comment.