Redirecting Users On Login VIII

Redirect users to a Manager page on login


Most user redirection on login takes place in the front-end, but someday you may want to redirect Manager users when they log in. Say, for example, that you have a user who only updates one page. It would be a kindness to redirect that user to the Create/Edit Resource page for that resource when they log in. They would no longer have to find the page in the tree and navigate to it. We'll see how in this article.


You can actually use any of the methods from the previous seven articles to redirect the user. The key is that instead of a resource ID, you need to use the full URL of a Manager page. In the previous articles, we used $modx->makeURL() to generate the URLs, but if you already have the URLs you can use them directly in $modx->sendRirect(). The URL itself can be in a user setting, a user group setting, or in the code of the plugin.


Getting the URL

This is the easiest part. Just go to the Manager page you want to send the user to and cut-and-paste the URL from the browser's address bar to wherever you want to store it. If you're using a setting, we'll call it LoginResourceUrl. It will contain a full URL instead of a resource ID number.


Redirecting Users Based on a User Setting

This code will be very similar to the code from the first article in this series. We'll change a few variable names to make it clear what we're doing, and will skip the $modx->makeUrl() step.

/* Set URL of default page to redirect to */
$url = 'https://yoursite.com/manager';

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

$groups = array(
    'Editors' => 'https:yoursite.com/manager/?a=resource/update&id=17',
    'Publishers' => 'https:yoursite.com/manager/?a=resource/update&id=22',
    'Members' => 'https:yoursite.com/manager/?a=resource/update&id=34',
);

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

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

        /* See if user is a member */
        if ($user->isMember($groupName)) {
            /* We're done -- set URL  */
            $url = $pageUrl;
            break;
        }
    }
}

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

We check for a user setting and use it if it exists. If not, we use the URL from the $group array for the first group the user is a member of. It there's no user setting and the user is not a member of any groups, the default URL will be used.


Coming Up

This article is the end of this series (at least for now), although there are many other ways to redirect a user and more tricks to use along the way. We could, for example, put the data that's in the $groups array in the code above in the plugin's property set instead, and parse it in the plugin. That way there would be no need to modify code to change things. I'm probably even more tired of this topic than you are, though, so let's move on.

In the next article, we'll switch to another topic. We'll look at a method for finding the offending code that is producing a mysterious error message.



Comments (2)

  1. Susan OttwellMar 20, 2017 at 09:41 PM

    It looks like you've got two too many closing braces in that code.

  2. Bob RayMar 22, 2017 at 10:30 AM

    Fixed. Thanks!


Please login to comment.

  (Login)