You are here

class UserRegister in Production check & Production monitor 8

User register settings check

Plugin annotation


@ProdCheck(
  id = "user_register",
  title = @Translation("User registration"),
  category = "settings",
)

Hierarchy

Expanded class hierarchy of UserRegister

File

src/Plugin/ProdCheck/Settings/UserRegister.php, line 18

Namespace

Drupal\prod_check\Plugin\ProdCheck\Settings
View source
class UserRegister extends ProdCheckBase {

  /**
   * The currently selected option
   */
  protected $current;

  /**
   * All the possible options
   */
  protected $options;

  /**
   * {@inheritdoc}
   */
  public function init() {
    $this->current = $this->configFactory
      ->get('user.settings')
      ->get('register');
    $this->options = [
      UserInterface::REGISTER_ADMINISTRATORS_ONLY => $this
        ->t('Administrators only'),
      UserInterface::REGISTER_VISITORS => $this
        ->t('Visitors'),
      UserInterface::REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL => $this
        ->t('Visitors, but administrator approval is required'),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function state() {
    $options = $this->configuration['options'];
    return !empty($options[$this->current]);
  }

  /**
   * {@inheritdoc}
   */
  public function successMessages() {
    return [
      'value' => $this->options[$this->current],
      'description' => $this
        ->generateDescription($this
        ->title(), 'entity.user.admin_form'),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function failMessages() {
    $link_array = $this
      ->generateLinkArray($this
      ->title(), 'entity.user.admin_form');
    return [
      'value' => $this->options[$this->current],
      'description' => $this
        ->t('Your %link settings are set to "@current". Are you sure this is what you want and did not mean to use @options? With improperly setup access rights, this can be dangerous...', [
        '%link' => implode($link_array),
        '@current' => $this->options[$this->current],
        '@options' => '"' . implode('" ' . t('or') . ' "', $this
          ->getSelectedOptions()) . '"',
      ]),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    $configuration = parent::defaultConfiguration();
    $configuration['options'] = [
      UserInterface::REGISTER_ADMINISTRATORS_ONLY => UserInterface::REGISTER_ADMINISTRATORS_ONLY,
      UserInterface::REGISTER_VISITORS => 0,
      UserInterface::REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL => UserInterface::REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL,
    ];
    return $configuration;
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);
    $form['options'] = [
      '#type' => 'checkboxes',
      '#title' => t('Valid user registration options'),
      '#default_value' => $this->configuration['options'],
      '#options' => $this->options,
      '#required' => TRUE,
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    parent::submitConfigurationForm($form, $form_state);
    $this->configuration['options'] = $form_state
      ->getValue('options');
  }

  /**
   * Fetches all the selected options.
   */
  protected function getSelectedOptions() {
    $selected_options = [];
    foreach ($this->configuration['options'] as $option) {
      if (!empty($option)) {
        $selected_options[] = $this->options[$option];
      }
    }
    return $selected_options;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
ProdCheckBase::$configFactory protected property The config factory
ProdCheckBase::$dateFormatter protected property The date formatter service
ProdCheckBase::$destination protected property The redirect destination service.
ProdCheckBase::$linkGenerator protected property The link generator service.
ProdCheckBase::$moduleHandler protected property The module handler service 1
ProdCheckBase::$processor protected property The prod check processor plugin manager.
ProdCheckBase::calculateDependencies public function
ProdCheckBase::category public function Returns the title of the check Overrides ProdCheckInterface::category
ProdCheckBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 1
ProdCheckBase::data public function Returns the extra data of the check. Overrides ProdCheckInterface::data 1
ProdCheckBase::generateDescription protected function Helper function to generate generic 'settings OK' description.
ProdCheckBase::generateLinkArray protected function Helper function to generate link array to pass to the t() function
ProdCheckBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
ProdCheckBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
ProdCheckBase::setProcessor public function Sets the processor
ProdCheckBase::severity public function Defines the severity of the check. Overrides ProdCheckInterface::severity 2
ProdCheckBase::title public function Returns the title of the plugin. Overrides ProdCheckInterface::title
ProdCheckBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
ProdCheckBase::__construct public function Constructs a Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct 1
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UserRegister::$current protected property The currently selected option
UserRegister::$options protected property All the possible options
UserRegister::buildConfigurationForm public function Form constructor. Overrides ProdCheckBase::buildConfigurationForm
UserRegister::defaultConfiguration public function Gets default configuration for this plugin. Overrides ProdCheckBase::defaultConfiguration
UserRegister::failMessages public function Returns the fail messages for the check Overrides ProdCheckInterface::failMessages
UserRegister::getSelectedOptions protected function Fetches all the selected options.
UserRegister::init public function Initializes the check plugin. Overrides ProdCheckBase::init
UserRegister::state public function Calculates the state for the check. Overrides ProdCheckInterface::state
UserRegister::submitConfigurationForm public function Form submission handler. Overrides ProdCheckBase::submitConfigurationForm
UserRegister::successMessages public function Returns the success messages for the check. Overrides ProdCheckInterface::successMessages