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:
		| @ -48,6 +48,18 @@ | ||||
|                             </select> | ||||
|                         </div> | ||||
|                     </div> | ||||
|                     <div class="row mb-1 align-items-center"> | ||||
|                         <Label Class="col-sm-3" For="effectiveDate" HelpText="The date that this module is active" ResourceKey="EffectiveDate">Effective Date: </Label> | ||||
|                         <div class="col-sm-9"> | ||||
|                             <input type="date" id="effectiveDate" class="form-control" @bind="@_effectivedate" /> | ||||
|                         </div> | ||||
|                     </div> | ||||
|                     <div class="row mb-1 align-items-center"> | ||||
|                         <Label Class="col-sm-3" For="expiryDate" HelpText="The date that this module expires" ResourceKey="ExpiryDate">Expiry Date: </Label> | ||||
|                         <div class="col-sm-9"> | ||||
|                             <input type="date" id="expiryDate" class="form-control" @bind="@_expirydate" /> | ||||
|                         </div> | ||||
|                     </div> | ||||
|                     <div class="row mb-1 align-items-center"> | ||||
|                         <Label Class="col-sm-3" For="allpages" HelpText="Indicate if this module should be displayed on all pages" ResourceKey="DisplayOnAllPages">Display On All Pages? </Label> | ||||
|                         <div class="col-sm-9"> | ||||
| @ -114,7 +126,7 @@ | ||||
|     <AuditInfo CreatedBy="@createdby" CreatedOn="@createdon" ModifiedBy="@modifiedby" ModifiedOn="@modifiedon"></AuditInfo> | ||||
| </form> | ||||
|  | ||||
| @code { | ||||
|     @code { | ||||
|     public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.Edit; | ||||
|     public override string Title => "Module Settings"; | ||||
|  | ||||
| @ -141,7 +153,8 @@ | ||||
|     private DateTime createdon; | ||||
|     private string modifiedby; | ||||
|     private DateTime modifiedon; | ||||
|  | ||||
|     private DateTime? _effectivedate = null; | ||||
|     private DateTime? _expirydate = null; | ||||
|     protected override void OnInitialized() | ||||
|     { | ||||
|         _module = ModuleState.ModuleDefinition.Name; | ||||
| @ -156,6 +169,8 @@ | ||||
|         createdon = ModuleState.CreatedOn; | ||||
|         modifiedby = ModuleState.ModifiedBy; | ||||
|         modifiedon = ModuleState.ModifiedOn; | ||||
|         _effectivedate = ModuleState.EffectiveDate; | ||||
|         _expirydate = ModuleState.ExpiryDate; | ||||
|  | ||||
|         if (ModuleState.ModuleDefinition != null) | ||||
|         { | ||||
| @ -214,12 +229,20 @@ | ||||
|         var interop = new Interop(JSRuntime); | ||||
|         if (await interop.FormValid(form)) | ||||
|         { | ||||
|  | ||||
|             if (!string.IsNullOrEmpty(_title)) | ||||
|             { | ||||
|                 if (!ValidateEffectiveExpiryDates(_effectivedate, _expirydate)) | ||||
|                 { | ||||
|                     AddModuleMessage(SharedLocalizer["Message.EffectiveExpiryDateError"], MessageType.Warning); | ||||
|                     return; | ||||
|                 }                 | ||||
|                 var pagemodule = await PageModuleService.GetPageModuleAsync(ModuleState.PageModuleId); | ||||
|                 pagemodule.PageId = int.Parse(_pageId); | ||||
|                 pagemodule.Title = _title; | ||||
|                 pagemodule.Pane = _pane; | ||||
|                 pagemodule.EffectiveDate = _effectivedate; | ||||
|                 pagemodule.ExpiryDate = _expirydate; | ||||
|                 pagemodule.ContainerType = (_containerType != "-") ? _containerType : string.Empty; | ||||
|                 if (!string.IsNullOrEmpty(pagemodule.ContainerType) && pagemodule.ContainerType == PageState.Page.DefaultContainerType) | ||||
| 				{ | ||||
| @ -269,5 +292,33 @@ | ||||
|             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; | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -119,6 +119,18 @@ | ||||
|                             <i class="@_icon"></i> | ||||
|                         </div> | ||||
|                     </div> | ||||
|                     <div class="row mb-1 align-items-center"> | ||||
|                         <Label Class="col-sm-3" For="effectiveDate" HelpText="The date that this page is active" ResourceKey="EffectiveDate">Effective Date: </Label> | ||||
|                         <div class="col-sm-9"> | ||||
|                             <input type="date" id="effectiveDate" class="form-control" @bind="@_effectivedate" /> | ||||
|                         </div> | ||||
|                     </div> | ||||
|                     <div class="row mb-1 align-items-center"> | ||||
|                         <Label Class="col-sm-3" For="expiryDate" HelpText="The date that this page expires" ResourceKey="ExpiryDate">Expiry Date: </Label> | ||||
|                         <div class="col-sm-9"> | ||||
|                             <input type="date" id="expiryDate" class="form-control" @bind="@_expirydate" /> | ||||
|                         </div> | ||||
|                     </div> | ||||
|                     <div class="row mb-1 align-items-center"> | ||||
|                         <Label Class="col-sm-3" For="personalizable" HelpText="Select whether you would like users to be able to personalize this page with their own content" ResourceKey="Personalizable">Personalizable? </Label> | ||||
|                         <div class="col-sm-9"> | ||||
| @ -233,6 +245,8 @@ | ||||
|     protected Page _parent = null; | ||||
|     protected Dictionary<string, string> _icons; | ||||
|     private string _iconresources = ""; | ||||
|     private DateTime? _effectivedate = null; | ||||
|     private DateTime? _expirydate = null; | ||||
|  | ||||
|     protected override async Task OnInitializedAsync() | ||||
|     { | ||||
| @ -265,6 +279,8 @@ | ||||
|                         _children.Add(p); | ||||
|                     } | ||||
|                 } | ||||
|                 _effectivedate = PageState.Page.EffectiveDate; | ||||
|                 _expirydate = PageState.Page.ExpiryDate; | ||||
|                 ThemeSettings(); | ||||
|                 _initialized = true; | ||||
|             } | ||||
| @ -274,18 +290,18 @@ | ||||
|                 AddModuleMessage(Localizer["Error.Page.Load"], MessageType.Error); | ||||
|             } | ||||
|         } | ||||
| 		catch (Exception ex) | ||||
| 		{ | ||||
| 			await logger.LogError(ex, "Error Loading Page {Error}", ex.Message); | ||||
| 			AddModuleMessage(Localizer["Error.Page.Load"], MessageType.Error); | ||||
| 		} | ||||
| 	} | ||||
|         catch (Exception ex) | ||||
|         { | ||||
|             await logger.LogError(ex, "Error Loading Page {Error}", ex.Message); | ||||
|             AddModuleMessage(Localizer["Error.Page.Load"], MessageType.Error); | ||||
|         } | ||||
|     } | ||||
|  | ||||
| 	private async void ParentChanged(ChangeEventArgs e) | ||||
| 	{ | ||||
| 		try | ||||
| 		{ | ||||
| 			_parentid = (string)e.Value; | ||||
|     private async void ParentChanged(ChangeEventArgs e) | ||||
|     { | ||||
|         try | ||||
|         { | ||||
|             _parentid = (string)e.Value; | ||||
|             _children = new List<Page>(); | ||||
|             foreach (Page p in PageState.Pages.Where(item => (_parentid == "-1" && item.ParentId == null) || (item.ParentId == int.Parse(_parentid)))) | ||||
|             { | ||||
| @ -295,13 +311,13 @@ | ||||
|                 } | ||||
|             } | ||||
|             StateHasChanged(); | ||||
| 		} | ||||
| 		catch (Exception ex) | ||||
| 		{ | ||||
| 			await logger.LogError(ex, "Error Loading Child Pages For Parent {PageId} {Error}", _parentid, ex.Message); | ||||
| 			AddModuleMessage(Localizer["Error.ChildPage.Load"], MessageType.Error); | ||||
| 		} | ||||
| 	} | ||||
|         } | ||||
|         catch (Exception ex) | ||||
|         { | ||||
|             await logger.LogError(ex, "Error Loading Child Pages For Parent {PageId} {Error}", _parentid, ex.Message); | ||||
|             AddModuleMessage(Localizer["Error.ChildPage.Load"], MessageType.Error); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     private void ThemeChanged(ChangeEventArgs e) | ||||
|     { | ||||
| @ -318,109 +334,116 @@ | ||||
|         } | ||||
|     } | ||||
|  | ||||
| 	private void ThemeSettings() | ||||
| 	{ | ||||
| 		_themeSettingsType = null; | ||||
|     private void ThemeSettings() | ||||
|     { | ||||
|         _themeSettingsType = null; | ||||
|         var theme = PageState.Site.Themes.FirstOrDefault(item => item.Themes.Any(themecontrol => themecontrol.TypeName.Equals(_themetype))); | ||||
| 		if (theme != null && !string.IsNullOrEmpty(theme.ThemeSettingsType)) | ||||
| 		{ | ||||
| 			_themeSettingsType = Type.GetType(theme.ThemeSettingsType); | ||||
| 			if (_themeSettingsType != null) | ||||
| 			{ | ||||
| 				ThemeSettingsComponent = builder => | ||||
| 				{ | ||||
| 					builder.OpenComponent(0, _themeSettingsType); | ||||
| 					builder.AddComponentReferenceCapture(1, inst => { _themeSettings = Convert.ChangeType(inst, _themeSettingsType); }); | ||||
| 					builder.CloseComponent(); | ||||
| 				}; | ||||
| 			} | ||||
| 			_refresh = true; | ||||
| 		} | ||||
| 	} | ||||
|         if (theme != null && !string.IsNullOrEmpty(theme.ThemeSettingsType)) | ||||
|         { | ||||
|             _themeSettingsType = Type.GetType(theme.ThemeSettingsType); | ||||
|             if (_themeSettingsType != null) | ||||
|             { | ||||
|                 ThemeSettingsComponent = builder => | ||||
|                 { | ||||
|                     builder.OpenComponent(0, _themeSettingsType); | ||||
|                     builder.AddComponentReferenceCapture(1, inst => { _themeSettings = Convert.ChangeType(inst, _themeSettingsType); }); | ||||
|                     builder.CloseComponent(); | ||||
|                 }; | ||||
|             } | ||||
|             _refresh = true; | ||||
|         } | ||||
|     } | ||||
|  | ||||
| 	private async Task SavePage() | ||||
| 	{ | ||||
| 		validated = true; | ||||
| 		var interop = new Interop(JSRuntime); | ||||
| 		if (await interop.FormValid(form)) | ||||
| 		{ | ||||
| 			Page page = null; | ||||
| 			try | ||||
| 			{ | ||||
|     private async Task SavePage() | ||||
|     { | ||||
|         validated = true; | ||||
|         var interop = new Interop(JSRuntime); | ||||
|         if (await interop.FormValid(form)) | ||||
|         { | ||||
|             Page page = null; | ||||
|             try | ||||
|             { | ||||
|                 if (!ValidateEffectiveExpiryDates(_effectivedate, _expirydate)) | ||||
|                 { | ||||
|                     AddModuleMessage(SharedLocalizer["Message.EffectiveExpiryDateError"], MessageType.Warning); | ||||
|                     return; | ||||
|                 } | ||||
|                 if (!string.IsNullOrEmpty(_themetype) && !string.IsNullOrEmpty(_containertype)) | ||||
| 				{ | ||||
| 					page = new Page(); | ||||
| 					page.SiteId = PageState.Page.SiteId; | ||||
| 					page.Name = _name; | ||||
|                 { | ||||
|                     page = new Page(); | ||||
|                     page.SiteId = PageState.Page.SiteId; | ||||
|                     page.Name = _name; | ||||
|  | ||||
| 					if (string.IsNullOrEmpty(_path)) | ||||
| 					{ | ||||
| 						_path = _name; | ||||
| 					} | ||||
| 					if (_path.Contains("/")) | ||||
| 					{ | ||||
| 						if (_path.EndsWith("/") && _path != "/") | ||||
| 						{ | ||||
| 							_path = _path.Substring(0, _path.Length - 1); | ||||
| 						} | ||||
| 						_path = _path.Substring(_path.LastIndexOf("/") + 1); | ||||
| 					} | ||||
|                     if (string.IsNullOrEmpty(_path)) | ||||
|                     { | ||||
|                         _path = _name; | ||||
|                     } | ||||
|                     if (_path.Contains("/")) | ||||
|                     { | ||||
|                         if (_path.EndsWith("/") && _path != "/") | ||||
|                         { | ||||
|                             _path = _path.Substring(0, _path.Length - 1); | ||||
|                         } | ||||
|                         _path = _path.Substring(_path.LastIndexOf("/") + 1); | ||||
|                     } | ||||
|  | ||||
| 					if (_parentid == "-1") | ||||
| 					{ | ||||
| 						page.ParentId = null; | ||||
| 						page.Path = Utilities.GetFriendlyUrl(_path); | ||||
| 					} | ||||
| 					else | ||||
| 					{ | ||||
| 						page.ParentId = Int32.Parse(_parentid); | ||||
| 						var parent = PageState.Pages.Where(item => item.PageId == page.ParentId).FirstOrDefault(); | ||||
| 						if (parent.Path == string.Empty) | ||||
| 						{ | ||||
| 							page.Path = Utilities.GetFriendlyUrl(parent.Name) + "/" + Utilities.GetFriendlyUrl(_path); | ||||
| 						} | ||||
| 						else | ||||
| 						{ | ||||
| 							page.Path = parent.Path + "/" + Utilities.GetFriendlyUrl(_path); | ||||
| 						} | ||||
| 					} | ||||
|                     if (_parentid == "-1") | ||||
|                     { | ||||
|                         page.ParentId = null; | ||||
|                         page.Path = Utilities.GetFriendlyUrl(_path); | ||||
|                     } | ||||
|                     else | ||||
|                     { | ||||
|                         page.ParentId = Int32.Parse(_parentid); | ||||
|                         var parent = PageState.Pages.Where(item => item.PageId == page.ParentId).FirstOrDefault(); | ||||
|                         if (parent.Path == string.Empty) | ||||
|                         { | ||||
|                             page.Path = Utilities.GetFriendlyUrl(parent.Name) + "/" + Utilities.GetFriendlyUrl(_path); | ||||
|                         } | ||||
|                         else | ||||
|                         { | ||||
|                             page.Path = parent.Path + "/" + Utilities.GetFriendlyUrl(_path); | ||||
|                         } | ||||
|                     } | ||||
|  | ||||
| 					var _pages = await PageService.GetPagesAsync(PageState.Site.SiteId); | ||||
| 					if (_pages.Any(item => item.Path == page.Path)) | ||||
| 					{ | ||||
| 						AddModuleMessage(string.Format(Localizer["Message.Page.Exists"], _path), MessageType.Warning); | ||||
| 						return; | ||||
| 					} | ||||
|                     var _pages = await PageService.GetPagesAsync(PageState.Site.SiteId); | ||||
|                     if (_pages.Any(item => item.Path == page.Path)) | ||||
|                     { | ||||
|                         AddModuleMessage(string.Format(Localizer["Message.Page.Exists"], _path), MessageType.Warning); | ||||
|                         return; | ||||
|                     } | ||||
|  | ||||
| 					if (page.ParentId == null && Constants.ReservedRoutes.Contains(page.Name.ToLower())) | ||||
| 					{ | ||||
| 						AddModuleMessage(string.Format(Localizer["Message.Page.Reserved"], page.Name), MessageType.Warning); | ||||
| 						return; | ||||
| 					} | ||||
|                     if (page.ParentId == null && Constants.ReservedRoutes.Contains(page.Name.ToLower())) | ||||
|                     { | ||||
|                         AddModuleMessage(string.Format(Localizer["Message.Page.Reserved"], page.Name), MessageType.Warning); | ||||
|                         return; | ||||
|                     } | ||||
|  | ||||
| 					Page child; | ||||
| 					switch (_insert) | ||||
| 					{ | ||||
| 						case "<<": | ||||
| 							page.Order = 0; | ||||
| 							break; | ||||
| 						case "<": | ||||
| 							child = PageState.Pages.Where(item => item.PageId == _childid).FirstOrDefault(); | ||||
| 							page.Order = child.Order - 1; | ||||
| 							break; | ||||
| 						case ">": | ||||
| 							child = PageState.Pages.Where(item => item.PageId == _childid).FirstOrDefault(); | ||||
| 							page.Order = child.Order + 1; | ||||
| 							break; | ||||
| 						case ">>": | ||||
| 							page.Order = int.MaxValue; | ||||
| 							break; | ||||
| 					} | ||||
|                     Page child; | ||||
|                     switch (_insert) | ||||
|                     { | ||||
|                         case "<<": | ||||
|                             page.Order = 0; | ||||
|                             break; | ||||
|                         case "<": | ||||
|                             child = PageState.Pages.Where(item => item.PageId == _childid).FirstOrDefault(); | ||||
|                             page.Order = child.Order - 1; | ||||
|                             break; | ||||
|                         case ">": | ||||
|                             child = PageState.Pages.Where(item => item.PageId == _childid).FirstOrDefault(); | ||||
|                             page.Order = child.Order + 1; | ||||
|                             break; | ||||
|                         case ">>": | ||||
|                             page.Order = int.MaxValue; | ||||
|                             break; | ||||
|                     } | ||||
|  | ||||
| 					page.IsNavigation = (_isnavigation == null ? true : Boolean.Parse(_isnavigation)); | ||||
| 					page.IsClickable = (_isclickable == null ? true : Boolean.Parse(_isclickable)); | ||||
|                     page.IsNavigation = (_isnavigation == null ? true : Boolean.Parse(_isnavigation)); | ||||
|                     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.UserId = null; | ||||
|  | ||||
|                     // appearance | ||||
| @ -490,4 +513,33 @@ | ||||
|     { | ||||
|         _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; | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -134,6 +134,18 @@ | ||||
|                                 <i class="@_icon"></i> | ||||
|                             </div> | ||||
|                         </div> | ||||
|                         <div class="row mb-1 align-items-center"> | ||||
|                             <Label Class="col-sm-3" For="effectiveDate" HelpText="The date that this page is active" ResourceKey="EffectiveDate">Effective Date: </Label> | ||||
|                             <div class="col-sm-9"> | ||||
|                                 <input type="date" id="effectiveDate" class="form-control" @bind="@_effectivedate" /> | ||||
|                             </div> | ||||
|                         </div> | ||||
|                         <div class="row mb-1 align-items-center"> | ||||
|                             <Label Class="col-sm-3" For="expiryDate" HelpText="The date that this page expires" ResourceKey="ExpiryDate">Expiry Date: </Label> | ||||
|                             <div class="col-sm-9"> | ||||
|                                 <input type="date" id="expiryDate" class="form-control" @bind="@_expirydate" /> | ||||
|                             </div> | ||||
|                         </div> | ||||
|                         <div class="row mb-1 align-items-center"> | ||||
|                             <Label Class="col-sm-3" For="personalizable" HelpText="Select whether you would like users to be able to personalize this page with their own content" ResourceKey="Personalizable">Personalizable? </Label> | ||||
|                             <div class="col-sm-9"> | ||||
| @ -322,6 +334,8 @@ | ||||
|     protected Page _parent = null; | ||||
|     protected Dictionary<string, string> _icons; | ||||
|     private string _iconresources = ""; | ||||
|     private DateTime? _effectivedate = null; | ||||
|     private DateTime? _expirydate = null; | ||||
|  | ||||
|     protected override async Task OnInitializedAsync() | ||||
|     { | ||||
| @ -370,6 +384,8 @@ | ||||
|                 } | ||||
|                 _url = _page.Url; | ||||
|                 _icon = _page.Icon; | ||||
|                 _effectivedate = _page.EffectiveDate; | ||||
|                 _expirydate = _page.ExpiryDate; | ||||
|                 _ispersonalizable = _page.IsPersonalizable.ToString(); | ||||
|  | ||||
|                 // appearance | ||||
| @ -487,6 +503,11 @@ | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 if (!ValidateEffectiveExpiryDates(_effectivedate, _expirydate)) | ||||
|                 { | ||||
|                     AddModuleMessage(SharedLocalizer["Message.EffectiveExpiryDateError"], MessageType.Warning); | ||||
|                     return; | ||||
|                 } | ||||
|                 if (!string.IsNullOrEmpty(_themetype) && _containertype != "-") | ||||
|                 { | ||||
|                     string currentPath = _page.Path; | ||||
| @ -563,6 +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.IsPersonalizable = (_ispersonalizable != null && Boolean.Parse(_ispersonalizable)); | ||||
|  | ||||
|                     // appearance | ||||
| @ -666,4 +689,33 @@ | ||||
|     { | ||||
|         _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; | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -34,13 +34,13 @@ else | ||||
|         <div class="row mb-1 align-items-center"> | ||||
|             <Label Class="col-sm-3" For="effectiveDate" HelpText="The date that this role assignment is active" ResourceKey="EffectiveDate">Effective Date: </Label> | ||||
|             <div class="col-sm-9"> | ||||
|                 <input type="date" id="effectiveDate" class="form-control" @bind="@effectivedate" /> | ||||
|                 <input type="date" id="effectiveDate" class="form-control" @bind="@_effectivedate" /> | ||||
|             </div> | ||||
|         </div> | ||||
|         <div class="row mb-1 align-items-center"> | ||||
|             <Label Class="col-sm-3" For="expiryDate" HelpText="The date that this role assignment expires" ResourceKey="ExpiryDate">Expiry Date: </Label> | ||||
|             <div class="col-sm-9"> | ||||
|                 <input type="date" id="expiryDate" class="form-control" @bind="@expirydate" /> | ||||
|                 <input type="date" id="expiryDate" class="form-control" @bind="@_expirydate" /> | ||||
|             </div> | ||||
|         </div> | ||||
|  | ||||
| @ -75,8 +75,8 @@ else | ||||
|     private string name = string.Empty; | ||||
|     private List<Role> roles; | ||||
|     private int roleid = -1; | ||||
|     private DateTime? effectivedate = null; | ||||
|     private DateTime? expirydate = null; | ||||
|     private DateTime? _effectivedate = null; | ||||
|     private DateTime? _expirydate = null; | ||||
|     private List<UserRole> userroles; | ||||
|  | ||||
|     public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.Edit; | ||||
| @ -92,7 +92,7 @@ else | ||||
|             if (UserSecurity.IsAuthorized(PageState.User, RoleNames.Host)) | ||||
|             { | ||||
|                 roles = await RoleService.GetRolesAsync(PageState.Site.SiteId, true); | ||||
| 				roles.RemoveAll(item => item.Name == RoleNames.Everyone || item.Name == RoleNames.Unauthenticated); | ||||
|                 roles.RemoveAll(item => item.Name == RoleNames.Everyone || item.Name == RoleNames.Unauthenticated); | ||||
|             } | ||||
|             else | ||||
|             { | ||||
| @ -127,11 +127,16 @@ else | ||||
|         { | ||||
|             if (roleid != -1) | ||||
|             { | ||||
|                 if (!ValidateEffectiveExpiryDates(_effectivedate,_expirydate)) | ||||
|                 { | ||||
|                     AddModuleMessage(SharedLocalizer["Message.EffectiveExpiryDateError"], MessageType.Warning); | ||||
|                     return; | ||||
|                 } | ||||
|                 var userrole = userroles.Where(item => item.UserId == userid && item.RoleId == roleid).FirstOrDefault(); | ||||
|                 if (userrole != null) | ||||
|                 { | ||||
|                     userrole.EffectiveDate = effectivedate; | ||||
|                     userrole.ExpiryDate = expirydate; | ||||
|                     userrole.EffectiveDate = _effectivedate; | ||||
|                     userrole.ExpiryDate = _expirydate; | ||||
|                     await UserRoleService.UpdateUserRoleAsync(userrole); | ||||
|                 } | ||||
|                 else | ||||
| @ -139,15 +144,15 @@ else | ||||
|                     userrole = new UserRole(); | ||||
|                     userrole.UserId = userid; | ||||
|                     userrole.RoleId = roleid; | ||||
|                     userrole.EffectiveDate = effectivedate; | ||||
|                     userrole.ExpiryDate = expirydate; | ||||
|                     userrole.EffectiveDate = _effectivedate; | ||||
|                     userrole.ExpiryDate = _expirydate; | ||||
|                     await UserRoleService.AddUserRoleAsync(userrole); | ||||
|                 } | ||||
|  | ||||
|                 await logger.LogInformation("User Assigned To Role {UserRole}", userrole); | ||||
|                 AddModuleMessage(Localizer["Success.User.AssignRole"], MessageType.Success); | ||||
|                 await GetUserRoles(); | ||||
|                 StateHasChanged(); | ||||
|                 StateHasChanged();                     | ||||
|             } | ||||
|             else | ||||
|             { | ||||
| @ -177,4 +182,34 @@ 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; | ||||
|     } | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 Leigh Pointer
					Leigh Pointer