Problem/Motivation

Currently a user has to have the 'administer redirect settings' permission to save the ignore, because the ignored path needs to be saved on the form.

Steps to reproduce

Proposed resolution

Save the ignore in '\Drupal\redirect_404\Controller\Fix404IgnoreController::ignorePath' and redirect according to permission.

Remaining tasks

User interface changes

API changes

Data model changes

Comments

Primsi created an issue. See original summary.

primsi’s picture

Status: Active » Needs review
StatusFileSize
new3.46 KB

Initial patch.

Status: Needs review » Needs work

The last submitted patch, 2: redirect-allow-non-admin-ignore-3344064-2.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

berdir’s picture

+++ b/modules/redirect_404/src/Controller/Fix404IgnoreController.php
@@ -60,26 +60,37 @@ class Fix404IgnoreController extends ControllerBase {
     $langcode = $request->query->get('langcode');
 
-    if (empty($ignored_paths) || !strpos($path, $ignored_paths)) {
+    $editable = $this->configuration->getEditable('redirect_404.settings');
+    $existing_config_raw = $editable->get('pages');
+    if (!empty($existing_config_raw) && !strpos($path, $existing_config_raw)) {
+      $existing_config = explode(PHP_EOL, $existing_config_raw);
+      foreach ($existing_config as $key => $item) {
+        $existing_config[$key] = trim($item);
+      }
+      $existing_config[] = $path;
+
+      $ignored_paths = implode(PHP_EOL, array_filter($existing_config)) . PHP_EOL;
+      $editable->set('pages', $ignored_paths);
+      $editable->save();
+
       $this->redirectStorage->resolveLogRequest($path, $langcode);
 

I frequently change the provided pattern and instead make it a wildcard, I think it makes sense to keep the behavior for admins for now. Maybe two separate operations (ignore and customize..) or something could make sense, we can look into that later.

primsi’s picture

Status: Needs work » Needs review
StatusFileSize
new4.14 KB
new3.45 KB

Reverted that back. We then probably need to test the non-admin functionality.

primsi’s picture

The other part where the permission is checked.

berdir’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests
  1. +++ b/modules/redirect_404/src/Controller/Fix404IgnoreController.php
    @@ -67,19 +67,42 @@ class Fix404IgnoreController extends ControllerBase {
         if (empty($ignored_paths) || !strpos($path, $ignored_paths)) {
           $this->redirectStorage->resolveLogRequest($path, $langcode);
    

    there is another langcode argument here that I think we should drop in the the other issue. Will probably conflict though, lets get one in and then reroll the other.

  2. +++ b/modules/redirect_404/src/Controller/Fix404IgnoreController.php
    @@ -67,19 +67,42 @@ class Fix404IgnoreController extends ControllerBase {
    +      // Users without 'administer redirect settings' and 'ignore 4040 request'
    +      // permission can also ignore pages, but are not redirected to the
    +      // settings page, but save config directly.
    +      if (!$this->currentUser()->hasPermission('administer redirect settings') && $this->currentUser()->hasPermission('ignore 404 requests')) {
    +        $editable = $this->configuration->getEditable('redirect_404.settings');
    +        $existing_config_raw = $editable->get('pages');
    +        $existing_config = explode(PHP_EOL, $existing_config_raw);
    +
    +        foreach ($existing_config as $key => $item) {
    +          $existing_config[$key] = trim($item);
    +        }
    

    The "but save config directly" sounds strange, also due to two "but" in a row. I think we can possibly just drop that part, seems pretty clear.

    Also, maybe just get the editable config from the start

    And yes, we definitely need tests, because I don't actually see you adding the new path to the list, you just trim the existing ones and then save them back. I don't think we need that loop at all, if you look at redirect_404_form_redirect_settings_form_alter(), we just add the new one to the list, can't we do the same here?

primsi’s picture

Status: Needs work » Needs review
StatusFileSize
new4.32 KB
new6.62 KB

Addressed and added test.

  • Berdir committed cccc57ba on 8.x-1.x authored by Primsi
    Issue #3344064 by Primsi: Allow non admin users to ignore 404 pages
    
berdir’s picture

Status: Needs review » Fixed
Issue tags: -Needs tests

Committed.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.