diff --git a/Oqtane.Client/Modules/Admin/Modules/Settings.razor b/Oqtane.Client/Modules/Admin/Modules/Settings.razor index 225a6e72..b1ed379a 100644 --- a/Oqtane.Client/Modules/Admin/Modules/Settings.razor +++ b/Oqtane.Client/Modules/Admin/Modules/Settings.razor @@ -169,8 +169,8 @@ createdon = ModuleState.CreatedOn; modifiedby = ModuleState.ModifiedBy; modifiedon = ModuleState.ModifiedOn; - _effectivedate = ModuleState.EffectiveDate; - _expirydate = ModuleState.ExpiryDate; + _effectivedate = Utilities.UtcAsLocalDate(ModuleState.EffectiveDate); + _expirydate = Utilities.UtcAsLocalDate(ModuleState.ExpiryDate); if (ModuleState.ModuleDefinition != null) { @@ -232,7 +232,7 @@ if (!string.IsNullOrEmpty(_title)) { - if (!ValidateEffectiveExpiryDates(_effectivedate, _expirydate)) + if (!Utilities.ValidateEffectiveExpiryDates(_effectivedate, _expirydate)) { AddModuleMessage(SharedLocalizer["Message.EffectiveExpiryDateError"], MessageType.Warning); return; @@ -241,8 +241,8 @@ pagemodule.PageId = int.Parse(_pageId); pagemodule.Title = _title; pagemodule.Pane = _pane; - pagemodule.EffectiveDate = _effectivedate; - pagemodule.ExpiryDate = _expirydate; + pagemodule.EffectiveDate = Utilities.LocalDateAndTimeAsUtc(_effectivedate); + pagemodule.ExpiryDate = Utilities.LocalDateAndTimeAsUtc(_expirydate); pagemodule.ContainerType = (_containerType != "-") ? _containerType : string.Empty; if (!string.IsNullOrEmpty(pagemodule.ContainerType) && pagemodule.ContainerType == PageState.Page.DefaultContainerType) { @@ -292,33 +292,4 @@ AddModuleMessage(SharedLocalizer["Message.InfoRequired"], MessageType.Warning); } } - private bool ValidateEffectiveExpiryDates(DateTime? effectiveDate, DateTime? expiryDate) - { - // Check if both dates are null, in which case the validation passes - if (effectiveDate == DateTime.MinValue && expiryDate == DateTime.MinValue) - { - return true; - } - - // Check if EffectiveDate is not null and ExpiryDate is null - if (effectiveDate != DateTime.MinValue && expiryDate == DateTime.MinValue) - { - return true; - } - - // Check if EffectiveDate is null and ExpiryDate is not null - if (effectiveDate == DateTime.MinValue && expiryDate != DateTime.MinValue) - { - return true; - } - - // Check if ExpiryDate is not null and EffectiveDate is after ExpiryDate - if (expiryDate != DateTime.MinValue && effectiveDate != DateTime.MinValue && effectiveDate > expiryDate) - { - return false; - } - - // If none of the above conditions are met, validation passes - return true; - } } diff --git a/Oqtane.Client/Modules/Admin/Pages/Add.razor b/Oqtane.Client/Modules/Admin/Pages/Add.razor index c03a7e0a..05a97d7a 100644 --- a/Oqtane.Client/Modules/Admin/Pages/Add.razor +++ b/Oqtane.Client/Modules/Admin/Pages/Add.razor @@ -279,8 +279,8 @@ _children.Add(p); } } - _effectivedate = PageState.Page.EffectiveDate; - _expirydate = PageState.Page.ExpiryDate; + _effectivedate = Utilities.UtcAsLocalDate(PageState.Page.EffectiveDate); + _expirydate = Utilities.UtcAsLocalDate(PageState.Page.ExpiryDate); ThemeSettings(); _initialized = true; } @@ -363,7 +363,7 @@ Page page = null; try { - if (!ValidateEffectiveExpiryDates(_effectivedate, _expirydate)) + if (!Utilities.ValidateEffectiveExpiryDates(_effectivedate, _expirydate)) { AddModuleMessage(SharedLocalizer["Message.EffectiveExpiryDateError"], MessageType.Warning); return; @@ -442,8 +442,8 @@ page.IsClickable = (_isclickable == null ? true : Boolean.Parse(_isclickable)); page.Url = _url; page.IsPersonalizable = (_ispersonalizable == null ? false : Boolean.Parse(_ispersonalizable)); - page.EffectiveDate = _effectivedate; - page.ExpiryDate = _expirydate; + page.EffectiveDate = Utilities.LocalDateAndTimeAsUtc(_effectivedate); + page.ExpiryDate = Utilities.LocalDateAndTimeAsUtc(_expirydate); page.UserId = null; // appearance @@ -513,33 +513,4 @@ { _icon = NewIcon; } - private bool ValidateEffectiveExpiryDates(DateTime? effectiveDate, DateTime? expiryDate) - { - // Check if both dates are null, in which case the validation passes - if (effectiveDate == DateTime.MinValue && expiryDate == DateTime.MinValue) - { - return true; - } - - // Check if EffectiveDate is not null and ExpiryDate is null - if (effectiveDate != DateTime.MinValue && expiryDate == DateTime.MinValue) - { - return true; - } - - // Check if EffectiveDate is null and ExpiryDate is not null - if (effectiveDate == DateTime.MinValue && expiryDate != DateTime.MinValue) - { - return true; - } - - // Check if ExpiryDate is not null and EffectiveDate is after ExpiryDate - if (expiryDate != DateTime.MinValue && effectiveDate != DateTime.MinValue && effectiveDate > expiryDate) - { - return false; - } - - // If none of the above conditions are met, validation passes - return true; - } } diff --git a/Oqtane.Client/Modules/Admin/Pages/Edit.razor b/Oqtane.Client/Modules/Admin/Pages/Edit.razor index 3b64188b..3084ef84 100644 --- a/Oqtane.Client/Modules/Admin/Pages/Edit.razor +++ b/Oqtane.Client/Modules/Admin/Pages/Edit.razor @@ -384,8 +384,8 @@ } _url = _page.Url; _icon = _page.Icon; - _effectivedate = _page.EffectiveDate; - _expirydate = _page.ExpiryDate; + _effectivedate = Utilities.UtcAsLocalDate(_page.EffectiveDate); + _expirydate = Utilities.UtcAsLocalDate(_page.ExpiryDate); _ispersonalizable = _page.IsPersonalizable.ToString(); // appearance @@ -503,7 +503,7 @@ { try { - if (!ValidateEffectiveExpiryDates(_effectivedate, _expirydate)) + if (!Utilities.ValidateEffectiveExpiryDates(_effectivedate, _expirydate)) { AddModuleMessage(SharedLocalizer["Message.EffectiveExpiryDateError"], MessageType.Warning); return; @@ -584,8 +584,8 @@ _page.IsClickable = (_isclickable == null ? true : Boolean.Parse(_isclickable)); _page.Url = _url; _page.Icon = _icon ?? string.Empty; - _page.EffectiveDate = _effectivedate; - _page.ExpiryDate = _expirydate; + _page.EffectiveDate = Utilities.LocalDateAndTimeAsUtc(_effectivedate); + _page.ExpiryDate = Utilities.LocalDateAndTimeAsUtc(_expirydate); _page.IsPersonalizable = (_ispersonalizable != null && Boolean.Parse(_ispersonalizable)); // appearance @@ -689,33 +689,4 @@ { _icon = NewIcon; } - private bool ValidateEffectiveExpiryDates(DateTime? effectiveDate, DateTime? expiryDate) - { - // Check if both dates are null, in which case the validation passes - if (effectiveDate == DateTime.MinValue && expiryDate == DateTime.MinValue) - { - return true; - } - - // Check if EffectiveDate is not null and ExpiryDate is null - if (effectiveDate != DateTime.MinValue && expiryDate == DateTime.MinValue) - { - return true; - } - - // Check if EffectiveDate is null and ExpiryDate is not null - if (effectiveDate == DateTime.MinValue && expiryDate != DateTime.MinValue) - { - return true; - } - - // Check if ExpiryDate is not null and EffectiveDate is after ExpiryDate - if (expiryDate != DateTime.MinValue && effectiveDate != DateTime.MinValue && effectiveDate > expiryDate) - { - return false; - } - - // If none of the above conditions are met, validation passes - return true; - } } diff --git a/Oqtane.Client/Modules/Admin/Users/Roles.razor b/Oqtane.Client/Modules/Admin/Users/Roles.razor index 990aea43..8d65480a 100644 --- a/Oqtane.Client/Modules/Admin/Users/Roles.razor +++ b/Oqtane.Client/Modules/Admin/Users/Roles.razor @@ -60,8 +60,8 @@ else @context.Role.Name - @context.EffectiveDate - @context.ExpiryDate + @Utilities.UtcAsLocalDate(context.EffectiveDate) + @Utilities.UtcAsLocalDate(context.ExpiryDate) @@ -113,6 +113,7 @@ else try { userroles = await UserRoleService.GetUserRolesAsync(PageState.Site.SiteId, userid); + } catch (Exception ex) { @@ -127,7 +128,7 @@ else { if (roleid != -1) { - if (!ValidateEffectiveExpiryDates(_effectivedate,_expirydate)) + if (!Utilities.ValidateEffectiveExpiryDates(_effectivedate, _expirydate)) { AddModuleMessage(SharedLocalizer["Message.EffectiveExpiryDateError"], MessageType.Warning); return; @@ -144,8 +145,8 @@ else userrole = new UserRole(); userrole.UserId = userid; userrole.RoleId = roleid; - userrole.EffectiveDate = _effectivedate; - userrole.ExpiryDate = _expirydate; + userrole.EffectiveDate = Utilities.UtcAsLocalDate(_effectivedate); + userrole.ExpiryDate = Utilities.UtcAsLocalDate(_expirydate); await UserRoleService.AddUserRoleAsync(userrole); } @@ -182,34 +183,4 @@ else AddModuleMessage(Localizer["Error.User.RemoveRole"], MessageType.Error); } } - - private bool ValidateEffectiveExpiryDates(DateTime? effectiveDate, DateTime? expiryDate) - { - // Check if both dates are null, in which case the validation passes - if (effectiveDate == DateTime.MinValue && expiryDate == DateTime.MinValue) - { - return true; - } - - // Check if EffectiveDate is not null and ExpiryDate is null - if (effectiveDate != DateTime.MinValue && expiryDate == DateTime.MinValue) - { - return true; - } - - // Check if EffectiveDate is null and ExpiryDate is not null - if (effectiveDate == DateTime.MinValue && expiryDate != DateTime.MinValue) - { - return true; - } - - // Check if ExpiryDate is not null and EffectiveDate is after ExpiryDate - if (expiryDate != DateTime.MinValue && effectiveDate != DateTime.MinValue && effectiveDate > expiryDate) - { - return false; - } - - // If none of the above conditions are met, validation passes - return true; - } } diff --git a/Oqtane.Client/UI/SiteRouter.razor b/Oqtane.Client/UI/SiteRouter.razor index 51805407..be5b6ac7 100644 --- a/Oqtane.Client/UI/SiteRouter.razor +++ b/Oqtane.Client/UI/SiteRouter.razor @@ -262,7 +262,7 @@ bool isAdminOrHost = false; if(user != null) { - isAdminOrHost = user.Roles.Contains(RoleNames.Host) || user.Roles.Contains(RoleNames.Admin); + isAdminOrHost = UserSecurity.IsAuthorized(user, RoleNames.Admin) || UserSecurity.IsAuthorized(user, PermissionNames.Edit, page.PermissionList); } if (page != null && (isAdminOrHost || IsPageModuleVisible(page.EffectiveDate, page.ExpiryDate))) @@ -270,7 +270,7 @@ // check if user is authorized to view page if (UserSecurity.IsAuthorized(user, PermissionNames.View, page.PermissionList)) { - if (isAdminOrHost || IsPageModuleVisible(page.EffectiveDate, page.ExpiryDate)) + if (isAdminOrHost || Utilities.ValidateEffectiveExpiryDates(page.EffectiveDate, page.ExpiryDate)) { // load additional metadata for current page page = ProcessPage(page, site, user, SiteState.Alias); diff --git a/Oqtane.Server/Controllers/SiteController.cs b/Oqtane.Server/Controllers/SiteController.cs index 3092297d..67d2a84e 100644 --- a/Oqtane.Server/Controllers/SiteController.cs +++ b/Oqtane.Server/Controllers/SiteController.cs @@ -94,21 +94,16 @@ namespace Oqtane.Controllers site.UploadableFiles = site.Settings.ContainsKey("UploadableFiles") && !string.IsNullOrEmpty(site.Settings["UploadableFiles"]) ? site.Settings["UploadableFiles"] : Constants.UploadableFiles; - var modelsUser = _userPermissions.GetUser(User); - var isAdminOrHost = modelsUser.Roles.Contains(RoleNames.Host) || modelsUser.Roles.Contains(RoleNames.Admin); - // pages List settings = _settings.GetSettings(EntityNames.Page).ToList(); site.Pages = new List(); - foreach (Page page in _pages.GetPages(site.SiteId).Where(p => !p.IsDeleted && _userPermissions.IsAuthorized(User, PermissionNames.View, p.PermissionList))) + foreach (Page page in _pages.GetPages(site.SiteId)) { - if (isAdminOrHost || IsPageModuleVisible(page.EffectiveDate, page.ExpiryDate)) + if (!page.IsDeleted && _userPermissions.IsAuthorized(User, PermissionNames.View, page.PermissionList)) { - page.Settings = settings - .Where(item => item.EntityId == page.PageId) + page.Settings = settings.Where(item => item.EntityId == page.PageId) .Where(item => !item.IsPrivate || _userPermissions.IsAuthorized(User, PermissionNames.Edit, page.PermissionList)) .ToDictionary(setting => setting.SettingName, setting => setting.SettingValue); - site.Pages.Add(page); } } @@ -121,7 +116,7 @@ namespace Oqtane.Controllers site.Modules = new List(); foreach (PageModule pagemodule in _pageModules.GetPageModules(site.SiteId).Where(pm => !pm.IsDeleted && _userPermissions.IsAuthorized(User, PermissionNames.View, pm.Module.PermissionList))) { - if (isAdminOrHost || IsPageModuleVisible(pagemodule.EffectiveDate, pagemodule.ExpiryDate)) + if (!pagemodule.IsDeleted && _userPermissions.IsAuthorized(User, PermissionNames.View, pagemodule.Module.PermissionList)) { Module module = new Module { @@ -291,30 +286,5 @@ namespace Oqtane.Controllers } return hierarchy; } - 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; - } - } } } diff --git a/Oqtane.Shared/Shared/Utilities.cs b/Oqtane.Shared/Shared/Utilities.cs index 82b32796..044abc2e 100644 --- a/Oqtane.Shared/Shared/Utilities.cs +++ b/Oqtane.Shared/Shared/Utilities.cs @@ -572,7 +572,35 @@ namespace Oqtane.Shared return (localDateTime?.Date, localTime); } + public static bool ValidateEffectiveExpiryDates(DateTime? effectiveDate, DateTime? expiryDate) + { + // Check if both dates are null, in which case the validation passes + if (effectiveDate == DateTime.MinValue && expiryDate == DateTime.MinValue) + { + return true; + } + // Check if EffectiveDate is not null and ExpiryDate is null + if (effectiveDate != DateTime.MinValue && expiryDate == DateTime.MinValue) + { + return true; + } + + // Check if EffectiveDate is null and ExpiryDate is not null + if (effectiveDate == DateTime.MinValue && expiryDate != DateTime.MinValue) + { + return true; + } + + // Check if ExpiryDate is not null and EffectiveDate is after ExpiryDate + if (expiryDate != DateTime.MinValue && effectiveDate != DateTime.MinValue && effectiveDate > expiryDate) + { + return false; + } + + // If none of the above conditions are met, validation passes + return true; + } [Obsolete("ContentUrl(Alias alias, int fileId) is deprecated. Use FileUrl(Alias alias, int fileId) instead.", false)] public static string ContentUrl(Alias alias, int fileId) {