Skip to content

Commit ea65f6e

Browse files
committed
now should work with 5.1 :(
1 parent d2a385a commit ea65f6e

File tree

4 files changed

+61
-36
lines changed

4 files changed

+61
-36
lines changed
Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,26 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<!-- <TargetFrameworks>net6.0;net472</TargetFrameworks> -->
5-
<TargetFrameworks>netstandard2.0</TargetFrameworks>
4+
<TargetFrameworks>net472;net8.0-windows</TargetFrameworks>
65
<Nullable>enable</Nullable>
76
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
87
<AssemblyName>ADEffectiveAccess</AssemblyName>
98
<LangVersion>latest</LangVersion>
10-
<NoWarn>CA1416</NoWarn>
9+
<!-- <NoWarn>CA1416</NoWarn> -->
1110
</PropertyGroup>
1211

1312
<PropertyGroup Condition=" '$(TargetFramework)' != 'net472' ">
1413
<DefineConstants>$(DefineConstants);CORE</DefineConstants>
1514
</PropertyGroup>
1615

17-
<!-- <ItemGroup Condition=" '$(TargetFramework)' != 'net472' ">
18-
<PackageReference Include="System.Security.Principal.Windows" Version="5.0.0"
19-
PrivateAssets="all" />
20-
<PackageReference Include="System.Management.Automation" Version="7.2.0" PrivateAssets="all" />
21-
<PackageReference Include="System.DirectoryServices" Version="6.0.0" PrivateAssets="all" />
22-
<PackageReference Include="System.DirectoryServices.AccountManagement" Version="6.0.0"
23-
PrivateAssets="all" />
24-
</ItemGroup> -->
25-
26-
<!-- <ItemGroup Condition=" '$(TargetFramework)' == 'net472' ">
16+
<ItemGroup Condition=" '$(TargetFramework)' == 'net472' ">
2717
<PackageReference Include="PowerShellStandard.Library" Version="5.1.1" PrivateAssets="all" />
28-
<Reference Include="System.DirectoryServices" PrivateAssets="all" />
29-
<Reference Include="System.DirectoryServices.AccountManagement" PrivateAssets="all" />
30-
</ItemGroup> -->
18+
<PackageReference Include="System.DirectoryServices" Version="9.0.10" PrivateAssets="all" />
19+
</ItemGroup>
3120

32-
<ItemGroup>
33-
<PackageReference Include="PowerShellStandard.Library" Version="5.1.1" PrivateAssets="all" />
34-
<PackageReference Include="System.DirectoryServices" Version="9.0.9" PrivateAssets="all" />
35-
<!-- <Reference Include="System.DirectoryServices" PrivateAssets="all" /> -->
36-
<!-- <Reference Include="System.DirectoryServices.AccountManagement" PrivateAssets="all" /> -->
21+
<ItemGroup Condition=" '$(TargetFramework)' != 'net472' ">
22+
<PackageReference Include="System.DirectoryServices" Version="9.0.10" PrivateAssets="all" />
23+
<PackageReference Include="System.Management.Automation" Version="7.4.0" PrivateAssets="all" />
3724
</ItemGroup>
3825

3926
</Project>

src/ADEffectiveAccess/Extensions.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ internal static bool TryAdd<TKey, TValue>(
2020
return !result;
2121
}
2222

23+
internal static string? GetProperty(this DirectoryEntry entry, string property)
24+
{
25+
if (!entry.Properties.Contains(property))
26+
{
27+
return null;
28+
}
29+
30+
return entry.Properties[property][0]?.ToString();
31+
}
32+
33+
2334
internal static void ThrowGuidResolverError(this Exception exception, PSCmdlet cmdlet)
2435
=> cmdlet.ThrowTerminatingError(
2536
new ErrorRecord(exception, "CreateGuidResolverError", ErrorCategory.ConnectionError, null));
@@ -40,4 +51,9 @@ internal static void WriteUnderterminedError(this Exception exception, PSCmdlet
4051

4152
internal static IdentityNotMappedException ToIdentityNotFoundException(this string identity, string? rootDn)
4253
=> new($"Cannot find an object with identity: '{identity}' under: '{rootDn}'.");
54+
55+
internal static InvalidOperationException ToInitializeException(this string path, string attribute)
56+
=> new(
57+
"Failed to initialize GuidResolver: " +
58+
$"The '{attribute}' attribute is missing or null in the RootDSE response for path '{path}'.");
4359
}

src/ADEffectiveAccess/GetADEffectiveAccessComand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public sealed class GetADEffectiveAccessComand : PSCmdlet, IDisposable
2626
ValueFromPipeline = true,
2727
ValueFromPipelineByPropertyName = true,
2828
ParameterSetName = IdentitySet)]
29-
[Alias("DistinguishedName", "ObjectGuid", "ObjectSid", "SamAccountName")]
29+
[Alias("DistinguishedName")]
3030
public string Identity { get; set; } = null!;
3131

3232
[Parameter(Position = 0, ParameterSetName = FilterSet)]
@@ -137,7 +137,7 @@ protected override void ProcessRecord()
137137
}
138138
}
139139

140-
private string? TryGetIdentityPath(string? identity) => identity switch
140+
private static string? TryGetIdentityPath(string? identity) => identity switch
141141
{
142142
_ when identity is null => null,
143143
_ when Guid.TryParse(identity, out Guid guid) => $"<GUID={guid:D}>",

src/ADEffectiveAccess/GuidResolver.cs

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ namespace ADEffectiveAccess;
66

77
internal 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

Comments
 (0)