/// <summary>
        /// Adds the person as group member.
        /// </summary>
        /// <param name="person">The person.</param>
        /// <param name="rockContext">The rock context.</param>
        private void AddPersonAsGroupMember(Person person, RockContext rockContext)
        {
            GroupMemberService groupMemberService = new GroupMemberService(rockContext);
            GroupTypeRole      role = new GroupTypeRoleService(rockContext).Get(_group.GroupType.DefaultGroupRoleId ?? 0);

            var groupMember = new GroupMember {
                Id = 0
            };

            groupMember.GroupId = _group.Id;

            // check to see if the person is already a member of the group/role
            var existingGroupMember = groupMemberService.GetByGroupIdAndPersonIdAndGroupRoleId(_group.Id, person.Id, _group.GroupType.DefaultGroupRoleId ?? 0);

            if (existingGroupMember != null)
            {
                return;
            }

            groupMember.PersonId          = person.Id;
            groupMember.GroupRoleId       = role.Id;
            groupMember.GroupMemberStatus = GroupMemberStatus.Active;

            if (groupMember.Id.Equals(0))
            {
                groupMemberService.Add(groupMember);
            }

            rockContext.SaveChanges();
        }
Exemplo n.º 2
0
        void lb_Click(object sender, EventArgs e)
        {
            if (CurrentPersonId.HasValue)
            {
                LinkButton lb = sender as LinkButton;
                if (lb != null)
                {
                    int groupId = 0;
                    if (Int32.TryParse(lb.Attributes["group"], out groupId))
                    {
                        int roleId = 0;
                        if (!Int32.TryParse(AttributeValue("GroupRole"), out roleId))
                        {
                            roleId = 0;
                        }

                        var group = groupService.Get(groupId);
                        if (group != null &&
                            group.AttributeValues.ContainsKey(_videoAttributeKey))
                        {
                            hfVideoUrl.Value = group.AttributeValues[_videoAttributeKey][0].Value;

                            GroupMemberService memberService = new GroupMemberService();
                            var groupMember = memberService.GetByGroupIdAndPersonIdAndGroupRoleId(
                                groupId, CurrentPersonId.Value, roleId);
                            if (groupMember == null)
                            {
                                groupMember             = new GroupMember();
                                groupMember.GroupId     = groupId;
                                groupMember.PersonId    = CurrentPersonId.Value;
                                groupMember.GroupRoleId = roleId;
                                memberService.Add(groupMember, CurrentPersonId);
                                memberService.Save(groupMember, CurrentPersonId);
                            }

                            HtmlGenericControl li = lb.Parent as HtmlGenericControl;
                            if (li != null)
                            {
                                li.RemoveCssClass("not-viewed");
                                li.AddCssClass("viewed");
                            }
                        }
                    }
                }
            }
        }
        private void AddGroupMember(Group group, RockDropDownList ddlRole)
        {
            int roleId = ddlRole.SelectedValue.AsInteger();

            if (Person != null &&
                roleId != 0 &&
                group.IsAuthorized(Authorization.EDIT, CurrentPerson))
            {
                RockContext        rockContext        = new RockContext();
                GroupMemberService groupMemberService = new GroupMemberService(rockContext);
                if (groupMemberService.GetByGroupIdAndPersonIdAndGroupRoleId(group.Id, Person.Id, roleId) == null)
                {
                    GroupMember groupMember = new GroupMember()
                    {
                        GroupId     = group.Id,
                        PersonId    = Person.Id,
                        GroupRoleId = roleId
                    };
                    groupMemberService.Add(groupMember);
                    rockContext.SaveChanges();
                }
            }
            ShowEdit();
        }
Exemplo n.º 4
0
        /// <summary>
        /// Handles the Click event of the btnSaveGroupMember 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 btnSaveGroupMember_Click(object sender, EventArgs e)
        {
            var rockContext = new RockContext();
            GroupMemberService groupMemberService = new GroupMemberService(rockContext);

            GroupTypeRole role = new GroupTypeRoleService(rockContext).Get(ddlGroupRole.SelectedValueAsInt() ?? 0);

            var groupMember = groupMemberService.Get(this.CurrentGroupMemberId);

            if (this.CurrentGroupMemberId == 0)
            {
                groupMember = new GroupMember {
                    Id = 0
                };
                groupMember.GroupId = _groupId;

                // check to see if the person is alread a member of the gorup/role
                var existingGroupMember = groupMemberService.GetByGroupIdAndPersonIdAndGroupRoleId(
                    _groupId, ppGroupMemberPerson.SelectedValue ?? 0, ddlGroupRole.SelectedValueAsId() ?? 0);

                if (existingGroupMember != null)
                {
                    // if so, don't add and show error message
                    var person = new PersonService(rockContext).Get((int)ppGroupMemberPerson.PersonId);

                    nbGroupMemberErrorMessage.Title = "Person Already In Group";
                    nbGroupMemberErrorMessage.Text  = string.Format(
                        "{0} already belongs to the {1} role for this {2}, and cannot be added again with the same role.",
                        person.FullName,
                        ddlGroupRole.SelectedItem.Text,
                        role.GroupType.GroupTerm,
                        RockPage.PageId,
                        existingGroupMember.Id);
                    return;
                }
            }

            groupMember.PersonId    = ppGroupMemberPerson.PersonId.Value;
            groupMember.GroupRoleId = role.Id;

            // set their status.  If HideInactiveGroupMemberStatus is True, and they are already Inactive, keep their status as Inactive;
            bool hideGroupMemberInactiveStatus = this.GetAttributeValue("HideInactiveGroupMemberStatus").AsBooleanOrNull() ?? false;
            var  selectedStatus = rblStatus.SelectedValueAsEnumOrNull <GroupMemberStatus>();

            if (!selectedStatus.HasValue)
            {
                if (hideGroupMemberInactiveStatus)
                {
                    selectedStatus = GroupMemberStatus.Inactive;
                }
                else
                {
                    selectedStatus = GroupMemberStatus.Active;
                }
            }

            groupMember.GroupMemberStatus = selectedStatus.Value;

            groupMember.LoadAttributes();

            Rock.Attribute.Helper.GetEditValues(phAttributes, groupMember);

            if (!Page.IsValid)
            {
                return;
            }

            // if the groupMember IsValid is false, and the UI controls didn't report any errors, it is probably because the custom rules of GroupMember didn't pass.
            // So, make sure a message is displayed in the validation summary
            cvEditGroupMember.IsValid = groupMember.IsValid;

            if (!cvEditGroupMember.IsValid)
            {
                cvEditGroupMember.ErrorMessage = groupMember.ValidationResults.Select(a => a.ErrorMessage).ToList().AsDelimited("<br />");
                return;
            }

            // using WrapTransaction because there are two Saves
            rockContext.WrapTransaction(() =>
            {
                if (groupMember.Id.Equals(0))
                {
                    groupMemberService.Add(groupMember);
                }

                rockContext.SaveChanges();
                groupMember.SaveAttributeValues(rockContext);
            });

            Group group = new GroupService(rockContext).Get(groupMember.GroupId);

            if (group.IsSecurityRole || group.GroupType.Guid.Equals(Rock.SystemGuid.GroupType.GROUPTYPE_SECURITY_ROLE.AsGuid()))
            {
                Rock.Security.Role.Flush(group.Id);
            }

            pnlEditGroupMember.Visible = false;
            pnlGroupView.Visible       = true;
            DisplayViewGroup();
            this.IsEditingGroupMember = false;
        }
Exemplo n.º 5
0
        protected void btnSaveGroupMember_Click(object sender, EventArgs e)
        {
            var rockContext = new RockContext();
            GroupMemberService groupMemberService = new GroupMemberService(rockContext);

            GroupTypeRole role = new GroupTypeRoleService(rockContext).Get(ddlGroupRole.SelectedValueAsInt() ?? 0);

            var groupMember = groupMemberService.Get(this.CurrentGroupMemberId);

            if (this.CurrentGroupMemberId == 0)
            {
                groupMember = new GroupMember {
                    Id = 0
                };
                groupMember.GroupId = _groupId;

                // check to see if the person is alread a member of the gorup/role
                var existingGroupMember = groupMemberService.GetByGroupIdAndPersonIdAndGroupRoleId(
                    _groupId, ppGroupMemberPerson.SelectedValue ?? 0, ddlGroupRole.SelectedValueAsId() ?? 0);

                if (existingGroupMember != null)
                {
                    // if so, don't add and show error message
                    var person = new PersonService(rockContext).Get((int)ppGroupMemberPerson.PersonId);

                    nbGroupMemberErrorMessage.Title = "Person Already In Group";
                    nbGroupMemberErrorMessage.Text  = string.Format(
                        "{0} already belongs to the {1} role for this {2}, and cannot be added again with the same role.",
                        person.FullName,
                        ddlGroupRole.SelectedItem.Text,
                        role.GroupType.GroupTerm,
                        RockPage.PageId,
                        existingGroupMember.Id);
                    return;
                }
            }

            groupMember.PersonId          = ppGroupMemberPerson.PersonId.Value;
            groupMember.GroupRoleId       = role.Id;
            groupMember.GroupMemberStatus = rblStatus.SelectedValueAsEnum <GroupMemberStatus>();

            groupMember.LoadAttributes();

            Rock.Attribute.Helper.GetEditValues(phAttributes, groupMember);

            // using WrapTransaction because there are two Saves
            rockContext.WrapTransaction(() =>
            {
                if (groupMember.Id.Equals(0))
                {
                    groupMemberService.Add(groupMember);
                }

                rockContext.SaveChanges();
                groupMember.SaveAttributeValues(rockContext);
            });

            Group group = new GroupService(rockContext).Get(groupMember.GroupId);

            if (group.IsSecurityRole || group.GroupType.Guid.Equals(Rock.SystemGuid.GroupType.GROUPTYPE_SECURITY_ROLE.AsGuid()))
            {
                Rock.Security.Role.Flush(group.Id);
                Rock.Security.Authorization.Flush();
            }

            pnlEditGroupMember.Visible = false;
            pnlGroupView.Visible       = true;
            DisplayViewGroup();
            this.IsEditingGroupMember = false;
        }
Exemplo n.º 6
0
        /// <summary>
        /// Handles the Click event of the btnSave 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 btnSave_Click(object sender, EventArgs e)
        {
            ClearErrorMessage();

            if (Page.IsValid)
            {
                var rockContext = new RockContext();

                GroupMemberService groupMemberService = new GroupMemberService(rockContext);
                GroupMember        groupMember;

                int groupMemberId = int.Parse(hfGroupMemberId.Value);

                GroupTypeRole role = new GroupTypeRoleService(rockContext).Get(ddlGroupRole.SelectedValueAsInt() ?? 0);

                // check to see if the user selected a role
                if (role == null)
                {
                    nbErrorMessage.Title = "Please select a Role";
                    return;
                }

                // if adding a new group member
                if (groupMemberId.Equals(0))
                {
                    groupMember = new GroupMember {
                        Id = 0
                    };
                    groupMember.GroupId = hfGroupId.ValueAsInt();

                    // check to see if the person is alread a member of the gorup/role
                    var existingGroupMember = groupMemberService.GetByGroupIdAndPersonIdAndGroupRoleId(
                        hfGroupId.ValueAsInt(), ppGroupMemberPerson.SelectedValue ?? 0, ddlGroupRole.SelectedValueAsId() ?? 0);

                    if (existingGroupMember != null)
                    {
                        // if so, don't add and show error message
                        var person = new PersonService(rockContext).Get((int)ppGroupMemberPerson.PersonId);

                        nbErrorMessage.Title = "Person already added";
                        nbErrorMessage.Text  = string.Format(
                            "{0} already belongs to the {1} role for this {2}, and cannot be added again with the same role. <a href=\"/page/{3}?groupMemberId={4}\">Click here</a> to view existing membership.",
                            person.FullName,
                            ddlGroupRole.SelectedItem.Text,
                            role.GroupType.GroupTerm,
                            RockPage.PageId,
                            existingGroupMember.Id);
                        return;
                    }
                }
                else
                {
                    // load existing group member
                    groupMember = groupMemberService.Get(groupMemberId);
                }

                int memberCountInRole = new GroupMemberService(rockContext).Queryable()
                                        .Where(m =>
                                               m.GroupId == groupMember.GroupId &&
                                               m.GroupRoleId == role.Id &&
                                               m.GroupMemberStatus == GroupMemberStatus.Active)
                                        .Count();

                bool roleMembershipAboveMax = false;

                // if adding new active group member..
                if (groupMemberId.Equals(0) && rblStatus.SelectedValueAsEnum <GroupMemberStatus>() == GroupMemberStatus.Active)
                {
                    // verify that active count has not exceeded the max
                    if (role.MaxCount != null && (memberCountInRole + 1) > role.MaxCount)
                    {
                        roleMembershipAboveMax = true;
                    }
                }
                else if (groupMemberId > 0 && (groupMember.GroupRoleId != role.Id || groupMember.GroupMemberStatus != rblStatus.SelectedValueAsEnum <GroupMemberStatus>()) &&
                         rblStatus.SelectedValueAsEnum <GroupMemberStatus>() == GroupMemberStatus.Active)
                {
                    // if existing group member changing role or status..
                    // verify that active count has not exceeded the max
                    if (role.MaxCount != null && (memberCountInRole + 1) > role.MaxCount)
                    {
                        roleMembershipAboveMax = true;
                    }
                }

                // show error if above max.. do not proceed
                if (roleMembershipAboveMax)
                {
                    var person = new PersonService(rockContext).Get((int)ppGroupMemberPerson.PersonId);

                    nbErrorMessage.Title = string.Format("Maximum {0} Exceeded", role.Name.Pluralize());
                    nbErrorMessage.Text  = string.Format(
                        "<br />The number of {0} for this {1} is at or above its maximum allowed limit of {2:N0} active {3}.",
                        role.Name.Pluralize(),
                        role.GroupType.GroupTerm,
                        role.MaxCount,
                        role.MaxCount == 1 ? role.GroupType.GroupMemberTerm : role.GroupType.GroupMemberTerm.Pluralize());

                    return;
                }

                groupMember.PersonId          = ppGroupMemberPerson.PersonId.Value;
                groupMember.GroupRoleId       = role.Id;
                groupMember.GroupMemberStatus = rblStatus.SelectedValueAsEnum <GroupMemberStatus>();

                groupMember.LoadAttributes();

                Rock.Attribute.Helper.GetEditValues(phAttributes, groupMember);

                if (!Page.IsValid)
                {
                    return;
                }

                if (!groupMember.IsValid)
                {
                    return;
                }

                // using WrapTransaction because there are two Saves
                RockTransactionScope.WrapTransaction(() =>
                {
                    if (groupMember.Id.Equals(0))
                    {
                        groupMemberService.Add(groupMember);
                    }

                    groupMember.SaveAttributeValues(rockContext);
                    rockContext.SaveChanges();
                });

                Group group = new GroupService(rockContext).Get(groupMember.GroupId);
                if (group.IsSecurityRole || group.GroupType.Guid.Equals(Rock.SystemGuid.GroupType.GROUPTYPE_SECURITY_ROLE.AsGuid()))
                {
                    Rock.Security.Role.Flush(group.Id);
                    Rock.Security.Authorization.Flush();
                }
            }

            Dictionary <string, string> qryString = new Dictionary <string, string>();

            qryString["GroupId"] = hfGroupId.Value;
            NavigateToParentPage(qryString);
        }
Exemplo n.º 7
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>();
            var  groupAttribute = GetAttributeValue(action, "GroupAttribute");
            Guid groupAttrGuid  = groupAttribute.AsGuid();

            var  personAttribute = GetAttributeValue(action, "PersonAttribute");
            Guid personAttrGuid  = personAttribute.AsGuid();

            var  groupRoleAttr = GetAttributeValue(action, "Group Role");
            Guid groupRoleGuid = groupRoleAttr.AsGuid();
            var  memberStatus  = (GroupMemberStatus)Enum.Parse(typeof(GroupMemberStatus), GetAttributeValue(action, "MemberStatus"));

            if (!groupAttrGuid.IsEmpty())
            {
                string attributeGroupValue = action.GetWorklowAttributeValue(groupAttrGuid);
                Guid   groupGuid           = attributeGroupValue.AsGuid();

                if (!personAttrGuid.IsEmpty())
                {
                    string attributePersonValue = action.GetWorklowAttributeValue(personAttrGuid);
                    Guid   personAliasGuid      = attributePersonValue.AsGuid();

                    if (!groupRoleGuid.IsEmpty())
                    {
                        if (!groupGuid.IsEmpty())
                        {
                            if (!personAliasGuid.IsEmpty())
                            {
                                using (var ctx = new RockContext())
                                {
                                    var group       = (new GroupService(ctx)).Get(groupGuid);
                                    var personAlias = (new PersonAliasService(ctx)).Get(personAliasGuid);
                                    var groupRole   = (new GroupTypeRoleService(ctx)).Get(groupRoleGuid);


                                    if (groupRole != null)
                                    {
                                        var groupMemberService = new GroupMemberService(ctx);
                                        var groupMember        = groupMemberService.GetByGroupIdAndPersonIdAndGroupRoleId(group.Id, personAlias.Person.Id, groupRole.Id);
                                        if (groupMember == null)
                                        {
                                            groupMember = new GroupMember();
                                            groupMemberService.Add(groupMember);
                                        }
                                        else
                                        {
                                            action.AddLogEntry(string.Format("{0} is already in group {1} as {2} with status {3}", personAlias.Person.FullName, group.Name, groupRole.Name, memberStatus.GetDescription()));
                                        }

                                        groupMember.Person            = personAlias.Person;
                                        groupMember.Group             = group;
                                        groupMember.GroupRole         = groupRole;
                                        groupMember.GroupMemberStatus = memberStatus;

                                        ctx.SaveChanges();

                                        action.AddLogEntry(string.Format("{0} added to group {1} as {2} with status {3}", personAlias.Person.FullName, group.Name, groupRole.Name, memberStatus.GetDescription()));
                                        return(true);
                                    }
                                    else
                                    {
                                        action.AddLogEntry(string.Format("GroupRole does not exist: {0}", groupRoleGuid));
                                    }
                                }
                            }
                            else
                            {
                                action.AddLogEntry("Person attribute, person does not exist");
                            }
                        }
                        else
                        {
                            action.AddLogEntry("Group attribute, Group does not exist");
                        }
                    }
                }
            }

            return(false);
        }