Block by time script
function toString(n){return (n < 10? '0' : '') + n;}
function tzOffset(s){
var i = s.indexOf(":");
return parseInt(s.substring(3, i)) * 60 + parseInt(s.substring(i+1));
}
// In the line below, replace <start_day> with the start of the working day of
the week,
// and <end_day> with the end of the working day of the week.
// Example: var workDays = ["1","5"];
var workDays = ["<start_day>","<end_day>"];
// Replace <start_time> with the start of business hours,
// and <end_time> with the end of business hours.
// Example: var officeHours = ["08:00:00","17:00:00"];
var officeHours = ["<start_time>","<end_time>"];
// Replace <time_zone> with the office time zone.
// Example: var officeTimeZone = "UTC-08:00";
var officeTimeZone = "<time_zone>";
if (workDays[0] == "<start_day>" || workDays[1] == "<end_day>"){
throw "<start_day> or <end_day> is not set. Please replace <start_day> and
<end_day> with the start and end of the working day of the week.";
}
var dayPatt = /^[0-6]$/i;
if (!workDays[0].match(dayPatt) || !workDays[1].match(dayPatt)){
throw "<start_day> or <end_day> is not properly set. Please use a number
between 0 and 6. 0 means Sunday and 6 means Saturday.";
}
if (parseInt(workDays[0]) > parseInt(workDays[1])){
throw "<start_day> or <end_day> is not properly set. Please make sure
<start_day> is no later than <end_day>.";
}
if (officeHours[0] == "<start_time>" || officeHours[1] == "<end_time>"){
throw "<start_time> or <end_time> is not set. Please replace <start_time>
and <end_time> with the start and end time of office hours.";
}
var timePatt = /^([01]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/i;
if (!officeHours[0].match(timePatt) || !officeHours[1].match(timePatt)){
throw "<start_time> or <end_time> is not properly set. Please use 24 hour
time notation 00:00:00 to 23:59:59.";
}
if (Date.parse("1970-01-01T"+officeHours[0]) > Date.parse("1970-01-01T"+officeHours[1])){
throw "<start_time> or <end_time> is not properly set. Please make sure
<start_time> is no later than <end_time>.";
}
if (officeTimeZone == "<time_zone>"){
throw "<time_zone> is not set. Please replace <time_zone> with the office
time zone.";
}
var tzPatt = /^UTC(\+|\-)([01]?[0-9]|2[0-3]):[0-5][0-9]$/i;
if (!officeTimeZone.match(tzPatt)){
throw "<time_zone> is not properly set. Please use format UTC-nn:nn or
UTC+nn:nn where -nn:nn or +nn:nn is the time zone's offset from UTC.";
}
trace("ipaddress: " + context.ipAddress);
if (context.onPrem){
trace("onprem");
var d = new Date();
d.setMinutes(d.getMinutes() + d.getTimezoneOffset() + tzOffset(officeTimeZone));
var curTime = d.getTime();
var curDay = d.getDay();
trace("current time: " + d.toLocaleString());
trace("curDay: " + curDay);
if (curDay < parseInt(workDays[0]) || curDay > parseInt(workDays[1])){
trace("block access - current day is not a working day.");
policy.Locked = true;
return;
}
var dateString = d.getFullYear() + '-' + toString(d.getMonth() + 1) + '-' + toString(d.getDate());
var startTime = Date.parse(dateString + 'T' + officeHours[0]);
trace("start time: " + dateString + 'T' + officeHours[0]);
var endTime = Date.parse(dateString + 'T' + officeHours[1]);
trace("end time: " + dateString + 'T' + officeHours[1]);
trace("curTime: " + curTime);
trace("startTime: " + startTime);
trace("endTime: " + endTime);
if (curTime < startTime || curTime > endTime){
trace("block access - current time is not within office hours.");
policy.Locked = true;
}
}
else {
trace("off premises");
trace("block access - user is currently not on premise.");
policy.Locked = true;
}