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.

  • can anonymous user explore site content? for what reason need to redirect login page? – monymirza Nov 12 '12 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 '12 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. – Boriana Ditcheva Nov 12 '12 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 '12 at 20:04
  • if anonymous users are not allowed to explore any site content, then remove permission to view content – monymirza Nov 13 '12 at 7:05
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" } } ]
  }
}
  • @TopTomato this is for drupal 7 – Aboodred1 Sep 26 '16 at 20:27
  • i made an error and it is working. – TopTomato Sep 27 '16 at 22:42
  • @TopTomato Awesome! Glad it worked for you. – Aboodred1 Sep 27 '16 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

  • 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 '12 at 17:51
  • then remove 'user' from (access denied) page field at 2nd step – monymirza Nov 14 '12 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 '12 at 5:33
  • yes. further can be done by rules module. you can also do it via trigger core module. – monymirza Nov 14 '12 at 5:39
  • 1
    If someone has access denied does not mean they are not logged in.See This Answer – Tanvir Ahmad Jan 3 '17 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');
}
  • 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 '12 at 5:37
  • @Chance, I'm curious. Where would this live within the .theme file? – TikaL13 Sep 12 '18 at 19:42
0

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

  • 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 '12 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.

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

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.