5

I'm using Rules to redirect users with certain roles upon login using the Page Redirect action. The "Force redirect" option is set to true (changing this to false does not fix the problem, and if it is false then users who log in from a block on the front page are taken back to the front page, which is not what I want).

My problem occurs when a user forgets their password and requests a one-time login link via e-mail. When the user clicks the link, he/she is taken to the login page. From this page, pressing "Login" will redirect the user according to my custom rule instead of taking the user to the page where the password can be changed.

How can I redirect my users using Rules without breaking the forgot password function?

5 Answers 5

8

You can do that with a rule like that:

{ "rules_after_login_redirect_to_news" : {
    "LABEL" : "After login redirect to news.",
    "PLUGIN" : "reaction rule",
    "WEIGHT" : "0",
    "REQUIRES" : [ "rules" ],
    "ON" : [ "user_login" ],
    "IF" : [
      { "NOT text_matches" : {
          "text" : [ "site:current-page:path" ],
          "match" : "^user\/reset\/",
          "operation" : "regex"
        }
      }
    ],
    "DO" : [ { "redirect" : { "url" : "news" } } ]
  }
}
2
  • That was an easy question, right? However, I suggest you add a minor explanation about the "^" you included in the line starting with "match". Commented Feb 3, 2017 at 13:14
  • I was able to get this to work by using the value user/reset, for anyone using the rules GUI (instead of ^user\/reset\/).
    – Christia
    Commented Sep 9, 2019 at 5:35
6
+50

OK...after doing some looking around and a bit of playing around, this solution seems to work:

Add a condition to your re-direct rule with the following php-evaluation (make sure you have PHP filter module turned on...It's part of the core):

if (arg(0) == 'user' && arg(1) == 'reset') { 
  return false; 
} else { 
  return true; 
}

This should check to see if the user is using a one-time password and if so skip the re-direct Rule.

Hope that helps! Found some different situations on Rules forum, and that seems to be a good compromise.

3
  • Thanks, that did the trick! Note for multilingual sites: I had to use arg(1) and arg(2) instead of arg(0) and arg(1) because my site uses a language path (/en/user/reset etc.) for all URLs. Commented Jul 31, 2012 at 7:08
  • What Event is needed for this rule?
    – adpo
    Commented Dec 1, 2012 at 11:30
  • @AdrianPorebski You should use, under "User," "user has logged in" in Drupal 7. I believe it is the same, or close to it in Drupal 6. Commented Dec 1, 2012 at 22:03
2

I tried the answer but it was not working for me, not sure what the ^ means but i ended up having my rule with the following:

{ "rules_after_login_redirect_to_news" : {
    "LABEL" : "After login redirect to news.",
    "PLUGIN" : "reaction rule",
    "ACTIVE" : false,
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "user_login" : [] },
    "IF" : [
      { "NOT text_matches" : {
          "text" : [ "site:current-page:path" ],
          "match" : "user\/reset\/*",
          "operation" : "regex"
        }
      }
    ],
    "DO" : [ { "redirect" : { "url" : "news" } } ]
  }
}

The * means that anything after user/reset will be brought into the rule.

Also it should be noted that if you are using this with anything else, you should add an 'AND' set with it so it will make sure it relates both sets properly.

1

I am sure there is 100 ways to do this. The Login Destination module looks like it may accomplish what you are looking for.

The Login Destination module allows you to customize the destination that a user is redirected to after logging in, registering to the site (7.x), using a one-time login link or logging out (7.x). The destination can be an internal page or an external URL. It is possible to specify certain conditions like referring pages or user roles and make the destination depend upon them. You may use PHP snippets to provide custom conditions and destinations. It is also possible to keep users on the currently visited page after logging in or out.

I looked through the issues, and it seems they have fixed the "broken forgot password" problem in their implementation. Might be worth a shot, although it looks like it isn't under "active" development. Last update was 2010.

3
  • Thanks, but I'd like to know how I can do this in Rules without installing another module. Commented Jul 30, 2012 at 2:51
  • @PatrickKenny Well, I did look around, and I did see a few other people complaining of the same problem. I didn't however find an answer. Sorry. Commented Jul 30, 2012 at 20:17
  • @PatrickKenny And by the way...the problem with users using a block to login and the rules/trigger re-direct not working seems to be a bug with the block login. I saw several people having this same issue and had the same issue myself when testing. If logging in at /user/, the re-direct works fine. Commented Jul 30, 2012 at 22:00
1

The Password Reset Landing Page (PRLP) modules works well without requiring Rules (...in my experience).

There's also Password Hustle, which almost got me there as well.

2
  • How did Password Hustle "almost get you there"? What were you trying to do, and what are the shortcomings of the module at the time you tried it? Commented Sep 27, 2015 at 3:29
  • 1
    The project I'm on uses both LDAP and Drupal authentication where the Drupal auth users register w/ their email address via the Email Registration module and am using Rules to require profile completion. Password Hustle didn't play well with all those moving parts. It worked fine on a test site w/ minimal module interaction. In the end I liked PRLP's interface better.
    – jay-ess
    Commented Sep 28, 2015 at 14:46

Your Answer

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

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