Giter VIP home page Giter VIP logo

ldifhelper's Introduction

LdifHelper

NuGet License

A library for reading and writing RFC 2849 LDIF files.

Usage

Reading

using System;
using System.IO;
using System.Text;
using LdifHelper;

internal class Program
{
    private static void Main()
    {
        // Open and parse an ASCII encoded file. Use 1252 when reading Microsoft's ldifde.exe output.
        using (TextReader textReader = new StreamReader("input.ldif", Encoding.GetEncoding(20127), false))
        {
            foreach (IChangeRecord changeRecord in LdifReader.Parse(textReader))
            {
                if (changeRecord is ChangeAdd changeAdd)
                {
                    Console.WriteLine($"Adding {changeAdd.DistinguishedName}");
                    foreach (LdifAttribute attribute in changeAdd)
                    {
                        Console.WriteLine($"  * {attribute.AttributeType}");
                        foreach (object value in attribute)
                        {
                            Console.WriteLine($"    - {value}");
                        }
                    }
                }
                else if (changeRecord is ChangeDelete changeDelete)
                {
                    Console.WriteLine($"Deleting {changeDelete.DistinguishedName}");
                }
                else if (changeRecord is ChangeModDn changeModDn)
                {
                    string[] components = Constants.DistinguishedNameRegex.Split(changeModDn.DistinguishedName);
                    if (components.Length < 2)
                    {
                        throw new InvalidOperationException(
                            $"Invalid distinguished name found for {changeModDn.DistinguishedName}.");
                    }

                    // Only the newsuperior field is optional.
                    string newDistinguishedName =
                        changeModDn.NewSuperior == null
                            ? $"{changeModDn.NewRdn},{changeModDn.DistinguishedName.Substring(components[0].Length + 1)}"
                            : $"{changeModDn.NewRdn},{changeModDn.NewSuperior}";

                    Console.WriteLine($"Renaming {changeModDn.DistinguishedName}");
                    Console.WriteLine($"  * New DN: {newDistinguishedName}.");
                    Console.WriteLine($"  * Delete old rdn: {changeModDn.DeleteOldRdn}.");
                }
                else if (changeRecord is ChangeModify changeModify)
                {
                    Console.WriteLine($"Modifying {changeModify.DistinguishedName}");
                    foreach (ModSpec modSpec in changeModify.ModSpecs)
                    {
                        switch (modSpec.ModSpecType)
                        {
                            case ModSpecType.Add:
                                Console.WriteLine($"  * Adding the following values to {modSpec.AttributeType}:");
                                foreach (object value in modSpec)
                                {
                                    Console.WriteLine($"    - {value}");
                                }

                                break;
                            case ModSpecType.Delete:
                                if (modSpec.Count == 0)
                                {
                                    Console.WriteLine($"  * Deleting all values from {modSpec.AttributeType}.");
                                }
                                else
                                {
                                    Console.WriteLine($"  * Deleting the following values from {modSpec.AttributeType}:");
                                    foreach (object value in modSpec)
                                    {
                                        Console.WriteLine($"    - {value}");
                                    }
                                }

                                break;
                            case ModSpecType.Replace:
                                Console.WriteLine($"  * Replacing all values from {modSpec.AttributeType}:");
                                foreach (object value in modSpec)
                                {
                                    Console.WriteLine($"    - {value}");
                                }

                                break;
                            default:
                                throw new InvalidOperationException(
                                    $"Unknown mod-spec type: {modSpec.ModSpecType}.");
                        }

                    }
                }
                else
                {
                    throw new InvalidOperationException(
                        $"Unknown change record type: {changeRecord.GetType().BaseType}.");
                }
            }
        }
    }
}

Writing

using System.Collections;
using System.Collections.Generic;
using System.DirectoryServices;
using System.IO;
using System.Text;
using LdifHelper;

internal class Program
{
    private static void Main()
    {
        using (FileStream fileStream = new FileStream("output.ldif", FileMode.Create))
        using (TextWriter textWriter = new StreamWriter(fileStream, Encoding.ASCII))
        using (DirectorySearcher directorySearcher = new DirectorySearcher())
        {
            foreach (SearchResult searchResult in directorySearcher.FindAll())
            {
                List<LdifAttribute> ldifAttributes = new List<LdifAttribute>();
                foreach (DictionaryEntry dictionaryEntry in searchResult.Properties)
                {
                    List<object> values = new List<object>();
                    foreach (object value in (ResultPropertyValueCollection)dictionaryEntry.Value)
                    {
                        /* 
                         * The library does not make assumptions on what the string representation of an object should be.
                         * All types must be converted to either a string or byte[] before being boxed.
                         */
                        values.Add(value is byte[] ? value : value.ToString());
                    }

                    ldifAttributes.Add(new LdifAttribute((string)dictionaryEntry.Key, values));
                }

                ChangeAdd changeAdd = new ChangeAdd(searchResult.Path.Substring(7), ldifAttributes);
                textWriter.WriteLine(changeAdd.Dump());
            }
        }
    }
}

ldifhelper's People

Contributors

jcasale avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.