Added time rule flags

This commit is contained in:
Alex Rennie-Lis
2024-06-06 23:41:46 +01:00
parent 8265e89d69
commit a3e0cc381b
2 changed files with 40 additions and 32 deletions

View File

@@ -41,40 +41,45 @@ const resolveRange = (str) => {
module.exports = {
checkAuth: (rules = []) => {
var authorised = false;
if (rules.length > 0) {
var now = new Date ();
var minuteOfDay = (now.getHours () * 60) + now.getMinutes (); // 0 - 1439
var day = now.getDay (); // 1 - 7
var date = now.getDate (); // 1 - 31
var month = now.getMonth () + 1; // 1 - 12
var actions = [];
rules.forEach ((rule) => {
var valid = false;
// Process rule
var r = {
startMinute: parseInt (rule.startTime.split (":")[0] * 60) + parseInt (rule.startTime.split (":")[1]),
endMinute: parseInt (rule.endTime.split (":")[0] * 60) + parseInt (rule.endTime.split (":")[1]),
days: resolveRange (rule.weekdays),
dates: resolveRange (rule.dates),
months: resolveRange (rule.months)
if (config.time_rules_enabled) {
if (rules.length > 0) {
var now = new Date ();
var minuteOfDay = (now.getHours () * 60) + now.getMinutes (); // 0 - 1439
var day = now.getDay (); // 1 - 7
var date = now.getDate (); // 1 - 31
var month = now.getMonth () + 1; // 1 - 12
var actions = [];
rules.forEach ((rule) => {
var valid = false;
// Process rule
var r = {
startMinute: parseInt (rule.startTime.split (":")[0] * 60) + parseInt (rule.startTime.split (":")[1]),
endMinute: parseInt (rule.endTime.split (":")[0] * 60) + parseInt (rule.endTime.split (":")[1]),
days: resolveRange (rule.weekdays),
dates: resolveRange (rule.dates),
months: resolveRange (rule.months)
}
if (
minuteOfDay >= r.startMinute &&
minuteOfDay <= r.endMinute &&
r.days.indexOf (day) !== -1 &&
r.dates.indexOf (date) !== -1 &&
r.months.indexOf (month) !== -1
) {
actions.push (rule.action.toLowerCase ());
}
});
if (actions.indexOf ("allow") !== -1) {
authorised = true;
}
if (
minuteOfDay >= r.startMinute &&
minuteOfDay <= r.endMinute &&
r.days.indexOf (day) !== -1 &&
r.dates.indexOf (date) !== -1 &&
r.months.indexOf (month) !== -1
) {
actions.push (rule.action.toLowerCase ());
if (actions.indexOf ("deny") !== -1) {
authorised = false;
}
});
if (actions.indexOf ("allow")) {
authorised = true;
}
if (actions.indexOf ("deny")) {
authorised = false;
}
}
else {
authorised = true;
}
return authorised;
}
}