Skip to content

A free open source IT asset/license management system

Styling: apply fallbacks for link colors properly

Marcus Moore7e8c5bcf012b
diff --git a/app/Models/User.php b/app/Models/User.php
index a7f46a0561..3f3f357fe2 100644
--- a/app/Models/User.php
+++ b/app/Models/User.php
@@ -753,21 +753,57 @@ class User extends SnipeModel implements AuthenticatableContract, AuthorizableCo
protected function linkLightColor(): Attribute
{
return Attribute::make(
- get: fn (?string $value) => CssColor::sanitize($value, '#296282'),
+ get: function (?string $value) {
+ $fallback = '#296282';
+
+ if ($value) {
+ return CssColor::sanitize($value, $fallback);
+ }
+
+ if (Setting::getSettings()) {
+ return CssColor::sanitize(Setting::getSettings()->link_light_color, $fallback);
+ }
+
+ return CssColor::sanitize($value, $fallback);
+ },
);
}
protected function linkDarkColor(): Attribute
{
return Attribute::make(
- get: fn (?string $value) => CssColor::sanitize($value, '#5fa4cc'),
+ get: function (?string $value) {
+ $fallback = '#5fa4cc';
+
+ if ($value) {
+ return CssColor::sanitize($value, $fallback);
+ }
+
+ if (Setting::getSettings()) {
+ return CssColor::sanitize(Setting::getSettings()->link_dark_color, $fallback);
+ }
+
+ return CssColor::sanitize($value, $fallback);
+ },
);
}
protected function navLinkColor(): Attribute
{
return Attribute::make(
- get: fn (?string $value) => CssColor::sanitize($value, '#ffffff'),
+ get: function (?string $value) {
+ $fallback = '#ffffff';
+
+ if ($value) {
+ return CssColor::sanitize($value, $fallback);
+ }
+
+ if (Setting::getSettings()) {
+ return CssColor::sanitize(Setting::getSettings()->nav_link_color, $fallback);
+ }
+
+ return CssColor::sanitize($value, $fallback);
+ },
);
}
diff --git a/tests/Unit/UserTest.php b/tests/Unit/UserTest.php
index 0988868c3c..61c06d1816 100644
--- a/tests/Unit/UserTest.php
+++ b/tests/Unit/UserTest.php
@@ -208,4 +208,76 @@ class UserTest extends TestCase
$user = User::generateFormattedNameFromFullName($fullname, 'firstnamelastinitial');
$this->assertEquals($expected_email, $user['username'].'@example.com');
}
+
+ public function test_link_light_color_attribute()
+ {
+ // if global branding and user are not set we use the app default defined in the accessor:
+ $this->settings->set(['link_light_color' => null]);
+ $this->assertEquals(
+ '#296282',
+ User::factory()->create()->link_light_color
+ );
+
+ // nothing set on user we use global branding:
+ $this->settings->set(['link_light_color' => '#ff6700']);
+ $this->assertEquals(
+ '#ff6700',
+ User::factory()->create()->link_light_color
+ );
+
+ // user has it set we use it:
+ $user = User::factory()->create(['link_light_color' => '#61b329']);
+ $this->assertEquals(
+ '#61b329',
+ $user->link_light_color
+ );
+ }
+
+ public function test_link_dark_color_attribute()
+ {
+ // if global branding and user are not set we use the app default defined in the accessor:
+ $this->settings->set(['link_dark_color' => null]);
+ $this->assertEquals(
+ '#5fa4cc',
+ User::factory()->create()->link_dark_color
+ );
+
+ // nothing set on user we use global branding:
+ $this->settings->set(['link_dark_color' => '#ff6700']);
+ $this->assertEquals(
+ '#ff6700',
+ User::factory()->create()->link_dark_color
+ );
+
+ // user has it set we use it:
+ $user = User::factory()->create(['link_dark_color' => '#61b329']);
+ $this->assertEquals(
+ '#61b329',
+ $user->link_dark_color
+ );
+ }
+
+ public function test_nav_link_color_attribute()
+ {
+ // if global branding and user are not set we use the app default defined in the accessor:
+ $this->settings->set(['nav_link_color' => null]);
+ $this->assertEquals(
+ '#ffffff',
+ User::factory()->create()->nav_link_color
+ );
+
+ // nothing set on user we use global branding:
+ $this->settings->set(['nav_link_color' => '#ff6700']);
+ $this->assertEquals(
+ '#ff6700',
+ User::factory()->create()->nav_link_color
+ );
+
+ // user has it set we use it:
+ $user = User::factory()->create(['nav_link_color' => '#61b329']);
+ $this->assertEquals(
+ '#61b329',
+ $user->nav_link_color
+ );
+ }
}