2

I installed and enabled the Login Destination module, and added a login destination rule.

In Internal page or external URL I added myhomepage which is a basic page URL that I want to show after a user performs a login. If authenticated user login then it would not redirect to default page.

My default dashboard page is /cms and dashboard page after user login is cms/96/dashboard (96 in it is the uid of the user).

I have a three types of users:

  • authenticated user
  • administrator
  • employee entry form

And I want authenticated user and employee entry form user to be redirect to cms/{uid}/dashboard (but not administrator users).

Some more details about my configuration:

  • Redirect upon triggers : I have only select the first one and that is Login, registration, one-time login link
  • Only the listed pages:

    • cms
    • cms/register
    • cms/*/edit
  • and under setting tab I have checked both checkboxes.

But when I try to login then user gets redirected to default page.

Any suggestions?

enter image description here

9
  • You wants to redirect all the login user to cms/{uid}/dashboard url right?
    – DRUPWAY
    Nov 17, 2016 at 7:46
  • yes please help. I have a three type of user authenticated user, administrator and employee entry form and i want authenticated and employee entry form user redirect to cms/{uid}/dashboard expect administrator users
    – user67248
    Nov 17, 2016 at 7:47
  • Rule is the best module to achieve this.
    – DRUPWAY
    Nov 17, 2016 at 7:55
  • try cms/[account:uid]/dashboard in page redirect. currently your rule redirect path is different.
    – DRUPWAY
    Nov 17, 2016 at 10:02
  • 1
    Added some links for reference please check those.
    – DRUPWAY
    Nov 21, 2016 at 6:21

2 Answers 2

2

You can use Rules module.

Events: User has logged in.
Condition: User has role.
Actions: Page redirect.

Other Way through coding:

You can use hook_user_login

function hook_user_login(&$edit, $account) {
 if($account->uid == 1)
  drupal_goto('cms');
 else
  drupal_goto('cms/{$account->uid}/dashboard');
}

Updated rule:

{ "rules_after_login_rule" : {
    "LABEL" : "After Login Rule",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "user_login" : [] },
    "IF" : [
      { "user_has_role" : { "account" : [ "account" ], "roles" : { "value" : { "4" : "4" } } } }
    ],
    "DO" : [ { "redirect" : { "url" : "cms\/[account:uid]\/myhomepage" } } ]
  }
}

Look at these post as well.

How to rediret to specific page depends upon user roles

I want to redirect the user to profile page after they log in

Redirect user based on role and node content

16
  • its open the same default page didn't redirect
    – user67248
    Nov 17, 2016 at 9:01
  • hook_user_login is not working
    – user67248
    Nov 17, 2016 at 9:07
  • Disable the module you have using for same purpose and try. secondly how you are using hook_user_login ?
    – DRUPWAY
    Nov 17, 2016 at 9:11
  • i did the same instructions which are you giving to add the rule, events,conditions and actions but when i tried to login it redirect to same default page that is cms/96/dashboard
    – user67248
    Nov 17, 2016 at 9:17
  • and for hook_user_login. i have changed the hook_user_login function and change the dashboard to dashboard1. When i try to login with authenticated user its going on same page and same for administrative user
    – user67248
    Nov 17, 2016 at 9:19
1

It's not clear to me what exactly you want to achieve with the Login destination module. But if all you want to do is to redirect a user after logging in, to a page that somehow corresponds to which roles the user has, then you can simply do that using the Rules module. And optionally also use the Conditional Rules module if you have more sophisticated redirect logic you need to implement.

Read on for more details about all this using some rules examples to get you going. These Rules are in Rules export format, which you can import in your own site if you have the Rules UI enabled, and after you enabled this rule

Example that should match your case

{ "rules_redirect_selected_roles_to_selected_pages" : {
    "LABEL" : "Redirect selected roles to selected pages",
    "PLUGIN" : "reaction rule",
    "ACTIVE" : false,
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "user_login" : [] },
    "IF" : [
      { "NOT user_has_role" : { "account" : [ "account" ], "roles" : { "value" : { "3" : "3" } } } },
      { "user_has_role" : { "account" : [ "account" ], "roles" : { "value" : { "4" : "4" } } } }
    ],
    "DO" : [ { "redirect" : { "url" : "cms\/[account:uid]\/myhomepage" } } ]
  }
}

Here is what this rule does, after a users completed a login:

  • Check if for the logged in user both of these Rules Conditions are true:

    • the user does not have role "administrator" (which is the role with role id = "3").
    • the user does have the role with role id = "4".
  • If both (!!!) Rules Conditions are satisfied, then the Rules Action will perform a redirect to cms/[account:uid]/myhomepage, whereas the [account:uid] in this redirect path will be replaced by the uid of the user who logged in.

Notes:

  • In this rule there is no check about "authenticated role", because as per the Rules Event (="User has logged in"), you can be sure the user is authenticated.
  • If needed, adapt the role id (="4") in the above rules example to fit the role id of your role that corresponds to role "employee entry form". If you have other custom roles (not mentioned in your question), then it could well be that the role id for "employee entry form" is another value.

More sophisticated redirect example

After you also enable the Conditional Rules module, you can implement (way) more sophisticated redirect logic, as in this Rules example:

{ "rules_redirect_selected_roles_to_selected_pages" : {
    "LABEL" : "Redirect selected roles to selected pages",
    "PLUGIN" : "reaction rule",
    "ACTIVE" : false,
    "OWNER" : "rules",
    "REQUIRES" : [ "rules_conditional", "rules" ],
    "ON" : { "user_login" : [] },
    "DO" : [
      { "CONDITIONAL" : [
          {
            "IF" : { "user_has_role" : { "account" : [ "account" ], "roles" : { "value" : { "3" : "3" } } } },
            "DO" : [ { "redirect" : { "url" : "node\/32" } } ]
          },
          { "ELSE" : [
              { "CONDITIONAL" : [
                  {
                    "IF" : { "user_has_role" : { "account" : [ "account" ], "roles" : { "value" : { "4" : "4" } } } },
                    "DO" : [ { "redirect" : { "url" : "node\/14" } } ]
                  },
                  { "ELSE" : [ { "redirect" : { "url" : "node\/22" } } ] }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
}

Here is what this rule does, after a users completed a login:

if the user has access to role with role id = 3 (= admin), then a redirect is done to node/32. else if the user has access to role with role id = 4 (= some custom role), then a redirect is done to node/14. else redirect to node/22.

Adapt the node ids (= 32, 14, 23) and role ids (=3, 4, ...) to whatever fits your case, and extend the if/then/else constructs as much as you want (or use a "switch"-alternative if you prefer).

Note: as an alternative to using the Conditional Rules module to implement the above Rules logic, you can also split such rules in multiple rules, and combine that with replacing the Rules Actions which invoke appropriate Rules Components. For more details on that, refer to the answer to "What's the alternative to using the Conditional Rules module?".

Conclusion

With only 1 custom rule you can implement whatever redirect logic so that for each role you implement an appropriate page to redirect to after logging in. That's really it, and it works like a charm.

Your Answer

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