Exemple #1
0
        public BlockActionResult SaveMember(Guid groupMemberGuid, MemberDataViewModel groupMemberData)
        {
            using (var rockContext = new RockContext())
            {
                var member = new GroupMemberService(rockContext).Get(groupMemberGuid);

                if (member == null || (!member.Group.IsAuthorized(Authorization.EDIT, RequestContext.CurrentPerson) && !member.Group.IsAuthorized(Authorization.MANAGE_MEMBERS, RequestContext.CurrentPerson)))
                {
                    return(ActionBadRequest("You are not authorized to edit members of this group."));
                }

                member.LoadAttributes(rockContext);

                // Verify and save the member role.
                if (AllowRoleChange)
                {
                    if (!groupMemberData.RoleGuid.HasValue)
                    {
                        return(ActionBadRequest("Invalid data."));
                    }

                    var groupRole = GroupTypeCache.Get(member.Group.GroupTypeId)
                                    .Roles
                                    .FirstOrDefault(r => r.Guid == groupMemberData.RoleGuid.Value);

                    if (groupRole == null)
                    {
                        return(ActionBadRequest("Invalid data."));
                    }

                    member.GroupRoleId = groupRole.Id;
                }

                // Verify and save the member status.
                if (AllowMemberStatusChange)
                {
                    if (!groupMemberData.MemberStatus.HasValue)
                    {
                        return(ActionBadRequest("Invalid data."));
                    }

                    member.GroupMemberStatus = groupMemberData.MemberStatus.Value;
                }

                // Verify and save the note.
                if (AllowNoteEdit)
                {
                    if (groupMemberData.Note == null)
                    {
                        return(ActionBadRequest("Invalid data."));
                    }

                    member.Note = groupMemberData.Note;
                }

                // Save all the attribute values.
                if (groupMemberData.FieldValues != null)
                {
                    member.LoadAttributes();
                    var attributes = GetEditableAttributes(member);
                    foreach (var attribute in attributes)
                    {
                        if (!groupMemberData.FieldValues.TryGetValue(attribute.Key, out var value))
                        {
                            continue;
                        }

                        member.SetAttributeValue(attribute.Key, value);
                    }
                }

                // Save all changes to database.
                rockContext.WrapTransaction(() =>
                {
                    rockContext.SaveChanges();
                    member.SaveAttributeValues(rockContext);
                });

                return(ActionOk());
            }
        }