Exemplo n.º 1
0
        public int CreateGroup(Group group)
        {
            //Creates the List attributes of the entry and add them to attributeset

            LdapAttributeSet attributeSet = GetAttributeSet(group);

            // DN of the entry to be added
            string dn = group.DN;

            LdapEntry newEntry = new LdapEntry(dn, attributeSet);


            var qMgmt = LdapQueryManager.Instance;

            try
            {
                qMgmt.AddEntry(newEntry);
                return(0);
            }
            catch (Exception ex)
            {
                logger.Error("Error saving group");
                logger.Log(LogLevel.Error, ex);
                return(-1);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Converts the data from the LDAP result
        /// </summary>
        /// <returns>The LDAP.</returns>
        /// <param name="entry">Entry.</param>
        private Group ConvertfromLdap(LdapEntry entry, Boolean _listCN = false)
        {
            var group = new Group();

            group.Name = entry.GetAttribute("name").StringValue;

            if (entry.GetAttribute("description") != null)
            {
                group.Description = entry.GetAttribute("description").StringValue;
            }

            //var sid = ConvertByteToStringSid((byte[])(Array)entry.GetAttribute("objectSid").ByteValue);

            //group.ID = sid;

            group.DN = entry.GetAttribute("distinguishedName").StringValue;


            if (entry.GetAttribute("memberOf") != null)
            {
                var moff = entry.GetAttribute("memberOf").StringValues;

                while (moff.MoveNext())
                {
                    String gmoff = "";
                    if (moff != null && moff.Current != null)
                    {
                        gmoff = moff.Current;
                    }
                    group.MemberOf.Add(gmoff);
                }
            }

            if (entry.GetAttribute("member") != null)
            {
                var m = entry.GetAttribute("member").StringValues;

                while (m.MoveNext())
                {
                    String member = "";
                    if (m != null && m.Current != null)
                    {
                        member = m.Current;
                        if (_listCN)
                        {
                            var regex  = new Regex("^(?:CN=)(?<cn>[^,]+?)(?:,)");
                            var result = regex.Match(member);
                            member = result.Groups["cn"].Value;
                        }

                        group.Member.Add(member);
                    }
                }
            }


            return(group);
        }
Exemplo n.º 3
0
        public int DeleteGroup(Group group)
        {
            var qMgmt = LdapQueryManager.Instance;

            try
            {
                qMgmt.DeleteEntry(group.DN);
                return(0);
            }
            catch (Exception ex)
            {
                logger.Error("Error deleting group={group}", group.DN);
                logger.Log(LogLevel.Error, ex);
                return(-1);
            }
        }
Exemplo n.º 4
0
        private LdapAttributeSet GetAttributeSet(Group group)
        {
            LdapAttributeSet attributeSet = new LdapAttributeSet();

            attributeSet.Add(new LdapAttribute("objectclass", new string[] { "top", "group" }));
            attributeSet.Add(new LdapAttribute("name", group.Name));
            attributeSet.Add(new LdapAttribute("sAMAccountName", group.Name));
            attributeSet.Add(new LdapAttribute("cn", group.Name));
            attributeSet.Add(new LdapAttribute("description", group.Description));

            var amember = new LdapAttribute("member");

            foreach (String member in group.Member)
            {
                amember.AddValue(member);
            }

            attributeSet.Add(amember);

            return(attributeSet);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Saves the group.
        /// </summary>
        /// <returns>The group. Must have DN set</returns>
        /// <param name="group">Group.</param>
        /// <param name="_listCN">If true the members will only contain the CN</param>
        public int SaveGroup(Group group)
        {
            var qMgmt = LdapQueryManager.Instance;

            var modList = new List <LdapModification>();

            var atributes = GetAttributeSet(group);

            //Get user from the Directory
            try
            {
                var dgroup = GetGroup(group.DN);

                var dattrs = GetAttributeSet(dgroup);

                bool members_clean = false;

                foreach (LdapAttribute attr in atributes)
                {
                    if (
                        attr.Name != "cn" &&
                        attr.Name != "objectclass" &&
                        attr.Name != "member"
                        )
                    {
                        var b1 = attr.ByteValue;

                        var attribute = dattrs.GetAttribute(attr.Name);

                        bool equal = true;

                        if (attribute != null)
                        {
                            var b2 = attribute.ByteValue;

                            equal = ByteTools.Equality(b1, b2);
                        }


                        if (!equal)
                        {
                            modList.Add(new LdapModification(LdapModification.Replace, attr));
                        }
                    }
                    else
                    {
                        if (attr.Name == "member")
                        {
                            if (!members_clean)
                            {
                                var dattr = dattrs.GetAttribute("member");

                                modList.Add(new LdapModification(LdapModification.Delete, dattr));

                                members_clean = true;
                            }


                            modList.Add(new LdapModification(LdapModification.Add, attr));
                        }
                    }
                }


                try
                {
                    qMgmt.SaveEntry(group.DN, modList.ToArray());
                    return(0);
                }
                catch (Exception ex)
                {
                    logger.Error("Error updating group");
                    logger.Log(LogLevel.Error, ex);
                    return(-1);
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error group not found");
                logger.Log(LogLevel.Error, ex);
                return(-1);
            }
        }