class ProtectedPagesLoginForm in Protected Pages 8
Provides login screen to access protected page.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\protected_pages\Form\ProtectedPagesLoginForm
Expanded class hierarchy of ProtectedPagesLoginForm
1 string reference to 'ProtectedPagesLoginForm'
File
- src/
Form/ ProtectedPagesLoginForm.php, line 18
Namespace
Drupal\protected_pages\FormView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
ProtectedPagesLoginForm:: |
protected | property | Account proxy service. | |
ProtectedPagesLoginForm:: |
protected | property | Provides the password hashing service object. | |
ProtectedPagesLoginForm:: |
protected | property | The protected pages storage service. | |
ProtectedPagesLoginForm:: |
protected | property | A date time instance. | |
ProtectedPagesLoginForm:: |
public | function | Checks access based permission and protected page id. | |
ProtectedPagesLoginForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
ProtectedPagesLoginForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
ProtectedPagesLoginForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
ProtectedPagesLoginForm:: |
public | function | Route title callback. | |
ProtectedPagesLoginForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
ProtectedPagesLoginForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
ProtectedPagesLoginForm:: |
public | function | Constructs a new ProtectedPagesLoginForm. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |