add validation constraints to user.settings
Closes #3436164
Merge request reports
Activity
- Resolved by Alex Pott
added 376 commits
-
2760ac87...a60890d4 - 373 commits from branch
project:11.x
- 906a45b4 - move to 11.x
- 44f50b61 - add password strength
- 226ca979 - revert space
Toggle commit list-
2760ac87...a60890d4 - 373 commits from branch
664 664 * 665 665 * See documentation of hook_user_cancel_methods_alter(). 666 666 * 667 * 668 * @param bool $getKeys 669 * Get keys for schema callback. 670 * 667 671 * @return array 668 672 * An array containing all account cancellation methods as form elements. 669 673 * - Comment on lines +667 to 673
8 use Drupal\Core\StringTranslation\TranslatableMarkup; 9 use Drupal\Core\Validation\Attribute\Constraint; 10 use Symfony\Component\DependencyInjection\ContainerInterface; 11 use Symfony\Component\Validator\Constraints\Choice; 12 13 #[Constraint( 14 id: 'UserCancelMethod', 15 label: new TranslatableMarkup('UserCancelMethod', [], ['context' => 'Validation']), 16 )] 17 class UserCancelMethodsConstraint implements ContainerFactoryPluginInterface { 18 19 /** 20 * {@inheritdoc} 21 */ 22 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): Choice { 23 $configuration['choices'] = array_keys(get_user_cancel_methods_array()); - Edited by Wim Leers
688 $form[$name]['#confirm_description'] = $method['description']; 689 } 690 if (isset($method['access'])) { 691 $form[$name]['#access'] = $method['access']; 692 } 693 } 694 return $form; 695 } 696 697 /** 698 * Helper function to return available account cancellation methods as an array. 699 * 700 * @return array[][] 701 * An array containing all account cancellation methods. 702 */ 703 function get_user_cancel_methods_array(): array { This method should start with user_ not get_user_ - potentially we could do user to denote it is not a hook and internal to the user module. But looking at @wimleers's feedback I think we should do what he says and wait for the issues he mentions in order to make this better.
I have one concern though... the configuration is used in validating itself... which feels very odd. See
$user_settings = \Drupal::config('user.settings'); $anonymous_name = $user_settings->get('anonymous');
Just we are throwing away this work during validation but it does feel super awkward. Not sure about the long term impacts...
Hm that is intriguing. But it's also harmless.
$anonymous_name
is used in the array values returned byuser_cancel_methods()
, but theUserCancelMethod
constraint validator only uses the array keys.Just we are throwing away this work during validation but it does feel super awkward.
Ah you noticed that too. I agree it's weird/awkward.
Not sure about the long term impacts...
I don't think there is any. But once [#3135592] and/or [#3043725] are fixed, that will have been refactored away
41 43 register: 42 44 type: string 43 45 label: 'Who can register accounts?' 46 # Choices are derived from the constants. 47 # @see \Drupal\user\UserInterface::REGISTER_* 48 constraints: 49 Choice: 50 choices: 51 - 'visitors' 52 - 'admin_only' 53 - 'visitors_admin_approval' [#3450782]
8 use Drupal\Core\StringTranslation\TranslatableMarkup; 9 use Drupal\Core\Validation\Attribute\Constraint; 10 use Symfony\Component\DependencyInjection\ContainerInterface; 11 use Symfony\Component\Validator\Constraints\Choice; 12 13 #[Constraint( 14 id: 'UserCancelMethod', 15 label: new TranslatableMarkup('UserCancelMethod', [], ['context' => 'Validation']), 16 )] 17 class UserCancelMethodsConstraint implements ContainerFactoryPluginInterface { 18 19 /** 20 * {@inheritdoc} 21 */ 22 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): Choice { 23 $configuration['choices'] = array_keys(user_cancel_methods()['#options']); 41 43 register: 42 44 type: string 43 45 label: 'Who can register accounts?' 46 # Choices are derived from the constants. 47 # @see \Drupal\user\UserInterface::REGISTER_* 48 constraints: 48 constraints: 49 Choice: 50 choices: 51 - 'visitors' 52 - 'admin_only' 53 - 'visitors_admin_approval' 44 54 cancel_method: 45 55 type: string 46 56 label: 'When cancelling a user account' 57 constraints: 58 UserCancelMethod: [] 47 59 password_reset_timeout: 48 60 type: integer 49 61 label: 'Password reset timeout' 62 # Increase min in a followup. 63 # @see https://www.drupal.org/i/3441772 fixed
Edited by Kunal Sachdevchanged this line in version 12 of the diff
1 <?php 2 3 declare(strict_types=1); 4 5 namespace Drupal\Tests\user\Functional; 6 7 use Drupal\Tests\BrowserTestBase; 8 9 /** 10 * @group system changed this line in version 12 of the diff
5 namespace Drupal\Tests\user\Functional; 6 7 use Drupal\Tests\BrowserTestBase; 8 9 /** 10 * @group system 11 */ 12 class UserConfigValidationTest extends BrowserTestBase { 13 14 /** 15 * {@inheritdoc} 16 */ 17 protected $defaultTheme = 'stark'; 18 19 /** 20 * Tests invalid register key config. changed this line in version 12 of the diff