Skip to content

Commit

Permalink
fix(theming): Correctly expose user and admin theming
Browse files Browse the repository at this point in the history
Signed-off-by: jld3103 <jld3103yt@gmail.com>
  • Loading branch information
provokateurin committed Oct 29, 2023
1 parent fe471da commit bc37cb4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
43 changes: 36 additions & 7 deletions apps/theming/lib/Capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@
*/
namespace OCA\Theming;

use Exception;
use OCA\Theming\AppInfo\Application;
use OCA\Theming\Service\BackgroundService;
use OCP\Capabilities\IPublicCapability;
use OCP\IConfig;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserSession;

/**
* Class Capabilities
Expand All @@ -50,17 +55,20 @@ class Capabilities implements IPublicCapability {
/** @var IConfig */
protected $config;

protected IUserSession $userSession;

/**
* @param ThemingDefaults $theming
* @param Util $util
* @param IURLGenerator $url
* @param IConfig $config
*/
public function __construct(ThemingDefaults $theming, Util $util, IURLGenerator $url, IConfig $config) {
public function __construct(ThemingDefaults $theming, Util $util, IURLGenerator $url, IConfig $config, IUserSession $userSession) {
$this->theming = $theming;
$this->util = $util;
$this->url = $url;
$this->config = $config;
$this->userSession = $userSession;
}

/**
Expand All @@ -86,23 +94,44 @@ public function __construct(ThemingDefaults $theming, Util $util, IURLGenerator
* }
*/
public function getCapabilities() {
$color = $this->theming->getDefaultColorPrimary();
$colorText = $this->theming->getDefaultTextColorPrimary();

$backgroundLogo = $this->config->getAppValue('theming', 'backgroundMime', '');
$color = $this->theming->getColorPrimary();
$backgroundPlain = $backgroundLogo === 'backgroundColor' || ($backgroundLogo === '' && $color !== '#0082c9');
$background = $backgroundPlain ? $color : $this->url->getAbsoluteURL($this->theming->getBackground());

$user = $this->userSession->getUser();
if ($user instanceof IUser) {
$color = $this->theming->getColorPrimary();
$colorText = $this->theming->getTextColorPrimary();

$backgroundImage = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'background_image', BackgroundService::BACKGROUND_DEFAULT);
if ($backgroundImage === BackgroundService::BACKGROUND_CUSTOM) {
$backgroundPlain = false;
$background = $this->url->linkToRouteAbsolute('theming.userTheme.getBackground');
} else if (isset(BackgroundService::SHIPPED_BACKGROUNDS[$backgroundImage])) {
$backgroundPlain = false;
$background = $this->url->linkTo(Application::APP_ID, "img/background/$backgroundImage");
} else if ($backgroundImage !== BackgroundService::BACKGROUND_DEFAULT) {
$backgroundPlain = true;
$background = $color;
}
}

return [
'theming' => [
'name' => $this->theming->getName(),
'url' => $this->theming->getBaseUrl(),
'slogan' => $this->theming->getSlogan(),
'color' => $color,
'color-text' => $this->theming->getTextColorPrimary(),
'color-text' => $colorText,
'color-element' => $this->util->elementColor($color),
'color-element-bright' => $this->util->elementColor($color),
'color-element-dark' => $this->util->elementColor($color, false),
'logo' => $this->url->getAbsoluteURL($this->theming->getLogo()),
'background' => $backgroundLogo === 'backgroundColor' || ($backgroundLogo === '' && $this->theming->getColorPrimary() !== '#0082c9') ?
$this->theming->getColorPrimary() :
$this->url->getAbsoluteURL($this->theming->getBackground()),
'background-plain' => $backgroundLogo === 'backgroundColor' || ($backgroundLogo === '' && $this->theming->getColorPrimary() !== '#0082c9'),
'background' => $background,
'background-plain' => $backgroundPlain,
'background-default' => !$this->util->isBackgroundThemed(),
'logoheader' => $this->url->getAbsoluteURL($this->theming->getLogo()),
'favicon' => $this->url->getAbsoluteURL($this->theming->getLogo()),
Expand Down
10 changes: 7 additions & 3 deletions apps/theming/tests/CapabilitiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use OCP\Files\IAppData;
use OCP\IConfig;
use OCP\IURLGenerator;
use OCP\IUserSession;
use Test\TestCase;

/**
Expand All @@ -56,6 +57,8 @@ class CapabilitiesTest extends TestCase {
/** @var Util|\PHPUnit\Framework\MockObject\MockObject */
protected $util;

protected IUserSession $userSession;

/** @var Capabilities */
protected $capabilities;

Expand All @@ -66,7 +69,8 @@ protected function setUp(): void {
$this->url = $this->getMockBuilder(IURLGenerator::class)->getMock();
$this->config = $this->createMock(IConfig::class);
$this->util = $this->createMock(Util::class);
$this->capabilities = new Capabilities($this->theming, $this->util, $this->url, $this->config);
$this->userSession = $this->createMock(IUserSession::class);
$this->capabilities = new Capabilities($this->theming, $this->util, $this->url, $this->config, $this->userSession);
}

public function dataGetCapabilities() {
Expand Down Expand Up @@ -165,13 +169,13 @@ public function testGetCapabilities($name, $url, $slogan, $color, $textColor, $l
->method('getSlogan')
->willReturn($slogan);
$this->theming->expects($this->atLeast(1))
->method('getColorPrimary')
->method('getDefaultColorPrimary')
->willReturn($color);
$this->theming->expects($this->exactly(3))
->method('getLogo')
->willReturn($logo);
$this->theming->expects($this->once())
->method('getTextColorPrimary')
->method('getDefaultTextColorPrimary')
->willReturn($textColor);

$util = new Util($this->config, $this->createMock(IAppManager::class), $this->createMock(IAppData::class), $this->createMock(ImageManager::class));
Expand Down

0 comments on commit bc37cb4

Please sign in to comment.