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?– monymirzaNov 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.– Aboodred1Nov 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.– Boriana DitchevaNov 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.– Aboodred1Nov 12, 2012 at 20:04
-
if anonymous users are not allowed to explore any site content, then remove permission to view content– monymirzaNov 13, 2012 at 7:05
5 Answers
The way I solved it as follow:
- Events: Drupal is initializing
Conditions:
- User has role(s) -
Parameter: User: [site:current-user], Roles: anonymous user
- NOT Text comparison -
Parameter: Text: [site:current-page:url], Matching text: user/login
- User has role(s) -
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" } } ]
}
}
Go to /admin/people/permissions
uncheck View published content for anonymous users
now go to /admin/config/system/site-information
add 'user' in (access denied) page field
-
good work around, but I still need to show access denied page because not all users have the same permissions. I appreciate your help. Nov 13, 2012 at 17:51
-
-
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 Nov 14, 2012 at 5:33
-
yes. further can be done by rules module. you can also do it via trigger core module. Nov 14, 2012 at 5:39
-
1If someone has access denied does not mean they are not logged in.See This Answer Jan 3, 2017 at 7:40
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" Nov 14, 2012 at 5:37
-
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. Nov 12, 2012 at 22:59
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.