@@ -6,7 +6,13 @@ namespace ADEffectiveAccess;
66
77internal sealed class GuidResolver
88{
9- private readonly Dictionary < string ? , Dictionary < Guid , string > > _map = [ ] ;
9+ private const string DefaultContext = "defaultNamingContext" ;
10+
11+ private const string SchemaContext = "schemaNamingContext" ;
12+
13+ private const string ConfigurationContext = "configurationNamingContext" ;
14+
15+ private readonly Dictionary < string , Dictionary < Guid , string > > _map = [ ] ;
1016
1117 private Dictionary < Guid , string > ? _current ;
1218
@@ -20,33 +26,49 @@ internal void SetCurrentContext(string? server, DirectoryEntryBuilder builder)
2026 {
2127 string path = server is null ? "RootDSE" : $ "{ server } /RootDSE";
2228 using DirectoryEntry root = builder . Create ( path ) ;
23- string ? ctxName = root . Properties [ "defaultNamingContext" ] [ 0 ] ? . ToString ( ) ;
24- if ( _map . TryGetValue ( ctxName , out Dictionary < Guid , string > current ) )
29+ string ? ctxName = root . GetProperty ( DefaultContext )
30+ ?? throw path . ToInitializeException ( DefaultContext ) ;
31+
32+ if ( _map . TryGetValue ( ctxName , out Dictionary < Guid , string > ? current ) )
2533 {
2634 _current = current ;
2735 return ;
2836 }
2937
3038 current = [ ] ;
31- string ? ctxSchema = root . Properties [ "schemaNamingContext" ] [ 0 ] ? . ToString ( ) ;
32- string ? ctxConfig = root . Properties [ "configurationNamingContext" ] [ 0 ] ? . ToString ( ) ;
33- if ( ctxSchema is not null ) PopulateSchema ( ctxSchema , current , builder ) ;
34- if ( ctxConfig is not null ) PopulateExtendedRights ( ctxConfig , current , builder ) ;
39+
40+ PopulateSchema (
41+ schemaNamingContext : root . GetProperty ( SchemaContext )
42+ ?? throw path . ToInitializeException ( SchemaContext ) ,
43+ map : current ,
44+ builder : builder ) ;
45+
46+ PopulateExtendedRights (
47+ configurationNamingContext : root . GetProperty ( ConfigurationContext )
48+ ?? throw path . ToInitializeException ( ConfigurationContext ) ,
49+ current ,
50+ builder ) ;
51+
3552 _map [ ctxName ] = current ;
3653 _current = current ;
3754 }
3855
3956 internal string Translate ( Guid guid , string defaultValue )
4057 {
41- if ( guid == Guid . Empty || _current ! . TryGetValue ( guid , out defaultValue ) )
58+ if ( guid == Guid . Empty )
4259 {
4360 return defaultValue ;
4461 }
4562
63+ if ( _current ! . TryGetValue ( guid , out string ? value ) )
64+ {
65+ return value ;
66+ }
67+
4668 return guid . ToString ( ) ;
4769 }
4870
49- private void PopulateSchema (
71+ private static void PopulateSchema (
5072 string schemaNamingContext ,
5173 Dictionary < Guid , string > map ,
5274 DirectoryEntryBuilder builder )
@@ -64,11 +86,11 @@ private void PopulateSchema(
6486 {
6587 map . TryAdd (
6688 new Guid ( ( byte [ ] ) result . Properties [ "schemaIdGuid" ] [ 0 ] ) ,
67- result . Properties [ "cn" ] [ 0 ] . ToString ( ) ) ;
89+ result . Properties [ "cn" ] [ 0 ] . ToString ( ) ! ) ;
6890 }
6991 }
7092
71- private void PopulateExtendedRights (
93+ private static void PopulateExtendedRights (
7294 string configurationNamingContext ,
7395 Dictionary < Guid , string > map ,
7496 DirectoryEntryBuilder builder )
@@ -85,8 +107,8 @@ private void PopulateExtendedRights(
85107 foreach ( SearchResult result in searcher . FindAll ( ) )
86108 {
87109 map . TryAdd (
88- Guid . Parse ( result . Properties [ "rightsGuid" ] [ 0 ] . ToString ( ) ) ,
89- result . Properties [ "cn" ] [ 0 ] . ToString ( ) ) ;
110+ Guid . Parse ( result . Properties [ "rightsGuid" ] [ 0 ] . ToString ( ) ! ) ,
111+ result . Properties [ "cn" ] [ 0 ] . ToString ( ) ! ) ;
90112 }
91113 }
92114}
0 commit comments