/// <summary>
        /// Handles the Click event of the DeleteGroupMember control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Rock.Web.UI.Controls.RowEventArgs" /> instance containing the event data.</param>
        protected void DeleteGroupMember_Click(object sender, Rock.Web.UI.Controls.RowEventArgs e)
        {
            RockTransactionScope.WrapTransaction(() =>
            {
                GroupMemberService groupMemberService = new GroupMemberService();
                GroupMember groupMember = groupMemberService.Get(e.RowKeyId);
                if (groupMember != null)
                {
                    string errorMessage;
                    if (!groupMemberService.CanDelete(groupMember, out errorMessage))
                    {
                        mdGridWarning.Show(errorMessage, ModalAlertType.Information);
                        return;
                    }

                    int groupId = groupMember.GroupId;

                    groupMemberService.Delete(groupMember, CurrentPersonId);
                    groupMemberService.Save(groupMember, CurrentPersonId);

                    Group group = new GroupService().Get(groupId);
                    if (group.IsSecurityRole)
                    {
                        // person removed from SecurityRole, Flush
                        Rock.Security.Authorization.Flush();
                    }
                }
            });

            BindGroupMembersGrid();
        }
Esempio n. 2
0
        /// <summary>
        /// Handles the Click event of the DeleteGroupMember control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Rock.Web.UI.Controls.RowEventArgs" /> instance containing the event data.</param>
        protected void DeleteGroupMember_Click(object sender, Rock.Web.UI.Controls.RowEventArgs e)
        {
            RockContext        rockContext        = new RockContext();
            GroupMemberService groupMemberService = new GroupMemberService(rockContext);
            GroupMember        groupMember        = groupMemberService.Get(e.RowKeyId);

            if (groupMember != null)
            {
                string errorMessage;
                if (!groupMemberService.CanDelete(groupMember, out errorMessage))
                {
                    mdGridWarning.Show(errorMessage, ModalAlertType.Information);
                    return;
                }

                int groupId = groupMember.GroupId;

                groupMemberService.Delete(groupMember);
                rockContext.SaveChanges();

                Group group = new GroupService(rockContext).Get(groupId);
                if (group.IsSecurityRole || group.GroupType.Guid.Equals(Rock.SystemGuid.GroupType.GROUPTYPE_SECURITY_ROLE.AsGuid()))
                {
                    // person removed from SecurityRole, Flush
                    Rock.Security.Role.Flush(group.Id);
                    Rock.Security.Authorization.Flush();
                }
            }

            BindGroupMembersGrid();
        }
Esempio n. 3
0
        public BlockActionResult RemoveMember(Guid groupMemberGuid)
        {
            using (var rockContext = new RockContext())
            {
                var groupMemberService = new GroupMemberService(rockContext);
                var member             = groupMemberService.Queryable()
                                         .Include(m => m.Group.GroupType)
                                         .FirstOrDefault(m => m.Guid == 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."));
                }

                var  groupMemberHistoricalService = new GroupMemberHistoricalService(rockContext);
                bool archive = false;

                if (member.Group.GroupType.EnableGroupHistory == true && groupMemberHistoricalService.Queryable().Any(a => a.GroupMemberId == member.Id))
                {
                    // if the group has GroupHistory enabled, and this group
                    // member has group member history snapshots, then we only
                    // archive.
                    archive = true;
                }
                else if (!groupMemberService.CanDelete(member, out var errorMessage))
                {
                    return(ActionBadRequest(errorMessage));
                }

                int groupId = member.GroupId;

                if (archive)
                {
                    // NOTE: Delete will AutoArchive, but since we know that we
                    // need to archive, we can call .Archive directly
                    groupMemberService.Archive(member, RequestContext.CurrentPerson?.PrimaryAliasId, true);
                }
                else
                {
                    groupMemberService.Delete(member, true);
                }

                rockContext.SaveChanges();

                return(ActionOk());
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Handles the Delete event of the gList control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RowEventArgs"/> instance containing the event data.</param>
        protected void gList_Delete(object sender, RowEventArgs e)
        {
            RockTransactionScope.WrapTransaction(() =>
            {
                var groupMemberService = new GroupMemberService();
                int groupMemberId      = (int)e.RowKeyValue;

                GroupMember groupMember = groupMemberService.Get(groupMemberId);
                if (groupMember != null)
                {
                    // check if person can be removed from the Group and also check if person can be removed from all the person assigned competencies
                    string errorMessage;
                    if (!groupMemberService.CanDelete(groupMember, out errorMessage))
                    {
                        mdGridWarning.Show(errorMessage, ModalAlertType.Information);
                        return;
                    }

                    var competencyPersonService = new ResidencyService <CompetencyPerson>();
                    var personCompetencyList    = competencyPersonService.Queryable().Where(a => a.PersonId.Equals(groupMember.PersonId));
                    foreach (var item in personCompetencyList)
                    {
                        if (!competencyPersonService.CanDelete(item, out errorMessage))
                        {
                            mdGridWarning.Show(errorMessage, ModalAlertType.Information);
                            return;
                        }
                    }

                    // if you made it this far, delete all person's assigned competencies, and finally delete from Group
                    foreach (var item in personCompetencyList)
                    {
                        competencyPersonService.Delete(item, CurrentPersonId);
                        competencyPersonService.Save(item, CurrentPersonId);
                    }

                    groupMemberService.Delete(groupMember, CurrentPersonId);
                    groupMemberService.Save(groupMember, CurrentPersonId);
                }
            });

            BindGrid();
        }
        /// <summary>
        /// Handles the SaveClick event of the mdPlaceElsewhere control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void mdPlaceElsewhere_SaveClick(object sender, EventArgs e)
        {
            using (var rockContext = new RockContext())
            {
                var groupService       = new GroupService(rockContext);
                var groupMemberService = new GroupMemberService(rockContext);
                var groupMember        = groupMemberService.Get(hfPlaceElsewhereGroupMemberId.Value.AsInteger());
                if (groupMember != null)
                {
                    string errorMessage;
                    if (!groupMemberService.CanDelete(groupMember, out errorMessage))
                    {
                        nbPlaceElsewhereWarning.Text = errorMessage;
                        return;
                    }


                    var trigger = _group.GetGroupMemberWorkflowTriggers().FirstOrDefault(a => a.Id == hfPlaceElsewhereTriggerId.Value.AsInteger());
                    if (trigger != null)
                    {
                        // create a transaction for the selected trigger
                        var transaction = new Rock.Transactions.GroupMemberPlacedElsewhereTransaction(groupMember, tbPlaceElsewhereNote.Text, trigger);

                        // delete the group member from the current group
                        groupMemberService.Delete(groupMember);

                        rockContext.SaveChanges();

                        // queue up the transaction
                        Rock.Transactions.RockQueue.TransactionQueue.Enqueue(transaction);
                    }
                }

                mdPlaceElsewhere.Hide();
                mdPlaceElsewhere.Visible = false;
                BindGroupMembersGrid();
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Handles the Delete event of the gGroups control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RowEventArgs" /> instance containing the event data.</param>
        protected void gGroups_Delete(object sender, RowEventArgs e)
        {
            var          rockContext  = new RockContext();
            GroupService groupService = new GroupService(rockContext);
            AuthService  authService  = new AuthService(rockContext);
            Group        group        = groupService.Get(e.RowKeyId);

            if (group != null)
            {
                string errorMessage;
                bool   isSecurityRoleGroup = group.IsSecurityRole || group.GroupType.Guid.Equals(Rock.SystemGuid.GroupType.GROUPTYPE_SECURITY_ROLE.AsGuid());
                int    personEntityTypeId  = EntityTypeCache.Read("Rock.Model.Person").Id;

                if (ContextTypesRequired.Any(a => a.Id == personEntityTypeId))
                {
                    var personContext = ContextEntity <Person>();
                    GroupMemberService            groupMemberService = new GroupMemberService(rockContext);
                    RegistrationRegistrantService registrantService  = new RegistrationRegistrantService(rockContext);
                    GroupMember groupMember = group.Members.SingleOrDefault(a => a.PersonId == personContext.Id);

                    if (!(group.IsAuthorized(Authorization.EDIT, this.CurrentPerson) || group.IsAuthorized(Authorization.MANAGE_MEMBERS, this.CurrentPerson)))
                    {
                        mdGridWarning.Show("You are not authorized to delete members from this group", ModalAlertType.Information);
                        return;
                    }

                    if (!groupMemberService.CanDelete(groupMember, out errorMessage))
                    {
                        mdGridWarning.Show(errorMessage, ModalAlertType.Information);
                        return;
                    }

                    foreach (var registrant in registrantService.Queryable().Where(r => r.GroupMemberId == groupMember.Id))
                    {
                        registrant.GroupMemberId = null;
                    }

                    if (group.IsSecurityRole || group.GroupType.Guid.Equals(Rock.SystemGuid.GroupType.GROUPTYPE_SECURITY_ROLE.AsGuid()))
                    {
                        // person removed from SecurityRole, Flush
                        Rock.Security.Role.Flush(group.Id);
                    }

                    groupMemberService.Delete(groupMember);
                }
                else
                {
                    if (!group.IsAuthorized(Authorization.EDIT, this.CurrentPerson))
                    {
                        mdGridWarning.Show("You are not authorized to delete this group", ModalAlertType.Information);
                        return;
                    }


                    if (!groupService.CanDelete(group, out errorMessage))
                    {
                        mdGridWarning.Show(errorMessage, ModalAlertType.Information);
                        return;
                    }


                    if (isSecurityRoleGroup)
                    {
                        Rock.Security.Role.Flush(group.Id);
                        foreach (var auth in authService.Queryable().Where(a => a.GroupId == group.Id).ToList())
                        {
                            authService.Delete(auth);
                        }
                    }

                    groupService.Delete(group);
                }

                rockContext.SaveChanges();

                if (isSecurityRoleGroup)
                {
                    Rock.Security.Authorization.Flush();
                }
            }

            BindGrid();
        }
Esempio n. 7
0
        /// <summary>
        /// Handles the Click event of the btnMoveGroupMember control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void btnMoveGroupMember_Click(object sender, EventArgs e)
        {
            var rockContext        = new RockContext();
            var groupMemberService = new GroupMemberService(rockContext);
            var groupMember        = groupMemberService.Get(hfGroupMemberId.Value.AsInteger());

            groupMember.LoadAttributes();
            int destGroupId = gpMoveGroupMember.SelectedValue.AsInteger();
            var destGroup   = new GroupService(rockContext).Get(destGroupId);

            var destGroupMember = groupMemberService.Queryable().Where(a =>
                                                                       a.GroupId == destGroupId &&
                                                                       a.PersonId == groupMember.PersonId &&
                                                                       a.GroupRoleId == grpMoveGroupMember.GroupRoleId).FirstOrDefault();

            if (destGroupMember != null)
            {
                nbMoveGroupMemberWarning.Visible = true;
                nbMoveGroupMemberWarning.Text    = string.Format("{0} is already in {1}", groupMember.Person, destGroupMember.Group);
                return;
            }

            if (!grpMoveGroupMember.GroupRoleId.HasValue)
            {
                nbMoveGroupMemberWarning.Visible = true;
                nbMoveGroupMemberWarning.Text    = string.Format("Please select a Group Role");
                return;
            }

            string canDeleteWarning;

            if (!groupMemberService.CanDelete(groupMember, out canDeleteWarning))
            {
                nbMoveGroupMemberWarning.Visible = true;
                nbMoveGroupMemberWarning.Text    = string.Format("Unable to remove {0} from {1}: {2}", groupMember.Person, groupMember.Group, canDeleteWarning);
                return;
            }

            destGroupMember             = new GroupMember();
            destGroupMember.GroupId     = destGroupId;
            destGroupMember.GroupRoleId = grpMoveGroupMember.GroupRoleId.Value;
            destGroupMember.PersonId    = groupMember.PersonId;
            destGroupMember.LoadAttributes();

            foreach (var attribute in groupMember.Attributes)
            {
                if (destGroupMember.Attributes.Any(a => a.Key == attribute.Key && a.Value.FieldTypeId == attribute.Value.FieldTypeId))
                {
                    destGroupMember.SetAttributeValue(attribute.Key, groupMember.GetAttributeValue(attribute.Key));
                }
            }

            // Un-link any registrant records that point to this group member.
            foreach (var registrant in new RegistrationRegistrantService(rockContext).Queryable()
                     .Where(r => r.GroupMemberId == groupMember.Id))
            {
                registrant.GroupMemberId = null;
            }

            rockContext.WrapTransaction(() =>
            {
                groupMemberService.Add(destGroupMember);
                rockContext.SaveChanges();
                destGroupMember.SaveAttributeValues(rockContext);

                // move any Note records that were associated with the old groupMember to the new groupMember record
                if (cbMoveGroupMemberMoveNotes.Checked)
                {
                    destGroupMember.Note        = groupMember.Note;
                    int groupMemberEntityTypeId = EntityTypeCache.GetId <Rock.Model.GroupMember>().Value;
                    var noteService             = new NoteService(rockContext);
                    var groupMemberNotes        = noteService.Queryable().Where(a => a.NoteType.EntityTypeId == groupMemberEntityTypeId && a.EntityId == groupMember.Id);
                    foreach (var note in groupMemberNotes)
                    {
                        note.EntityId = destGroupMember.Id;
                    }

                    rockContext.SaveChanges();
                }

                groupMemberService.Delete(groupMember);
                rockContext.SaveChanges();

                destGroupMember.CalculateRequirements(rockContext, true);
            });

            var queryString = new Dictionary <string, string>();

            queryString.Add("GroupMemberId", destGroupMember.Id.ToString());
            this.NavigateToPage(this.RockPage.Guid, queryString);
        }
Esempio n. 8
0
        /// <summary>
        /// Handles the Click event of the delete/archive button in the grid
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RowEventArgs" /> instance containing the event data.</param>
        protected void gGroups_DeleteOrArchive(object sender, RowEventArgs e)
        {
            var                rockContext        = new RockContext();
            GroupService       groupService       = new GroupService(rockContext);
            GroupMemberService groupMemberService = new GroupMemberService(rockContext);
            AuthService        authService        = new AuthService(rockContext);
            Group              group       = null;
            GroupMember        groupMember = null;

            if (GroupListGridMode == GridListGridMode.GroupsPersonMemberOf)
            {
                // the DataKey Id of the grid is GroupMemberId
                groupMember = groupMemberService.Get(e.RowKeyId);
                if (groupMember != null)
                {
                    group = groupMember.Group;
                }
            }
            else
            {
                // the DataKey Id of the grid is GroupId
                group = groupService.Get(e.RowKeyId);
            }

            if (group != null)
            {
                bool isSecurityRoleGroup = group.IsSecurityRole || group.GroupType.Guid.Equals(Rock.SystemGuid.GroupType.GROUPTYPE_SECURITY_ROLE.AsGuid());

                if (GroupListGridMode == GridListGridMode.GroupsPersonMemberOf)
                {
                    // Grid is in 'Groups that Person is member of' mode
                    GroupMemberHistoricalService groupMemberHistoricalService = new GroupMemberHistoricalService(rockContext);

                    bool archive = false;
                    if (group.GroupType.EnableGroupHistory == true && groupMemberHistoricalService.Queryable().Any(a => a.GroupMemberId == groupMember.Id))
                    {
                        // if the group has GroupHistory enabled, and this group member has group member history snapshots, they were prompted to Archive
                        archive = true;
                    }
                    else
                    {
                        if (!(group.IsAuthorized(Authorization.EDIT, this.CurrentPerson) || group.IsAuthorized(Authorization.MANAGE_MEMBERS, this.CurrentPerson)))
                        {
                            mdGridWarning.Show("You are not authorized to delete members from this group", ModalAlertType.Information);
                            return;
                        }

                        string errorMessage;
                        if (!groupMemberService.CanDelete(groupMember, out errorMessage))
                        {
                            mdGridWarning.Show(errorMessage, ModalAlertType.Information);
                            return;
                        }
                    }

                    int groupId = groupMember.GroupId;

                    if (archive)
                    {
                        // NOTE: Delete will AutoArchive, but since we know that we need to archive, we can call .Archive directly
                        groupMemberService.Archive(groupMember, this.CurrentPersonAliasId, true);
                    }
                    else
                    {
                        groupMemberService.Delete(groupMember, true);
                    }

                    rockContext.SaveChanges();
                }
                else
                {
                    // Grid is in 'Group List' mode
                    bool archive = false;
                    var  groupMemberHistoricalService = new GroupHistoricalService(rockContext);
                    if (group.GroupType.EnableGroupHistory == true && groupMemberHistoricalService.Queryable().Any(a => a.GroupId == group.Id))
                    {
                        // if the group has GroupHistory enabled and has history snapshots, and they were prompted to Archive
                        archive = true;
                    }

                    if (archive)
                    {
                        if (!group.IsAuthorized(Authorization.EDIT, this.CurrentPerson))
                        {
                            mdGridWarning.Show("You are not authorized to archive this group", ModalAlertType.Information);
                            return;
                        }

                        // NOTE: groupService.Delete will automatically Archive instead Delete if this Group has GroupHistory enabled, but since this block has UI logic for Archive vs Delete, we can do a direct Archive
                        groupService.Archive(group, this.CurrentPersonAliasId, true);
                    }
                    else
                    {
                        if (!group.IsAuthorized(Authorization.EDIT, this.CurrentPerson))
                        {
                            mdGridWarning.Show("You are not authorized to delete this group", ModalAlertType.Information);
                            return;
                        }

                        string errorMessage;
                        if (!groupService.CanDelete(group, out errorMessage))
                        {
                            mdGridWarning.Show(errorMessage, ModalAlertType.Information);
                            return;
                        }

                        groupService.Delete(group, true);
                    }
                }

                rockContext.SaveChanges();

                if (isSecurityRoleGroup)
                {
                    Rock.Security.Authorization.Clear();
                }
            }

            BindGrid();
        }