示例#1
0
        public IMemberGroup CreateIfNotExists(string roleName)
        {
            using (var transaction = Database.GetTransaction())
            {
                var qry    = new Query <IMemberGroup>().Where(group => group.Name.Equals(roleName));
                var result = GetByQuery(qry);

                if (result.Any())
                {
                    return(null);
                }

                var grp = new MemberGroup
                {
                    Name = roleName
                };
                PersistNewItem(grp);

                if (SavingMemberGroup.IsRaisedEventCancelled(new SaveEventArgs <IMemberGroup>(grp), this))
                {
                    return(null);
                }

                transaction.Complete();

                SavedMemberGroup.RaiseEvent(new SaveEventArgs <IMemberGroup>(grp), this);

                return(grp);
            }
        }
示例#2
0
        public void AssignRolesInternal(int[] memberIds, string[] roleNames)
        {
            //ensure they're unique
            memberIds = memberIds.Distinct().ToArray();

            //create the missing roles first

            var existingSql = new Sql()
                              .Select("*")
                              .From <NodeDto>()
                              .Where <NodeDto>(dto => dto.NodeObjectType == NodeObjectTypeId)
                              .Where("umbracoNode." + SqlSyntax.GetQuotedColumnName("text") + " in (@names)", new { names = roleNames });
            var existingRoles = Database.Fetch <NodeDto>(existingSql).Select(x => x.Text);
            var missingRoles  = roleNames.Except(existingRoles);
            var missingGroups = missingRoles.Select(x => new MemberGroup {
                Name = x
            }).ToArray();

            if (SavingMemberGroup.IsRaisedEventCancelled(new SaveEventArgs <IMemberGroup>(missingGroups), this))
            {
                return;
            }
            foreach (var m in missingGroups)
            {
                PersistNewItem(m);
            }
            SavedMemberGroup.RaiseEvent(new SaveEventArgs <IMemberGroup>(missingGroups), this);

            //now go get all the dto's for roles with these role names
            var rolesForNames = Database.Fetch <NodeDto>(existingSql).ToArray();

            //get the groups that are currently assigned to any of these members

            var assignedSql = new Sql();

            assignedSql.Select(string.Format(
                                   "{0},{1},{2}",
                                   SqlSyntax.GetQuotedColumnName("text"),
                                   SqlSyntax.GetQuotedColumnName("Member"),
                                   SqlSyntax.GetQuotedColumnName("MemberGroup")))
            .From <NodeDto>()
            .InnerJoin <Member2MemberGroupDto>()
            .On <NodeDto, Member2MemberGroupDto>(dto => dto.NodeId, dto => dto.MemberGroup)
            .Where <NodeDto>(x => x.NodeObjectType == NodeObjectTypeId)
            .Where("cmsMember2MemberGroup.Member in (@ids)", new { ids = memberIds });

            var currentlyAssigned = Database.Fetch <AssignedRolesDto>(assignedSql).ToArray();

            //assign the roles for each member id

            foreach (var memberId in memberIds)
            {
                //find any roles for the current member that are currently assigned that
                //exist in the roleNames list, then determine which ones are not currently assigned.
                var mId              = memberId;
                var found            = currentlyAssigned.Where(x => x.MemberId == mId).ToArray();
                var assignedRoles    = found.Where(x => roleNames.Contains(x.RoleName)).Select(x => x.RoleName);
                var nonAssignedRoles = roleNames.Except(assignedRoles);
                foreach (var toAssign in nonAssignedRoles)
                {
                    var groupId = rolesForNames.First(x => x.Text == toAssign).NodeId;
                    Database.Insert(new Member2MemberGroupDto {
                        Member = mId, MemberGroup = groupId
                    });
                }
            }
        }