class ProtectedUserFieldConstraintValidator in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/user/src/Plugin/Validation/Constraint/ProtectedUserFieldConstraintValidator.php \Drupal\user\Plugin\Validation\Constraint\ProtectedUserFieldConstraintValidator
Validates the ProtectedUserFieldConstraint constraint.
Hierarchy
- class \Symfony\Component\Validator\ConstraintValidator implements ConstraintValidatorInterface
- class \Drupal\user\Plugin\Validation\Constraint\ProtectedUserFieldConstraintValidator implements ContainerInjectionInterface
Expanded class hierarchy of ProtectedUserFieldConstraintValidator
1 file declares its use of ProtectedUserFieldConstraintValidator
- ProtectedUserFieldConstraintValidatorTest.php in core/
modules/ user/ tests/ src/ Unit/ Plugin/ Validation/ Constraint/ ProtectedUserFieldConstraintValidatorTest.php - Contains \Drupal\Tests\user\Unit\Plugin\Validation\Constraint\ProtectedUserFieldConstraintValidatorTest.
File
- core/
modules/ user/ src/ Plugin/ Validation/ Constraint/ ProtectedUserFieldConstraintValidator.php, line 19 - Contains \Drupal\user\Plugin\Validation\Constraint\ProtectedUserFieldConstraintValidator.
Namespace
Drupal\user\Plugin\Validation\ConstraintView source
class ProtectedUserFieldConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
/**
* User storage handler.
*
* @var \Drupal\user\UserStorageInterface
*/
protected $userStorage;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;
/**
* Constructs the object.
*
* @param \Drupal\user\UserStorageInterface $user_storage
* The user storage handler.
* @param \Drupal\Core\Session\AccountProxyInterface $current_user
* The current user.
*/
public function __construct(UserStorageInterface $user_storage, AccountProxyInterface $current_user) {
$this->userStorage = $user_storage;
$this->currentUser = $current_user;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.manager')
->getStorage('user'), $container
->get('current_user'));
}
/**
* {@inheritdoc}
*/
public function validate($items, Constraint $constraint) {
if (!isset($items)) {
return;
}
/* @var \Drupal\Core\Field\FieldItemListInterface $items */
$field = $items
->getFieldDefinition();
/* @var \Drupal\user\UserInterface $account */
$account = $items
->getEntity();
if (!isset($account) || !empty($account->_skipProtectedUserFieldConstraint)) {
// Looks like we are validating a field not being part of a user, or the
// constraint should be skipped, so do nothing.
return;
}
// Only validate for existing entities and if this is the current user.
if (!$account
->isNew() && $account
->id() == $this->currentUser
->id()) {
/* @var \Drupal\user\UserInterface $account_unchanged */
$account_unchanged = $this->userStorage
->loadUnchanged($account
->id());
$changed = FALSE;
// Special case for the password, it being empty means that the existing
// password should not be changed, ignore empty password fields.
$value = $items->value;
if ($field
->getName() != 'pass' || !empty($value)) {
// Compare the values of the field this is being validated on.
$changed = $items
->getValue() != $account_unchanged
->get($field
->getName())
->getValue();
}
if ($changed && !$account
->checkExistingPassword($account_unchanged)) {
$this->context
->addViolation($constraint->message, array(
'%name' => $field
->getLabel(),
));
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConstraintValidator:: |
protected | property | 3 | |
ConstraintValidator:: |
protected | function | Wrapper for {@link ExecutionContextInterface::buildViolation} that supports the 2.4 context API. | |
ConstraintValidator:: |
protected | function | Wrapper for {@link ExecutionContextInterface::buildViolation} that supports the 2.4 context API. | |
ConstraintValidator:: |
protected | function | Returns a string representation of the type of the value. | |
ConstraintValidator:: |
protected | function | Returns a string representation of the value. | |
ConstraintValidator:: |
protected | function | Returns a string representation of a list of values. | |
ConstraintValidator:: |
public | function |
Initializes the constraint validator. Overrides ConstraintValidatorInterface:: |
1 |
ConstraintValidator:: |
constant | Whether to cast objects with a "__toString()" method to strings. | ||
ConstraintValidator:: |
constant | Whether to format {@link \DateTime} objects as RFC-3339 dates ("Y-m-d H:i:s"). | ||
ProtectedUserFieldConstraintValidator:: |
protected | property | The current user. | |
ProtectedUserFieldConstraintValidator:: |
protected | property | User storage handler. | |
ProtectedUserFieldConstraintValidator:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
|
ProtectedUserFieldConstraintValidator:: |
public | function |
Checks if the passed value is valid. Overrides ConstraintValidatorInterface:: |
|
ProtectedUserFieldConstraintValidator:: |
public | function | Constructs the object. |