"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/modules/user/src/Plugin/Validation/Constraint/ProtectedUserFieldConstraintValidator.php" (26 Nov 2020, 2950 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ProtectedUserFieldConstraintValidator.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 namespace Drupal\user\Plugin\Validation\Constraint;
    4 
    5 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
    6 use Drupal\Core\Session\AccountProxyInterface;
    7 use Drupal\user\UserStorageInterface;
    8 use Symfony\Component\DependencyInjection\ContainerInterface;
    9 use Symfony\Component\Validator\Constraint;
   10 use Symfony\Component\Validator\ConstraintValidator;
   11 
   12 /**
   13  * Validates the ProtectedUserFieldConstraint constraint.
   14  */
   15 class ProtectedUserFieldConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
   16 
   17   /**
   18    * User storage handler.
   19    *
   20    * @var \Drupal\user\UserStorageInterface
   21    */
   22   protected $userStorage;
   23 
   24   /**
   25    * The current user.
   26    *
   27    * @var \Drupal\Core\Session\AccountProxyInterface
   28    */
   29   protected $currentUser;
   30 
   31   /**
   32    * Constructs the object.
   33    *
   34    * @param \Drupal\user\UserStorageInterface $user_storage
   35    *   The user storage handler.
   36    * @param \Drupal\Core\Session\AccountProxyInterface $current_user
   37    *   The current user.
   38    */
   39   public function __construct(UserStorageInterface $user_storage, AccountProxyInterface $current_user) {
   40     $this->userStorage = $user_storage;
   41     $this->currentUser = $current_user;
   42   }
   43 
   44   /**
   45    * {@inheritdoc}
   46    */
   47   public static function create(ContainerInterface $container) {
   48     return new static(
   49       $container->get('entity_type.manager')->getStorage('user'),
   50       $container->get('current_user')
   51     );
   52   }
   53 
   54   /**
   55    * {@inheritdoc}
   56    */
   57   public function validate($items, Constraint $constraint) {
   58     if (!isset($items)) {
   59       return;
   60     }
   61     /* @var \Drupal\Core\Field\FieldItemListInterface $items */
   62     $field = $items->getFieldDefinition();
   63 
   64     /* @var \Drupal\user\UserInterface $account */
   65     $account = $items->getEntity();
   66     if (!isset($account) || !empty($account->_skipProtectedUserFieldConstraint)) {
   67       // Looks like we are validating a field not being part of a user, or the
   68       // constraint should be skipped, so do nothing.
   69       return;
   70     }
   71 
   72     // Only validate for existing entities and if this is the current user.
   73     if (!$account->isNew() && $account->id() == $this->currentUser->id()) {
   74 
   75       /* @var \Drupal\user\UserInterface $account_unchanged */
   76       $account_unchanged = $this->userStorage
   77         ->loadUnchanged($account->id());
   78 
   79       $changed = FALSE;
   80 
   81       // Special case for the password, it being empty means that the existing
   82       // password should not be changed, ignore empty password fields.
   83       $value = $items->value;
   84       if ($field->getName() != 'pass' || !empty($value)) {
   85         // Compare the values of the field this is being validated on.
   86         $changed = $items->getValue() != $account_unchanged->get($field->getName())->getValue();
   87       }
   88       if ($changed && (!$account->checkExistingPassword($account_unchanged))) {
   89         $this->context->addViolation($constraint->message, ['%name' => $field->getLabel()]);
   90       }
   91     }
   92   }
   93 
   94 }