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(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 GetList( 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)null; ResultPropertyValueCollection property = source.Properties[propertyName.ToLower()]; return property.Count > 0 ? (IEnumerable)property.Cast().ToList() : (IEnumerable)null; } catch (Exception ex) { throw new FieldAccessException(string.Format("Fehler beim Lesen von {0}.{1}", (object)source.Path, (object)propertyName), ex); } } public static IEnumerable GetList( 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)null; PropertyValueCollection property = source.Properties[propertyName]; return property.Count > 0 ? (IEnumerable)property.Cast().ToList() : (IEnumerable)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(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(this SearchResult source) where T : ISearchResultWrapper { T instance = Activator.CreateInstance(); instance.Bind(source); return instance; } public static IEnumerable Make(this SearchResultCollection source) where T : ISearchResultWrapper { List objList = new List(); foreach (SearchResult source1 in source) objList.Add(source1.Make()); return (IEnumerable)objList; } public static T FindAs(this DirectorySearcher searcher, IEnumerable filter) where T : ASearchResultWrapper { string str = "(&(" + string.Join(")(", filter) + "))"; IEnumerable source = searcher.PropertiesToLoad.Cast(); string filter1 = searcher.Filter; T instance = Activator.CreateInstance(); 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()); return one.Make(); } } }