1

Can we restrict some page like

  • node/add
  • /user
  • /user/register - as I dont have login/registration page at all
  • /user/login
  • node/200 or node/500 - so that may be of any content type

and it should be unlimited. I need to show Access denied page when somebody accessed those pages.

Though I can use programmatically drupal_access_denied() function, do we have any other option on this.

  • How will admin get access to the site if you restrict /user/login – J. Reynolds Apr 4 '16 at 11:26
  • I am using drupal.org/project/rename_admin_paths, so i moved /user/login to /somethingelse – NTT Apr 4 '16 at 11:28
  • Part of the solution can be provided with Path Access which just implements hook_menu_alter – J. Reynolds Apr 4 '16 at 11:31
  • @Reynolds: Yes agreed but i need to alter each and every menu through module, is there anyother ways something like drupal.org/project/pathauto module, here you can create aliases and much more. – NTT Apr 4 '16 at 11:55
  • @Nagarjuna : merci (thank you) for the accept! Are you familiar with using the Rules module ? – Pierre.Vriens Apr 13 '16 at 10:21
1

You can use the Rules module to restrict access to those selected pages. Here is a rule (in Rules export format) which should be pretty close to implement such access restrictions as you're asking for:

{ "rules_limit_access_to_some_urls_" : {
    "LABEL" : "Limit access to some URLs",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "init" : [] },
    "IF" : [
      { "user_has_role" : {
          "account" : [ "site:current-user" ],
          "roles" : { "value" : { "1" : "1" } }
        }
      },
      { "OR" : [
          { "data_is" : {
              "data" : [ "site:current-page:path" ],
              "op" : "IN",
              "value" : { "value" : [
                  "node\/add",
                  "user",
                  "user\/register",
                  "user\/login",
                  "node\/200",
                  "node\/500"
                ]
              }
            }
          },
          { "text_matches" : {
              "text" : [ "site:current-page:url" ],
              "match" : "node\/\\d+\/something",
              "operation" : "regex"
            }
          }
        ]
      }
    ],
    "DO" : [
      { "redirect" : { "url" : "no_access" } },
      { "drupal_message" : {
          "message" : "Access to the content located at the path you tried to use is not allowed.\r\n\u003Cbr \/\u003E\r\nIf you think this is an error, please contact us.",
          "type" : "warning"
        }
      }
    ]
  }
}

Check the paths in the above rule to ensure it fits your own needs. Also note that I include an extra path that corresponds to "node/*/something" (in case you'd also want to limit access to any such URL. Replace the something in it to fit your needs. Or if you don't have such URLs then just remove that entire Rules Conditions (with the regex in it).

And indicate the correct path of the redirect to be perform (= change the "no_access" make it fit your "no access" page). Possibly you may also want to adapt the roles for which these validations should be applied.

For more details/background about using such rule, refer to my answer to the question "How to make the permissions of a module more granular?".

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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