Skip to content
Snippets Groups Projects

add validation constraints to user.settings

Closes #3436164

Merge request reports

Loading
Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
  • omkar podey added 1 commit

    added 1 commit

    Compare with previous version

  • omkar podey added 1 commit

    added 1 commit

    Compare with previous version

  • omkar podey added 376 commits

    added 376 commits

    Compare with previous version

  • 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 *
  • omkar podey added 1 commit

    added 1 commit

    Compare with previous version

  • 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());
    • Why not:

      Suggested change
      23 $configuration['choices'] = array_keys(get_user_cancel_methods_array());
      23 $configuration['choices'] = array_keys(user_cancel_methods()['#options]);

      ? That'd remove the need for any additions/changes to user.module.

      … but I see that the new function is what @alexpott suggested. :thumbsup: I don't feel strongly about either approach. Until [#3135592] and [#3043725] happen, interacting with the "user cancel methods" will be awkward anyway :sweat_smile:

      Edited by Wim Leers
    • Let's do @wimleers's suggestion - less change here and the those issues can make this beautiful.

    • Please register or sign in to reply
  • 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 by user_cancel_methods(), but the UserCancelMethod 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 :blush:

    • Please register or sign in to reply
  • omkar podey added 1 commit

    added 1 commit

    Compare with previous version

  • 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'
  • 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']);
  • Looking good, I think we're missing some tests for some of the new configuration validation - e.g. it would be good to see a test that tried to set values outside the constraints

  • omkar podey added 1 commit
  • omkar podey added 149 commits

    added 149 commits

    Compare with previous version

  • omkar podey added 9 commits

    added 9 commits

    Compare with previous version

  • 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
  • 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
  • 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.
  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Please register or sign in to reply
    Loading