I'm currently working on a custom module with an access form and set permissions for anonymous users.
I'm trying to make it so that when an anonymous user enters the password in the access form, they are logged successfully into 'node/3.'
The trick is, 'node/3' and any other basic page node is blocked for anonymous users via the following code I placed in my .MODULE file.
use Drupal\node\NodeInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;
function cookie_authentication_node_access(NodeInterface $node, $op, AccountInterface $account) {
if ($account->isAnonymous() && $node->gettype() == 'page') {
return AccessResult::forbidden()->cachePerPermissions();
}
return AccessResult::neutral();
}
Now, how do I get the anonymous user to access the page only for the condition that the password is entered in the access form?
Here's the code for the access form:
<?php
/**
* @file
* Contains \Drupal\resume\Form\CookieForm.
*/
namespace Drupal\cookie_authentication\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
class CookieForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'cookie_authentication';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['password'] = array(
'#type' => 'textfield',
'#title' => t('Access'),
'#required' => TRUE,
);
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => $this->t('Submit'),
'#button_type' => 'primary',
);
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$values = $form_state->getValue('password');
//$configValue = $config->get('access');
$configValue = \Drupal::config('cookie_authentication.settings')->get('access');
if ($values != $configValue) {
$form_state->setErrorByName('password', $this->t('This value is not valid!'));
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$password = $form_state->getValue('password');
setcookie("password", $password );
drupal_set_message('valid code');
$response = Url::fromUserInput('/node/3');
$form_state->setRedirectUrl($response);
}
}
The password is loaded through a config form I built into the back-end...