June 5, 2009

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

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.

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.