Exemplo n.º 1
0
        /// <summary>
        /// Validates the group member does not already exist based on GroupId, GroupRoleId, and PersonId.
        /// Returns false if there is a duplicate member problem.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="groupType">Type of the group.</param>
        /// <param name="errorMessage">The error message.</param>
        /// <returns></returns>
        private bool ValidateGroupMemberDuplicateMember(RockContext rockContext, GroupTypeCache groupType, out string errorMessage)
        {
            if (GroupService.AllowsDuplicateMembers())
            {
                errorMessage = string.Empty;
                return(true);
            }

            bool isNewOrChangedGroupMember = this.IsNewOrChangedGroupMember(rockContext);

            // if this isn't a new group member, then we can't really do anything about a duplicate record since it already existed before editing this record, so treat it as valid
            if (!isNewOrChangedGroupMember)
            {
                errorMessage = string.Empty;
                return(true);
            }

            errorMessage = string.Empty;
            var groupService = new GroupService(rockContext);

            // If the group member record is changing (new person, role, archive status, active status) check if there are any duplicates of this Person and Role for this group (besides the current record).
            var duplicateGroupMembers = new GroupMemberService(rockContext).GetByGroupIdAndPersonId(this.GroupId, this.PersonId).Where(a => a.GroupRoleId == this.GroupRoleId && this.Id != a.Id);

            if (duplicateGroupMembers.Any())
            {
                var person = this.Person ?? new PersonService(rockContext).GetNoTracking(this.PersonId);

                var groupRole = groupType.Roles.Where(a => a.Id == this.GroupRoleId).FirstOrDefault();
                errorMessage = $"{person} already belongs to the {groupRole.Name.ToLower()} role for this {groupType.GroupTerm.ToLower()}, and cannot be added again with the same role";

                return(false);
            }

            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Validates the group member does not already exist based on GroupId, GroupRoleId, and PersonId
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="groupType">Type of the group.</param>
        /// <param name="errorMessage">The error message.</param>
        /// <returns></returns>
        private bool ValidateGroupMemberDuplicateMember(RockContext rockContext, GroupTypeCache groupType, out string errorMessage)
        {
            errorMessage = string.Empty;
            var groupService = new GroupService(rockContext);

            if (!GroupService.AllowsDuplicateMembers())
            {
                var groupMember = new GroupMemberService(rockContext).GetByGroupIdAndPersonIdAndGroupRoleId(this.GroupId, this.PersonId, this.GroupRoleId);
                if (groupMember != null && groupMember.Id != this.Id)
                {
                    var person = this.Person ?? new PersonService(rockContext).Get(this.PersonId);

                    var groupRole = groupType.Roles.Where(a => a.Id == this.GroupRoleId).FirstOrDefault();
                    errorMessage = string.Format(
                        "{0} already belongs to the {1} role for this {2}, and cannot be added again with the same role",
                        person,
                        groupRole.Name.ToLower(),
                        groupType.GroupTerm.ToLower());

                    return(false);
                }
            }

            return(true);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Validates the group membership.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="errorMessage">The error message.</param>
        /// <returns></returns>
        private bool ValidateGroupMembership(RockContext rockContext, out string errorMessage)
        {
            errorMessage = string.Empty;
            var group = this.Group ?? new GroupService(rockContext).GetNoTracking(this.GroupId);

            var groupType = GroupTypeCache.Get(group.GroupTypeId);

            if (groupType == null)
            {
                // For some reason we could not get a GroupType
                errorMessage = $"The GroupTypeId {group.GroupTypeId} used by {this.Group.Name} does not exist.";
                return(false);
            }

            var groupRole = groupType.Roles.Where(a => a.Id == this.GroupRoleId).FirstOrDefault();

            if (groupRole == null)
            {
                // This is the only point in the method where we need a person object loaded.
                this.Person = this.Person ?? new PersonService(rockContext).GetNoTracking(this.PersonId);

                // For some reason we could not get a GroupTypeRole for the GroupRoleId for this GroupMember.
                errorMessage = $"The GroupRoleId {this.GroupRoleId} for the group member {this.Person.FullName} in group {group.Name} does not exist in the GroupType.Roles for GroupType {groupType.Name}.";
                return(false);
            }

            // Verify duplicate role/person
            if (!GroupService.AllowsDuplicateMembers())
            {
                if (!ValidateGroupMemberDuplicateMember(rockContext, groupType, out errorMessage))
                {
                    return(false);
                }
            }

            // Verify max group role membership
            if (groupRole.MaxCount.HasValue && this.GroupMemberStatus == GroupMemberStatus.Active)
            {
                int memberCountInRole = group.Members
                                        .Where(m =>
                                               m.GroupRoleId == this.GroupRoleId &&
                                               m.GroupMemberStatus == GroupMemberStatus.Active)
                                        .Where(m => m != this)
                                        .Count();

                bool roleMembershipAboveMax = false;

                // if adding new group member..
                if (this.Id.Equals(0))
                {
                    // verify that active count has not exceeded the max
                    if ((memberCountInRole + 1) > groupRole.MaxCount)
                    {
                        roleMembershipAboveMax = true;
                    }
                }
                else
                {
                    // if existing group member changing role or status..
                    if (this.IsStatusOrRoleModified(rockContext))
                    {
                        // verify that active count has not exceeded the max
                        if ((memberCountInRole + 1) > groupRole.MaxCount)
                        {
                            roleMembershipAboveMax = true;
                        }
                    }
                }

                // throw error if above max.. do not proceed
                if (roleMembershipAboveMax)
                {
                    errorMessage = $"The number of {groupRole.Name.Pluralize().ToLower()} for this {groupType.GroupTerm.ToLower()} is above its maximum allowed limit of {groupRole.MaxCount:N0} active {groupType.GroupMemberTerm.Pluralize( groupRole.MaxCount == 1 ).ToLower()}.";
                    return(false);
                }
            }

            // if the GroupMember is getting Added (or if Person or Role is different), and if this Group has requirements that must be met before the person is added, check those
            if (this.IsNewOrChangedGroupMember(rockContext))
            {
                if (group.GetGroupRequirements(rockContext).Any(a => a.MustMeetRequirementToAddMember))
                {
                    var requirementStatusesRequiredForAdd = group.PersonMeetsGroupRequirements(rockContext, this.PersonId, this.GroupRoleId)
                                                            .Where(a => a.MeetsGroupRequirement == MeetsGroupRequirement.NotMet &&
                                                                   ((a.GroupRequirement.GroupRequirementType.RequirementCheckType != RequirementCheckType.Manual) && (a.GroupRequirement.MustMeetRequirementToAddMember == true)));

                    if (requirementStatusesRequiredForAdd.Any())
                    {
                        // deny if any of the non-manual MustMeetRequirementToAddMember requirements are not met
                        errorMessage = "This person must meet the following requirements before they are added or made an active member in this group: "
                                       + requirementStatusesRequiredForAdd
                                       .Select(a => string.Format("{0}", a.GroupRequirement.GroupRequirementType))
                                       .ToList().AsDelimited(", ");

                        return(false);
                    }
                }
            }

            // check group capacity
            if (groupType.GroupCapacityRule == GroupCapacityRule.Hard && group.GroupCapacity.HasValue && this.GroupMemberStatus == GroupMemberStatus.Active)
            {
                var currentActiveGroupMemberCount    = group.ActiveMembers().Count(gm => gm.Id != Id);
                var existingGroupMembershipForPerson = group.Members.Where(m => m.PersonId == this.PersonId);

                // check if this would be adding an active group member (new active group member or changing existing group member status to active)
                if (this.Id == 0)
                {
                    if (currentActiveGroupMemberCount + 1 > group.GroupCapacity)
                    {
                        errorMessage = "Adding this individual would put the group over capacity.";
                        return(false);
                    }
                }
                else if (existingGroupMembershipForPerson.Where(m => m.Id == this.Id && m.GroupMemberStatus != GroupMemberStatus.Active).Any())
                {
                    if (currentActiveGroupMemberCount + 1 > group.GroupCapacity)
                    {
                        errorMessage = "Adding this individual would put the group over capacity.";
                        return(false);
                    }
                }
            }

            return(true);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Validates the group membership.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="errorMessage">The error message.</param>
        /// <returns></returns>
        private bool ValidateGroupMembership(RockContext rockContext, out string errorMessage)
        {
            errorMessage = string.Empty;

            // load group including members to save queries in group member validation
            var groupService = new GroupService(rockContext);
            var group        = this.Group ?? new GroupService(rockContext).Queryable("Members").Where(g => g.Id == this.GroupId).FirstOrDefault();

            var groupType = GroupTypeCache.Get(group.GroupTypeId);
            var groupRole = groupType.Roles.First(a => a.Id == this.GroupRoleId);

            var existingGroupMembership = group.Members.Where(m => m.PersonId == this.PersonId);

            // check to see if the person is already a member of the group/role
            bool allowDuplicateGroupMembers = groupService.AllowsDuplicateMembers(group);

            if (!allowDuplicateGroupMembers)
            {
                if (existingGroupMembership.Any(a => a.GroupRoleId == this.GroupRoleId && a.Id != this.Id))
                {
                    var person = this.Person ?? new PersonService(rockContext).Get(this.PersonId);

                    errorMessage = string.Format(
                        "{0} already belongs to the {1} role for this {2}, and cannot be added again with the same role",
                        person,
                        groupRole.Name.ToLower(),
                        groupType.GroupTerm.ToLower());

                    return(false);
                }
            }

            if (groupRole.MaxCount.HasValue && this.GroupMemberStatus == GroupMemberStatus.Active)
            {
                int memberCountInRole = group.Members
                                        .Where(m =>
                                               m.GroupRoleId == this.GroupRoleId &&
                                               m.GroupMemberStatus == GroupMemberStatus.Active)
                                        .Where(m => m != this)
                                        .Count();

                bool roleMembershipAboveMax = false;

                // if adding new group member..
                if (this.Id.Equals(0))
                {
                    // verify that active count has not exceeded the max
                    if ((memberCountInRole + 1) > groupRole.MaxCount)
                    {
                        roleMembershipAboveMax = true;
                    }
                }
                else
                {
                    // if existing group member changing role or status..
                    if (this.IsStatusOrRoleModified(rockContext))
                    {
                        // verify that active count has not exceeded the max
                        if (groupRole.MaxCount != null && (memberCountInRole + 1) > groupRole.MaxCount)
                        {
                            roleMembershipAboveMax = true;
                        }
                    }
                }

                // throw error if above max.. do not proceed
                if (roleMembershipAboveMax)
                {
                    errorMessage = string.Format(
                        "The number of {0} for this {1} is above its maximum allowed limit of {2:N0} active {3}.",
                        groupRole.Name.Pluralize().ToLower(),
                        groupType.GroupTerm.ToLower(),
                        groupRole.MaxCount,
                        groupType.GroupMemberTerm.Pluralize(groupRole.MaxCount == 1)).ToLower();
                    return(false);
                }
            }

            // if the GroupMember is getting Added (or if Person or Role is different), and if this Group has requirements that must be met before the person is added, check those
            if (this.IsNewOrChangedGroupMember(rockContext))
            {
                var requirementStatusesRequiredForAdd = group.PersonMeetsGroupRequirements(rockContext, this.PersonId, this.GroupRoleId)
                                                        .Where(a => a.MeetsGroupRequirement == MeetsGroupRequirement.NotMet &&
                                                               ((a.GroupRequirement.GroupRequirementType.RequirementCheckType != RequirementCheckType.Manual) && (a.GroupRequirement.MustMeetRequirementToAddMember == true)));

                if (requirementStatusesRequiredForAdd.Any())
                {
                    // deny if any of the non-manual MustMeetRequirementToAddMember requirements are not met
                    errorMessage = "This person must meet the following requirements before they are added to this group: "
                                   + requirementStatusesRequiredForAdd
                                   .Select(a => string.Format("{0}", a.GroupRequirement.GroupRequirementType))
                                   .ToList().AsDelimited(", ");

                    return(false);
                }
            }

            // check group capacity
            if (groupType.GroupCapacityRule == GroupCapacityRule.Hard && group.GroupCapacity.HasValue)
            {
                var currentActiveGroupMemberCount = group.ActiveMembers().Count();

                // check if this would be adding an active group member (new active group member or changing existing group member status to active)
                if (
                    (this.Id.Equals(0) && this.GroupMemberStatus == GroupMemberStatus.Active) ||
                    (!this.Id.Equals(0) &&
                     existingGroupMembership.Where(m => m.Id == this.Id && m.GroupMemberStatus != GroupMemberStatus.Active).Any() &&
                     this.GroupMemberStatus == GroupMemberStatus.Active)
                    )
                {
                    if (currentActiveGroupMemberCount + 1 > group.GroupCapacity)
                    {
                        errorMessage = "Adding this individual would put the group over capacity.";
                        return(false);
                    }
                }
            }

            return(true);
        }