55namespace Drupal \os2loop_cura_login \Form ;
66
77use Drupal \Component \Utility \Random ;
8+ use Drupal \Core \Config \ConfigFactoryInterface ;
9+ use Drupal \Core \DependencyInjection \AutowireTrait ;
810use Drupal \Core \Form \ConfigFormBase ;
911use Drupal \Core \Form \FormStateInterface ;
1012use Drupal \Core \Logger \RfcLogLevel ;
1113use Drupal \Core \Url ;
14+ use Drupal \os2loop_cura_login \Settings ;
1215
1316/**
1417 * Configure OS2Loop Cura login settings for this site.
1518 */
1619final class SettingsForm extends ConfigFormBase {
20+ use AutowireTrait;
21+
22+ public function __construct (
23+ ConfigFactoryInterface $ config_factory ,
24+ private readonly Settings $ settings ,
25+ ) {
26+ parent ::__construct ($ config_factory );
27+ }
1728
1829 /**
1930 * {@inheritdoc}
@@ -26,34 +37,30 @@ public function getFormId(): string {
2637 * {@inheritdoc}
2738 */
2839 protected function getEditableConfigNames (): array {
29- return [' os2loop_cura_login.settings ' ];
40+ return [Settings:: CONFIG_NAME ];
3041 }
3142
3243 /**
3344 * {@inheritdoc}
3445 */
3546 public function buildForm (array $ form , FormStateInterface $ form_state ): array {
36- $ config = $ this -> config ( ' os2loop_cura_login.settings ' );
47+ $ settings = \Drupal:: service (Settings::class );
3748 $ form ['signing_algorithm ' ] = [
3849 '#required ' => TRUE ,
3950 '#type ' => 'select ' ,
40- '#options ' => [
41- 'HS256 ' => 'HS256 ' ,
42- 'HS384 ' => 'HS384 ' ,
43- 'HS512 ' => 'HS512 ' ,
44- ],
51+ '#options ' => Settings::SIGNING_ALGORITHMS ,
4552 '#title ' => $ this ->t ('Signing algorithm ' ),
46- '#default_value ' => $ config -> get ( ' signing_algorithm ' ),
53+ '#default_value ' => $ this -> settings -> getSigningAlgorithm ( ),
4754 ];
4855
49- $ hasSigningSecret = !empty ($ config -> get ( ' signing_secret ' ));
56+ $ hasSigningSecret = !empty ($ this -> settings -> getSigningSecret ( ));
5057
5158 $ form ['signing_secret ' ] = [
5259 '#type ' => 'textfield ' ,
5360 '#size ' => 128 ,
5461 '#required ' => $ hasSigningSecret ,
5562 '#title ' => $ this ->t ('Signing secret ' ),
56- '#default_value ' => $ config -> get ( ' signing_secret ' ),
63+ '#default_value ' => $ this -> settings -> getSigningSecret ( ),
5764 '#description ' => !$ hasSigningSecret
5865 ? $ this ->t ('Save the configuration to generate a random secret. ' )
5966 : '' ,
@@ -69,21 +76,21 @@ public function buildForm(array $form, FormStateInterface $form_state): array {
6976 $ form ['payload_name ' ] = [
7077 '#type ' => 'textfield ' ,
7178 '#title ' => $ this ->t ('Payload name ' ),
72- '#default_value ' => $ config -> get ( ' payload_name ' ) ?? ' payload ' ,
79+ '#default_value ' => $ this -> settings -> getPayloadName () ,
7380 '#description ' => $ this ->t ('Name of parameter used for payload ' ),
7481 ];
7582
7683 $ form ['jwt_leeway ' ] = [
7784 '#type ' => 'textfield ' ,
7885 '#title ' => $ this ->t ('JWT leeway ' ),
79- '#default_value ' => $ config -> get ( ' jwt_leeway ' ) ?? 0 ,
86+ '#default_value ' => $ this -> settings -> getJwtLeeway () ,
8087 ];
8188
8289 $ form ['log_level ' ] = [
8390 '#type ' => 'select ' ,
8491 '#options ' => RfcLogLevel::getLevels (),
8592 '#title ' => $ this ->t ('Log level ' ),
86- '#default_value ' => $ config -> get ( ' log_level ' ) ?? RfcLogLevel:: ERROR ,
93+ '#default_value ' => $ this -> settings -> getLogLevel () ,
8794 ];
8895
8996 $ authenticationStartUrl = Url::fromRoute ('os2loop_cura_login.start ' )->setAbsolute ()->toString (TRUE )->getGeneratedUrl ();
@@ -100,7 +107,7 @@ public function buildForm(array $form, FormStateInterface $form_state): array {
100107 '#markup ' => $ this ->t ('Use <a href=":url">:url</a> as <code>linkURL</code> for <core><code>postToGetLinkURL ≡ false</code>. ' , [':url ' => $ authenticationStartUrl ]),
101108 ];
102109
103- if ($ name = $ config -> get ( ' payload_name ' )) {
110+ if ($ name = $ this -> settings -> getPayloadName ( )) {
104111 $ authenticationStartUrl = Url::fromRoute ('os2loop_cura_login.start ' , [$ name => '… ' ])->setAbsolute ()->toString (TRUE )->getGeneratedUrl ();
105112 $ authenticationStartUrl = str_replace (urlencode ('… ' ), '' , $ authenticationStartUrl );
106113 $ form ['info ' ]['#items ' ][] = [
@@ -117,15 +124,12 @@ public function buildForm(array $form, FormStateInterface $form_state): array {
117124 public function submitForm (array &$ form , FormStateInterface $ form_state ): void {
118125 $ secret = $ form_state ->getValue ('signing_secret ' );
119126 if ($ form_state ->getValue ('generate_new_secret ' )) {
120- $ secret = base64_encode ((new Random ())->string (64 ));
127+ $ form_state -> setValue ( ' signing_secret ' , base64_encode ((new Random ())->string (64 ) ));
121128 }
122- $ this ->config ('os2loop_cura_login.settings ' )
123- ->set ('signing_algorithm ' , $ form_state ->getValue ('signing_algorithm ' ))
124- ->set ('signing_secret ' , $ secret )
125- ->set ('payload_name ' , $ form_state ->getValue ('payload_name ' ))
126- ->set ('jwt_leeway ' , $ form_state ->getValue ('jwt_leeway ' ))
127- ->set ('log_level ' , $ form_state ->getValue ('log_level ' ))
128- ->save ();
129+ /** @var \Drupal\os2loop_cura_login\Settings $settings */
130+ $ settings = \Drupal::service (Settings::class);
131+ $ settings ->saveSettings ($ form_state );
132+
129133 parent ::submitForm ($ form , $ form_state );
130134 }
131135
0 commit comments