Problem/Motivation

UserRestrictionsFormBase::validationForm() contains the following code.

    // Check for duplicate pattern.
    $existing = $this->entityTypeManager
      ->getStorage('user_restriction')
      ->loadByProperties([
        'rule_type' => $form_state->getValue('rule_type'),
        'pattern' => $form_state->getValue('pattern'),
      ]);
    /** @var \Drupal\Core\Entity\EntityFormInterface $form_object */
    $form_object = $form_state->getFormObject();

    if ($original_id = $form_object->getEntity()->getOriginalId()) {
      unset($existing[$original_id]);
    }

The code can be optimized, and search for restrictions with an ID that is different from the ID used by the edited restriction, which use the same pattern and the same rule type.
There is no need to check the original ID, since the ID cannot be changed after the restriction has been saved.

Proposed resolution

Replace that code with the following one.

$query = $this->entityTypeManager->getStorage('user_restriction')
  ->getQuery()
  ->accessCheck(FALSE)
  ->condition('id', $form_state->getValue('id'), '<>')
  ->condition('rule_type', $form_state->getValue('rule_type'))
  ->condition('pattern', $form_state->getValue('pattern'));
$existing = $query->execute();
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

avpaderno created an issue. See original summary.

avpaderno’s picture

Issue summary: View changes
avpaderno’s picture

Issue summary: View changes
avpaderno’s picture

Issue summary: View changes

avpaderno’s picture

Issue summary: View changes
Status: Active » Needs review
avpaderno’s picture

Issue summary: View changes

  • avpaderno committed 9111d268 on 3.0.x
    Issue #3552280: Optimize the code that checks for duplicate patterns
    
avpaderno’s picture

Version: 3.0.x-dev » 2.1.x-dev

  • avpaderno committed e29ba55c on 2.1.x
    Issue #3552280: Optimize the code that checks for duplicate patterns
    
avpaderno’s picture

Status: Needs review » Fixed

Now that this issue is closed, please review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, please credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.