Skip to content

Commit

Permalink
fix(theming): Add some strict checking for userId
Browse files Browse the repository at this point in the history
Signed-off-by: Ferdinand Thiessen <[email protected]>
  • Loading branch information
susnux committed May 20, 2024
1 parent 827eae1 commit 70052cc
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion apps/theming/lib/Service/BackgroundService.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use OCP\IConfig;
use OCP\Lock\LockedException;
use OCP\PreConditionNotMetException;
use RuntimeException;

class BackgroundService {
public const DEFAULT_COLOR = '#00679e';
Expand Down Expand Up @@ -231,6 +232,9 @@ public function setDefaultBackground(): void {
* @throws NoUserException
*/
public function setFileBackground($path): void {
if ($this->userId === null) {
throw new RuntimeException('No currently logged-in user');
}
$userFolder = $this->rootFolder->getUserFolder($this->userId);

/** @var File $file */
Expand All @@ -251,6 +255,9 @@ public function setFileBackground($path): void {
}

public function setShippedBackground($fileName): void {
if ($this->userId === null) {
throw new RuntimeException('No currently logged-in user');
}
if (!array_key_exists($fileName, self::SHIPPED_BACKGROUNDS)) {
throw new InvalidArgumentException('The given file name is invalid');
}
Expand All @@ -263,6 +270,9 @@ public function setShippedBackground($fileName): void {
* Set the background to color only
*/
public function setColorBackground(string $color): void {
if ($this->userId === null) {
throw new RuntimeException('No currently logged-in user');
}
if (!preg_match('/^#([0-9a-f]{3}|[0-9a-f]{6})$/i', $color)) {
throw new InvalidArgumentException('The given color is invalid');
}
Expand All @@ -271,6 +281,9 @@ public function setColorBackground(string $color): void {
}

public function deleteBackgroundImage(): void {
if ($this->userId === null) {
throw new RuntimeException('No currently logged-in user');
}
$this->config->setUserValue($this->userId, Application::APP_ID, 'background_image', self::BACKGROUND_COLOR);
}

Expand Down Expand Up @@ -314,7 +327,7 @@ private function calculateMeanColor(\OCP\Image $image): false|string {
/**
* Small helper to ensure one channel is returned as 8byte hex
*/
function toHex(int $channel) {
function toHex(int $channel): string {
$hex = dechex($channel);
return match (strlen($hex)) {
0 => '00',
Expand Down

0 comments on commit 70052cc

Please sign in to comment.