Beispiel #1
0
        /// <summary>
        /// Executes the specified workflow.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="action">The action.</param>
        /// <param name="entity">The entity.</param>
        /// <param name="errorMessages">The error messages.</param>
        /// <returns></returns>
        public override bool Execute(RockContext rockContext, WorkflowAction action, Object entity, out List <string> errorMessages)
        {
            errorMessages = new List <string>();

            // Determine which group to add the person to
            Group group       = null;
            int?  groupRoleId = null;

            var guidGroupAttribute = GetAttributeValue(action, "Group").AsGuidOrNull();

            if (guidGroupAttribute.HasValue)
            {
                var attributeGroup = AttributeCache.Get(guidGroupAttribute.Value, rockContext);
                if (attributeGroup != null)
                {
                    var groupGuid = action.GetWorklowAttributeValue(guidGroupAttribute.Value).AsGuidOrNull();

                    if (groupGuid.HasValue)
                    {
                        group = new GroupService(rockContext).Get(groupGuid.Value);

                        if (group != null)
                        {
                            // use the group's grouptype's default group role if a group role wasn't specified
                            groupRoleId = GroupTypeCache.Get(group.GroupTypeId).DefaultGroupRoleId;
                        }
                    }
                }
            }

            if (group == null)
            {
                errorMessages.Add("No group was provided");
            }

            if (!groupRoleId.HasValue)
            {
                errorMessages.Add("Provided group doesn't have a default group role");
            }

            // determine the person that will be added to the group
            Person person = null;

            // get the Attribute.Guid for this workflow's Person Attribute so that we can lookup the value
            var guidPersonAttribute = GetAttributeValue(action, "Person").AsGuidOrNull();

            if (guidPersonAttribute.HasValue)
            {
                var attributePerson = AttributeCache.Get(guidPersonAttribute.Value, rockContext);
                if (attributePerson != null)
                {
                    string attributePersonValue = action.GetWorklowAttributeValue(guidPersonAttribute.Value);
                    if (!string.IsNullOrWhiteSpace(attributePersonValue))
                    {
                        if (attributePerson.FieldType.Class == typeof(Rock.Field.Types.PersonFieldType).FullName)
                        {
                            Guid personAliasGuid = attributePersonValue.AsGuid();
                            if (!personAliasGuid.IsEmpty())
                            {
                                person = new PersonAliasService(rockContext).Queryable()
                                         .Where(a => a.Guid.Equals(personAliasGuid))
                                         .Select(a => a.Person)
                                         .FirstOrDefault();
                            }
                        }
                        else
                        {
                            errorMessages.Add("The attribute used to provide the person was not of type 'Person'.");
                        }
                    }
                }
            }

            if (person == null)
            {
                errorMessages.Add(string.Format("Person could not be found for selected value ('{0}')!", guidPersonAttribute.ToString()));
            }

            // Add Person to Group
            if (!errorMessages.Any())
            {
                var  groupMemberService = new GroupMemberService(rockContext);
                var  groupMember        = groupMemberService.GetByGroupIdAndPersonIdAndPreferredGroupRoleId(group.Id, person.Id, groupRoleId.Value);
                bool isNew = false;
                if (groupMember == null)
                {
                    groupMember          = new GroupMember();
                    groupMember.PersonId = person.Id;
                    groupMember.GroupId  = group.Id;
                    isNew = true;
                }
                else
                {
                    action.AddLogEntry($"{person.FullName} was already a member of the selected group.", true);
                }

                groupMember.GroupRoleId       = groupRoleId.Value;
                groupMember.GroupMemberStatus = GroupMemberStatus.Active;

                if (groupMember.IsValidGroupMember(rockContext))
                {
                    if (isNew)
                    {
                        groupMemberService.Add(groupMember);
                    }
                    rockContext.SaveChanges();
                }
                else
                {
                    // if the group member couldn't be added (for example, one of the group membership rules didn't pass), add the validation messages to the errormessages
                    errorMessages.AddRange(groupMember.ValidationResults.Select(a => a.ErrorMessage));
                }
            }

            errorMessages.ForEach(m => action.AddLogEntry(m, true));

            return(true);
        }
Beispiel #2
0
        /// <summary>
        /// Executes the specified workflow.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="action">The action.</param>
        /// <param name="entity">The entity.</param>
        /// <param name="errorMessages">The error messages.</param>
        /// <returns></returns>
        public override bool Execute(RockContext rockContext, WorkflowAction action, object entity, out List <string> errorMessages)
        {
            errorMessages = new List <string>();

            // Determine which group to add the person to
            Group group       = null;
            int?  groupRoleId = null;

            var guidGroupAttribute = GetAttributeValue(action, AttributeKey.GroupKey).AsGuidOrNull();

            if (guidGroupAttribute.HasValue)
            {
                var attributeGroup = AttributeCache.Get(guidGroupAttribute.Value, rockContext);
                if (attributeGroup != null)
                {
                    var groupGuid = action.GetWorkflowAttributeValue(guidGroupAttribute.Value).AsGuidOrNull();

                    if (groupGuid.HasValue)
                    {
                        group = new GroupService(rockContext).Get(groupGuid.Value);

                        if (group != null)
                        {
                            // use the group's grouptype's default group role if a group role wasn't specified
                            groupRoleId = GroupTypeCache.Get(group.GroupTypeId).DefaultGroupRoleId;
                        }
                    }
                }
            }

            if (group == null)
            {
                errorMessages.Add("No group was provided");
            }
            else
            {
                // Check if this is a security group and show an error if that functionality has been disabled.
                var disableSecurityGroups = GetAttributeValue(action, AttributeKey.DisableSecurityGroups).AsBooleanOrNull() ?? false;
                if (group.IsSecurityRoleOrSecurityGroupType() && disableSecurityGroups)
                {
                    errorMessages.Add($"\"{group.Name}\" is a Security group. The settings for this workflow action do not allow it to add a person to a security group.");
                }

                // If LimitToGroupsOfType has any values check if this Group's GroupType is in that list
                var limitToGroupsOfTypeGuid = GetAttributeValue(action, AttributeKey.LimitToGroupsOfType).AsGuidOrNull();
                if (limitToGroupsOfTypeGuid.HasValue)
                {
                    var limitToGroupType    = GroupTypeCache.Get(limitToGroupsOfTypeGuid.Value);
                    var limitToGroupTypeIds = new GroupTypeService(rockContext).GetChildGroupTypes(limitToGroupType.Id).Select(a => a.Id).ToList();
                    limitToGroupTypeIds.Add(limitToGroupType.Id);

                    if (!limitToGroupTypeIds.Contains(group.GroupTypeId))
                    {
                        errorMessages.Add($"The group type for group \"{group.Name} is \"{group.GroupType.Name}\". This action is configured to only add persons to groups of type \"{limitToGroupType.Name}\" and its child types.");
                    }
                }

                // If LimitToGroupsUnderSpecificParentGroup has any values check if this Group is a child of that group
                var limitToChildGroupsOfGroupGuid = GetAttributeValue(action, AttributeKey.LimitToGroupsUnderSpecificParentGroup).AsGuidOrNull();
                if (limitToChildGroupsOfGroupGuid.HasValue)
                {
                    var groupService = new GroupService(rockContext);
                    var limitToChildGroupsOfGroup = groupService.Get(limitToChildGroupsOfGroupGuid.Value);
                    var limitToGroupIds           = groupService.GetAllDescendentGroupIds(limitToChildGroupsOfGroup.Id, true);
                    limitToGroupIds.Add(limitToChildGroupsOfGroup.Id);

                    if (!limitToGroupIds.Contains(group.Id))
                    {
                        errorMessages.Add($"Cannot add the person to group \"{group.Name}\". This workflow action is configured to only add persons to groups that are a descendant of Group {limitToChildGroupsOfGroup.Name}.");
                    }
                }
            }

            if (!groupRoleId.HasValue)
            {
                errorMessages.Add("Provided group doesn't have a default group role");
            }

            // determine the person that will be added to the group
            Person person = null;

            // get the Attribute.Guid for this workflow's Person Attribute so that we can lookup the value
            var guidPersonAttribute = GetAttributeValue(action, AttributeKey.PersonKey).AsGuidOrNull();

            if (guidPersonAttribute.HasValue)
            {
                var attributePerson = AttributeCache.Get(guidPersonAttribute.Value, rockContext);
                if (attributePerson != null)
                {
                    string attributePersonValue = action.GetWorkflowAttributeValue(guidPersonAttribute.Value);
                    if (!string.IsNullOrWhiteSpace(attributePersonValue))
                    {
                        if (attributePerson.FieldType.Class == typeof(Rock.Field.Types.PersonFieldType).FullName)
                        {
                            Guid personAliasGuid = attributePersonValue.AsGuid();
                            if (!personAliasGuid.IsEmpty())
                            {
                                person = new PersonAliasService(rockContext).Queryable()
                                         .Where(a => a.Guid.Equals(personAliasGuid))
                                         .Select(a => a.Person)
                                         .FirstOrDefault();
                            }
                        }
                        else
                        {
                            errorMessages.Add("The attribute used to provide the person was not of type 'Person'.");
                        }
                    }
                }
            }

            if (person == null)
            {
                errorMessages.Add(string.Format("Person could not be found for selected value ('{0}')!", guidPersonAttribute.ToString()));
            }

            // Add Person to Group
            if (!errorMessages.Any())
            {
                var  groupMemberService = new GroupMemberService(rockContext);
                var  groupMember        = groupMemberService.GetByGroupIdAndPersonIdAndPreferredGroupRoleId(group.Id, person.Id, groupRoleId.Value);
                bool isNew = false;
                if (groupMember == null)
                {
                    groupMember          = new GroupMember();
                    groupMember.PersonId = person.Id;
                    groupMember.GroupId  = group.Id;
                    isNew = true;
                }
                else
                {
                    action.AddLogEntry($"{person.FullName} was already a member of the selected group.", true);
                }

                groupMember.GroupRoleId       = groupRoleId.Value;
                groupMember.GroupMemberStatus = GroupMemberStatus.Active;

                if (groupMember.IsValidGroupMember(rockContext))
                {
                    if (isNew)
                    {
                        groupMemberService.Add(groupMember);
                    }

                    rockContext.SaveChanges();
                }
                else
                {
                    // if the group member couldn't be added (for example, one of the group membership rules didn't pass), add the validation messages to the errormessages
                    errorMessages.AddRange(groupMember.ValidationResults.Select(a => a.ErrorMessage));
                }
            }

            errorMessages.ForEach(m => action.AddLogEntry(m, true));

            return(true);
        }