1

I would like to restrict certain roles from visiting certain pages, ideally by using Rules.

I am able to do this with Nodes, however for Pages such as a Views Page, I can't see any way of triggering this redirect?

0

4 Answers 4

3

If you simply want to restrict the access to the View, you should alter the settings for the View.

Under the access settings, select Role instead of Permission, and select the appropriate role.

However, if you have to use Rules for some other reason, I would create a rule that triggered every time Drupal is initializing. And then, add a condition with custom PHP code. You could do something like

global $user;
$rid = 1;
return ($_GET['q'] == 'my/forbidden/path' && isset($user->roles[$rid]));

and then add an action that redirects the user to another page.

1

I guess you can achieve what you're looking for using Page Manager Redirect instead of Rules.

1
  • I have never tried it myself, but the module description fits. Always try drupalmodules.com to look for modules. Aug 11, 2011 at 18:37
1

Selection rules like this are why we typically use Panels for displaying all pages. Rather than use different access methods for different page types, you get a consistent interface.

In you case, you would set up your view as a Content Pane and not a Page. You would then build a new panel page, and when you can add content, your view will show up in the View Panes tab.

You can then set up your selection rules. Among the rules are User Permissions and User Roles.

1

Sure there is a way to exactly do what you are asking for, i.e. "... restrict certain roles from visiting certain pages, ideally by using Rules.".

The trick (clue) to actually do this is like so:

  • Use Rules Event "Drupal is initializing".
  • Add the appropriate Rules Conditions to check for the "Roles" you want to verify, and the "Path(s)" you want to apply this to.
  • Typical Rules Actions you might want to do are to set some Drupal Message, and/or some redirect to some other page.

Below is an example of such rule (in Rules export format) that is pretty close to what you asked for:

{ "rules_limit_access_to_some_urls_" : {
    "LABEL" : "Limit access to some URLs",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "init" : [] },
    "IF" : [
      { "NOT user_has_role" : {
          "account" : [ "site:current-user" ],
          "roles" : { "value" : { "3" : "3" } }
        }
      },
      { "OR" : [
          { "data_is" : {
              "data" : [ "site:current-page:path" ],
              "op" : "IN",
              "value" : { "value" : [
                  "abc\/x123",
                  "efg\/y456",
                  "hij\/z789"
                ]
              }
            }
          },
          { "text_matches" : {
              "text" : [ "site:current-page:path" ],
              "match" : "node\/\\d+\/something",
              "operation" : "regex"
            }
          }
        ]
      }
    ],
    "DO" : [
      { "redirect" : { "url" : "some_path" } },
      { "drupal_message" : {
          "message" : "Access to the content located at \u0022\u003Cstrong\u003E[site:current-page:path]\u003C\/strong\u003E\u0022 is not allowed.",
          "type" : "warning"
        }
      }
    ]
  }
}

Some more details about verification in the Rules Conditions in the above example:

  • check if the current user is not an administrator (for administrators the Rules Action does not apply)
  • check if the "path that will be viewed" is either abc/x123, efg/y456 or hij/z789, or if it is a path with a pattern like node/x/something (with x some digit(s)).

If both Rules Conditions are true, then the Rules Action will perform a redirect (to a path like some_path), and some message is shown to the user.

To make the above rule work in your case, just adapt the paths (change them to whatever fits), and the message to be shown for those who are not allowed to access those paths.

Note that it doesn't matter what these paths relate to: they can be nodes, views pages, user profiles, etc (all such cases should work).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.