Jump to content

Coders unite? Need a little help - Adding IP Address and Time checks


Recommended Posts

  • 3 weeks later...
Posted (edited)

So I am back to working on this because it does not seem to be working completely.  WE ran into that we have a range of IPs.  I tried to edit it as below.  It does not seem to hide widget.  I am also getting a malformed java error.  Thoughts

Matt

5ad63d3c837c1_ScreenShot2018-04-17at2_27_54PM.png.3948bd2ab0c6bde073a99a279b00a519.png

@Graham Quince

@pconkie

Edited by mmclaughlin
Posted

If I was javascript, I would probably not like

image.png.4b73050a9ca1f6d126408844ea5a56dc.png

You are trying to use an ip address as though it was a number and comparing it to ip[0] which is a string

10 is a number

10.0 is a number

10.0.112 is not a valid number

10.0.112.1 is not a valid number

so ......

var r = /\b(\d{1,3}).(\d{1,3}).(\d{1,3})\b/;

// capture the individual parts of the ip address

var ip = data.match(r);

if (ip && ip[1] === '10' && ip[2] === '0' && (parseInt(ip[3]) >= 112 && parseInt(ip[3]) <= 127)) {

 

Posted

So I replaced the if statement with 

4 hours ago, Simon Law said:

if (ip && ip[1] === '10' && ip[2] === '0' && (parseInt(ip[3]) >= 112 && parseInt(ip[3]) <= 127)) {

 

Does not seem to have worked.

Posted
8 minutes ago, mmclaughlin said:

So I replaced the if statement with 

Does not seem to have worked.

The regex has also changed and you need that too

Where can I see the widget working/not working (site/page)?

Posted (edited)
<script>
    ///////////////////////////////////////
    //logic:
    /////Always hide widget.. 
    ////Show widget if all criteria met
    ///////////////////////////////////////
    
    //declare and set vars
    //change these values as required
  //SL my widget has different uuid
    var widget_to_hide = "7E50015020028EF283501F8FD1F30A0096EE04DCC9DB2497";
    var school_ip_lower = "10.0.112.1";
    var school_ip_upper = "10.0.127.254";
    var format = 'hh:mm:ss';
    var time = moment();
    var beforeTime = moment('07:30:00', format);
    //SL I am working late so I set for late night
    var afterTime = moment('19:00:00', format);
    var currentuser = FrogOS.getUser();
    var dayOfWeek = moment().day();
    //SL made theWidget so I could use it again and again
    var theWidget = $("div[data-content-uuid='"+widget_to_hide+"']");
    //SL added debugger so I could step through in dev tools
    debugger;
    //hide widget
    theWidget.hide();
    //SL logged in as not a student for testing
    if (currentuser.profile.type == "student") {
        //they are not a student - show the widget
        theWidget.show();
    } else {
        //async load of users school ip address...waits for response...
        $.get( "../ipaddress.php", function( data ) {
//SL added debugger so I don't have to step through ipaddress call
 debugger;
 var r = /\b(\d{1,3}).(\d{1,3}).(\d{1,3})\b/;
 var ip = data.match(r);
// capture the individual parts of the ip address

// my ip was 192.168.99 so I had to if condition
if (ip && ip[1] === '192' && ip[2] === '168' && (parseInt(ip[3]) >= 90 && parseInt(ip[3]) <= 99)) {
            
                //they are at school
                //if (dayOfWeek === 1 || dayOfWeek === 2 || dayOfWeek === 3 || dayOfWeek === 4 || dayOfWeek === 5) {
  				//SL cleaner to use inArray
                if ($.inArray(dayOfWeek, [1,2,3,4,5])) {
                    //0=sunday, 1=monday etc BUT perhaps this isn't correct for US?
                    //it should be Mon-Fri
                    if (time.isBetween(beforeTime, afterTime)) {
                        //it's within school hours
                        //finally show the widget!
                        theWidget.show();
                    }
                }
            } 
        });
    } 
</script>

I had to alter code see //SL but it worked for me

Edited by Simon Law
added more comments and extra debugger
Posted

I suggest you add the debugger line, open up dev tools and step through the code to see where it is failing, because the code I just submitted worked

Posted

Used the code... I changed the time. and I changed the 192 to 10 the 168 to 0 the 90 to 112 and the 99 to 127.  I also made sure to target the correct uuid for the site.  

See below for the changes.

I also change the == to a !== to make it disappear for the student and appear for staff.  It now is in a state of disappear and I don't see it for student thought I would be in the correct ip.  Will try to work with someone on it some more.  I understand your code pretty well and it should be working. 

Matt

 

<script>
    ///////////////////////////////////////
    //logic:
    /////Always hide widget.. 
    ////Show widget if all criteria met
    ///////////////////////////////////////
    
    //declare and set vars
    //change these values as required
    var widget_to_hide = "A1209E3C20028742D15E5F56768642005CF72A5C3CA5F865";
    var school_ip_lower = "10.0.112.1";
    var school_ip_upper = "10.0.127.254";
    var format = 'hh:mm:ss';
    var time = moment();
    //time is in UTC
    var beforeTime = moment('11:30:00', format);
    //SL I am working late so I set for late night
    var afterTime = moment('19:00:00', format);
    var currentuser = FrogOS.getUser();
    var dayOfWeek = moment().day();
    //SL made theWidget so I could use it again and again
    var theWidget = $("div[data-content-uuid='"+widget_to_hide+"']");
    //SL added debugger so I could step through in dev tools
    debugger;
    //hide widget
    theWidget.hide();
    //SL logged in as not a student for testing
    if (currentuser.profile.type !== "student") {
        //they are not a student - show the widget
        theWidget.show();
    } else {
        //async load of users school ip address...waits for response...
        $.get( "../ipaddress.php", function( data ) {
                      
 var r = /\b(\d{1,3}).(\d{1,3}).(\d{1,3})\b/;
 var ip = data.match(r);
// capture the individual parts of the ip address

// see above for the range
if (ip && ip[1] === '10' && ip[2] === '0' && (parseInt(ip[3]) >= 112 && parseInt(ip[3]) <= 127)) {
              //they are at school
                //if (dayOfWeek === 1 || dayOfWeek === 2 || dayOfWeek === 3 || dayOfWeek === 4 || dayOfWeek === 5) {
                  //SL cleaner to use inArray
                if ($.inArray(dayOfWeek, [1,2,3,4,5])) {
                    //0=sunday, 1=monday etc BUT perhaps this isn't correct for US?
                    //it should be Mon-Fri
                    if (time.isBetween(beforeTime, afterTime)) {
                        //it's within school hours
                        //finally show the widget!
              
                        theWidget.show();
                    }
                }
            } 
        });
    } 
</script>

  • Graham Quince changed the title to Coders unite? Need a little help - Adding IP Address and Time checks
  • 3 weeks later...
  • 5 months later...
Posted

Hi

This code is almost exactly what I need. - can anyone help me with amendments to achieve the following. We would like to automatically show the tutor dashboard to tutors during tutor time. all other staff would go to the default staff dashboard

 

so for........

Staff in usergroups "tutor year 7" & 'Tutor year group 8' etc

logged in on School IP address 

Mon - Fri

between 8.30 and 8.55

show the Tutor Time dashboard rather than the default staff dashboard

 

I would guess that the showing of a different site could be dealt with using rules but how do change the code to  show a frog site rather that 'not show a widget' as it is currently written

 

Any help gratefully received

 

Ann

 

 

Posted
38 minutes ago, ann said:

Hi

This code is almost exactly what I need. - can anyone help me with amendments to achieve the following. We would like to automatically show the tutor dashboard to tutors during tutor time. all other staff would go to the default staff dashboard

 

so for........

Staff in usergroups "tutor year 7" & 'Tutor year group 8' etc

logged in on School IP address 

Mon - Fri

between 8.30 and 8.55

show the Tutor Time dashboard rather than the default staff dashboard

 

I would guess that the showing of a different site could be dealt with using rules but how do change the code to  show a frog site rather that 'not show a widget' as it is currently written

 

Any help gratefully received

 

Ann

 

 

You could always use a 100% theme @Graham Quince has and then nest each dashboard..... and hide a widget depending on time etc?

Posted
5 hours ago, ann said:

Hi

This code is almost exactly what I need. - can anyone help me with amendments to achieve the following. We would like to automatically show the tutor dashboard to tutors during tutor time. all other staff would go to the default staff dashboard

 

so for........

Staff in usergroups "tutor year 7" & 'Tutor year group 8' etc

logged in on School IP address 

Mon - Fri

between 8.30 and 8.55

show the Tutor Time dashboard rather than the default staff dashboard

 

I would guess that the showing of a different site could be dealt with using rules but how do change the code to  show a frog site rather that 'not show a widget' as it is currently written

 

Any help gratefully received

 

Ann

 

 

Hi @ann

I'd be really nervous about trying to automate a dashboard change.  Would it not be easier to provide links to the Tutor sections? Seeing as it's only for a short time each day.  

Graham

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...