Skip to content

Commit

Permalink
Merge pull request #78 from marius-wieschollek/#67
Browse files Browse the repository at this point in the history
[#67] Add option to overwrite dashboard background
  • Loading branch information
newhinton authored Jan 16, 2021
2 parents 12ad996 + 4abadff commit 6ee7ea5
Show file tree
Hide file tree
Showing 17 changed files with 201 additions and 61 deletions.
14 changes: 0 additions & 14 deletions appinfo/app.php

This file was deleted.

1 change: 1 addition & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@
</dependencies>
<settings>
<admin>OCA\Unsplash\Settings\AdminSettings</admin>
<personal>\OCA\Unsplash\Settings\PersonalSettings</personal>
</settings>
</info>
3 changes: 3 additions & 0 deletions css/dashboard.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#app-dashboard {
background-image : url("https://source.unsplash.com/featured/?nature") !important;
}
1 change: 1 addition & 0 deletions l10n/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"Random Background Images":"Zufällige Hintergrundbilder",
"Set random image as login background":"Verwende ein zufälliges Bild als Hintergrund der Anmeldeseite",
"Set random image as header background":"Verwende ein zufälliges Bild als Hintergrund der Navigationsleiste",
"Set random image as dashboard background":"Verwende ein zufälliges Bild als Hintergrund des Dashboards",
"You can customize %s with random background images.":"Du kannst %s mit zufälligen Hintergrundbildern anpassen.",
"Here you can specify where random backgrounds should be used by default.":"Hier kannst du festlegen wo zufällige Hintergrundbilder standardmäßig verwendet werden sollen.",
"These images are provided %s by %s.":"Diese Bilder werden %s von %s bereitgestellt.",
Expand Down
1 change: 1 addition & 0 deletions l10n/de_DE.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"Random Background Images":"Zufällige Hintergrundbilder",
"Set random image as login background":"Verwende ein zufälliges Bild als Hintergrund der Anmeldeseite",
"Set random image as header background":"Verwende ein zufälliges Bild als Hintergrund der Navigationsleiste",
"Set random image as dashboard background":"Verwende ein zufälliges Bild als Hintergrund des Dashboards",
"You can customize %s with random background images.":"Sie können %s mit zufälligen Hintergrundbildern anpassen.",
"Here you can specify where random backgrounds should be used by default.":"Hier können Sie festlegen wo zufällige Hintergrundbilder standardmäßig verwendet werden sollen.",
"These images are provided %s by %s.":"Diese Bilder werden %s von %s bereitgestellt.",
Expand Down
1 change: 1 addition & 0 deletions l10n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"Random Background Images":"Random Background Images",
"Set random image as login background":"Set random image as login background",
"Set random image as header background":"Set random image as header background",
"Set random image as dashboard background":"Set random image as dashboard background",
"You can customize %s with random background images.":"You can customize %s with random background images.",
"Here you can specify where random backgrounds should be used by default.":"Here you can specify where Unsplash backgrounds should be used by default.",
"These images are provided %s by %s.":"These images are provided %s by %s.",
Expand Down
1 change: 1 addition & 0 deletions l10n/en_GB.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"Random Background Images":"Random Background Images",
"Set random image as login background":"Set random image as login background",
"Set random image as header background":"Set random image as header background",
"Set random image as dashboard background":"Set random image as dashboard background",
"You can customize %s with random background images.":"You can customize %s with random background images.",
"Here you can specify where random backgrounds should be used by default.":"Here you can specify where Unsplash backgrounds should be used by default.",
"These images are provided %s by %s.":"These images are provided %s by %s.",
Expand Down
57 changes: 15 additions & 42 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

namespace OCA\Unsplash\AppInfo;

use OC\Security\CSP\ContentSecurityPolicy;
use OCA\Unsplash\Services\SettingsService;
use OCA\Unsplash\EventListener\AddContentSecurityPolicyEventListener;
use OCA\Unsplash\EventListener\BeforeTemplateRenderedEventListener;
use OCP\AppFramework\App;
use OCP\Util;
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Security\CSP\AddContentSecurityPolicyEvent;

/**
* Class Application
Expand All @@ -25,59 +27,30 @@ class Application extends App {
*/
public function __construct(array $urlParams = []) {
parent::__construct('unsplash', $urlParams);
$this->registerSystemEvents();
}

/**
* Register all app functionality
*
* @throws \OCP\AppFramework\QueryException
* Register app functionality
*/
public function register() {
$this->registerPersonalSettings();
$this->registerStyleSheets();
$this->registerCsp();
}

/**
* Add the personal settings page
*/
public function registerPersonalSettings() {
\OCP\App::registerPersonal('unsplash', 'templates/personal');
}

/**
* Add the stylesheets
*
* @throws \OCP\AppFramework\QueryException
*/
public function registerStyleSheets() {
/** @var SettingsService $settings */
$settings = $this->getContainer()->query(SettingsService::class);

if($settings->getUserStyleHeaderEnabled()) {
Util::addStyle('unsplash', 'header');
}
if($settings->getServerStyleLoginEnabled()) {
Util::addStyle('unsplash', 'login');
}
protected function registerSystemEvents() {
/* @var IEventDispatcher $eventDispatcher */
$dispatcher = $this->getContainer()->get(IEventDispatcher::class);
$dispatcher->addServiceListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedEventListener::class);
$dispatcher->addServiceListener(AddContentSecurityPolicyEvent::class, AddContentSecurityPolicyEventListener::class);
}

/**
* Allow Unsplash hosts in the csp
*
* @throws \OCP\AppFramework\QueryException
* Add the personal settings page
*/
public function registerCsp() {
/** @var SettingsService $settings */
$settings = $this->getContainer()->query(SettingsService::class);

if($settings->getUserStyleHeaderEnabled() || $settings->getServerStyleLoginEnabled()) {
$manager = $this->getContainer()->getServer()->getContentSecurityPolicyManager();
$policy = new ContentSecurityPolicy();
$policy->addAllowedImageDomain('https://source.unsplash.com');
$policy->addAllowedImageDomain('https://images.unsplash.com');
$manager->addDefaultPolicy($policy);
}
public function registerPersonalSettings() {
\OCP\App::registerPersonal('unsplash', 'templates/personal');
}

}
2 changes: 2 additions & 0 deletions lib/Controller/AdminSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public function set(string $key, $value): JSONResponse {
$this->settings->setServerStyleHeaderEnabled($value);
} else if($key === 'style/login') {
$this->settings->setServerStyleLoginEnabled($value);
} else if($key === 'style/dashboard') {
$this->settings->setServerStyleDashboardEnabled($value);
} else {
return new JSONResponse(['status' => 'error'], Http::STATUS_BAD_REQUEST);
}
Expand Down
2 changes: 2 additions & 0 deletions lib/Controller/PersonalSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public function set(string $key, $value): JSONResponse {

if($key === 'style/header') {
$this->settings->setUserStyleHeaderEnabled($value);
} else if($key === 'style/dashboard') {
$this->settings->setUserStyleDashboardEnabled($value);
} else {
return new JSONResponse(['status' => 'error'], Http::STATUS_BAD_REQUEST);
}
Expand Down
45 changes: 45 additions & 0 deletions lib/EventListener/AddContentSecurityPolicyEventListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* This file is part of the Unpslash App
* and licensed under the AGPL.
*/

namespace OCA\Unsplash\EventListener;

use OC\Security\CSP\ContentSecurityPolicy;
use OCA\Unsplash\Services\SettingsService;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Security\CSP\AddContentSecurityPolicyEvent;

class AddContentSecurityPolicyEventListener implements IEventListener {
/**
* @var SettingsService
*/
protected $settingsService;

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

/**
* @param Event $event
*/
public function handle(Event $event): void {
if(!($event instanceof AddContentSecurityPolicyEvent)) {
return;
}

if($this->settingsService->getUserStyleHeaderEnabled() || $this->settingsService->getServerStyleLoginEnabled() || $this->settingsService->getUserStyleDashboardEnabled()) {
$policy = new ContentSecurityPolicy();
$policy->addAllowedImageDomain('https://source.unsplash.com');
$policy->addAllowedImageDomain('https://images.unsplash.com');
$event->addPolicy($policy);
}
}
}
60 changes: 60 additions & 0 deletions lib/EventListener/BeforeTemplateRenderedEventListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* This file is part of the Unpslash App
* and licensed under the AGPL.
*/

namespace OCA\Unsplash\EventListener;

use OCA\Unsplash\Services\SettingsService;
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\IRequest;
use OCP\Util;

class BeforeTemplateRenderedEventListener implements IEventListener {
/**
* @var SettingsService
*/
protected $settingsService;

/**
* @var IRequest
*/
protected $request;

/**
* BeforeTemplateRenderedEventListener constructor.
*
* @param SettingsService $settingsService
* @param IRequest $request
*/
public function __construct(SettingsService $settingsService, IRequest $request) {
$this->settingsService = $settingsService;
$this->request = $request;
}

/**
* @param Event $event
*/
public function handle(Event $event): void {
if(!($event instanceof BeforeTemplateRenderedEvent)) {
return;
}

if($event->isLoggedIn()) {
if($this->settingsService->getUserStyleHeaderEnabled() && $this->request->getParam('_route') !== 'dashboard.dashboard.index') {
Util::addStyle('unsplash', 'header');
}

if($this->settingsService->getUserStyleDashboardEnabled() && $this->request->getParam('_route') === 'dashboard.dashboard.index') {
Util::addStyle('unsplash', 'dashboard');
}
}

if(!$event->isLoggedIn() && $this->settingsService->getServerStyleLoginEnabled()) {
Util::addStyle('unsplash', 'login');
}
}
}
51 changes: 48 additions & 3 deletions lib/Services/SettingsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
*/
class SettingsService {

const STYLE_LOGIN = 'unsplash/style/login';
const STYLE_HEADER = 'unsplash/style/header';
const USER_STYLE_HEADER = 'unsplash/style/header';
const STYLE_LOGIN = 'unsplash/style/login';
const STYLE_HEADER = 'unsplash/style/header';
const STYLE_DASHBORAD = 'unsplash/style/dashborad';
const USER_STYLE_HEADER = 'unsplash/style/header';
const USER_STYLE_DASHBORAD = 'unsplash/style/dashborad';

/**
* @var IConfig
Expand Down Expand Up @@ -75,6 +77,31 @@ public function setUserStyleHeaderEnabled(int $styleHeader = 1) {
$this->config->setUserValue($this->userId, $this->appName, self::USER_STYLE_HEADER, $styleHeader);
}

/**
* If the dashboard should be styled for this user
*
* @return bool
*/
public function getUserStyleDashboardEnabled(): bool {
$styleHeader = $this->config->getUserValue($this->userId, $this->appName, self::USER_STYLE_DASHBORAD, null);

if($styleHeader === null) return $this->getServerStyleDashboardEnabled();

return $styleHeader == 1;
}

/**
* Set if the dashboard should be styled for this user
*
* @param int $styleDashboard
*
* @return void
* @throws \OCP\PreConditionNotMetException
*/
public function setUserStyleDashboardEnabled(int $styleDashboard = 1) {
$this->config->setUserValue($this->userId, $this->appName, self::USER_STYLE_DASHBORAD, $styleDashboard);
}

/**
* If the page header should be styled by default
*
Expand All @@ -93,6 +120,24 @@ public function setServerStyleHeaderEnabled(int $styleHeader = 1) {
$this->config->setAppValue($this->appName, self::STYLE_HEADER, $styleHeader);
}

/**
* If the page dashboard be styled by default
*
* @return bool
*/
public function getServerStyleDashboardEnabled(): bool {
return $this->config->getAppValue($this->appName, self::STYLE_DASHBORAD, 0) == 1;
}

/**
* Set if the dashboard should be styled by default
*
* @param int $styleDashboard
*/
public function setServerStyleDashboardEnabled(int $styleDashboard = 1) {
$this->config->setAppValue($this->appName, self::STYLE_DASHBORAD, $styleDashboard);
}

/**
* If the login page should be styled by default
*
Expand Down
3 changes: 2 additions & 1 deletion lib/Settings/AdminSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public function getForm(): TemplateResponse {
return new TemplateResponse('unsplash', 'settings/admin', [
'saveSettingsUrl' => $this->urlGenerator->linkToRouteAbsolute('unsplash.admin_settings.set'),
'styleLogin' => $this->settings->getServerStyleLoginEnabled(),
'styleHeader' => $this->settings->getServerStyleHeaderEnabled()
'styleHeader' => $this->settings->getServerStyleHeaderEnabled(),
'styleDashboard' => $this->settings->getServerStyleDashboardEnabled()
]);
}

Expand Down
12 changes: 11 additions & 1 deletion lib/Settings/PersonalSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
use OCA\Unsplash\Services\SettingsService;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\IURLGenerator;
use OCP\Settings\ISettings;

/**
* Class PersonalSettingsController
*
* @package OCA\Unsplash\Controller\Settings
*/
class PersonalSettings {
class PersonalSettings implements ISettings {

/**
* @var IURLGenerator
Expand Down Expand Up @@ -56,7 +57,16 @@ public function getForm() {
return new TemplateResponse('unsplash', 'settings/personal', [
'saveSettingsUrl' => $this->urlGenerator->linkToRouteAbsolute('unsplash.personal_settings.set'),
'styleHeader' => $this->settings->getUserStyleHeaderEnabled(),
'styleDashboard' => $this->settings->getUserStyleDashboardEnabled(),
'label' => $this->theming->getEntity()
], '');
}

public function getSection() {
return 'additional';
}

public function getPriority(): int {
return 75;
}
}
Loading

0 comments on commit 6ee7ea5

Please sign in to comment.