You are here

class CasProtectedUserFieldConstraintValidator in CAS 8

Same name and namespace in other branches
  1. 2.x src/Plugin/Validation/Constraint/CasProtectedUserFieldConstraintValidator.php \Drupal\cas\Plugin\Validation\Constraint\CasProtectedUserFieldConstraintValidator

Decorates the ProtectedUserFieldConstraint constraint.

Hierarchy

Expanded class hierarchy of CasProtectedUserFieldConstraintValidator

File

src/Plugin/Validation/Constraint/CasProtectedUserFieldConstraintValidator.php, line 16

Namespace

Drupal\cas\Plugin\Validation\Constraint
View source
class CasProtectedUserFieldConstraintValidator extends ProtectedUserFieldConstraintValidator {

  /**
   * The CasUserManager service.
   *
   * @var \Drupal\cas\Service\CasUserManager
   */
  protected $casUserManager;

  /**
   * Whether or not restricted password managment is enabled.
   *
   * @var bool
   */
  protected $restrictedPasswordManagement = FALSE;

  /**
   * Constructs the object.
   *
   * @param \Drupal\user\UserStorageInterface $user_storage
   *   The user storage handler.
   * @param \Drupal\Core\Session\AccountProxyInterface $current_user
   *   The current user.
   * @param \Drupal\cas\Service\CasUserManager $cas_user_manager
   *   The CAS user manager.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   */
  public function __construct(UserStorageInterface $user_storage, AccountProxyInterface $current_user, CasUserManager $cas_user_manager, ConfigFactoryInterface $config_factory) {
    parent::__construct($user_storage, $current_user);
    $this->casUserManager = $cas_user_manager;
    $this->restrictedPasswordManagement = (bool) $config_factory
      ->get('cas.settings')
      ->get('user_accounts.restrict_password_management');
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity_type.manager')
      ->getStorage('user'), $container
      ->get('current_user'), $container
      ->get('cas.user_manager'), $container
      ->get('config.factory'));
  }

  /**
   * {@inheritdoc}
   */
  public function validate($items, Constraint $constraint) {

    // Skip the validator if CAS is configured with restricted password
    // management and if the user being validated is a CAS user.
    if (!empty($items)) {
      $account = $items
        ->getEntity();
      if ($account && $this->restrictedPasswordManagement && !empty($this->casUserManager
        ->getCasUsernameForAccount($account
        ->id()))) {
        return;
      }
    }
    parent::validate($items, $constraint);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CasProtectedUserFieldConstraintValidator::$casUserManager protected property The CasUserManager service.
CasProtectedUserFieldConstraintValidator::$restrictedPasswordManagement protected property Whether or not restricted password managment is enabled.
CasProtectedUserFieldConstraintValidator::create public static function Instantiates a new instance of this class. Overrides ProtectedUserFieldConstraintValidator::create
CasProtectedUserFieldConstraintValidator::validate public function Checks if the passed value is valid. Overrides ProtectedUserFieldConstraintValidator::validate
CasProtectedUserFieldConstraintValidator::__construct public function Constructs the object. Overrides ProtectedUserFieldConstraintValidator::__construct
ProtectedUserFieldConstraintValidator::$currentUser protected property The current user.
ProtectedUserFieldConstraintValidator::$userStorage protected property User storage handler.