Commit b599e8c0 authored by Sahal V A's avatar Sahal V A Committed by Hussain Abbas
Browse files

Issue #3255230 by sahal_va, hussainweb: Add validation to check the username...

Issue #3255230 by sahal_va, hussainweb: Add validation to check the username information from the API
parent a021dccb
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
@@ -65,4 +65,17 @@ class DOUserInfoRetriever {
    return $userData->current();
  }

  /**
   * Checks if given username is a valid d.o username.
   *
   * @param string $doUsername
   *   The drupal.org username.
   *
   * @return bool
   *   TRUE if username is valid, FALSE otherwise.
   */
  public function isValidUser(string $doUsername): bool {
    return $this->getUserInformation($doUsername)->__isset('uid');
  }

}
+19 −1
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ use Drupal\user\UserInterface;
 *   description = @Translation("This field stores a drupal.org Username."),
 *   default_widget = "do_username_text",
 *   default_formatter = "do_username_default",
 *   constraints = {"DoUsernameFormat" = {}}
 *   constraints = {"DoUsernameFormat" = {}, "UsernameValid" = {}}
 * )
 */
class DrupalOrgUsernameItem extends FieldItemBase {
@@ -31,6 +31,7 @@ class DrupalOrgUsernameItem extends FieldItemBase {
  public static function defaultStorageSettings() {
    return [
      'max_length' => UserInterface::USERNAME_MAX_LENGTH,
      'do_validate' => TRUE,
    ] + parent::defaultStorageSettings();
  }

@@ -136,6 +137,13 @@ class DrupalOrgUsernameItem extends FieldItemBase {
      '#disabled' => $has_data,
    ];

    $elements['do_validate'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Enable username validation'),
      '#description' => $this->t('Enable this field to validate whether the given username exists in Drupal.org website.'),
      '#default_value' => $this->getSetting('do_validate'),
    ];

    return $elements;
  }

@@ -147,4 +155,14 @@ class DrupalOrgUsernameItem extends FieldItemBase {
    return $value === NULL || $value === '';
  }

  /**
   * Check for username validation is enabled.
   *
   * @return bool
   *   TRUE if username validation is enabled.
   */
  public function validateUsername() {
    return $this->getSetting('do_validate');
  }

}
+24 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\do_username\Plugin\Validation\Constraint;

use Symfony\Component\Validator\Constraint;

/**
 * Provides an Username Valid constraint.
 *
 * @Constraint(
 *   id = "UsernameValid",
 *   label = @Translation("Username Valid", context = "Validation"),
 * )
 */
class UsernameValidConstraint extends Constraint {

  /**
   * Violation message when username was not found on drupal.org.
   *
   * @var string
   */
  public $invalidUserMessage = 'The username was not found on drupal.org.';

}
+58 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\do_username\Plugin\Validation\Constraint;

use Drupal\do_username\DOUserInfoRetriever;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Validates the Username Valid constraint.
 */
class UsernameValidConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {

  /**
   * The DO User information retriever service.
   *
   * @var \Drupal\do_username\DOUserInfoRetriever
   */
  protected $doUserService;

  /**
   * ConfigEntityUpdater constructor.
   *
   * @param \Drupal\do_username\DOUserInfoRetriever $do_user_service
   *   The DO User information retriever service.
   */
  public function __construct(DOUserInfoRetriever $do_user_service) {
    $this->doUserService = $do_user_service;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {

    return new static(
      $container->get('do_username.user_service')
    );

  }

  /**
   * {@inheritdoc}
   */
  public function validate($item, Constraint $constraint) {
    /** @var \Drupal\do_username\Plugin\Field\FieldType\DrupalOrgUsernameItem $item */
    /** @var \Drupal\do_username\Plugin\Validation\Constraint\UsernameValidConstraint $constraint */
    $name = $item->getValue()['value'];

    if ($item->validateUsername() && !$this->doUserService->isValidUser($name)) {
      $this->context->addViolation($constraint->invalidUserMessage);
    }

  }

}