You are here

class ProtectedPagesLoginForm in Protected Pages 8

Provides login screen to access protected page.

Hierarchy

Expanded class hierarchy of ProtectedPagesLoginForm

1 string reference to 'ProtectedPagesLoginForm'
protected_pages.routing.yml in ./protected_pages.routing.yml
protected_pages.routing.yml

File

src/Form/ProtectedPagesLoginForm.php, line 18

Namespace

Drupal\protected_pages\Form
View source
class ProtectedPagesLoginForm extends FormBase {

  /**
   * The protected pages storage service.
   *
   * @var \Drupal\protected_pages\ProtectedPagesStorage
   */
  protected $protectedPagesStorage;

  /**
   * Provides the password hashing service object.
   *
   * @var \Drupal\Core\Password\PasswordInterface
   */
  protected $password;

  /**
   * Account proxy service.
   *
   * @var \Drupal\Core\Session\AccountProxy
   */
  protected $currentUser;

  /**
   * A date time instance.
   *
   * @var \Drupal\Component\Datetime\TimeInterface
   */
  protected $time;

  /**
   * Constructs a new ProtectedPagesLoginForm.
   *
   * @param \Drupal\Core\Password\PasswordInterface $password
   *   The password hashing service.
   * @param \Drupal\protected_pages\ProtectedPagesStorage $protectedPagesStorage
   *   The protected pages storage.
   * @param \Drupal\Core\Session\AccountProxy $currentUser
   *   The current user service.
   * @param \Drupal\Component\Datetime\TimeInterface $time
   *   A date time instance.
   */
  public function __construct(PasswordInterface $password, ProtectedPagesStorage $protectedPagesStorage, AccountProxy $currentUser, TimeInterface $time) {
    $this->password = $password;
    $this->protectedPagesStorage = $protectedPagesStorage;
    $this->currentUser = $currentUser;
    $this->time = $time;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('password'), $container
      ->get('protected_pages.storage'), $container
      ->get('current_user'), $container
      ->get('datetime.time'));
  }

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

  /**
   * Checks access based permission and protected page id.
   *
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
   */
  public function accessProtectedPageLoginScreen() {
    $param_protected_page = $this
      ->getRequest()->query
      ->get('protected_page');
    $param_exists = isset($param_protected_page) && is_numeric($param_protected_page);
    return AccessResult::allowedIf(($this->currentUser
      ->hasPermission('access protected page password screen') || $this->currentUser
      ->id() == 1) && $param_exists);
  }

  /**
   * Route title callback.
   *
   * @return string
   *   The protected page login screen title.
   */
  public function protectedPageTitle() {
    $config = $this
      ->config('protected_pages.settings');
    return Html::escape($config
      ->get('others.protected_pages_title'));
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('protected_pages.settings');
    $form['protected_page_enter_password'] = [
      '#type' => 'fieldset',
      '#collapsible' => FALSE,
      '#title' => $config
        ->get('others.protected_pages_password_fieldset_legend'),
    ];
    $form['protected_page_enter_password']['protected_page_pid'] = [
      '#markup' => '<div class="protected_pages_description"><strong>' . $config
        ->get('others.protected_pages_description') . '</strong></div>',
    ];
    $form['protected_page_enter_password']['password'] = [
      '#type' => 'password',
      '#title' => $config
        ->get('others.protected_pages_password_label'),
      '#size' => 20,
      '#required' => TRUE,
    ];
    $form['protected_page_pid'] = [
      '#type' => 'hidden',
      '#value' => $this
        ->getRequest()->query
        ->get('protected_page'),
    ];
    $form['protected_page_enter_password']['submit'] = [
      '#type' => 'submit',
      '#value' => $config
        ->get('others.protected_pages_submit_button_text'),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $config = $this
      ->config('protected_pages.settings');
    $global_password_setting = $config
      ->get('password.per_page_or_global');
    if ($global_password_setting == 'per_page_password') {
      $fields = [
        'password',
      ];
      $conditions = [];
      $conditions['general'][] = [
        'field' => 'pid',
        'value' => $form_state
          ->getValue('protected_page_pid'),
        'operator' => '=',
      ];
      $password = $this->protectedPagesStorage
        ->loadProtectedPage($fields, $conditions, TRUE);
      if (!$this->password
        ->check($form_state
        ->getValue('password'), $password)) {
        $form_state
          ->setErrorByName('password', $config
          ->get('others.protected_pages_incorrect_password_msg'));
      }
    }
    elseif ($global_password_setting == 'per_page_or_global') {
      $fields = [
        'password',
      ];
      $conditions = [];
      $conditions['general'][] = [
        'field' => 'pid',
        'value' => $form_state
          ->getValue('protected_page_pid'),
        'operator' => '=',
      ];
      $password = $this->protectedPagesStorage
        ->loadProtectedPage($fields, $conditions, TRUE);
      $global_password = $config
        ->get('password.protected_pages_global_password');
      if (!$this->password
        ->check($form_state
        ->getValue('password'), $password) && !$this->password
        ->check($form_state
        ->getValue('password'), $global_password)) {
        $form_state
          ->setErrorByName('password', $config
          ->get('others.protected_pages_incorrect_password_msg'));
      }
    }
    else {
      $global_password = $config
        ->get('password.protected_pages_global_password');
      if (!$this->password
        ->check($form_state
        ->getValue('password'), $global_password)) {
        $form_state
          ->setErrorByName('password', $config
          ->get('others.protected_pages_incorrect_password_msg'));
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $this
      ->config('protected_pages.settings');
    $_SESSION['_protected_page']['passwords'][$form_state
      ->getValue('protected_page_pid')]['request_time'] = $this->time
      ->getRequestTime();
    $session_expire_time = $config
      ->get('password.protected_pages_session_expire_time');
    if ($session_expire_time) {
      $_SESSION['_protected_page']['passwords'][$form_state
        ->getValue('protected_page_pid')]['expire_time'] = strtotime("+{$session_expire_time} minutes");
    }
  }

}

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
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
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.
ProtectedPagesLoginForm::$currentUser protected property Account proxy service.
ProtectedPagesLoginForm::$password protected property Provides the password hashing service object.
ProtectedPagesLoginForm::$protectedPagesStorage protected property The protected pages storage service.
ProtectedPagesLoginForm::$time protected property A date time instance.
ProtectedPagesLoginForm::accessProtectedPageLoginScreen public function Checks access based permission and protected page id.
ProtectedPagesLoginForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
ProtectedPagesLoginForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
ProtectedPagesLoginForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
ProtectedPagesLoginForm::protectedPageTitle public function Route title callback.
ProtectedPagesLoginForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
ProtectedPagesLoginForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
ProtectedPagesLoginForm::__construct public function Constructs a new ProtectedPagesLoginForm.
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.
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.