0

I am trying to redirect any user with a particular Role at login to a URL. I am using Backdrop (Drupal 7). I have looked at most of the posts relating to this problem, and tried many of the examples but the redirect fails. There are no messages in the Logs, I have debugging on in Rules. I have tried this which is from a post I found, and modified for my page redirection which works:

    { "rules_after_login_redirect_to_host_registration" : {
    "LABEL" : "After login redirect to Host Registration.",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "user_login" : [] },
    "IF" : [
      { "NOT text_matches" : {
          "text" : [ "site:current-page:path" ],
          "match" : "^user\/reset\/",
          "operation" : "regex"
        }
      }
    ],
    "DO" : [
      { "redirect" : { "url" : "webform\/host-registration" } },
      { "backdrop_message" : { "message" : "Redirecting to Host Registration" } }  ]
  }
}

However my Rule to Redirect a user at login with a Role of Host does not work:

{ "rules_redirect_role_host_to_selected_page" : {
    "LABEL" : "Redirect role Host to selected page",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "user_login" : [] },
    "IF" : [
      { "user_has_role" : { "account" : [ "account" ], "roles" : { "value" : { "host" : "host" } } } }
    ],
    "DO" : [ { "redirect" : { "url" : "webform\/host-registration" } } ]
  }
}

I am baffled. Why is the Role Condition not picked up?

2 Answers 2

0

Here is a working rule to redirect users to different URL's depending on user role, you will need Rules conditional module to acomplish task, enjoy.

{ "rules_user_has_logged_in" : {
"LABEL" : "user has logged in, redirect based on role",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"TAGS" : [ "redirect" ],
"REQUIRES" : [ "rules_conditional", "rules" ],
"ON" : { "user_login" : [] },
"DO" : [
  { "CONDITIONAL" : [
      {
        "IF" : { "user_has_role" : { "account" : [ "account" ], "roles" : { "value" : { "4" : "4" } } } },
        "DO" : [ { "redirect" : { "url" : "polizas\/todas" } } ]
      },
      {
        "ELSE IF" : { "user_has_role" : { "account" : [ "account" ], "roles" : { "value" : { "6" : "6" } } } },
        "DO" : [ { "redirect" : { "url" : "polizas\/todas" } } ]
      },
      {
        "ELSE IF" : { "user_has_role" : { "account" : [ "account" ], "roles" : { "value" : { "5" : "5" } } } },
        "DO" : [ { "redirect" : { "url" : "pagos" } } ]
      },
      {
        "ELSE IF" : { "user_has_role" : { "account" : [ "account" ], "roles" : { "value" : { "7" : "7" } } } },
        "DO" : [ { "redirect" : { "url" : "polizas" } } ]
      },
      {
        "ELSE IF" : { "user_has_role" : { "account" : [ "account" ], "roles" : { "value" : { "12" : "12" } } } },
        "DO" : [ { "redirect" : { "url" : "polizas\/todas", "destination" : "1" } } ]
      }
    ]
  }
]

} }

and an img to see how my rule looks.

enter image description here

0

Your problem is here: "roles" : { "value" : { "host" : "host" } }

Instead of "host", which is is role name it should be a number, which is the corresponding role ID (or rid). You need to change this in both places where "host" appears. Apparently you edited that by hand?

To be clear, the Rules Conditional module is NOT needed unless you want the "else if" functionality.

1
  • Thank you @pinueve for your nice script. I do not need the "else if" so can try and keep it simple. I have not edited by hand that is what I am seeing on export. I have found the ID of my User Role, substituded that and imported it, but still no redirection. I wonder if it is something to do with Backdrop, so I will ask on that forum, and see what they say, then get back here
    – themetman
    Nov 5, 2019 at 11:48

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.