Most user redirection on login takes place in the front-end, but someday you may want to redirect 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.
[code language=”php”]
/* LoginRedirect plugin */
/* Set URL of default page to redirect to */
$url = ‘http://yoursite.com/manager’;
/* Set the criteria for the setting search */
$searchArray = array(
‘user’ => $user->get(‘id’);
‘key’ => ‘LoginResourceUrl’,
);
/* Attempt to get the user setting */
$setting = $modx->getObject(‘modUserSetting’, $searchArray);
if ($setting) {
$value = $setting->get(‘value’);
if (! empty ($value) {
$url = $value;
}
}
$modx->sendRedirect($url);
[/code]
Note that we no longer use $modx->makeUrl()
since we already have the URL. If the user has no setting, they’ll be sent to the default dashboard (the Manager’s home page).
Redirecting on the Basis of User Group Membership
If you have places in the Manager that you’d like to send members of a user group to, we can use the slightly modified code from Article VII in this series. Again, get the URL of the page by visiting it in the Manager and copying the URL from the browser’s address bar. The URLs go in the array at the beginning of the plugin.
[code language=”php”]
/* Set URL of default page to redirect to */
$url = ‘http://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’ => ‘http:yoursite.com/manager/?a=resource/update&id=17’,
‘Publishers’ => http:yoursite.com/manager/?a=resource/update&id=22,
‘Members’ => http: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);
[/code]
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.
For more information on how to use MODX to create a web site, see my web site Bob’s Guides, or
better yet, buy my book: MODX: The Official Guide.
Looking for quality MODX Web Hosting? Look no further than Arvixe Web Hosting!