Attention

This document was written for an unsupported version of Plone, Plone 2.1.x, and was last updated 1239 days ago.

For more information, see the version support policy.

To learn how to upgrade to the current version of Plone, read the upgrade manual.

Show or hide a tab based on IP address

by Adrian James last modified Dec 30, 2008 03:02 PM
Shows you how to show or hide a portal tab based on the client IP address. Useful for intranet environments.
I was asked to modify our intranet so that a portal tab would display if (and only if) you are sitting in a particular office. This had to apply for anonymous users, so using security was not an option. On the flipside, this is NOT a solution to secure content, it affects the display only.

The one thing we have specific to each office is the network address of the client machine. Specifically, the subnet portion of the IP address.

I knew that I had to use the "condition" field on the portal action, so after many hours of searching and reading (which I hope saves at least one person the same amount of trouble), I came up with the following.

Simply put the following string as a condition on your portal action -

python:request.getClientAddr()[:12] == '192.168.100.'

This makes the tab appear for any client machine that has an IP address starting with 192.168.100

The [:12] is a count of the number characters in the quoted string. You could also use -

python:request.getClientAddr()[:10] == '192.168.1.'

You get the idea. I am sure you could use other operators, it just takes some experimenting.

This has been tested on 2.0.x and 2.1, but nothing earlier. I believe the getClientAddr() API was new at some stage, so this may not work on earlier versions.


Contribute

Something wrong or out of date? Anybody can edit or create a new article in the knowledge base. Simply create an account on this site, log in, and click the Edit button to contribute.