Skip to content

Commit f48791e

Browse files
committed
Use a special form type for api key settings, that hide the api key by default as dots
1 parent a75a60f commit f48791e

File tree

16 files changed

+191
-12
lines changed

16 files changed

+191
-12
lines changed

assets/controllers.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
{
22
"controllers": {
3+
"@symfony/ux-toggle-password": {
4+
"toggle-password": {
5+
"enabled": true,
6+
"fetch": "eager",
7+
"autoimport": {
8+
"@symfony/ux-toggle-password/dist/style.min.css": true
9+
}
10+
}
11+
},
312
"@symfony/ux-turbo": {
413
"turbo-core": {
514
"enabled": true,

assets/css/app/bs-overrides.css

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,11 @@ ins {
120120
del {
121121
background-color: #f09595;
122122
font-weight: bold;
123-
}
123+
}
124+
125+
/****************************************
126+
* Password toggle
127+
****************************************/
128+
.toggle-password-button {
129+
top: 0.7rem !important;
130+
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
"symfony/string": "7.3.*",
8181
"symfony/translation": "7.3.*",
8282
"symfony/twig-bundle": "7.3.*",
83+
"symfony/ux-toggle-password": "^2.29",
8384
"symfony/ux-translator": "^2.10",
8485
"symfony/ux-turbo": "^2.0",
8586
"symfony/validator": "7.3.*",

composer.lock

Lines changed: 85 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/bundles.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@
3333
Jbtronics\SettingsBundle\JbtronicsSettingsBundle::class => ['all' => true],
3434
Jbtronics\TranslationEditorBundle\JbtronicsTranslationEditorBundle::class => ['dev' => true],
3535
ApiPlatform\Symfony\Bundle\ApiPlatformBundle::class => ['all' => true],
36+
Symfony\UX\TogglePassword\TogglePasswordBundle::class => ['all' => true],
3637
];

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"@hotwired/turbo": "^8.0.1",
88
"@popperjs/core": "^2.10.2",
99
"@symfony/stimulus-bridge": "^4.0.0",
10+
"@symfony/ux-toggle-password": "file:vendor/symfony/ux-toggle-password/assets",
1011
"@symfony/ux-translator": "file:vendor/symfony/ux-translator/assets",
1112
"@symfony/ux-turbo": "file:vendor/symfony/ux-turbo/assets",
1213
"@symfony/webpack-encore": "^5.0.0",

src/Form/Type/APIKeyType.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/*
3+
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4+
*
5+
* Copyright (C) 2019 - 2025 Jan Böhmer (https://github.com/jbtronics)
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Affero General Public License as published
9+
* by the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License
18+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
21+
declare(strict_types=1);
22+
23+
24+
namespace App\Form\Type;
25+
26+
use Symfony\Component\Form\AbstractType;
27+
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
28+
use Symfony\Component\Form\FormInterface;
29+
use Symfony\Component\Form\FormView;
30+
use Symfony\Component\OptionsResolver\OptionsResolver;
31+
32+
class APIKeyType extends AbstractType
33+
{
34+
public function getParent(): string
35+
{
36+
return PasswordType::class;
37+
}
38+
39+
public function buildView(FormView $view, FormInterface $form, array $options): void
40+
{
41+
//Ensure that the field is never empty
42+
$view->vars['value'] = $form->getViewData();
43+
}
44+
45+
public function configureOptions(OptionsResolver $resolver): void
46+
{
47+
$resolver->setDefaults([
48+
'always_empty' => false,
49+
'toggle' => true,
50+
'attr' => ['autocomplete' => 'off'],
51+
]);
52+
}
53+
}

src/Settings/InfoProviderSystem/DigikeySettings.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
namespace App\Settings\InfoProviderSystem;
2525

26+
use App\Form\Type\APIKeyType;
2627
use App\Settings\SettingsIcon;
2728
use Jbtronics\SettingsBundle\Metadata\EnvVarMode;
2829
use Jbtronics\SettingsBundle\Settings\Settings;
@@ -42,12 +43,14 @@ class DigikeySettings
4243

4344
#[SettingsParameter(
4445
label: new TM("settings.ips.digikey.client_id"),
46+
formType: APIKeyType::class,
4547
envVar: "PROVIDER_DIGIKEY_CLIENT_ID", envVarMode: EnvVarMode::OVERWRITE
4648
)]
4749
public ?string $clientId = null;
4850

4951
#[SettingsParameter(
5052
label: new TM("settings.ips.digikey.secret"),
53+
formType: APIKeyType::class,
5154
envVar: "PROVIDER_DIGIKEY_SECRET", envVarMode: EnvVarMode::OVERWRITE
5255
)]
5356
public ?string $secret = null;
@@ -67,4 +70,4 @@ class DigikeySettings
6770
envVar: "PROVIDER_DIGIKEY_LANGUAGE", envVarMode: EnvVarMode::OVERWRITE)]
6871
#[Assert\Language]
6972
public string $language = "en";
70-
}
73+
}

src/Settings/InfoProviderSystem/Element14Settings.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
namespace App\Settings\InfoProviderSystem;
2525

26+
use App\Form\Type\APIKeyType;
2627
use App\Settings\SettingsIcon;
2728
use Jbtronics\SettingsBundle\Metadata\EnvVarMode;
2829
use Jbtronics\SettingsBundle\Settings\Settings;
@@ -36,11 +37,12 @@ class Element14Settings
3637
{
3738
use SettingsTrait;
3839

39-
#[SettingsParameter(label: new TM("settings.ips.element14.apiKey"), description: new TM("settings.ips.element14.apiKey.help"),
40+
#[SettingsParameter(label: new TM("settings.ips.element14.apiKey"), description: new TM("settings.ips.element14.apiKey.help"),#
41+
formType: APIKeyType::class,
4042
formOptions: ["help_html" => true], envVar: "PROVIDER_ELEMENT14_KEY", envVarMode: EnvVarMode::OVERWRITE)]
4143
public ?string $apiKey = null;
4244

4345
#[SettingsParameter(label: new TM("settings.ips.element14.storeId"), description: new TM("settings.ips.element14.storeId.help"),
4446
formOptions: ["help_html" => true], envVar: "PROVIDER_ELEMENT14_STORE_ID", envVarMode: EnvVarMode::OVERWRITE)]
4547
public string $storeId = "de.farnell.com";
46-
}
48+
}

src/Settings/InfoProviderSystem/MouserSettings.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
namespace App\Settings\InfoProviderSystem;
2525

26+
use App\Form\Type\APIKeyType;
2627
use App\Settings\SettingsIcon;
2728
use Jbtronics\SettingsBundle\Metadata\EnvVarMode;
2829
use Jbtronics\SettingsBundle\Settings\Settings;
@@ -35,6 +36,7 @@
3536
class MouserSettings
3637
{
3738
#[SettingsParameter(label: new TM("settings.ips.mouser.apiKey"), description: new TM("settings.ips.mouser.apiKey.help"),
39+
formType: APIKeyType::class,
3840
formOptions: ["help_html" => true], envVar: "PROVIDER_MOUSER_KEY", envVarMode: EnvVarMode::OVERWRITE)]
3941
public ?string $apiKey = null;
4042

@@ -64,4 +66,4 @@ public static function mapSearchOptionEnvVar(?string $value): MouserSearchOption
6466
return MouserSearchOptions::tryFrom($value) ?? MouserSearchOptions::NONE;
6567
}
6668

67-
}
69+
}

0 commit comments

Comments
 (0)