You are here

class SecureLoginConfigForm in Secure Login 8

Implements a SecureLogin Config form.

Hierarchy

Expanded class hierarchy of SecureLoginConfigForm

1 string reference to 'SecureLoginConfigForm'
securelogin.routing.yml in ./securelogin.routing.yml
securelogin.routing.yml

File

src/Form/SecureLoginConfigForm.php, line 15

Namespace

Drupal\securelogin\Form
View source
class SecureLoginConfigForm extends ConfigFormBase {

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * Creates a new SecureLoginConfigForm object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   */
  public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler) {
    parent::__construct($config_factory);
    $this->moduleHandler = $module_handler;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('module_handler'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'securelogin_config_form';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'securelogin.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    global $base_secure_url;
    $config = $this
      ->config('securelogin.settings');
    $notice = $this
      ->t('Must be checked to enforce secure authenticated sessions.');
    $states = [
      'invisible' => [
        ':input[name="all_forms"]' => [
          'checked' => TRUE,
        ],
      ],
    ];
    $form['base_url'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Secure base URL'),
      '#default_value' => $config
        ->get('base_url'),
      '#description' => $this
        ->t('The base URL for secure pages. Leave blank to allow Drupal to determine it automatically (recommended). It is not allowed to have a trailing slash; Drupal will add it for you. For example: %base_secure_url%.', [
        '%base_secure_url%' => $base_secure_url,
      ]),
    ];
    $form['secure_forms'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Redirect form pages to secure URL'),
      '#default_value' => $config
        ->get('secure_forms'),
      '#description' => $this
        ->t('If enabled, any pages containing the forms enabled below will be redirected to the secure URL. Users can be assured that they are entering their private data on a secure URL, the contents of which have not been tampered with.'),
    ];
    $form['all_forms'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Submit all forms to secure URL'),
      '#default_value' => $config
        ->get('all_forms'),
      '#description' => $this
        ->t('If enabled, all forms will be submitted to the secure URL.'),
    ];
    $form['forms'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Forms'),
      '#description' => $this
        ->t('If checked, these forms will be submitted to the secure URL. Note, some forms must be checked to enforce secure authenticated sessions.'),
      '#states' => $states,
    ];
    $form['forms']['form_user_form'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('User edit form'),
      '#default_value' => $config
        ->get('form_user_form'),
    ];
    $form['forms']['form_user_login_form'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('User login form'),
      '#default_value' => $config
        ->get('form_user_login_form'),
      '#description' => $notice,
    ];
    $form['forms']['form_user_pass'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('User password request form'),
      '#default_value' => $config
        ->get('form_user_pass'),
      '#description' => $notice,
    ];
    $form['forms']['form_user_pass_reset'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('User password reset form'),
      '#default_value' => $config
        ->get('form_user_pass_reset'),
      '#description' => $notice,
    ];
    $form['forms']['form_user_register_form'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('User registration form'),
      '#default_value' => $config
        ->get('form_user_register_form'),
      '#description' => $notice,
    ];
    $forms = [];
    $this->moduleHandler
      ->alter('securelogin', $forms);
    foreach ($forms as $id => $item) {
      $form['forms']["form_{$id}"] = [
        '#type' => 'checkbox',
        '#title' => $item['#title'],
        '#default_value' => $config
          ->get("form_{$id}"),
        '#description' => isset($item['#description']) ? $item['#description'] : '',
      ];
    }
    $form['other_forms'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Other forms to secure'),
      '#default_value' => $config
        ->get('other_forms'),
      '#description' => $this
        ->t('List the form IDs of any other forms that you want secured, separated by a space. If the form has a base form ID, you must list the base form ID rather than the form ID.'),
      '#states' => $states,
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this
      ->config('securelogin.settings')
      ->set('base_url', $form_state
      ->getValue('base_url'))
      ->set('secure_forms', $form_state
      ->getValue('secure_forms'))
      ->set('all_forms', $form_state
      ->getValue('all_forms'))
      ->set('other_forms', $form_state
      ->getValue('other_forms'))
      ->set('form_user_login_form', $form_state
      ->getValue('form_user_login_form'))
      ->set('form_user_pass_reset', $form_state
      ->getValue('form_user_pass_reset'))
      ->set('form_user_form', $form_state
      ->getValue('form_user_form'))
      ->set('form_user_register_form', $form_state
      ->getValue('form_user_register_form'))
      ->set('form_user_pass', $form_state
      ->getValue('form_user_pass'))
      ->save();
    $forms = [];
    $this->moduleHandler
      ->alter('securelogin', $forms);
    foreach ($forms as $id => $item) {
      $this
        ->config('securelogin.settings')
        ->set("form_{$id}", $form_state
        ->getValue("form_{$id}"))
        ->save();
    }
    drupal_register_shutdown_function('drupal_flush_all_caches');
    parent::submitForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    if (!$form_state
      ->getValue('base_url')) {
      $form_state
        ->setValue('base_url', NULL);
    }
    elseif (!UrlHelper::isValid($form_state
      ->getValue('base_url'), TRUE)) {
      $form_state
        ->setErrorByName('base_url', $this
        ->t('The secure base URL must be a valid URL.'));
    }
    elseif (strtolower(parse_url($form_state
      ->getValue('base_url'), PHP_URL_SCHEME)) !== 'https') {
      $form_state
        ->setErrorByName('base_url', $this
        ->t('The secure base URL must start with <em>https://</em>.'));
    }
    parent::validateForm($form, $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
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
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
SecureLoginConfigForm::$moduleHandler protected property The module handler.
SecureLoginConfigForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
SecureLoginConfigForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
SecureLoginConfigForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
SecureLoginConfigForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
SecureLoginConfigForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
SecureLoginConfigForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
SecureLoginConfigForm::__construct public function Creates a new SecureLoginConfigForm object. Overrides ConfigFormBase::__construct
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.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.