Redirecting Users On Login VII

Use group IDs to speed up the membership test


As I mentioned in the last article, having to get each user group object just to get its ID is kind of wasteful. In this one, we'll add the group IDs to the plugin to speed it up. It works exactly as the last one did, only faster.


Redirecting Users Based on a User Group Setting

In this article, we'll redirect the user after login based on a resource ID stored in a user group setting, but we'll control the order in which the groups are checked. We'll speed it up by adding the user group ID to the snippet.

We're investing our own time by looking up the group IDs ourselves just once, rather than having the plugin get them from the database with a specific query for each group every time it runs. We'll get our investment back many times over in page-load speed every time a user logs in.


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

To use user group settings, but control the order in which they are checked, we'll need to get the settings directly from the database. We'll also check for a user setting before we check the groups. That will allow us to override the plugin's behavior for individual users by creating a LoginResource user setting for them.

/* LoginRedirect plugin */


/* Set ID of default page to redirect to */
$default = 12;

/* Set up array of groups to check - in the order we want them checked.
   Group name on the left; group ID on the right. */

$groups = array(
    'Editors' => 14,
    'Publishers' => 17,
    'Members' => 22,
);

$sendTo = $default;

/* Check for a user setting */
$userId = $user->get('id');
$searchCriteria = array(
    'user' => $userId,
    'key' => 'LoginResource',
);
$userSetting = $modx->getObject('modUserSetting', $searchCriteria);

if ($userSetting) {
    $docId = $userSetting->get('value');
} else {
    /* No user setting, check groups */
    foreach($groups as $groupName => $groupId) {

        /* See if user is a member */
        if ($user->isMember($groupName)) {

                /* Get setting value */
                $settingCriteria = array(
                    'key' => 'LoginResource',
                    'group' => $groupId,
                );
                $setting = $modx->getObject('[LoginRedirect plugin] modUserGroupSetting', $settingCriteria);
                if ($setting) {
                    $value = $setting->get('value');
                    if (! empty($value)) {
                        $sendTo = (int) $value;
                        break;
                    }
                } else {
                    $modx->log(modX::LOG_LEVEL_ERROR, '[LoginRedirect plugin] Could not find LoginResource user group setting for user group: ' . $groupName);
                }
        }
    }
}

/* Create the URL */
$url = $modx->makeUrl($sendTo, "", "", "full");

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

We've removed the whole section that retrieved the user group object. In fact we never needed that object except to get its ID, which we've now put in the $groups array. Also gone, though, is our error message if the group is not found (though we still log an error message if the group doesn't have our setting). Neither the user group, nor the group ID is tested for validity. We could check them by getting the user group object, but that would slow things down again. Because we're not checking these, we need to be very careful when entering them, and test to make sure that users are redirected to the appropriate page. If they're not, the first thing to check is the $groups array. This would be a perfect case for unit testing, but that's a subject for another article.


Coming Up

Are you getting tired of this topic? I certainly am. I only have the energy for one more of these. In the next article, we'll look at redirecting the user to a specific Manager page when they log into the MODX Manager.



Comments (0)


Please login to comment.

  (Login)