Skip to content

Commit

Permalink
Resolves #10 and #11.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjordan committed May 9, 2024
1 parent 00d9a7a commit ded1660
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
16 changes: 16 additions & 0 deletions config/schema/ip_range_access.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,19 @@ condition.plugin.user_ip_address:
ip_ranges:
type: text
label: 'IP ranges'
log_requests:
type: boolean
label: "Log user's IP address and whether or not their request has met this condition."

reaction.plugin.deny_access:
type: reaction.plugin
mapping:
deny_access:
type: checkbox
label: 'Deny access'
proxy_prepend_url:
type: text
label: "URL to redirect users to, e.g., an Ezproxy login URL."
log_executed:
type: checkbox
label: "Log that this reaction was executed."
2 changes: 1 addition & 1 deletion ip_range_access.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ package: Context
core_version_requirement: ^9 || ^10
dependencies:
- context
version: '1.0.0'
version: '1.0.1'
12 changes: 11 additions & 1 deletion src/Plugin/Condition/UserIpAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
'Separate the low and high ends of each range with a colon, e.g. 111.111.111.111:222.222.222.222. ' .
' Asterisks are not allowed. Single IP addresses are also allowed, each on its own line.'),
];
$form['log_requests'] = [
'#type' => 'checkbox',
'#title' => $this->t('Log user requests'),
'#default_value' => $this->configuration['log_requests'],
'#description' => $this->t("Log users' requests."),
];
return parent::buildConfigurationForm($form, $form_state);
}

Expand All @@ -45,6 +51,7 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
public function defaultConfiguration() {
return [
'ip_ranges' => '',
'log_requests' => FALSE,
] + parent::defaultConfiguration();
}

Expand All @@ -53,6 +60,7 @@ public function defaultConfiguration() {
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['ip_ranges'] = $form_state->getValue('ip_ranges');
$this->configuration['log_requests'] = $form_state->getValue('log_requests');
parent::submitConfigurationForm($form, $form_state);
}

Expand All @@ -73,7 +81,9 @@ public function evaluate() {
} else {
$met_condition_string = 'No';
}
\Drupal::logger('ip_range_access')->info("User's IP address is %ip. Met condition: %met", ['%ip' => $ip, '%met' => $met_condition_string]);
if ($this->configuration['log_requests']) {
\Drupal::logger('ip_range_access')->info("User's IP address is %ip. Met condition: %met", ['%ip' => $ip, '%met' => $met_condition_string]);
}
return $met_condition;
}

Expand Down
19 changes: 16 additions & 3 deletions src/Plugin/ContextReaction/DenyAccessReaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,18 @@ public function summary() {
*/
public function execute() {
$config = $this->getConfiguration();
$current_url = \Drupal::request()->getSchemeAndHttpHost() . \Drupal::service('path.current')->getPath();
if ($config['deny_access']) {
if ($this->configuration['log_executed']) {
\Drupal::logger('ip_range_access')->info("User was denied access to %url.", ['%url' => $current_url]);
}
throw new AccessDeniedHttpException();
}
if (strlen($config['proxy_prepend_url'])) {
$current_url = \Drupal::request()->getSchemeAndHttpHost() . \Drupal::service('path.current')->getPath();
$redirect_url = $config['proxy_prepend_url'] . $current_url;
if ($this->configuration['log_executed']) {
\Drupal::logger('ip_range_access')->info("User was redirected to %url.", ['%url' => $redirect_url]);
}
$response = new RedirectResponse($redirect_url);
$response->send();
exit;
Expand All @@ -59,15 +65,21 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
$form['deny_access'] = [
'#title' => $this->t('Deny access to node or media'),
'#type' => 'checkbox',
'#description' => $this->t('Check this box to return a 403 Access Denied response to the user.'),
'#description' => $this->t("Check this box to return a 403 Access Denied response to the user. If you enter a proxy URL below, you should uncheck this box. Note: you should consider adding the \"User's Role\" condition to this Context to prevent administrators from being blocked from accessing content."),
'#default_value' => isset($config['deny_access']) ? $config['deny_access'] : FALSE,
];
$form['log_executed'] = [
'#title' => $this->t('Log that this reaction was executed'),
'#type' => 'checkbox',
'#description' => $this->t('Check this box to log that this reaction was executed.'),
'#default_value' => isset($config['log_executed']) ? $config['log_executed'] : FALSE,
];
$form['proxy_prepend_url'] = [
'#type' => 'textfield',
'#title' => t('Proxy URL'),
'#default_value' => $this->configuration['proxy_prepend_url'],
'#maxlength' => 256,
'#description' => t('URL to redirect users to, e.g., an Ezproxy login URL. If you use this option, you should uncheck the "Deny access to node or media" option above. Leave this field blank to not redirect user.'),
'#description' => t('URL to redirect users to, e.g., an Ezproxy login URL. The current URL will be appended to this URL. If you use this option, you should uncheck the "Deny access to node or media" option above because you are not denying access, you are redirecting the user. Leave this field blank to not redirect user.'),
];
return $form;
}
Expand All @@ -78,6 +90,7 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->setConfiguration([
'deny_access' => $form_state->getValue('deny_access'),
'log_executed' => $form_state->getValue('log_executed'),
'proxy_prepend_url' => $form_state->getValue('proxy_prepend_url'),
]);
}
Expand Down

0 comments on commit ded1660

Please # to comment.