1

Say I have a site called drupal.local, the user login page would be drupal.local/user. And the admin 'homepage' would be drupal.local/admin.

The problem is that lots of people seem to expect drupal.local/admin to lead them to the login page if they are not already logged in. Unfortunately instead of doing that, they just get:

You are not authorized to access this page.

I guess (thinking about it) a decent solution would be to put a link to the login page in the 403 error template text - but it would be more elegant to redirect the user to /user if they weren't already logged in.

Is it possible to do such a thing?

5

Sure, you can install a module like Redirect 403 to User Login:

Redirect the HTTP 403 error page to the Drupal /user/login page with an optional message that reads:

"Access denied! You must login to view this page."

Also, the desired page is appended in the url query string so that, once login is successful, the user is taken directly where they were originally trying to go.

  • Awesome, just what I was looking for :) – Sean Feb 10 '15 at 9:53
3

You can also go for LoginToboggan

Provide a login form on Access Denied pages for non-logged-in (anonymous) users.
  • Thanks, seems like there's plenty of other useful things you can do with this one too :) – Sean Feb 10 '15 at 9:54
1

You could use the Rules module for this. Here is the rule you'd need for it (in Rules export format, just use copy/paste to import it in your own setup):

{ "rules_redirect_admin_path_to_user_path" : {
    "LABEL" : "Redirect admin path to user path",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "init" : [] },
    "IF" : [
      { "user_has_role" : {
          "account" : [ "site:current-user" ],
          "roles" : { "value" : { "1" : "1" } }
        }
      },
      { "text_matches" : {
          "text" : [ "site:current-page:path" ],
          "match" : "admin",
          "operation" : "starts"
        }
      }
    ],
    "DO" : [
      { "drupal_message" : { "message" : "To access this path (= /admin) you need to login first, you will be redirected automatically to  path (= /user)." } },
      { "redirect" : { "url" : user" } }
    ]
  }
}

This rule uses 2 Rules Conditions:

  • Is this an anonymous user?
  • Does the current path start with /admin?

Whenever such redirect actually happens (that is: both Rules Conditions are satisfied), the user will also receive an informational message like so:

To access this path (= /admin) you need to login first, you will be redirected automatically to path (= /user).

If you don't want such message to appear, then just delete the line containing drupal_message in the exported rule above.

Similar to the previous answers, it might require you to enable an extra contributed module (Rules). But, as indicated by its growing popularity also, that module is probably already enabled in mostly any site (similar to the Views module), because there are dozens of use-cases for this module).

Note: IMO it makes sense to perform such redirect for any path that starts with /admin. But should you rather prefer to perform such redirect only for an exact match for path /admin (instead of starting with such path), then simply replace the 2nd Rules Condition with this one (that is: omit the "operation" which is "starts"):

  { "text_matches" : {
      "text" : [ "site:current-page:path" ],
      "match" : "admin"
    }
  }

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.