Hi, I've tried to modify the module code to be able to redirect to different pages according different roles:

  global $user;

  // customize the path as desired.
  if (in_array('editor', $user->roles)) {
    $path = 'editor_user_home';
    return url($_GET['q'], "destination=$path");
  } else {
      $path = 'user';
      return url($_GET['q'], "destination=$path");
  }

... but it doesn't work, it always jumps to node page. What is wrong with it?

CommentFileSizeAuthor
#2 login_destination_roles.patch1.42 KBgreggles

Comments

ymcp’s picture

I'm trying to do something similar & have come across the same problem.

Try putting this at the top of the login_destination_get_destination() function:

global $user;
print_r($user);
exit;

That will print out the values of $user. It seems that $user->uid = 0, ie the function is being called *before* the login process has completed, so $user doesn't contain anything useful. :-(

Not sure how to get around this... any ideas?

greggles’s picture

Status: Active » Needs work
StatusFileSize
new1.42 KB

I "got around" this with the attached patch. It just provides a callback and you set that callback as your destination. Then, in that page you do a drupal_goto based on role. You could just as easily do this in a php formatted node, though.

The roles and $dest values are obviously pretty specific to my site, so they would need to be edited to your situation. But that's the nature of the module anyway...

jorditr’s picture

Hi greggles, thanks for your answer and patch.

Uhmm, it's an interesting solution, but I find it a bit complex for its purpose. Reading your code I've realized that maybe I'm more interested in why my proposal doesn't work. I'll have a deeper look on your code later on :-)

greggles’s picture

JordiTR - let me walk through an explanation of how the code works and I think you'll see why my approach is basically the only one that will work:

This is a top to bottom run down of login_destination.module which is also basically the order in which they are called:

1. login_destination_help is just there to be able to have an explanation of the module on the admin/modules page so you know what it is when you enable it.
2. login_destination_form_alter will get called during every form rendering on your site, but it only takes action if the form is a user_login_block or user_login type of form. In the case that it is one of these forms, it will replace the "action" with the return value of the login_destination_get_destination function. Note that we are still at the point where the login form is being presented to the user - which is to say - they have NOT logged in yet.
3. login_destination_get_destination then is where you implement your custom logic. If you try to get the user role at this point you are still prior to login and $user only contains the anonymous information

That's it. You will never have role information in the module in current form.

So, the rest of my code is as follows:

4. login_destination_menu is a standard function used to provide menus, but also to provide callbacks. Callbacks being the path "my_home" so that once this menu is implemented you can visit "www.example.com/drupal/install/dir/my_home" and it will pass that along to the function named in the callback - login_destination_postfacto. So, as the comments in the code suggest, if you set the login_destination_get_destination to return "my_home" then it will be directing users to the login_destination_postfacto function as a page.

5. login_destination_postfacto then gets the user AFTER LOGIN - which is to say - when the $user object is populated with information about this specific user. Then you can do fun stuff like a drupal_goto to the proper place.

Most of the things that I've done are pretty basic Drupal module code. I assure you, if they were complex they couldn't have come from me!

greggles’s picture

One more thought on this - you could use profile_module to store a page on the site that the user wants to have as their "home" and in the login_destination_postfacto function pull up that page from their profile and redirect them to that page. So, if a user wants to go to a certain forum every time they login, or they want to go to the issue queue, or an image gallery, whatever it is they could set that in their profile and get taken to that location when they login.

zxmon’s picture

Greggles et al: THANKS FOR POSTING THIS! I have been trying to do this for way too long, I couldn't believe my eyes when I finally saw a working solution!

I want to contribute something, so this extra bit is all I have to offer on the subject. Not sure if it's really a refinement, but I find it more intuitive to call roles by name instead of role ID#. Anyway, at least I'm contributing something I've learned along the way.

In my site, there is a "games" role, which phpmyadmin tells me is rid #3. So to call it by name instead of number ...

Instead of ...

if (in_array(3,array_keys($user->roles))) {

I used ...

if (is_array($user->roles) && in_array('games', $user->roles)) {

And it works!

So the final functions read like this in the (already patched) "login_destination.module"

function login_destination_postfacto() {
  //implement custom post-login redirection code here
  //also be sure to set your destination correctly to match this callback

  $dest = 'home';
  global $user;

//  if (in_array(3,array_keys($user->roles))) {
if (is_array($user->roles) && in_array('games', $user->roles)) {
    $dest = 'http://abcd.com/subsite01';
  }

//  if (in_array(4,array_keys($user->roles))) {
if (is_array($user->roles) && in_array('web2', $user->roles)) {
    $dest = 'http://abcdcom/subsite02';
  }
  drupal_goto($dest);
}
greggles’s picture

@zxmon - users will occasionally change the title of the permission which would then break your code. It's less likely that they will delete a permission, add a new one with the same name, and have it mean the same thing to the code. Hence, I prefer using the check for the role ID (even though it makes the code harder to understand at a first glance) because it's more likely to work for a longer amount of time.

armanjava’s picture

I'm not a coder but all i need is to redirect every user upon login to the home page, that's it. where and how can i put that url in? also, i'm using the logintoboggin module instead of the normal user login module.... will that need to be referenced differently? Thanks in advance for any help.

ardas’s picture

Try this new upgradet version of this module http://drupal.org/node/98836
There is an ability to put the URL you need right in settings page - very convenient and no need to patch sources.

Axel_V’s picture

I've uploaded the patch and had a look at the settings which is generally just a text field.
1. How do I put in different URLs for different user roles?
2. How can I force specific users onto a specific pageroute so that once they filled in a certain node type the next time they log-in they get normal access?
I'm not a coder either and would be happy for a prefab code snippet if anyone has done something similar
cheers, xl

ardas’s picture

You can now use PHP snippet setting for login_destination.module and enter there your specific logic which will determine where to login.

ardas’s picture

Hardcoding such logic in the module is a bad practice because it is very specific to your needs.
Please try to use your logic in PHP snippet (setting page). This will help you to customize your path. Be sure to return only relative path just like 'admin/node' or 'user'.

greggles’s picture

Status: Needs work » Closed (fixed)

So then close the status if you've implemented this.

I edited the php file because that was an expedient option. Obviously it's better to expose that but it wasn't a priority to me.

ardas’s picture

Okay.

Your idea with roles helped me to understand that PHP snippet is needed because there are so much different logics possible. I myself have several ones.:)

ardas’s picture

By the way, how did you change the issue status?
After Moshe gave me access rights to be this module mainteiner I saw an ability to change issue parameters only once :) Now, when I'm answering an issue there are no these parameters.

greggles’s picture

When you do a followup you should have lots of fields on the top of the page like Category, Priority, Assigned, Status, etc.