Drupal node access and redirect of users based on role and username

// June 5th, 2009 // Code

My boss asked me to start setting up a client area on our company website where we could share documents with our clients and also do some minor commenting about the documents. It was also a requirement that the clients would be taken to their own specific client page without access to any other clients page. Therefore I installed the ACL and content_access module to be able to add node access on a user basis. The ACL module allows for user specific listings and the content_access module integrates with ACL and allows for the restrictions we need. Thus far, all good.

Small problem came up when I wanted to redirect the users after logging in using the login_destination module. The module allows for PHP snippets to be inserted to be able to do redirecting.

global $user;
if ($user && !in_array('administrator', $user->roles)) {
// Redirect the everyone who is not administrators
return $user->name;
}
else {
return 'admin';
}

This makes sure that all users who are not admins are redirected to a page with the same path as their username and it takes all admins to the admin page.

—- UPDATE FOR ERROR PAGES —–
It hit me that it could be a good idea to redirect users back to their front page since this system wont have a user navigation. Of course the admins would want to go back to the admin page.

I set up an “error” page as a normal page and activaded the php filter module. Then in the settings i set both the 403 and 404 page (the non access and no file found pages) to the same page as to not give away if the page/client exists or not. On that page I typed in the following code.

<?php
global $user;
function getPage()
{
if ($user && !in_array('administrator', $user->roles)) {
// Redirect the everyone who is not administrators
return $user->name;
}
else {
return 'admin';
}
}
$page = getPage();
echo '<h1>'.$user->name.'</h1>';
echo "<p>Du har kommit till en sida som ligger utanf&ouml;r kundsystemet. Om 5 sekunder kommer sidan automatiskt att g&aring; tillbaka till f&ouml;rsta sidan.</p>";
echo "<script type='text/javascript'>";
echo "var user = '".$page."';";
echo "var time = null;";
echo "function move(){var newPage = 'http://customer.doppio.se/' + user;window.location = newPage;}";
echo "timer=setTimeout('move()',5000);</script>";
?>

Comments are closed.