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()); } }
/// <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(); }