Skip to content

Commit

Permalink
chore: refactor out function for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
rvazarkar committed Jul 22, 2024
1 parent bb2abb2 commit 959e1ea
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions src/CommonLib/Processors/ComputerAvailability.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,29 +88,22 @@ await SendComputerStatus(new CSVComputerStatus
};
}

if (!_skipPasswordCheck)
if (!_skipPasswordCheck && !IsComputerActive(pwdLastSet, lastLogon))
{
var passwordLastSet = Helpers.ConvertLdapTimeToLong(pwdLastSet);
var lastLogonTimeStamp = Helpers.ConvertLdapTimeToLong(lastLogon);
var threshold = DateTime.Now.AddDays(_computerExpiryDays * -1).ToFileTimeUtc();

if (passwordLastSet < threshold && lastLogonTimeStamp < threshold)
_log.LogDebug(
"{ComputerName} is not available because password last set and lastlogontimestamp are out of range",
computerName);
await SendComputerStatus(new CSVComputerStatus
{
Status = ComputerStatus.NotActive,
Task = "ComputerAvailability",
ComputerName = computerName
});
return new ComputerStatus
{
_log.LogDebug(
"{ComputerName} is not available because password last set and lastlogontimestamp are out of range",
computerName);
await SendComputerStatus(new CSVComputerStatus
{
Status = ComputerStatus.NotActive,
Task = "ComputerAvailability",
ComputerName = computerName
});
return new ComputerStatus
{
Connectable = false,
Error = ComputerStatus.NotActive
};
}
Connectable = false,
Error = ComputerStatus.NotActive
};
}

if (_skipPortScan)
Expand Down Expand Up @@ -153,6 +146,20 @@ await SendComputerStatus(new CSVComputerStatus
};
}

/// <summary>
/// Checks if a computer's passwordlastset/lastlogontimestamp attributes are within a certain range
/// </summary>
/// <param name="pwdLastSet"></param>
/// <param name="lastLogonTimestamp"></param>
/// <returns></returns>
private bool IsComputerActive(string pwdLastSet, string lastLogonTimestamp) {
var passwordLastSet = Helpers.ConvertLdapTimeToLong(pwdLastSet);
var lastLogonTimeStamp = Helpers.ConvertLdapTimeToLong(lastLogonTimestamp);
var threshold = DateTime.Now.AddDays(_computerExpiryDays * -1).ToFileTimeUtc();

return passwordLastSet >= threshold || lastLogonTimeStamp >= threshold;
}

private async Task SendComputerStatus(CSVComputerStatus status)
{
if (ComputerStatusEvent is not null) await ComputerStatusEvent.Invoke(status);
Expand Down

0 comments on commit 959e1ea

Please sign in to comment.