Start Date and Expiry Date for Module instances and Pages #3538

This is complete excluding Reporting and Notifications which can be added at a later date.  I just really wanted to get the schema and the functionality into place.
This commit is contained in:
Leigh Pointer
2023-12-31 12:21:38 +01:00
parent b0a079dce9
commit 233f40f3e9
16 changed files with 571 additions and 182 deletions

View File

@ -258,42 +258,51 @@
}
}
}
bool isAdminOrHost = false;
if(user != null)
{
isAdminOrHost = user.Roles.Contains(RoleNames.Host) || user.Roles.Contains(RoleNames.Admin);
}
if (page != null)
if (page != null && (isAdminOrHost || IsPageModuleVisible(page.EffectiveDate, page.ExpiryDate)))
{
// check if user is authorized to view page
if (UserSecurity.IsAuthorized(user, PermissionNames.View, page.PermissionList))
{
// load additional metadata for current page
page = ProcessPage(page, site, user, SiteState.Alias);
// load additional metadata for modules
(page, site.Modules) = ProcessModules(page, site.Modules, moduleid, action, (!string.IsNullOrEmpty(page.DefaultContainerType)) ? page.DefaultContainerType : site.DefaultContainerType, SiteState.Alias);
// populate page state (which acts as a client-side cache for subsequent requests)
_pagestate = new PageState
if (isAdminOrHost || IsPageModuleVisible(page.EffectiveDate, page.ExpiryDate))
{
Alias = SiteState.Alias,
Site = site,
Page = page,
User = user,
Uri = new Uri(_absoluteUri, UriKind.Absolute),
Route = route,
QueryString = querystring,
UrlParameters = route.UrlParameters,
ModuleId = moduleid,
Action = action,
EditMode = editmode,
LastSyncDate = lastsyncdate,
Runtime = runtime,
VisitorId = VisitorId,
RemoteIPAddress = SiteState.RemoteIPAddress,
ReturnUrl = returnurl,
IsInternalNavigation = _isInternalNavigation
};
// load additional metadata for current page
page = ProcessPage(page, site, user, SiteState.Alias);
OnStateChange?.Invoke(_pagestate);
await ScrollToFragment(_pagestate.Uri);
// load additional metadata for modules
(page, site.Modules) = ProcessModules(page, site.Modules, moduleid, action, (!string.IsNullOrEmpty(page.DefaultContainerType)) ? page.DefaultContainerType : site.DefaultContainerType, SiteState.Alias);
// populate page state (which acts as a client-side cache for subsequent requests)
_pagestate = new PageState
{
Alias = SiteState.Alias,
Site = site,
Page = page,
User = user,
Uri = new Uri(_absoluteUri, UriKind.Absolute),
Route = route,
QueryString = querystring,
UrlParameters = route.UrlParameters,
ModuleId = moduleid,
Action = action,
EditMode = editmode,
LastSyncDate = lastsyncdate,
Runtime = runtime,
VisitorId = VisitorId,
RemoteIPAddress = SiteState.RemoteIPAddress,
ReturnUrl = returnurl,
IsInternalNavigation = _isInternalNavigation
};
OnStateChange?.Invoke(_pagestate);
await ScrollToFragment(_pagestate.Uri);
}
}
}
else // page not found
@ -307,7 +316,7 @@
}
else // not mapped
{
if (user == null)
if (user == null && IsPageModuleVisible(page.EffectiveDate, page.ExpiryDate))
{
// redirect to login page if user not logged in as they may need to be authenticated
NavigationManager.NavigateTo(Utilities.NavigateUrl(SiteState.Alias.Path, "login", "?returnurl=" + WebUtility.UrlEncode(route.PathAndQuery)));
@ -578,4 +587,29 @@
}
}
}
private bool IsPageModuleVisible(DateTime? effectiveDate, DateTime? expiryDate)
{
DateTime currentUtcTime = DateTime.UtcNow;
// Check if either effectiveDate or expiryDate is provided
if (effectiveDate.HasValue && expiryDate.HasValue)
{
return currentUtcTime >= effectiveDate.Value && currentUtcTime <= expiryDate.Value;
}
// Check if only effectiveDate is provided
else if (effectiveDate.HasValue)
{
return currentUtcTime >= effectiveDate.Value;
}
// Check if only expiryDate is provided
else if (expiryDate.HasValue)
{
return currentUtcTime <= expiryDate.Value;
}
// If neither effectiveDate nor expiryDate is provided, consider the page/module visible
else
{
return true;
}
}
}