extensions/ActiveDirectoryExtensions.cs
2020-04-08 18:40:10 +02:00

174 lines
7.7 KiB
C#

using Chrosey.Extensions.Interfaces;
using Chrosey.Extensions.Wrapper;
using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Chrosey.Extensions
{
public static class ActiveDirectoryExtensions
{
public static T Get<T>(this DirectoryEntry entry, string propertyName) where T : class
{
try
{
if (string.IsNullOrEmpty(propertyName))
throw new ArgumentNullException("Der PropertyName[" + propertyName + "] kann nicht null oder leer sein");
if (entry == null)
throw new ArgumentNullException("Kann keine Eigenschaft von null abrufen");
return !entry.Properties.Contains(propertyName) ? default(T) : entry.Properties[propertyName].Value as T;
}
catch (Exception ex)
{
throw new FieldAccessException(string.Format("Fehler beim Lesen von {0}.{1}", (object)entry.Name, (object)propertyName), ex);
}
}
public static string GetValue(this DirectoryEntry entry, string propertyName)
{
try
{
if (string.IsNullOrEmpty(propertyName))
throw new ArgumentNullException("Der PropertyName[" + propertyName + "] kann nicht null oder leer sein");
if (entry == null)
throw new ArgumentNullException("Kann keine Eigenschaft von null abrufen");
return !entry.Properties.Contains(propertyName) ? string.Empty : entry.Properties[propertyName].Value.ToString();
}
catch (Exception ex)
{
throw new FieldAccessException(string.Format("Fehler beim Lesen von {0}.{1}", (object)entry.Name, (object)propertyName), ex);
}
}
public static byte[] Photo(this DirectoryEntry entry)
{
try
{
if (entry == null)
throw new ArgumentNullException("Kann keine Eigenschaft von null abrufen");
return !entry.Properties.Contains("thumbnailPhoto") ? (byte[])null : entry.Properties["thumbnailPhoto"].Value as byte[];
}
catch (Exception ex)
{
throw new FieldAccessException(string.Format("Fehler beim Lesen von {0}.{1}", (object)entry.Name, (object)"thumbnailPhoto"), ex);
}
}
public static string GetValue(this SearchResult sr, string propertyName)
{
try
{
if (string.IsNullOrEmpty(propertyName))
throw new ArgumentNullException("Der PropertyName[" + propertyName + "] kann nicht null oder leer sein");
if (sr == null)
throw new NullReferenceException("Kann keine Eigenschaft von null abrufen");
ResultPropertyValueCollection property = sr.Properties[propertyName.ToLower()];
return property.Count > 0 ? property[0].ToString() : string.Empty;
}
catch (Exception ex)
{
throw new FieldAccessException(string.Format("Fehler beim Lesen von {0}.{1}", (object)sr.Path, (object)propertyName), ex);
}
}
public static IEnumerable<T> GetList<T>(
this SearchResult source,
string propertyName)
where T : class
{
try
{
if (string.IsNullOrEmpty(propertyName))
throw new ArgumentNullException("Der PropertyName[" + propertyName + "] kann nicht null oder leer sein");
if (source == null)
throw new ArgumentNullException("Kann keine Eigenschaft von null abrufen");
if (!source.Properties.Contains(propertyName))
return (IEnumerable<T>)null;
ResultPropertyValueCollection property = source.Properties[propertyName.ToLower()];
return property.Count > 0 ? (IEnumerable<T>)property.Cast<T>().ToList<T>() : (IEnumerable<T>)null;
}
catch (Exception ex)
{
throw new FieldAccessException(string.Format("Fehler beim Lesen von {0}.{1}", (object)source.Path, (object)propertyName), ex);
}
}
public static IEnumerable<T> GetList<T>(
this DirectoryEntry source,
string propertyName)
where T : class
{
try
{
if (string.IsNullOrEmpty(propertyName))
throw new ArgumentNullException("Der PropertyName[" + propertyName + "] kann nicht null oder leer sein");
if (source == null)
throw new ArgumentNullException("Kann keine Eigenschaft von null abrufen");
if (!source.Properties.Contains(propertyName))
return (IEnumerable<T>)null;
PropertyValueCollection property = source.Properties[propertyName];
return property.Count > 0 ? (IEnumerable<T>)property.Cast<T>().ToList<T>() : (IEnumerable<T>)null;
}
catch (Exception ex)
{
throw new FieldAccessException(string.Format("Fehler beim Lesen von {0}.{1}", (object)source.Path, (object)propertyName), ex);
}
}
public static T Get<T>(this SearchResult source, string propertyName) where T : class
{
try
{
if (string.IsNullOrEmpty(propertyName))
throw new ArgumentNullException("Der PropertyName[" + propertyName + "] kann nicht null oder leer sein");
if (source == null)
throw new ArgumentNullException("Kann keine Eigenschaft von null abrufen");
if (!source.Properties.Contains(propertyName))
return default(T);
ResultPropertyValueCollection property = source.Properties[propertyName.ToLower()];
return property.Count > 0 ? property[0] as T : default(T);
}
catch (Exception ex)
{
throw new FieldAccessException(string.Format("Fehler beim Lesen von {0}.{1}", (object)source.Path, (object)propertyName), ex);
}
}
public static T Make<T>(this SearchResult source) where T : ISearchResultWrapper
{
T instance = Activator.CreateInstance<T>();
instance.Bind(source);
return instance;
}
public static IEnumerable<T> Make<T>(this SearchResultCollection source) where T : ISearchResultWrapper
{
List<T> objList = new List<T>();
foreach (SearchResult source1 in source)
objList.Add(source1.Make<T>());
return (IEnumerable<T>)objList;
}
public static T FindAs<T>(this DirectorySearcher searcher, IEnumerable<string> filter) where T : ASearchResultWrapper
{
string str = "(&(" + string.Join(")(", filter) + "))";
IEnumerable<string> source = searcher.PropertiesToLoad.Cast<string>();
string filter1 = searcher.Filter;
T instance = Activator.CreateInstance<T>();
searcher.PropertiesToLoad.Clear();
searcher.PropertiesToLoad.AddRange(instance.propertiesToLoad);
searcher.Filter = str;
SearchResult one = searcher.FindOne();
if (one == null)
throw new NotFoundException("Element wurde nicht gefunden");
searcher.Filter = filter1;
searcher.PropertiesToLoad.Clear();
searcher.PropertiesToLoad.AddRange(source.ToArray<string>());
return one.Make<T>();
}
}
}