예제 #1
0
        public void Can_Add_Update_ModifyMembersDynamic_Group()
        {
            var members = _context.Query <IntegrationUserTest>().Select(u => u.DistinguishedName).ToList();

            members.Count.Should().Be.GreaterThan(0);

            var group = new DirectoryAttributes("CN=TestGroup,CN=Roles,CN=Employees,DC=Northwind,DC=local");

            group.Set("objectClass", new[] { "top", "group" });
            group.Set("Member", new Collection <string>(members.GetRange(0, members.Count - 1)));

            var added = _context.AddAndGet(group);

            var addedMembers = added.GetStrings("member").ToList();

            foreach (var member in members.GetRange(0, members.Count - 1))
            {
                addedMembers.Should().Contain(member);
            }

            int removedIndex = members.IndexOf(addedMembers[0]);

            addedMembers.Should().Not.Contain(members.Last());
            addedMembers.RemoveAt(0);
            addedMembers.Add(members.Last());

            added.Set("member", addedMembers);

            var updated = _context.UpdateAndGet(added);

            updated.GetStrings("member").Should().Not.Contain(members[removedIndex])
            .And.Contain(members.Last());

            _context.Delete(updated.DistinguishedName);
        }
예제 #2
0
        private static IDirectoryAttributes AddEntryIfNecessary(string dn, string objectClass, IDirectoryContext context)
        {
            IDirectoryAttributes entry = null;

            try
            {
                entry = context.GetByDN(dn);
            }
            catch (DirectoryOperationException ex)
            {
                if (ex.Message.IndexOf("object does not exist", StringComparison.OrdinalIgnoreCase) == -1)
                {
                    throw;
                }
            }

            if (entry == null)
            {
                entry = new DirectoryAttributes(dn);
                entry.Set("objectClass", objectClass);

                Console.WriteLine("Adding {0}", dn);
                return(context.AddAndGet(entry));
            }

            Console.WriteLine("{0} already exists", dn);
            return(entry);
        }
예제 #3
0
        private static DirectoryAttributes AddDirectoriesRecursive(DirectoryAttributes folder, string current, ref int fCount)
        {
            //current = Path.Combine(current, folder.RelativePath);
            foreach (var subd in Directory.GetDirectories(folder.FullPath))
            {
                try
                {
                    DirectoryAttributes subda = new DirectoryAttributes(new DirectoryInfo(subd));
                    string p = Path.Combine(current, subda.Name);
                    subda.RelativePath = p;
                    folder.SubFolders.Add(subda);

                    foreach (var subf in Directory.GetFiles(subd))
                    {
                        FileAttributes a = new FileAttributes(new FileInfo(subf));
                        fCount++;
                        a.RelativePath = Path.Combine(p, a.FileName);
                        subda.Files.Add(a);
                    }

                    AddDirectoriesRecursive(subda, Path.Combine(current, subda.Name), ref fCount);
                }
                catch (Exception ex)
                {
                    ISLogger.Write("An error occurred while reading directory {0}. {1}", subd, ex.Message);
                }
            }
            return(folder);
        }
예제 #4
0
        private static ClipboardVirtualFileData ReadFileDrop(string[] data)
        {
            string[]            files = data;
            DirectoryAttributes root  = new DirectoryAttributes("", new List <FileAttributes>(), new List <DirectoryAttributes>(), "");
            int fCount = 0;

            foreach (var file in files)
            {
                try
                {
                    if (string.IsNullOrWhiteSpace(file))
                    {
                        continue;
                    }

                    System.IO.FileAttributes fa = System.IO.File.GetAttributes(file);
                    if (fa.HasFlag(System.IO.FileAttributes.Directory))
                    {
                        DirectoryAttributes di = new DirectoryAttributes(new DirectoryInfo(file));
                        root.SubFolders.Add(di);

                        foreach (var baseFile in Directory.GetFiles(file))
                        {
                            FileAttributes df = new FileAttributes(new FileInfo(baseFile));
                            df.RelativePath = Path.Combine(di.Name, df.FileName);
                            di.Files.Add(df);
                        }
                    }
                    else
                    {
                        root.Files.Add(new FileAttributes(new FileInfo(file)));
                    }
                }
                catch (Exception ex)
                {
                    ISLogger.Write("An error occurred while reading attributes for {0}. File not copied: {1}", file, ex.Message);
                    ISLogger.Write(ex.StackTrace);
                }
            }

            foreach (var folder in root.SubFolders)
            {
                AddDirectoriesRecursive(folder, folder.Name, ref fCount);
            }

            return(new ClipboardVirtualFileData(root));
        }
예제 #5
0
        public void DeleteSubTree()
        {
            var container = new DirectoryAttributes("CN=DeleteContainer,CN=Employees,DC=Northwind,DC=local");

            container.Set("objectClass", new[] { "top", "container" });

            _context.Add(container);

            var attributes = new DirectoryAttributes("CN=IntegrationTest," + container.DistinguishedName);

            attributes.SetNull("AccountExpires");
            attributes.Set("objectclass", "user");

            _context.AddAndGet(attributes);

            _context.Delete(container.DistinguishedName, new TreeDeleteControl());

            Executing.This(() => _context.GetByDN(container.DistinguishedName))
            .Should().Throw <DirectoryOperationException>().And.Exception.Message
            .Should().Contain("does not exist");
        }
예제 #6
0
        public void Can_Add_Update_Remove_Dynamic()
        {
            var attributes = new DirectoryAttributes("CN=IntegrationTest," + IntegrationUserTest.NamingContext);

            attributes.SetNull("AccountExpires");
            attributes.Set("objectclass", "user");

            var added = _context.AddAndGet(attributes);

            added.Should().Not.Be.Null();

            added.GetString("cn").Should().Be.EqualTo("IntegrationTest");
            added.GetString("accountexpires").Should().Be.Null();
            added.GetGuid("objectguid").Should().Have.Value();
            added.GetSecurityIdentifier("objectsid").Should().Not.Be.Null();
            added.GetSecurityIdentifiers("objectsid").Should().Not.Be.Empty();
            added.GetStrings("objectclass").Should().Have.Count.GreaterThan(1);

            added.Set("accountExpires", "9223372036854775807").SetNull("manager");

            added = _context.UpdateAndGet(added);

            added.GetString("accountExpires").Should().Be.EqualTo("9223372036854775807");
            added.GetDateTime("accountExpires", null).Should().Not.Have.Value();
            added.GetString("manager").Should().Be.Null();

            var renamed = _context.RenameEntry(added.DistinguishedName, "IntegrationTest2");

            var moved = _context.MoveEntry(renamed, IntegrationUserTest.NamingContext2);

            _context.Delete(moved);

            Executing.This(() => _context.GetByDN(moved))
            .Should().Throw <DirectoryOperationException>().And.Exception.Message
            .Should().Contain("does not exist");
        }
예제 #7
0
        private static void PropulateDirectoryForInheritance()
        {
            AddContainerIfNecessary("CN", "InheritanceTest");

            using (var context = new DirectoryContext())
            {
                int entryCount = context.Query(InheritanceDirectoryContainer).Count();
                if (entryCount > 1)
                {
                    Console.WriteLine("Inheritince Directory already populated with {0} entries", entryCount);
                    return;
                }
            }

            var users = new DirectoryAttributes[10000];

            Parallel.For(0, 10000, i =>
            {
                var firstName         = GetRandomName() + i;
                var lastName          = GetRandomName() + i;
                var distinguishedName = "CN=" + firstName + " " + lastName + "," + InheritanceDirectoryContainer;

                var user = new DirectoryAttributes(distinguishedName);

                var objectClass = i % 5 == 0 ? "user" : (i % 3 == 0 ? "organizationalPerson" : "person");

                user.Set("objectClass", objectClass);

                if (objectClass == "user")
                {
                    user.Set("givenname", firstName)
                    .Set("sn", lastName)
                    .Set("employeeid", 50 + i)
                    .Set("comment", "This entry was generated by random data.")
                    .Set("telephonenumber", "(123) 555-9857")
                    .Set("title", GetRandomTitle())
                    .Set("street", "1234 Cool St.")
                    .Set("l", GetRandomCity())
                    .Set("c", GetRandomCountry())
                    .Set("PostalCode", "12345");
                }
                else if (objectClass == "person")
                {
                    user.Set("sn", lastName);
                }
                else
                {
                    user.Set("givenname", firstName)
                    .Set("sn", lastName)
                    .Set("employeeid", 50 + i)
                    .Set("comment", "This entry was generated by random data.")
                    .Set("telephonenumber", "(123) 555-9857")
                    .Set("title", GetRandomTitle())
                    .Set("street", "1234 Cool St.")
                    .Set("l", GetRandomCity())
                    .Set("c", GetRandomCountry())
                    .Set("postalCode", "12345");
                }


                users[i] = user;
            });

            foreach (var user in users.AsParallel())
            {
                using (var context = new DirectoryContext())
                {
                    context.Add(user);
                }
                Console.WriteLine("{0} added", user.DistinguishedName);
            }
        }
예제 #8
0
        public object Transform(SearchResultEntry entry)
        {
            var attributes = new DirectoryAttributes(entry);

            return(_projection == null ? attributes : _projection.Projection.DynamicInvoke(attributes));
        }
 /// <summary>
 /// Executes <see cref="DirectoryContext.Add(DirectoryAttributes)"/>
 /// </summary>
 /// <param name="context">The <see cref="DirectoryContext"/>.</param>
 /// <param name="entry">The attributes for the entry</param>
 public static Task AddAsync(this IDirectoryContext context, DirectoryAttributes entry)
 {
     return(Task.Factory.StartNew(() => context.Add(entry)));
 }
 /// <summary>
 /// Executes <see cref="DirectoryContext.UpdateAndGet(DirectoryAttributes)"/> within a <see cref="Task"/>.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <param name="entry">The attributes for the entry.</param>
 /// <returns></returns>
 /// <exception cref="ArgumentNullException">Thrown if <paramref name="entry"/> is null.
 /// </exception>
 /// <exception cref="DirectoryOperationException">Thrown if the operation fails</exception>
 /// <exception cref="LdapException">Thrown if the operation fails</exception>
 public static Task <IDirectoryAttributes> UpdateAndGetAsync(this IDirectoryContext context, DirectoryAttributes entry)
 {
     return(Task.Factory.StartNew(() => context.UpdateAndGet(entry)));
 }
 /// <summary>
 /// Executes <see cref="DirectoryContext.AddAndGet(DirectoryAttributes,DirectoryControl[])"/>
 /// </summary>
 /// <param name="context">The <see cref="DirectoryContext"/>.</param>
 /// <param name="entry">The attributes for the entry</param>
 /// <param name="controls">Any <see cref="DirectoryControl"/>s to be sent with the request</param>
 public static Task <IDirectoryAttributes> AddAndGetAsync(this IDirectoryContext context, DirectoryAttributes entry,
                                                          DirectoryControl[] controls)
 {
     return(Task.Factory.StartNew(() => context.AddAndGet(entry, controls)));
 }