Redirecting Users on Login IV

Redirect users on the basis of user-group membership using a user-group setting


In the last article, we saw how to redirect users after login based on a user setting. In this one, we'll see how to do it on the basis of their membership in a user group. Since we don't know who the user is until *after* they've logged in, we need to put the code in a plugin connected to the OnWebLogin System Event.


Redirecting Users Based on a User-Group Setting

There are two logical places to put code for redirecting users after they login. One is in a snippet that you designate as a Login postHook in the Login tag. The other is in a plugin attached to the OnWebLogin System Event. Either one works, but I prefer doing it in a plugin because the $user object is immediately available to you, and you don't have to worry about potential interference from other Login hooks. The code would be essentially the same for this example.

In this article, we'll redirect the user after login based on a resource ID stored in a user-group setting. The code is very simple, but first, lets look at creating the user-group setting.

Creating a User-Group Setting

You might think that you could do this by going to Content -> User Groups on the Manager's Top Menu. Unfortunately, user-group settings aren't available there at this writing. Here's what you need to do:

  • Go to System (Gear Icon) -> Access Control Lists
  • Click on the "User Groups & and Users" tab if you're not there already
  • Right-click on a user group and select "Update User Group"
  • Click on the the Settings tab, then on the "Create New" button
  • Enter LoginResource in both the Key and Name fields
  • In the Value field, put the ID of the resource you'd like to forward the group's users to on login
  • Click on the Save button at the upper right to save the user group
  • Repeat for any other groups

The Plugin Code

This code should look familiar. It's similar to the code we used in the last article, but with the addition of the lines to get the user-group setting.

This plugin code (we'll call it LoginRedirect) is quite simple. The Plugin is attached to the OnWebLogin event. As soon as a user has successfully logged on in the front end of the site, MODX invokes the OnWebLogin System Event and our plugin code executes. The code gets the user setting and forwards the user to the resource with that ID:

/* LoginRedirect plugin */

/* Get the user-group setting */

$settings = $modx->user->getUserGroupSettings('LoginResource');

$docId = $modx->getOption('LoginResource', $settings, '', true);

if (empty($docId)) {
    return '';
}
/* Create the URL */
$url = $modx->makeUrl($docId, "", "", "full");

/* Forward the User */
$modx->sendRedirect($url);

Note that the groups are checked in order of their rank. The group with the lowest rank is considered the user's primary group and any settings for that group will take precedence. The other groups will be checked in order of their rank (ascending) and the last group with the setting set will provide the value if the setting is not set for the primary group.

At this writing, there's no way to set a user group's rank without editing the database, so they will be generally be checked in the order they were created, with the oldest group being the primary group.

I haven't tested this, but Login postHooks will probably not execute if you use this technique. Login preHooks should work fine.

If you want to set a default page to send the user to if the setting is not set for any groups they belong to you can set a default in the $docId line. For example, if you want to send them to page 12 by default:

$docId = $modx->getOption('LoginResource', $settings, 12, true);

Coming Up

This technique works great if users will belong to only one user group, but it's tricky when users belong to more than one group and you want to set up a more complex test based on their user-group memberships. In the next article, we'll see how to redirect users based on user-group membership when you want to test the groups in a particular order without regard to their rank.

 

Comments (0)


Please login to comment.

  (Login)