Use created when there is no logs to find expire date

This commit is contained in:
Martijn Scheepers
2026-01-22 10:14:24 +01:00
parent 7938e8ba0d
commit 4f9e01939e
7 changed files with 158 additions and 58 deletions

View File

@@ -36,22 +36,8 @@ namespace Aperio_Control_Centre.ACSDatabase.UnitTest
Assert.IsTrue(user.IsWatched);
}
//[TestMethod]
//public void WatchedTelegramTest()
//{
// User user = new()
// {
// };
// Assert.IsFalse(user.IsWatched);
// Assert.IsFalse(user.IsWatchedTelegram);
// user.IsWatchedTelegram = true;
// Assert.IsTrue(user.IsWatched);
// Assert.IsTrue(user.IsWatchedTelegram);
//}
[TestMethod]
public void ExpiryTest()
public void NeverExpiryTest()
{
User user = new()
{
@@ -67,5 +53,96 @@ namespace Aperio_Control_Centre.ACSDatabase.UnitTest
Assert.IsNull(user.Expires);
Assert.IsFalse(user.NeverExpires);
}
[TestMethod]
public void ExpiryDate_NoLog_Test()
{
User user = new()
{
Created = new(2025, 1, 1, 0, 0, 0),
ExpiryDays = 30,
NeverExpires = false
};
Assert.IsNull(user.Expires);
Assert.IsFalse(user.NeverExpires);
Assert.AreEqual(new(2025, 1, 31, 0, 0, 0), user.ExpiryDate);
Assert.IsFalse(user.IsExpiredwithTime(new(2025, 1, 10, 0, 0, 0)));
Assert.IsTrue(user.IsExpiredwithTime(new(2025, 2, 10, 0, 0, 0)));
}
[TestMethod]
public void ExpiryDate_WithLog_Test()
{
User user = new()
{
Created = new(2025, 1, 1, 0, 0, 0),
ExpiryDays = 30,
NeverExpires = false,
Loggings = [
new(){
Access = AccessTypes.Granted,
Time = new(2025, 1, 31, 0, 0, 0)
}
]
};
Assert.IsNull(user.Expires);
Assert.IsFalse(user.NeverExpires);
Assert.AreEqual(new(2025, 3, 2, 0, 0, 0), user.ExpiryDate);
Assert.IsFalse(user.IsExpiredwithTime(new(2025, 1, 10, 0, 0, 0)));
Assert.IsTrue(user.IsExpiredwithTime(new(2025, 3, 10, 0, 0, 0)));
}
[TestMethod]
public void ExpiryDate_Neverexpires_Test()
{
User user = new()
{
Created = new(2025, 1, 1, 0, 0, 0),
ExpiryDays = 30,
NeverExpires = true,
Loggings = [
new(){
Access = AccessTypes.Granted,
Time = new(2025, 1, 31, 0, 0, 0)
}
]
};
Assert.AreEqual(ExpiryType.Never, user.Expires);
Assert.IsTrue(user.NeverExpires);
Assert.IsNull(user.ExpiryDate);
Assert.IsFalse(user.IsExpiredwithTime(new(2025, 1, 10, 0, 0, 0)));
Assert.IsFalse(user.IsExpiredwithTime(new(2025, 3, 10, 0, 0, 0)));
}
[TestMethod]
public void LastUsed_Test()
{
User user = new()
{
Created = new(2025, 1, 1, 0, 0, 0),
ExpiryDays = 30,
NeverExpires = true,
Loggings = [
new(){
Access = AccessTypes.Granted,
Time = new(2025, 1, 31, 11, 59, 0)
},
new(){
Access = AccessTypes.Granted,
Time = new(2025, 1, 30, 11, 00, 0)
},
new(){
Access = AccessTypes.Granted,
Time = new(2025, 1, 1, 1, 55, 0)
}
]
};
Assert.AreEqual(new(2025, 1, 31, 11, 59, 0), user.LastUsed);
Assert.AreEqual(5, user.LastUsedDaysWithTime(new(2025, 2, 5, 0, 0, 0)));
}
}
}

View File

@@ -140,6 +140,7 @@ namespace Aperio_Control_Centre.ACSDatabase.Models
}
[NotMapped]
[Display(Name = nameof(NeverExpires))]
public bool? NeverExpires
@@ -170,12 +171,13 @@ namespace Aperio_Control_Centre.ACSDatabase.Models
{
return null;
}
Logging? log = Loggings?.Where(c => c.Access != null).OrderByDescending(t => t.Time).FirstOrDefault();
if (log == null)
DateTime? last = LastUsed;
if (last == null)
{
return null;
//no access logging, use create date
return Created.AddDays(ExpiryDays).Date;
}
return log.Time.AddDays(ExpiryDays).Date;
return last.Value.AddDays(ExpiryDays).Date;
}
}
@@ -185,16 +187,17 @@ namespace Aperio_Control_Centre.ACSDatabase.Models
{
get
{
if (ExpiryDate != null)
{
return ExpiryDate < DateTime.UtcNow;
}
return false;
return IsExpiredwithTime(DateTime.UtcNow);
}
}
public bool IsExpiredwithTime(DateTime dateTime)
{
if (ExpiryDate != null)
{
return ExpiryDate < dateTime;
}
return false;
}
[NotMapped]
[Display(Name = nameof(LastUsed))]
@@ -217,15 +220,19 @@ namespace Aperio_Control_Centre.ACSDatabase.Models
{
get
{
DateTime? lastused = LastUsed;
if (lastused != null)
{
TimeSpan? days = DateTime.UtcNow - lastused;
return Math.Round(days.Value.TotalDays, 0);
}
return null;
return LastUsedDaysWithTime(DateTime.UtcNow);
}
}
public double? LastUsedDaysWithTime(DateTime dateTime)
{
DateTime? lastused = LastUsed;
if (lastused != null)
{
TimeSpan? days = dateTime - lastused;
return Math.Round(days.Value.TotalDays, 0);
}
return null;
}
}
}

View File

@@ -37,7 +37,7 @@ namespace Aperio_Control_Centre.Controllers
.Where(g => g.UserGroups.Any(a => usergroupsSelected!.Contains(a.Id)))
.Include(d => d.Department)
.Include(g => g.UserGroups)
.Include(l => l.Loggings!.OrderByDescending(d => d.Time).Take(25))
.Include(l => l.Loggings!.Where(l => l.Access != null).OrderByDescending(d => d.Time).Take(25))
.OrderBy(p => p.PassNumber)
.ToListAsync(token);
@@ -701,7 +701,7 @@ namespace Aperio_Control_Centre.Controllers
.Where(g => departmentsSelected!.Contains(g.DepartmentId!.Value))
.Include(d => d.Department)
.Include(g => g.UserGroups)
.Include(l => l.Loggings!.OrderByDescending(d => d.Time).Take(25))
.Include(l => l.Loggings!.Where(l => l.Access != null).OrderByDescending(d => d.Time).Take(25))
.OrderBy(p => p.PassNumber)
.ToListAsync(token);

View File

@@ -11,6 +11,7 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Localization;
using System.Data;
using System.Diagnostics;
using System.Security.Principal;
using System.Threading;
@@ -213,20 +214,26 @@ namespace Aperio_Control_Centre.Controllers
await foreach (var user in db.Users
.Where(u => u.Active == ActiveStates.Active)
.Where(e => e.Expires != ExpiryType.Never)
.Include(l => l.Loggings!.OrderByDescending(d => d.Time).Take(25))
.Include(l => l.Loggings!.Where(l => l.Access != null).OrderByDescending(d => d.Time).Take(25))
.Include(d => d.Department)
.Include(g => g.UserGroups)
.AsNoTracking()
.AsAsyncEnumerable())
{
if (user.Loggings != null)
Debug.WriteLine($"{user.Name} - {user.Loggings?.Count}");
if (user.Loggings?.Count != 0)
{
if (user.Loggings.Count > 0)
if (user.Loggings?.FirstOrDefault()?.Time < timeout)
{
if (user.Loggings.Where(c => c.Access != null).FirstOrDefault()?.Time < timeout)
{
userlist.Add(user);
}
userlist.Add(user);
}
}
else
{
Debug.WriteLine($"---- {user.Name} - {user.Created} - {timeout}");
if (user.Created < timeout)
{
userlist.Add(user);
}
}
}
@@ -253,7 +260,7 @@ namespace Aperio_Control_Centre.Controllers
.Where(w => w.Expires == ExpiryType.Never)
.Include(I => I.Department)
.Include(g => g.UserGroups)
.Include(l => l.Loggings!.OrderByDescending(d => d.Time).Take(25))
.Include(l => l.Loggings!.Where(l => l.Access != null).OrderByDescending(d => d.Time).Take(25))
.AsNoTracking();
sortOrder = Response.Cookies.SetSortorderCollection(Request.Cookies.GetSortorderCollection(), sortOrder, "NeverExpiredList");

View File

@@ -122,14 +122,6 @@
<th>
@Html.CheckBoxFor(model => model.ColumnSelect["IsExpired"], new { @class = "form-check-input" })
@Html.DisplayNameFor(model => model.UserList.First().Expires)
@* @Html.DisplayNameFor(model => model.UserList.First().IsExpired(model.ExpiryDays)) *@
@* @Html.DisplayName(Model.UserList.First().IsExpired(Model.ExpiryDays)) *@
@* @if (Model.UserList.Any())
{
@Html.Raw("Expired ")
@Model.UserList.First().ExpiryDays
@Html.Raw(" dagen")
} *@
</th>
</tr>
@@ -195,10 +187,6 @@
@item.LastUsed.ToACSLocalTime()
</td>
<td>
@* @item.IsExpired(Model.ExpiryDays) *@
@* @item.IsExpired *@
@* @Html.CheckBox(item.IsExpired(Model.ExpiryDays), item.Selected, new { @class = "form-check-input"}) *@
@* @Html.CheckBox(item.IsExpired(Model.ExpiryDays), item.IsExpired(Model.ExpiryDays), new { @class = "form-check-input" }) *@
@if (item.IsExpired)
{
<span class="icon--mdi mdi--check-outline"></span>

View File

@@ -31,6 +31,7 @@
<div class="card-body">
@await Html.PartialAsync("_PartialUserTable", Model, new ViewDataDictionary(ViewData) {
{ "ActiveState", ActiveStates.Active },
{ "ShowCreated", true },
{ "ShowLastUsed", true },
{ "ShowLastUsedDays", true },
{ "SelectActive", true }})

View File

@@ -88,6 +88,20 @@
</div>
</th>
}
@if ((ViewData["ShowCreated"] as bool?) ?? false)
{
<th>
<div class="d-flex">
@Html.DisplayNameFor(model => model.Created)
<a class="icon-link" href="@Url.Action(null, "User", new { sortOrder = "created_desc" })">
<span class="icon--mdi mdi--arrow-up"></span>
</a>
<a class="icon-link" href="@Url.Action(null, "User", new { sortOrder = "created_asc" })">
<span class="icon--mdi mdi--arrow-down"></span>
</a>
</div>
</th>
}
<th>
<div class="d-flex">
@Html.DisplayNameFor(model => model.LastChanged)
@@ -206,6 +220,12 @@
}
</td>
}
@if ((ViewData["ShowCreated"] as bool?) ?? false)
{
<td>
@item.Created.ToACSLocalTime()
</td>
}
<td>
@item.LastChanged.ToACSLocalTime()
</td>