Commit caa33b01 authored by Paulo Henrique Cota Starling's avatar Paulo Henrique Cota Starling
Browse files

Issue #3303125 by paulocs: Calculate dependencies for the PasswordPolicy

parent 10be7251
Loading
Loading
Loading
Loading
+40 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\password_policy\Entity;

use Drupal\Core\Plugin\DefaultLazyPluginCollection;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\password_policy\PasswordPolicyInterface;

@@ -99,6 +100,13 @@ class PasswordPolicy extends ConfigEntityBase implements PasswordPolicyInterface
   */
  protected $roles = [];

  /**
   * The constraints as a collection.
   *
   * @var \Drupal\Core\Plugin\DefaultLazyPluginCollection
   */
  protected $constraintsCollection;

  /**
   * {@inheritdoc}
   */
@@ -146,6 +154,20 @@ class PasswordPolicy extends ConfigEntityBase implements PasswordPolicyInterface
    return $this->password_reset;
  }

  /**
   * Get the plugin collections used by this entity.
   *
   * @return Drupal\Core\Plugin\DefaultLazyPluginCollection
   *   An array of plugin collections, keyed by the property name they use to
   *   store their configuration.
   */
  public function getConstraintsCollection() {
    if (!isset($this->constraintsCollection)) {
      $this->constraintsCollection = new DefaultLazyPluginCollection(\Drupal::service('plugin.manager.password_policy.password_constraint'), $this->getConstraints());
    }
    return $this->constraintsCollection;
  }

  /**
   * Return the password reset email value from the policy.
   *
@@ -176,4 +198,22 @@ class PasswordPolicy extends ConfigEntityBase implements PasswordPolicyInterface
    return $this->roles;
  }

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    parent::calculateDependencies();
    $constraints_collection = $this->getConstraintsCollection();
    if (empty($constraints_collection)) {
      return $this;
    }
    $constraint_plugin_ids = $constraints_collection->getInstanceIds();
    foreach ($constraint_plugin_ids as $constraint_plugin_id) {
      $constraint_plugin = $constraints_collection->get($constraint_plugin_id);
      $constraint_plugin_dependencies = $this->getPluginDependencies($constraint_plugin);
      $this->addDependencies($constraint_plugin_dependencies);
    }
    return $this;
  }

}