12

I'm using rules module with drupal 7 in order to redirect anonymous users to the login page, so I created a rule with react on event "Drupal is initializing", condition "User has role" with anonymous user and "Not site login url", then added an action "Page redirect" to site login url. When I tried to test the rule; drupal kept redirecting to the login page over over and over until apache server stopped working. Any recommendations.

9
  • can anonymous user explore site content? for what reason need to redirect login page?
    – monymirza
    Nov 12, 2012 at 17:24
  • anonymous users are not allowed to explore any site content and not allowed to register except with an invitation. Users have to login to use the site.
    – Aboodred1
    Nov 12, 2012 at 17:26
  • I don't understand this part "rule drupal kept redirecting to the login page without showing it." Could you explain what you mean? Otherwise I'm not sure I understand which part isn't working. Nov 12, 2012 at 18:37
  • Well when I go to the homepage or any other page but not login page the rule works fine; its redirecting the user to the login page, however the browser keeps redirecting to login page over and over until apache stops working.
    – Aboodred1
    Nov 12, 2012 at 20:04
  • if anonymous users are not allowed to explore any site content, then remove permission to view content
    – monymirza
    Nov 13, 2012 at 7:05

5 Answers 5

12

The way I solved it as follow:

  • Events: Drupal is initializing
  • Conditions:

    1. User has role(s) - Parameter: User: [site:current-user], Roles: anonymous user
    2. NOT Text comparison - Parameter: Text: [site:current-page:url], Matching text: user/login
  • Actions: Page redirect - Parameter: URL: user/login


Export rule as follow:

{ "rules_login" : {
    "LABEL" : "login",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "TAGS" : [ "login" ],
    "REQUIRES" : [ "rules" ],
    "ON" : { "init" : [] },
    "IF" : [
      { "user_has_role" : {
          "account" : [ "site:current-user" ],
          "roles" : { "value" : { "1" : "1" } }
        }
      },
      { "NOT text_matches" : { "text" : [ "site:current-page:url" ], "match" : "user\/login" } }
    ],
    "DO" : [ { "redirect" : { "url" : "user\/login" } } ]
  }
}
3
  • @TopTomato this is for drupal 7
    – Aboodred1
    Sep 26, 2016 at 20:27
  • i made an error and it is working.
    – TopTomato
    Sep 27, 2016 at 22:42
  • @TopTomato Awesome! Glad it worked for you.
    – Aboodred1
    Sep 27, 2016 at 22:47
3

Go to /admin/people/permissions uncheck View published content for anonymous users

enter image description here

now go to /admin/config/system/site-information add 'user' in (access denied) page field

enter image description here

6
  • good work around, but I still need to show access denied page because not all users have the same permissions. I appreciate your help.
    – Aboodred1
    Nov 13, 2012 at 17:51
  • then remove 'user' from (access denied) page field at 2nd step
    – monymirza
    Nov 14, 2012 at 4:36
  • if I remove it; anonymous users can only see access denied without redirecting to the login page. I'm sure that there's a way I can do it in rules module
    – Aboodred1
    Nov 14, 2012 at 5:33
  • yes. further can be done by rules module. you can also do it via trigger core module.
    – monymirza
    Nov 14, 2012 at 5:39
  • 1
    If someone has access denied does not mean they are not logged in.See This Answer Jan 3, 2017 at 7:40
1

I'm sure you can pull this off with rules, but it would be easy to do in your theme's template.php file as well.

Something like this should work:

global $user;
if ($user->uid == 0 && arg(0) != 'user' && arg(1) != 'login'){
  drupal_goto('user/login');
}

Or, if access to the user/registration & user/password pages is still wanted:

global $user;
if ($user->uid == 0 && arg(0) != 'user'){
  drupal_goto('user/login');
}
2
  • Thanks @Chance I already found this solution before I posted my question. I want to use rules module or at least understand why its not working with "Drupal is initializing"
    – Aboodred1
    Nov 14, 2012 at 5:37
  • @Chance, I'm curious. Where would this live within the .theme file?
    – TikaL13
    Sep 12, 2018 at 19:42
0

The problem here is the event in question. Instead of Drupal is initializing, try Content is viewed.

1
  • I tried it before I post this question and it worked fine with me except with view pages, registration page, basically any page that is not node.
    – Aboodred1
    Nov 12, 2012 at 22:59
0

I know this is a post from two years ago. However, the solution for redirecting an anonymous user to the user login page with rules is as follows:

  • event: System log entry is created
  • condition: User has role(s): anonymous
  • action: page redirect value: user

Hope this will help someone.

1
  • have you tried your solution before you post it here?
    – Aboodred1
    Apr 14, 2014 at 0:53

Your Answer

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

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