Drupal 7: Login Destination Based On User Role

The Login Destination module is a neat little module that allows site admins to customize where the user will be sent to when they login. It provides a few of different ways of customizing, including a PHP snippet box that allows fine grained control.

What I needed to do on a recent project was to change the login destination for a single user role, but redirect everyone else to their user profile page. This required the use of the PHP snippet box. In order to get access to the current user object I had to include it into the scope of the PHP snippet code by using the following.

global $user;

With this in hand I could then use the roles property of the $user object to find out what role the current user. However, because the roles property is an array (due to the ability assign users to more than one role) a direct comparison is not possible. So I used PHP function in_array() to search the array for the role name. The following snippet is what I came up with, it redirects any user assigned to "An Awesome Role" to the create new page form, everyone else gets redirected to their user profile. Note that you should not use the php tags in the snippet area.

global $user;
if (in_array('An Awesome Role', $user->roles)) {
  return 'node/add/page';
} else{
  return 'user';
}

This works for most circumstances, but site admins do actually have the ability to edit the role names, which will mean that if they do your rules will no longer work. This won't happen much, but when it does you will have to update the redirect rules with the new role names. It is possible to use the role ID instead as this won't change, even if the role names are updated. In order to use this you'll need to get the keys of the roles array as the keys contain the role IDs. This can be done using the array_keys() function in the following manner.

global $user;
if (in_array(3, array_keys($user->roles))) {
  return 'node/add/page';
} else{
  return 'user';
}

This means that no matter what you change your user roles to this rule will always work.

Comments

I use Login Destination and use it for this exact purpose, user roles that is. However, I also use LoginToboggan to display the user login form on access denied pages so that a user that tries to directly access a page in which they must login first to view via a bookmark or in my case, a notification email, they can simply login and they will be redirected to the page they were trying to view. This worked perfectly until I used the same logic you describe in Login Destination. Do you know how you would preserve this functionality while still using Login Destination to redirect based on role?

Permalink

I think there is probably an overlap between the LoginToboggan and Login Destination modules, in which case one is overriding the functionality of the other. In this case I think the Login Destination module takes precidence. I haven't tested anything, but I think removing the else block of the if statement in Login Destination should stop it overriding anything that LoginToboggan is trying to do.

Worth a shot anyway...

Name
Philip Norton
Permalink

I figured this out and it is very simple to preserve the LoginToboggan functionality. There is a fieldset titled "Redirection Conditions" in the Login Destination settings page. Here, it simply asks, when do you want redirection to occur? I checked the PHP snippet option and entered:

return !isset($_REQUEST['destination']);

This way it won't redirect if another module, like LoginToboggan, has set the destination. I imagine this is not desirable behavior for all situations, but it worked perfectly in my case.

Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
6 + 1 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.