11using System ;
2+ using System . Diagnostics ;
3+ using System . Diagnostics . CodeAnalysis ;
24using System . DirectoryServices ;
35using System . Management . Automation ;
46using System . Security . Principal ;
@@ -16,8 +18,7 @@ public sealed class GetADEffectiveAccessComand : PSCmdlet, IDisposable
1618
1719 private const string IdentitySet = "Identity" ;
1820
19- private static SecurityMasks Masks = SecurityMasks . Group
20- | SecurityMasks . Dacl | SecurityMasks . Owner ;
21+ private SecurityMasks _masks = SecurityMasks . Group | SecurityMasks . Dacl | SecurityMasks . Owner ;
2122
2223 private DirectoryEntryBuilder ? _entryBuilder ;
2324
@@ -71,7 +72,7 @@ protected override void BeginProcessing()
7172 {
7273 if ( Audit )
7374 {
74- Masks |= SecurityMasks . Sacl ;
75+ _masks |= SecurityMasks . Sacl ;
7576 }
7677
7778 try
@@ -82,7 +83,7 @@ protected override void BeginProcessing()
8283 server : Server ,
8384 searchBase : SearchBase ) ;
8485
85- _map ?? = GuidResolver . GetFromTLS ( ) ;
86+ _map = GuidResolver . GetFromTLS ( ) ;
8687 _map . SetContext ( Server , _entryBuilder ) ;
8788 }
8889 catch ( Exception exception )
@@ -93,7 +94,8 @@ protected override void BeginProcessing()
9394
9495 protected override void ProcessRecord ( )
9596 {
96- if ( _entryBuilder is null ) return ;
97+ Assert ( _entryBuilder is not null ) ;
98+ Assert ( _map is not null ) ;
9799
98100 try
99101 {
@@ -112,12 +114,12 @@ protected override void ProcessRecord()
112114 Tombstone = IncludeDeletedObjects ,
113115 SearchScope = SearchScope ,
114116 PageSize = PageSize ,
115- SecurityMasks = Masks
117+ SecurityMasks = _masks
116118 } ;
117119
118- foreach ( SearchResult obj in searcher . FindAll ( ) )
120+ foreach ( SearchResult result in searcher . FindAll ( ) )
119121 {
120- WriteRules ( obj ) ;
122+ WriteRules ( result ) ;
121123 }
122124 }
123125 catch ( Exception _ ) when ( _ is PipelineStoppedException or FlowControlException )
@@ -134,17 +136,19 @@ protected override void ProcessRecord()
134136 }
135137 }
136138
137- private void WriteRules ( SearchResult obj )
139+ private void WriteRules ( SearchResult searchResult )
138140 {
139- if ( ! obj . TryGetProperty ( SecurityDescriptor , out byte [ ] ? descriptor ) )
141+ Assert ( _map is not null ) ;
142+
143+ if ( ! searchResult . TryGetProperty ( SecurityDescriptor , out byte [ ] ? descriptor ) )
140144 {
141- obj . WriteInvalidSecurityDescriptorError ( this ) ;
145+ searchResult . WriteInvalidSecurityDescriptorError ( this ) ;
142146 return ;
143147 }
144148
145- AclBuilder builder = new ( obj . Path , descriptor ) ;
149+ AclBuilder builder = new ( searchResult . Path , descriptor ) ;
146150 WriteObject (
147- builder . EnumerateRules ( _map ! , includeAudit : Audit ) ,
151+ builder . EnumerateRules ( _map , includeAudit : Audit ) ,
148152 enumerateCollection : true ) ;
149153 }
150154
@@ -162,7 +166,7 @@ _ when LanguagePrimitives.TryConvertTo(identity, out SecurityIdentifier sid) =>
162166 filter : ldapFilter ,
163167 propertiesToLoad : [ SecurityDescriptor ] )
164168 {
165- SecurityMasks = Masks ,
169+ SecurityMasks = _masks ,
166170 Tombstone = IncludeDeletedObjects
167171 } ;
168172
@@ -172,6 +176,10 @@ _ when LanguagePrimitives.TryConvertTo(identity, out SecurityIdentifier sid) =>
172176 WriteRules ( result ) ;
173177 }
174178
179+ [ Conditional ( "DEBUG" ) ]
180+ private static void Assert ( [ DoesNotReturnIf ( false ) ] bool condition , string ? message = null )
181+ => Debug . Assert ( condition , message ) ;
182+
175183 public void Dispose ( )
176184 {
177185 _entryBuilder ? . Dispose ( ) ;
0 commit comments