From 33eaf46288cebb25db395517a849902bf80334ef Mon Sep 17 00:00:00 2001 From: David Leigh Date: Thu, 26 Oct 2023 00:41:15 +1100 Subject: [PATCH] + (Check-in) Fixed the Checkin Group List to correctly identify circular references in the Group Type inheritance chain. (Fixes #5637) --- .../Blocks/CheckIn/CheckinGroupList.ascx.cs | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/RockWeb/Blocks/CheckIn/CheckinGroupList.ascx.cs b/RockWeb/Blocks/CheckIn/CheckinGroupList.ascx.cs index 8082399fbfd..b2ac7634c3a 100644 --- a/RockWeb/Blocks/CheckIn/CheckinGroupList.ascx.cs +++ b/RockWeb/Blocks/CheckIn/CheckinGroupList.ascx.cs @@ -152,33 +152,40 @@ protected void bddlCampus_SelectionChanged( object sender, EventArgs e ) #region Methods - private GroupType GetRootGroupType(int groupId) + private GroupType GetRootGroupType( int groupId ) { - List parentRecursionHistory = new List(); - GroupTypeService groupTypeService = new GroupTypeService( _rockContext ); - var groupType = groupTypeService.Queryable().AsNoTracking().Include( t => t.ParentGroupTypes ).Where( t => t.Groups.Select( g => g.Id ).Contains( groupId ) ).FirstOrDefault(); - - while (groupType != null && groupType.ParentGroupTypes.Count != 0 ) + var parentRecursionHistory = new List(); + var groupTypeService = new GroupTypeService( _rockContext ); + var groupType = groupTypeService.Queryable().AsNoTracking() + .Include( t => t.ParentGroupTypes ) + .Where( t => t.Groups.Select( g => g.Id ).Contains( groupId ) ) + .FirstOrDefault(); + + while ( groupType != null && groupType.ParentGroupTypes.Count != 0 ) { - if ( parentRecursionHistory.Contains( groupType.Id ) ) + var currentGroupTypeId = groupType.Id; + if ( parentRecursionHistory.Contains( currentGroupTypeId ) ) { - var exception = new Exception("Infinite Recursion detected in GetRootGroupType for groupId: " + groupId.ToString()); + // This Group Type has been previously encountered in the inheritance chain, so a circular reference exists. + var exception = new Exception( "Infinite Recursion detected in GetRootGroupType for groupId: " + groupId.ToString() ); LogException( exception ); return null; } else { var parentGroupType = GetParentGroupType( groupType ); - if (parentGroupType != null && parentGroupType.Id == groupType.Id) + if ( parentGroupType != null && parentGroupType.Id == currentGroupTypeId ) { - // the group type's parent is itself + // This Group Type is a parent of itself, so it must be the root of the inheritance chain. return groupType; } + // Process the parent Group Type next. groupType = parentGroupType; } - - parentRecursionHistory.Add(groupType.Id); + + // Add the processed GroupType to the recursion tracking list. + parentRecursionHistory.Add( currentGroupTypeId ); } return groupType;