Verified Commit f4ae13d0 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3445215 by narendraR, borisson_, mtift, mikelutz, smustgrave, Wim...

Issue #3445215 by narendraR, borisson_, mtift, mikelutz, smustgrave, Wim Leers, alexpott: Add validation constraints to user.role.*
parent b2109b4e
Loading
Loading
Loading
Loading
Loading
+0 −10
Original line number Diff line number Diff line
@@ -59,16 +59,6 @@ public function testDraggableList(): void {

    $this->drupalGet('admin/people/roles');
    $this->assertSession()->responseContains('<td>' . Html::escape($role_name));

    // Make sure that NULL weights do not break the list builder. Use the
    // configuration API so that the value does not get type-casted according to
    // the configuration schema.
    \Drupal::configFactory()
      ->getEditable('user.role.role_0')
      ->set('weight', NULL)
      ->save(TRUE);
    $this->drupalGet('admin/people/roles');
    $this->assertSession()->statusCodeEquals(200);
  }

}
+1 −1
Original line number Diff line number Diff line
@@ -91,7 +91,7 @@ protected function getExpectedDocument() {
          'status' => TRUE,
          'dependencies' => [],
          'label' => 'Llama',
          'is_admin' => NULL,
          'is_admin' => FALSE,
          'permissions' => [],
          'drupal_internal__id' => 'llama',
        ],
+1 −2
Original line number Diff line number Diff line
@@ -58,8 +58,7 @@ protected function setUp(): void {
    $this->installSchema('node', 'node_access');
    $this->installEntitySchema('user');
    $this->installEntitySchema('node');
    $this->installConfig('filter');
    $this->installConfig('node');
    $this->installConfig(['filter', 'node', 'user']);

    $this->accessHandler = \Drupal::entityTypeManager()->getAccessControlHandler('node');

+5 −0
Original line number Diff line number Diff line
@@ -123,6 +123,8 @@ user.flood:
user.role.*:
  type: config_entity
  label: 'User role settings'
  constraints:
    FullyValidatable: ~
  mapping:
    id:
      type: machine_name
@@ -143,6 +145,9 @@ user.role.*:
      sequence:
        type: string
        label: 'Permission'
        constraints:
          Callback:
            callback: [\Drupal\user\Entity\Role, getAllValidPermissions]

action.configuration.user_add_role_action:
  type: mapping
+20 −6
Original line number Diff line number Diff line
@@ -88,7 +88,7 @@ class Role extends ConfigEntityBase implements RoleInterface {
   *
   * @var bool
   */
  protected $is_admin;
  protected $is_admin = FALSE;

  /**
   * {@inheritdoc}
@@ -181,12 +181,11 @@ public static function postLoad(EntityStorageInterface $storage, array &$entitie
  public function preSave(EntityStorageInterface $storage) {
    parent::preSave($storage);

    if (!isset($this->weight) && ($roles = $storage->loadMultiple())) {
    if (!isset($this->weight)) {
      // Set a role weight to make this new role last.
      $max = array_reduce($roles, function ($max, $role) {
        return $max > $role->weight ? $max : $role->weight;
      });
      $this->weight = $max + 1;
      $this->weight = array_reduce($storage->loadMultiple(), function ($max, $role) {
        return $max > $role->weight ? $max : $role->weight + 1;
      }, 0);
    }

    if (!$this->isSyncing() && $this->hasTrustedData()) {
@@ -265,4 +264,19 @@ public function onDependencyRemoval(array $dependencies) {
    return $changed;
  }

  /**
   * Returns all valid permissions.
   *
   * @return string[]
   *   All possible valid permissions.
   *
   * @see \Drupal\user\PermissionHandler::getPermissions()
   *
   * @internal
   * @todo Revisit in https://www.drupal.org/node/3446364
   */
  public static function getAllValidPermissions(): array {
    return array_keys(\Drupal::service('user.permissions')->getPermissions());
  }

}
Loading