Skip to content

Commit 3ff1f0f

Browse files
committed
some fixes
1 parent 0264341 commit 3ff1f0f

File tree

3 files changed

+33
-24
lines changed

3 files changed

+33
-24
lines changed

src/ADEffectiveAccess/DirectoryEntryBuilder.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@ internal sealed class DirectoryEntryBuilder : IDisposable
1212

1313
private readonly AuthenticationTypes _authenticationTypes;
1414

15-
internal DirectoryEntry RootEntry { get; }
15+
internal DirectoryEntry DomainEntry { get; }
1616

1717
internal DirectoryEntry SearchBase { get; }
1818

19-
internal string? Root { get; }
19+
internal string? DomainDistinguishedName
20+
{
21+
get => DomainEntry.Properties["distinguishedName"][0]?.ToString();
22+
}
2023

2124
internal DirectoryEntryBuilder(
2225
PSCredential? credential,
@@ -27,8 +30,8 @@ internal DirectoryEntryBuilder(
2730
_username = credential?.UserName;
2831
_password = credential?.GetNetworkCredential().Password;
2932
_authenticationTypes = authenticationTypes;
30-
RootEntry = Create(server: server);
31-
SearchBase = Create(searchBase: searchBase);
33+
DomainEntry = Create(server: server);
34+
SearchBase = searchBase is null ? DomainEntry : Create(searchBase: searchBase);
3235
}
3336

3437
internal DirectoryEntry Create(string? server = null, string? searchBase = null)
@@ -41,14 +44,12 @@ internal DirectoryEntry Create(string? server = null, string? searchBase = null)
4144
_ => $"LDAP://{server}/{searchBase}"
4245
};
4346

44-
return path is null
45-
? RootEntry
46-
: new DirectoryEntry(path, _username, _password, _authenticationTypes);
47+
return new DirectoryEntry(path, _username, _password, _authenticationTypes);
4748
}
4849

4950
public void Dispose()
5051
{
51-
RootEntry.Dispose();
52+
DomainEntry.Dispose();
5253
SearchBase.Dispose();
5354
GC.SuppressFinalize(this);
5455
}

src/ADEffectiveAccess/Extensions.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using System.Management.Automation;
55
using System.Security.Principal;
66
using System.Text;
7+
using System.Text.RegularExpressions;
8+
79
#if !NETCOREAPP
810
using System.Collections.Generic;
911
#endif
@@ -40,13 +42,19 @@ internal static string ToFilter(this Guid guid)
4042
internal static string ToFilter(this SecurityIdentifier sid) => $"(objectSid={sid})";
4143

4244
internal static string ToFilter(this string identity)
43-
#if NETCOREAPP
44-
=> identity.Contains('=')
45-
#else
46-
=> identity.Contains("=")
47-
#endif
48-
? $"(distinguishedName={identity})"
49-
: $"(samAccountName={identity})";
45+
{
46+
if (!identity.Contains("="))
47+
{
48+
return $"(samAccountName={identity})";
49+
}
50+
51+
if (!identity.Contains("DEL:"))
52+
{
53+
return $"(distinguishedName={identity})";
54+
}
55+
56+
return $"(&(isDeleted=TRUE)({Regex.Replace(identity, @"(?<!\\),.+", "")}))";
57+
}
5058

5159
internal static T GetProperty<T>(this SearchResult search, string property)
5260
=> LanguagePrimitives.ConvertTo<T>(search.Properties[property][0]);

src/ADEffectiveAccess/GetADEffectiveAccessComand.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public sealed class GetADEffectiveAccessComand : PSCmdlet, IDisposable
2020

2121
private SecurityMasks _masks = SecurityMasks.Group | SecurityMasks.Dacl | SecurityMasks.Owner;
2222

23-
private DirectoryEntryBuilder? _entryBuilder;
23+
private DirectoryEntryBuilder? _builder;
2424

2525
private GuidResolver? _map;
2626

@@ -77,14 +77,14 @@ protected override void BeginProcessing()
7777

7878
try
7979
{
80-
_entryBuilder = new DirectoryEntryBuilder(
80+
_builder = new DirectoryEntryBuilder(
8181
credential: Credential,
8282
authenticationTypes: AuthenticationTypes,
8383
server: Server,
8484
searchBase: SearchBase);
8585

8686
_map = GuidResolver.GetFromTLS();
87-
_map.SetContext(Server, _entryBuilder);
87+
_map.SetContext(Server, _builder);
8888
}
8989
catch (Exception exception)
9090
{
@@ -94,19 +94,19 @@ protected override void BeginProcessing()
9494

9595
protected override void ProcessRecord()
9696
{
97-
Assert(_entryBuilder is not null);
97+
Assert(_builder is not null);
9898
Assert(_map is not null);
9999

100100
try
101101
{
102102
if (Identity is not null)
103103
{
104-
GetByIdentity(_entryBuilder, Identity);
104+
GetByIdentity(_builder, Identity);
105105
return;
106106
}
107107

108108
using DirectorySearcher searcher = new(
109-
searchRoot: _entryBuilder.SearchBase,
109+
searchRoot: _builder.SearchBase,
110110
filter: LdapFilter,
111111
propertiesToLoad: [SecurityDescriptor])
112112
{
@@ -162,7 +162,7 @@ _ when LanguagePrimitives.TryConvertTo(identity, out SecurityIdentifier sid) =>
162162
};
163163

164164
using DirectorySearcher searcher = new(
165-
searchRoot: builder.RootEntry,
165+
searchRoot: builder.DomainEntry,
166166
filter: ldapFilter,
167167
propertiesToLoad: [SecurityDescriptor])
168168
{
@@ -171,7 +171,7 @@ _ when LanguagePrimitives.TryConvertTo(identity, out SecurityIdentifier sid) =>
171171
};
172172

173173
SearchResult result = searcher.FindOne()
174-
?? throw identity.ToIdentityNotFoundException(builder.Root);
174+
?? throw identity.ToIdentityNotFoundException(builder.DomainDistinguishedName);
175175

176176
WriteRules(result);
177177
}
@@ -182,7 +182,7 @@ private static void Assert([DoesNotReturnIf(false)] bool condition, string? mess
182182

183183
public void Dispose()
184184
{
185-
_entryBuilder?.Dispose();
185+
_builder?.Dispose();
186186
GC.SuppressFinalize(this);
187187
}
188188
}

0 commit comments

Comments
 (0)