2

Is it possible, using Apache, Javascript, or CSS (or something in Plone for that matter) to make a <div> visible to only a set range of IP addresses?

Below is the code I would like to be available to a certain range of IP addresses:

<div id="user">
    <ul>
        <li><a href="#" title="Login">Login</a></li>
    </ul>
</div>

Ideally, I would like this to only be visible from 3 sets of IP addresses. I have a separate site running the following restriction (IP addresses changed, of course) in Apache to control access, but to a whole site, rather than just a div.

Order allow,deny
   Allow from 000.0.00
   Allow from 000.0.01

      # Order deny,allow
      # Deny from all

The purpose of this would be to restrict a pop-up login (or login page) to anyone not accessing the site from within a certain location. Even if I could just hide the "user" div (which is the login button when not logged in) to anyone not accessing from the range of IP addresses, that would be fine. Any help would be greatly appreciated, thanks!

raytheengineer
  • 109
  • 2
  • 11
  • Why exactly do you want this? Hope it's not a kind of protection(security)? – GuyT Aug 01 '14 at 09:59
  • ideally i think that should happen on server side while rendering the HTML. I think IP address are lower level details which should not get to client side i.e. JS/CSS. – Anil Namde Aug 01 '14 at 10:00
  • It is not really for security, as anyone with the url could get to the login page, it is more to make it a little less obvious that the login is available unless in a certain building. PHP is not available, as far as I am aware. – raytheengineer Aug 01 '14 at 10:06
  • What about doing it based on URL? So that if someone visits www.mysite.com, it is not visible, but if they were to visit www.mysiteeditor.com/mysite, it would be? – raytheengineer Aug 01 '14 at 10:17
  • IIRC, you should be able to switch (Diazo) themes on URL, and you could use Diazo to strip out the stock element on your (slightly adjusted) public theme. There might be something in Plone theming docs on this. – sdupton Aug 01 '14 at 17:08

3 Answers3

2

You can use Products.AutoRole to assign a group automatically to visitors from certain IP ranges, and then adapt your Plone templates so that your div is conditional on that group. (And for your ultimate problem, we should probably file a feature request that "can log in" should be tied to a permission, which by default would be granted to anonymous.)

FWIW, at work, we also restrict /login|/login_form in the apache config.

Ulrich Schwarz
  • 7,598
  • 1
  • 36
  • 48
  • In a lot of environment the ip address can be hidden by front-end software. In that case I suggest you to use Products.AutoRoleFromHostHeader – keul Aug 01 '14 at 17:23
1

You need some css:

#user {
   display: none;
}

And some js:

<script type="application/javascript">
    function getip(json){
      if(json.ip === "127.0.0.1") {
         document.getElementById('user').style.display = 'block';
      }
    }
</script>

<script type="application/javascript" src="http://jsonip.appspot.com/?callback=getip"></script>

I got this from here: https://stackoverflow.com/a/810461/3132718

Now the div won't be visible on the page by default but could be still seen by viewing the source code.

Community
  • 1
  • 1
Al.G.
  • 4,327
  • 6
  • 31
  • 56
0
*multiple class hiding*
<script type="application/javascript">
function getip(json){
  if(json.query == "87.1.1.5") {
    var elems = document.getElementsByClassName('price');
    for (i = 0; i < elems.length; i++) {
    elems[i].style.display = 'block';
     }
  }
}

<script type="application/javascript" src="http://ip-api.com/json/?callback=getip"></script>